-
Notifications
You must be signed in to change notification settings - Fork 141
/
converter_config_test.cpp
124 lines (99 loc) · 3.5 KB
/
converter_config_test.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Jubatus: Online machine learning framework for distributed environment
// Copyright (C) 2011 Preferred Infrastructure and Nippon Telegraph and Telephone Corporation.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License version 2.1 as published by the Free Software Foundation.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include <gtest/gtest.h>
#include <fstream>
#include <string>
#include <cmath>
#include <pficommon/text/json.h>
#include "test_util.hpp"
#include "converter_config.hpp"
#include "datum_to_fv_converter.hpp"
#include "datum.hpp"
#include "exception.hpp"
using namespace std;
using namespace jubatus;
using namespace jubatus::fv_converter;
using namespace pfi::text::json;
TEST(converter_config, config) {
try {
#ifdef HAVE_RE2
ifstream ifs("./test_input/config.json");
#else
ifstream ifs("./test_input/config_wo_re2.json");
#endif
converter_config config;
ifs >> via_json(config);
datum_to_fv_converter conv;
initialize_converter(config, conv);
datum d;
d.string_values_.push_back(make_pair("user/name", "Taro Yamada"));
d.string_values_.push_back(make_pair("user/text", "hoge fuga <foo>"));
d.num_values_.push_back(make_pair("user/id", 1000));
d.num_values_.push_back(make_pair("user/age", 20));
sfv_t f;
conv.convert(d, f);
sfv_t exp;
exp.push_back(make_pair("user/name$Taro Yamada@str#bin/bin", 1.));
#ifdef HAVE_RE2
// only when re2 is enabled, detagging filter works
exp.push_back(make_pair("user/text-detagged$hoge fuga @str#bin/bin", 1.));
#endif
exp.push_back(make_pair("user/id@str$1000", 1.));
exp.push_back(make_pair("user/age@num", 20.));
exp.push_back(make_pair("user/age@log", log(20.)));
exp.push_back(make_pair("user/age+1@num", 21.));
sort(f.begin(), f.end());
sort(exp.begin(), exp.end());
PairVectorEquals(exp, f);
} catch (const std::exception& e) {
cout << e.what() << endl;
throw;
} catch (const std::string& e) {
cout << e << endl;
throw;
}
}
TEST(converter_config, hash) {
converter_config config;
num_rule r = {"*", "str"};
config.num_rules.push_back(r);
config.hash_max_size = 1;
datum_to_fv_converter conv;
initialize_converter(config, conv);
datum d;
d.num_values_.push_back(make_pair("age", 10));
sfv_t f;
conv.convert(d, f);
EXPECT_EQ("0", f[0].first);
}
TEST(converter_config, hash_negative) {
converter_config config;
config.hash_max_size = 0;
datum_to_fv_converter conv;
EXPECT_THROW(initialize_converter(config, conv), converter_exception);
}
TEST(make_fv_converter, empty) {
EXPECT_THROW(make_fv_converter(""), fv_converter::converter_exception);
}
TEST(make_fv_converter, invalid_config_json) {
EXPECT_THROW(make_fv_converter("{"), fv_converter::converter_exception);
}
TEST(make_fv_converter, config_json_parse_error) {
EXPECT_THROW(make_fv_converter("AA"), fv_converter::converter_exception);
}
TEST(make_fv_converter, config_cast_error) {
EXPECT_THROW(make_fv_converter("{}"), fv_converter::converter_exception);
}