forked from Preparation-Publication-BD2K/db_compress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumerical_model_test.cpp
More file actions
166 lines (152 loc) · 5.47 KB
/
numerical_model_test.cpp
File metadata and controls
166 lines (152 loc) · 5.47 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "base.h"
#include "data_io.h"
#include "model.h"
#include "utility.h"
#include "numerical_model.h"
#include <cmath>
#include <vector>
#include <memory>
#include <iostream>
namespace db_compress {
class MockAttr : public AttrValue {
private:
int val_;
public:
void Set(int val) { val_ = val; }
int Val() const { return val_; }
};
class MockInterpreter : public AttrInterpreter {
public:
bool EnumInterpretable() const { return true; }
int EnumCap() const { return 3; }
int EnumInterpret(const AttrValue* attr) const {
return static_cast<const MockAttr*>(attr)->Val();
}
};
Schema schema;
MockAttr attr;
IntegerAttrValue num_attr;
std::vector<size_t> pred;
Tuple tuple(2);
const Tuple& GetTuple(size_t a, int b) {
attr.Set(a);
num_attr.Set(b);
tuple.attr[0] = &attr;
tuple.attr[1] = &num_attr;
return tuple;
}
void PrepareData() {
RegisterAttrModel(1, new TableLaplaceIntCreator());
std::vector<int> schema_(2);
schema = Schema(schema_);
pred.push_back(0);
RegisterAttrInterpreter(0, new MockInterpreter());
}
void TestProbTree() {
std::unique_ptr<Model> model(GetAttrModel(1)[0]->CreateModel(schema, pred, 1, 0.1));
for (int i = -2; i <= 2; ++i)
model->FeedTuple(GetTuple(0, i));
model->FeedTuple(GetTuple(0, 0));
for (int i = 0; i < 3; ++i)
model->FeedTuple(GetTuple(1, (int)0));
model->EndOfData();
int test_a[9] = {0, 0, 0, 0, 0, 0, 0, 0, 1};
int test_branch[9] = {0, 1, 2, 3, 11, 12, 31, 32, 0};
bool test_next_branch[9] = {true, true, false, true, true, false, false, true, false};
int branch_prob[9] = {19875, 24109, 0, 65536 - 24109, 24109, 0, 0, 65536 - 24109, 0};
int boundary[9] = {0, -1, 0, 2, -2, 0, 0, 3, 0};
int result[9] = {0, 0, 0, 0, 0, -1, 1, 0, 0};
for (int i = 0; i < 9; ++i) {
ProbTree* tree = model->GetProbTree(GetTuple(test_a[i], 0));
int branch = test_branch[i];
std::vector<int> branches;
while (branch > 0) {
branches.push_back(branch % 10 - 1);
branch /= 10;
}
for (int j = branches.size() - 1; j >= 0; --j) {
if (!tree->HasNextBranch())
std::cerr << "ProbTree Unit Test Failed!\n";
tree->GenerateNextBranch();
tree->ChooseNextBranch(branches[j]);
}
if (tree->HasNextBranch() != test_next_branch[i])
std::cerr << "ProbTree Unit Test Failed!\n";
if (tree->HasNextBranch()) {
tree->GenerateNextBranch();
Prob prob = tree->GetProbSegs()[0];
if (prob != GetProb(branch_prob[i], 16))
std::cerr << "ProbTree Unit Test Failed!\n";
IntegerAttrValue attr(boundary[i]), l_attr(boundary[i] - 1);
if (tree->GetNextBranch(&attr) != 1 ||
tree->GetNextBranch(&l_attr) != 0)
std::cerr << "ProbTree Unit Test Failed!\n";
} else {
const AttrValue* attr(tree->GetResultAttr());
if (static_cast<const IntegerAttrValue*>(attr)->Value() != result[i])
std::cerr << "ProbTree Unit Test Failed!\n";
}
}
}
void TestModelCost() {
std::unique_ptr<Model> model(GetAttrModel(1)[0]->CreateModel(schema, pred, 1, 0.1));
for (int i = -1; i <= 1; ++ i)
model->FeedTuple(GetTuple(0, i * 100));
model->FeedTuple(GetTuple(0, 0));
model->EndOfData();
if (model->GetModelDescriptionLength() != 248)
std::cerr << "Model Cost Unit Test Failed!\n";
if (model->GetModelCost() != 280)
std::cerr << "Model Cost Unit Test Failed!\n";
}
void TestModelDescription() {
std::unique_ptr<Model> model(GetAttrModel(1)[0]->CreateModel(schema, pred, 1, 1));
for (int i = -2; i <= 2; ++i)
model->FeedTuple(GetTuple(0, i));
model->FeedTuple(GetTuple(0, 0));
for (int i = 0; i < 3; ++i)
model->FeedTuple(GetTuple(1, (int)0));
for (int i = -2; i <= 2; ++i)
model->FeedTuple(GetTuple(2, i * 100));
model->FeedTuple(GetTuple(2, 0));
model->EndOfData();
{
std::vector<size_t> block;
block.push_back(model->GetModelDescriptionLength());
ByteWriter writer(&block, "byte_writer_test.txt");
model->WriteModel(&writer, 0);
}
{
ByteReader reader("byte_writer_test.txt");
std::unique_ptr<Model> new_model(GetAttrModel(1)[0]->ReadModel(&reader, schema, 1));
if (new_model->GetTargetVar() != 1 || new_model->GetPredictorList().size() != 1 ||
new_model->GetPredictorList()[0] != 0)
std::cerr << "Model Description Unit Test Failed!\n";
int sdev[3] = {1, 0, 100};
for (int i = 0; i < 3; ++i) {
int dev = sdev[i];
ProbTree* tree = new_model->GetProbTree(GetTuple(i, 0));
if (dev == 0) {
if (tree->HasNextBranch())
std::cerr << "Model Description Unit Test Failed!\n";
} else {
tree->GenerateNextBranch();
tree->ChooseNextBranch(2);
tree->GenerateNextBranch();
IntegerAttrValue l((dev + 2) / 3 * 3 + 1), r((dev + 2) / 3 * 3 + 2);
if (tree->GetNextBranch(&l) != 0 || tree->GetNextBranch(&r) != 1)
std::cerr << "Model Description Unit Test Failed!\n";
}
}
}
}
void Test() {
PrepareData();
TestProbTree();
TestModelCost();
TestModelDescription();
}
} // namespace db_compress
int main() {
db_compress::Test();
}