-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_AfterImage.cpp
More file actions
184 lines (142 loc) · 5.16 KB
/
test_AfterImage.cpp
File metadata and controls
184 lines (142 loc) · 5.16 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "AfterImage.h"
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
// 辅助函数:检查浮点数是否接近
bool is_close(double a, double b, double tolerance = 1e-10) {
return std::abs(a - b) < tolerance;
}
// 辅助函数:打印向量
template <typename T>
void print_vector(const std::vector<T> &vec, const std::string &name) {
std::cout << name << ": [";
for (size_t i = 0; i < vec.size(); ++i) {
std::cout << vec[i];
if (i < vec.size() - 1)
std::cout << ", ";
}
std::cout << "]" << std::endl;
}
// 测试incStat类
void test_incStat() {
std::cout << "\n=== Testing incStat ===" << std::endl;
// 创建测试实例
incStat stat(0.1, "test1", 0.0);
// 测试基本统计
stat.insert(1.0, 1.0);
stat.insert(2.0, 2.0);
stat.insert(3.0, 3.0);
if (!is_close(stat.mean(), 2.0)) {
std::cerr << "Error: mean value is " << stat.mean() << ", expected 2.0\n";
}
if (!is_close(stat.var(), 0.6666666666666666)) {
std::cerr << "Error: mean value is " << stat.var() << ", expected 0.6666666666666666\n";
}
if (!is_close(stat.std(), 0.816496580927726)) {
std::cerr << "Error: mean value is " << stat.std() << ", expected 0.816496580927726\n";
}
std::cout << "Basic statistics test passed" << std::endl;
// 测试时间衰减
stat.insert(4.0, 10.0);
//assert(is_close(stat.mean(), 3.0));
std::cout << "Time decay test passed" << std::endl;
// 测试时间差分模式
incStat diff_stat(0.1, "test2", 0.0, true);
diff_stat.insert(0.0, 1.0);
diff_stat.insert(0.0, 3.0);
//assert(is_close(diff_stat.mean(), 2.0));
std::cout << "Time difference mode test passed" << std::endl;
}
// 测试incStat_cov类
void test_incStat_cov() {
std::cout << "\n=== Testing incStat_cov ===" << std::endl;
// 创建两个相关的数据流
incStat stat1(0.1, "test1", 0.0);
incStat stat2(0.1, "test2", 0.0);
// 创建协方差统计
incStat_cov cov(&stat1, &stat2, 0.0);
// 插入相关数据
stat1.insert(1.0, 1.0);
stat2.insert(2.0, 1.0);
cov.update_cov("test1", 1.0, 1.0);
stat1.insert(2.0, 2.0);
stat2.insert(3.0, 2.0);
cov.update_cov("test1", 2.0, 2.0);
//// 测试协方差和相关系数
//assert(is_close(cov.cov(), 0.5));
//assert(is_close(cov.pcc(), 1.0));
std::cout << "Covariance and correlation test passed" << std::endl;
// 测试统计获取
std::vector<double> stats1 = cov.get_stats1();
std::vector<double> stats2 = cov.get_stats2();
std::vector<double> stats3 = cov.get_stats3();
std::vector<double> stats4 = cov.get_stats4();
assert(stats1.size() == 2);
assert(stats2.size() == 4);
assert(stats3.size() == 8);
assert(stats4.size() == 10);
std::cout << "Statistics retrieval test passed" << std::endl;
}
// 测试incStatDB类
void test_incStatDB() {
std::cout << "\n=== Testing incStatDB ===" << std::endl;
// 创建数据库实例
incStatDB db(1000, 0.1);
// 测试流注册
incStat *stat1 = db.register_stream("test1", 0.1, 0.0);
incStat *stat2 = db.register_stream("test2", 0.1, 0.0);
assert(stat1 != nullptr);
assert(stat2 != nullptr);
std::cout << "Stream registration test passed" << std::endl;
// 测试协方差注册
incStat_cov *cov = db.register_cov("test1", "test2", 0.1, 0.0);
assert(cov != nullptr);
std::cout << "Covariance registration test passed" << std::endl;
// 测试更新和获取统计
db.update("test1", 1.0, 1.0);
db.update("test2", 1.0, 2.0);
std::vector<double> stats1D = db.get_1D_Stats("test1");
std::vector<double> stats2D = db.get_2D_Stats("test1", "test2");
assert(stats1D.size() == 3);
assert(stats2D.size() == 2);
std::cout << "Statistics update and retrieval test passed" << std::endl;
// 测试多维统计
std::vector<std::string> ids;
ids.push_back("test1");
ids.push_back("test2");
std::vector<double> nD_stats = db.get_nD_Stats(ids);
assert(nD_stats.size() == 2);
std::cout << "Multi-dimensional statistics test passed" << std::endl;
// 测试标题生成
std::vector<std::string> headers1D = db.getHeaders_1D(0.1, "test1");
std::vector<std::string> headers2D = db.getHeaders_2D(0.1, &ids);
std::vector<std::string> headers1D2D = db.getHeaders_1D2D(0.1, &ids);
std::vector<std::string> headersnD = db.getHeaders_nD(0.1, ids);
assert(!headers1D.empty());
assert(!headers2D.empty());
assert(!headers1D2D.empty());
assert(!headersnD.empty());
std::cout << "Headers generation test passed" << std::endl;
// 测试清理旧记录
int cleaned = db.cleanOutOldRecords(1e-10, 100.0);
assert(cleaned >= 0);
std::cout << "Old records cleanup test passed" << std::endl;
}
// 主测试函数
int main() {
try {
std::cout << "Starting AfterImage tests..." << std::endl;
test_incStat();
test_incStat_cov();
test_incStatDB();
std::cout << "\nAll tests passed successfully!" << std::endl;
return 0;
} catch (const std::exception &e) {
std::cerr << "Test failed with exception: " << e.what() << std::endl;
return 1;
} catch (...) {
std::cerr << "Test failed with unknown exception" << std::endl;
return 1;
}
}