-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_fluint8.cpp
98 lines (84 loc) · 2.78 KB
/
test_fluint8.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
#include "fluint8.hpp"
#include <iostream>
int main() {
uint32_t failed = 0;
std::cerr << "Testing 'get_bits':"; std::cerr.flush();
for (uint32_t a = 0; a < 256; ++a) {
uint8_t expected = uint8_t(a);
fluint8 fla = fluint8(a);
uint8_t result;
fla.get_bits(&result);
if (expected != result) {
std::cerr << "Failed: get_bits(" << a << ") expected " << uint32_t(expected) << " got " << uint32_t(result) << "." << std::endl;
++failed;
}
}
std::cerr << " done." << std::endl;
std::cerr << "Testing 'set_bits':"; std::cerr.flush();
for (uint32_t a = 0; a < 256; ++a) {
fluint8 expected = fluint8(a);
fluint8 result = fluint8(0.0f);
uint8_t a8 = uint8_t(a);
result.set_bits(&a8);
if (expected.val != result.val) {
std::cerr << "Failed: set_bits(" << a << ") expected " << expected.val << " got " << result.val << "." << std::endl;
++failed;
}
}
std::cerr << " done." << std::endl;
#define TEST_UNARYOP( OP ) \
std::cerr << "Testing '" #OP "':"; std::cerr.flush(); \
for (uint32_t a = 0; a < 256; ++a) { \
uint8_t expected = OP uint8_t(a); \
fluint8 result = OP fluint8(a); \
if (float(expected) != result.val) { \
std::cerr << "Failed: " #OP << a << " expected " << uint32_t(expected) << " got " << result.val << "." << std::endl; \
++failed; \
} \
} \
std::cerr << " done." << std::endl;
TEST_UNARYOP( ~ )
TEST_UNARYOP( - )
TEST_UNARYOP( + )
#define TEST_BINARYOP( OP, BSTART ) \
std::cerr << "Testing '" #OP "':"; std::cerr.flush(); \
for (uint32_t a = 0; a < 256; ++a) { \
for (uint32_t b = BSTART; b < 256; ++b) { \
uint8_t expected = uint8_t(a) OP uint8_t(b); \
fluint8 result = fluint8(a) OP fluint8(b); \
if (float(expected) != result.val) { \
std::cerr << "Failed: " << a << " " #OP " " << b << " expected " << uint32_t(expected) << " got " << result.val << "." << std::endl; \
++failed; \
} \
} \
} \
std::cerr << " done." << std::endl;
TEST_BINARYOP( &, 0 )
TEST_BINARYOP( ^, 0 )
TEST_BINARYOP( |, 0 )
#define TEST_EQUALOP( OP, BSTART ) \
std::cerr << "Testing '" #OP "':"; std::cerr.flush(); \
for (uint32_t a = 0; a < 256; ++a) { \
for (uint32_t b = BSTART; b < 256; ++b) { \
uint8_t expected = uint8_t(a); \
expected OP uint8_t(b); \
fluint8 result = fluint8(a); \
result OP fluint8(b); \
if (float(expected) != result.val) { \
std::cerr << "Failed: " << a << " " #OP " " << b << " expected " << uint32_t(expected) << " got " << result.val << "." << std::endl; \
++failed; \
} \
} \
} \
std::cerr << " done." << std::endl;
TEST_EQUALOP( += , 0)
TEST_EQUALOP( -= , 0)
TEST_EQUALOP( *= , 0)
TEST_EQUALOP( /= , 1)
TEST_EQUALOP( %= , 1)
TEST_EQUALOP( &= , 0)
TEST_EQUALOP( ^= , 0)
TEST_EQUALOP( |= , 0)
if (failed > 0) return 1;
else return 0;
}