forked from HIT-SCIR/ltp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_template_unittest.cpp
107 lines (94 loc) · 2.7 KB
/
utils_template_unittest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Unittest case for functions in ltp::strutils module.
#include <iostream>
#include <vector>
#include <gtest/gtest.h>
#include "utils/template.hpp"
using namespace std;
using namespace ltp::utility;
TEST(template_unittest, test_template_basic) {
Template T("1={test}");
Template::Data data;
data.set("test","basic");
string feat;
T.render(data,feat);
EXPECT_STREQ("1=basic",feat.c_str());
}
TEST(template_unittest, test_template_space) {
Template T("1={test}");
Template::Data data;
data.set("test"," ");
string feat;
T.render(data,feat);
EXPECT_STREQ("1= ",feat.c_str());
}
TEST(template_unittest, test_template_empty) {
Template T("1={test}");
Template::Data data;
data.set("test","");
string feat;
T.render(data,feat);
EXPECT_STREQ("1=",feat.c_str());
}
TEST(template_unittest, test_template_concate) {
/*Template T("1={slot1}{slot2}");
Template::Data data;
data.set("slot1", "-");
data.set("slot2", "+");
string feat;
T.render(data, feat);
EXPECT_STREQ("1=-+", feat.c_str());*/
}
TEST(template_unittest, test_template_chinese) {
Template T("中文{test}");
Template::Data data;
data.set("test", "测试");
string feat;
T.render(data, feat);
EXPECT_STREQ("中文测试", feat.c_str());
}
TEST(template_unittest, test_template_duplicate) {
Template T("template-with-two-{{token}}");
Template::Data data;
data.set("token", "brackets");
string feat;
T.render(data, feat);
EXPECT_STREQ("template-with-two-{brackets}", feat.c_str());
}
TEST(template_unittest, test_template_efficiency) {
std::vector<Template *> repos;
repos.push_back(new Template("1={w0}"));
repos.push_back(new Template("2={p0}"));
repos.push_back(new Template("3={w0}-{p0}"));
repos.push_back(new Template("4={w1}"));
repos.push_back(new Template("5={p1}"));
repos.push_back(new Template("6={w1}-{p1}"));
string payload;
payload.reserve(128);
long start_time = clock();
int kNumRepeats = 1024 * 1024;
int kNumTemplates = repos.size();
for (int t = 0; t < 1024 * 1024; ++ t) {
Template::Data data;
data.set("w0", "am");
data.set("p0", "v");
data.set("w1", "I");
data.set("p1", "r");
for (int i = 0; i < repos.size(); ++ i) {
Template* T = repos[i];
T->render(data, payload);
}
}
long throughput_per_millisecond = ((kNumRepeats * kNumTemplates)
/ ((clock() -start_time) / 1000));
std::cerr << "#throughput: " <<throughput_per_millisecond << std::endl;
}
TEST(template_DeathTest, test_template_null) {
Template T("1={test}");
Template::Data data;
// Till now (2014-04-30) this usage will result in a core
// dump in LTP
char* ptr = NULL;
data.set("test", ptr);
string feat;
ASSERT_DEATH(T.render(data, feat), "");
}