-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
123 lines (105 loc) · 2.2 KB
/
test.cpp
File metadata and controls
123 lines (105 loc) · 2.2 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
#include <stdlib.h>
#include <iostream>
#include "logreal.h"
using namespace std;
#define EPSILON 0.0000000001
bool withinEpsilon(double p, double d){
return abs(p - d) < EPSILON;
}
union intDouble {
int i;
double d;
};
double rdouble(){
intDouble v;
v.i = (0x3F800000 | (0x7FFFFF & rand()));
return (v.d - 1.0);
}
void testing(string s){ cout << "TESTING: " << s << " "; }
void result(bool r){ cout << (r ? "PASSED!" : "FAILED") << endl; }
void test_identity()
{
testing("identity");
LogDouble d = 0.5;
result( d == d
&& d == 0.5
&& 0.5 == d );
return;
}
void test_multiply()
{
testing("multiply");
LogDouble a = 0.5;
LogDouble b = 0.5;
LogDouble c = a * b;
result( c == 0.25
&& a * 0.5 == 0.25
&& 0.5 * b == 0.25);
return;
}
void test_add()
{
testing("add");
LogDouble a = 0.4;
LogDouble b = 0.1;
LogDouble c = 0.5;
result( a + b == c );
}
void test_subtract()
{
testing("subtract");
LogDouble a = 0.5;
LogDouble b = -1.0;
LogDouble c = -0.5;
result( a + b == c );
}
void test_sum()
{
testing("sum");
LogDouble arr[5] = { 0.1, 0.2, 0.3, 0.4, 0.5 };
result( LogDouble::sum(arr, 5) == 1.5 );
}
void test_product()
{
testing("product");
LogDouble arr[4] = { 0.1, 0.2, 0.3, 0.4 };
LogDouble expected = 0.1 * 0.2 * 0.3 * 0.4;
result( LogDouble::product(arr, 4) == expected );
}
void test_underflow()
{
testing("underflow safety");
double MULTIPLIER = 0.01;
LogDouble ld = MULTIPLIER;
double rd = MULTIPLIER;
int ii = 0;
while( rd != 0.0 ){
rd *= MULTIPLIER;
++ii;
}
for( int jj = 0; jj < ii * 10; ++jj ){
ld = ld * MULTIPLIER;
}
result( ld != 0.0 );
}
void test_ostream()
{
LogDouble zero = 0.0;
LogDouble half = 0.5;
LogDouble neg_ten = -10.0;
cout << zero << " == " << 0.0 << endl;
cout << half << " == e^-0.693147" << endl;
cout << neg_ten << " == -e^2.30259" << endl;
}
int main(int argc, char** argv)
{
test_identity();
test_multiply();
test_add();
test_subtract();
test_sum();
test_product();
test_underflow();
test_ostream();
return 0;
}