FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpyquickhelper/code/unit_tests.cpp at master · sdpython/cpyquickhelper · GitHub
This repository was archived by the owner on Jan 13, 2024. It is now read-only.
sdpython
cpyquickhelper
Repository navigation
Code
Issues
1
(1)
Pull requests
2
(2)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpyquickhelper
/
code
/
unit_tests.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
289 lines (257 loc) · 8.43 KB
Breadcrumbs
cpyquickhelper
/
code
/
unit_tests.cpp
Copy path
File metadata and controls
289 lines (257 loc) · 8.43 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#
if
!defined(_CRT_SECURE_NO_WARNINGS)
#
define
_CRT_SECURE_NO_WARNINGS
#
endif
#
include
<
iostream
>
#
include
<
chrono
>
#
include
<
exception
>
#
include
<
string
>
#
include
<
vector
>
#
include
"
unit_tests.h
"
#
include
"
weighted_number.h
"
#
include
"
cbenchmark_dot.h
"
void
ASSERT_EQUAL
(
double
a,
double
b) {
if
(a != b) {
char
buffer[
100
];
sprintf
(buffer,
"
%f != %f
"
, a, b);
throw
std::exception
(buffer);
}
}
void
ASSERT_EQUAL_STRING
(
const
std::string &a,
const
std::string &b) {
if
(a != b) {
std::string
quote
(
"
'
"
);
std::string message = quote + a + quote +
std::string
(
"
!=
"
) + quote + b + quote;
throw
new
std::exception
(message.
c_str
());
}
}
void
test_addition
() {
WeightedDouble
n1
(
1
,
1
);
WeightedDouble
n2
(
1
,
1
);
auto
res = n1 + n2;
ASSERT_EQUAL
(res.
value
,
2
);
ASSERT_EQUAL
(res.
weight
,
2
);
n1 += n2;
ASSERT_EQUAL
(n1.
value
,
2
);
ASSERT_EQUAL
(n1.
weight
,
2
);
}
void
test_addition2
() {
WeightedDouble
n1
(
1
,
1
);
auto
res = n1 +
1
;
ASSERT_EQUAL
(res.
value
,
2
);
ASSERT_EQUAL
(res.
weight
,
2
);
n1 +=
1
;
ASSERT_EQUAL
(n1.
value
,
2
);
ASSERT_EQUAL
(n1.
weight
,
2
);
}
void
test_multiplication
() {
WeightedDouble
n1
(
1
,
1
);
WeightedDouble
n2
(
1
,
1
);
auto
res = n1 * n2;
ASSERT_EQUAL
(res.
value
,
1
);
ASSERT_EQUAL
(res.
weight
,
1
);
n1 *= n2;
ASSERT_EQUAL
(n1.
value
,
1
);
ASSERT_EQUAL
(n1.
weight
,
1
);
}
void
test_division
() {
WeightedDouble
n1
(
1
,
1
);
WeightedDouble
n2
(
1
,
1
);
auto
res = n1 / n2;
ASSERT_EQUAL
(res.
value
,
1
);
ASSERT_EQUAL
(res.
weight
,
1
);
n1 /= n2;
ASSERT_EQUAL
(n1.
value
,
1
);
ASSERT_EQUAL
(n1.
weight
,
1
);
}
void
test_soustraction
() {
WeightedDouble
n1
(
1
,
1
);
WeightedDouble
n2
(
1
,
1
);
auto
res = n1 - n2;
ASSERT_EQUAL
(res.
value
,
0
);
ASSERT_EQUAL
(res.
weight
,
0
);
n1 -= n2;
ASSERT_EQUAL
(n1.
value
,
0
);
ASSERT_EQUAL
(n1.
weight
,
0
);
}
void
test_soustraction2
() {
WeightedDouble
n1
(
1
,
1
);
auto
res = n1 -
1
;
ASSERT_EQUAL
(res.
value
,
0
);
ASSERT_EQUAL
(res.
weight
,
0
);
n1 -=
1
;
ASSERT_EQUAL
(n1.
value
,
0
);
ASSERT_EQUAL
(n1.
weight
,
0
);
}
void
test_multiplication2
() {
WeightedDouble
n1
(
1
,
1
);
auto
res = n1 *
2
;
ASSERT_EQUAL
(res.
value
,
2
);
ASSERT_EQUAL
(res.
weight
,
2
);
n1 *=
2
;
ASSERT_EQUAL
(n1.
value
,
2
);
ASSERT_EQUAL
(n1.
weight
,
2
);
}
void
test_division2
() {
WeightedDouble
n1
(
1
,
1
);
auto
res = n1 /
2
.;
ASSERT_EQUAL
(res.
value
,
0.5
);
ASSERT_EQUAL
(res.
weight
,
0.5
);
n1 /=
2
.;
ASSERT_EQUAL
(n1.
value
,
0.5
);
ASSERT_EQUAL
(n1.
weight
,
0.5
);
}
void
test_equal
() {
WeightedDouble
n1
(
1
,
1
);
WeightedDouble
n2
(
0
,
0
);
auto
v = -n1;
WeightedDouble
op
(v);
n2 = op;
ASSERT_EQUAL
(n2.
value
, -
1
);
ASSERT_EQUAL
(n2.
weight
, -
1
);
}
void
test_string
() {
WeightedDouble
n1
(
1
,
2
);
auto
s = n1.
__repr__
();
ASSERT_EQUAL_STRING
(s,
"
WeightedDouble(1.000000, 2)
"
);
s = n1.
__str__
();
ASSERT_EQUAL_STRING
(s,
"
1.000000 (2)
"
);
}
void
test_comparisons
() {
WeightedDouble
n1
(
1
,
1
);
WeightedDouble
n2
(
1
,
1
);
if
(!(n1 == n2))
throw
std::exception
(
"
Issue
"
);
if
(n1 != n2)
throw
std::exception
(
"
Issue
"
);
WeightedDouble
m1
(
1
,
2
);
WeightedDouble
m2
(
2
,
1
);
if
(!(m1 == m2))
throw
std::exception
(
"
Issue
"
);
if
(m1 != m2)
throw
std::exception
(
"
Issue
"
);
if
(m1 == n2)
throw
std::exception
(
"
Issue
"
);
if
(m1 < n2)
throw
std::exception
(
"
Issue
"
);
if
(m1 <= n2)
throw
std::exception
(
"
Issue
"
);
if
(!(m1 > n2))
throw
std::exception
(
"
Issue
"
);
if
(!(m1 >= n2))
throw
std::exception
(
"
Issue
"
);
}
#
define
THROWERRORMSG
(
d1, d2
) \
{ \
char
buffer[
200
]; \
sprintf
(buffer,
"
%g != %g
"
, d1, d2); \
throw
std::runtime_error
(buffer); \
}
void
test_vector_dot
() {
std::vector<
float
> v1{
1
,
2
,
3
};
std::vector<
float
> v2{
1.1
,
2.1
,
3.1
};
float
d1 =
vector_dot_product_pointer
(v1.
data
(), v2.
data
(), v1.
size
());
float
d2 =
vector_dot_product_pointer16
(v1.
data
(), v2.
data
(), v1.
size
());
float
d3 =
vector_dot_product_pointer16_nofcall
(v1.
data
(), v2.
data
(), v1.
size
());
float
d4 =
vector_dot_product_pointer16_sse
(v1.
data
(), v2.
data
(), v1.
size
());
if
(!(d1 == d2))
THROWERRORMSG
(d1, d2);
if
(!(d1 == d3))
THROWERRORMSG
(d1, d3);
if
(!(d1 == d4))
THROWERRORMSG
(d1, d4);
#
if
defined(__AVX512F__)
float
d5 =
vector_dot_product_pointer16_avx512
(v1.
data
(), v2.
data
(), v1.
size
());
if
(!(d1 == d5))
THROWERRORMSG
(d1, d5);
#
endif
}
typedef
float
dot_type
(
const
float
*,
const
float
*,
size_t
size);
double
benchmark
(dot_type * fct,
const
std::vector<
float
>& v1,
const
std::vector<
float
>& v2,
int
repeat,
int
number) {
const
float
* p1 = v1.
data
();
const
float
* p2 = v2.
data
();
double
sum =
0
;
auto
start =
std::chrono::high_resolution_clock::now
();
for
(
int
r =
0
; r < repeat; ++r)
{
for
(
int
i =
0
; i < number; ++i) {
sum += (*fct)(p1, p2, v1.
size
());
}
}
auto
end =
std::chrono::high_resolution_clock::now
();
if
(sum <
0
)
throw
std::runtime_error
(
"
issue 2
"
);
return
std::chrono::duration<
double
>(end - start).
count
() / repeat;
}
typedef
int64_t
sum_odd_type
(
const
int64_t
*,
size_t
size);
double
benchmark
(sum_odd_type* fct,
const
std::vector<
int64_t
>& v1,
int
repeat,
int
number) {
const
int64_t
* p1 = v1.
data
();
double
sum =
0
;
auto
start =
std::chrono::high_resolution_clock::now
();
for
(
int
r =
0
; r < repeat; ++r)
{
for
(
int
i =
0
; i < number; ++i) {
sum += (*fct)(p1, v1.
size
());
}
}
auto
end =
std::chrono::high_resolution_clock::now
();
if
(sum <
0
)
throw
std::runtime_error
(
"
issue 2
"
);
return
std::chrono::duration<
double
>(end - start).
count
() / repeat;
}
void
test_vector_dot_benchmark
(
int
n,
int
repeat =
10
,
int
number =
50
) {
std::vector<
float
>
v1
(n);
std::vector<
float
>
v2
(n);
for
(
int
i =
0
; i < v1.
size
(); ++i) {
v1[i] = i +
0
.
32453f
;
v2[i] = i +
0
.
32142f
;
}
double
t;
t =
benchmark
(&vector_dot_product_pointer, v1, v2, repeat, number);
std::cout << v1.
size
() <<
"
-- vector_dot_product_pointer:
"
<< t <<
"
\n
"
;
t =
benchmark
(&vector_dot_product_pointer_openmp_default, v1, v2, repeat, number);
std::cout << v1.
size
() <<
"
-- vector_dot_product_pointer_openmp:
"
<< t <<
"
\n
"
;
t =
benchmark
(&vector_dot_product_pointer16, v1, v2, repeat, number);
std::cout << v1.
size
() <<
"
-- vector_dot_product_pointer16:
"
<< t <<
"
\n
"
;
t =
benchmark
(&vector_dot_product_pointer16_nofcall, v1, v2, repeat, number);
std::cout << v1.
size
() <<
"
-- vector_dot_product_pointer16_nofcall:
"
<< t <<
"
\n
"
;
t =
benchmark
(&vector_dot_product_pointer16_sse, v1, v2, repeat, number);
std::cout << v1.
size
() <<
"
-- vector_dot_product_pointer16_sse:
"
<< t <<
"
\n
"
;
}
void
test_sum_odd_benchmark
(
int
n,
int
repeat =
20
,
int
number =
100
) {
std::vector<
int64_t
>
v1
(n);
for
(
int
i =
0
; i < v1.
size
(); ++i) {
v1[i] = i;
}
double
t;
t =
benchmark
(&sum_odd_test, v1, repeat, number);
std::cout << v1.
size
() <<
"
-- sum_odd_test:
"
<< t <<
"
\n
"
;
t =
benchmark
(&sum_odd_inline, v1, repeat, number);
std::cout << v1.
size
() <<
"
-- sum_odd_inline:
"
<< t <<
"
\n
"
;
t =
benchmark
(&sum_odd_b, v1, repeat, number);
std::cout << v1.
size
() <<
"
-- sum_odd_b:
"
<< t <<
"
\n
"
;
}
void
test_sum_odd0_benchmark
(
int
n,
int
repeat =
20
,
int
number =
100
) {
std::vector<
int64_t
>
v1
(n);
for
(
int
i =
0
; i < v1.
size
(); ++i) {
v1[i] = i;
}
double
t;
t =
benchmark
(&sum_odd0_test, v1, repeat, number);
std::cout << v1.
size
() <<
"
-- sum_odd0_test:
"
<< t <<
"
\n
"
;
t =
benchmark
(&sum_odd0_inline, v1, repeat, number);
std::cout << v1.
size
() <<
"
-- sum_odd0_inline:
"
<< t <<
"
\n
"
;
t =
benchmark
(&sum_odd0_b, v1, repeat, number);
std::cout << v1.
size
() <<
"
-- sum_odd0_b:
"
<< t <<
"
\n
"
;
}
void
run_all_tests
() {
test_addition
();
test_addition2
();
test_division
();
test_division2
();
test_multiplication
();
test_multiplication2
();
test_soustraction
();
test_soustraction2
();
test_equal
();
test_string
();
test_vector_dot
();
for
(
int
n =
12
; n <
620000
; n +=
200000
) {
std::cout <<
"
\n
"
;
test_sum_odd_benchmark
(n);
}
for
(
int
n =
12
; n <
620000
; n +=
200000
) {
std::cout <<
"
\n
"
;
test_sum_odd0_benchmark
(n);
}
for
(
int
n =
10
; n <
620000
; n +=
200000
) {
std::cout <<
"
\n
"
;
test_vector_dot_benchmark
(n);
}
printf
(
"
end
\n
"
);
}
Back
|
FazBrowse Home
|
New Git URL