Skip to content

Commit 4d5f52f

Browse files
committed
Transform float_utils unit test to use CATCH and enable it
It now tests both the approximating and non-approximating version of float_utilst.
1 parent 855d0c1 commit 4d5f52f

File tree

5 files changed

+208
-135
lines changed

5 files changed

+208
-135
lines changed

unit/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ list(REMOVE_ITEM sources
77
${CMAKE_CURRENT_SOURCE_DIR}/miniBDD.cpp
88

99
# Don't build
10-
${CMAKE_CURRENT_SOURCE_DIR}/sharing_map.cpp
1110
${CMAKE_CURRENT_SOURCE_DIR}/elf_reader.cpp
1211
${CMAKE_CURRENT_SOURCE_DIR}/smt2_parser.cpp
1312
${CMAKE_CURRENT_SOURCE_DIR}/json.cpp
@@ -16,7 +15,6 @@ list(REMOVE_ITEM sources
1615
${CMAKE_CURRENT_SOURCE_DIR}/unicode.cpp
1716
${CMAKE_CURRENT_SOURCE_DIR}/wp.cpp
1817
${CMAKE_CURRENT_SOURCE_DIR}/cpp_scanner.cpp
19-
${CMAKE_CURRENT_SOURCE_DIR}/float_utils.cpp
2018
${CMAKE_CURRENT_SOURCE_DIR}/ieee_float.cpp
2119

2220
# Will be built into a separate library and linked

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ SRC += unit_tests.cpp \
1616
analyses/does_remove_const/is_type_at_least_as_const_as.cpp \
1717
goto-programs/goto_trace_output.cpp \
1818
path_strategies.cpp \
19+
solvers/floatbv/float_utils.cpp \
1920
solvers/refinement/array_pool/array_pool.cpp \
2021
solvers/refinement/string_constraint_generator_valueof/calculate_max_string_length.cpp \
2122
solvers/refinement/string_constraint_generator_valueof/get_numeric_value_from_character.cpp \

unit/float_utils.cpp

Lines changed: 0 additions & 133 deletions
This file was deleted.

unit/solvers/floatbv/float_utils.cpp

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
/*******************************************************************\
2+
3+
Module: Unit tests for float utils and approximation
4+
5+
Author: Daniel Kroening
6+
7+
\*******************************************************************/
8+
9+
#include <testing-utils/catch.hpp>
10+
11+
// for debug output in case of failure
12+
#include <iostream>
13+
14+
#include <solvers/floatbv/float_approximation.h>
15+
#include <solvers/floatbv/float_utils.h>
16+
#include <solvers/sat/satcheck.h>
17+
18+
float random_float()
19+
{
20+
union
21+
{
22+
float f;
23+
unsigned int i;
24+
} u;
25+
26+
u.i = random();
27+
u.i = (u.i << 16) ^ random();
28+
29+
return u.f;
30+
}
31+
32+
bool eq(const ieee_floatt &a, const ieee_floatt &b)
33+
{
34+
return (a.is_NaN() && b.is_NaN()) ||
35+
(a.is_infinity() && b.is_infinity() && a.get_sign() == b.get_sign()) ||
36+
a == b;
37+
}
38+
39+
std::string to_str(const bvt &bv)
40+
{
41+
std::string result;
42+
for(unsigned i=0; i<bv.size(); i++)
43+
{
44+
char ch;
45+
if(bv[i]==const_literal(true))
46+
ch='1';
47+
else if(bv[i]==const_literal(false))
48+
ch='0';
49+
else
50+
ch='?';
51+
result=ch+result;
52+
}
53+
return result;
54+
}
55+
56+
typedef enum { PLUS=0, MINUS=1, MULT=2, DIV=3 } binopt;
57+
const char *binopsyms[]={ " + ", " - ", " * ", " / " };
58+
59+
float set_values(
60+
float_utilst &float_utils,
61+
float &f1,
62+
float &f2,
63+
ieee_floatt &i1,
64+
ieee_floatt &i2)
65+
{
66+
f1 = random_float();
67+
f2 = random_float();
68+
i1.from_float(f1);
69+
i2.from_float(f2);
70+
float_utils.spec = i1.spec;
71+
72+
return f1;
73+
}
74+
75+
bvt compute(
76+
unsigned i,
77+
float_utilst &float_utils,
78+
const float &f2,
79+
float &f3,
80+
const ieee_floatt &i1,
81+
const ieee_floatt &i2)
82+
{
83+
const bvt b1 = float_utils.build_constant(i1);
84+
const bvt b2 = float_utils.build_constant(i2);
85+
86+
const auto op = i % 3;
87+
88+
switch(op)
89+
{
90+
case PLUS:
91+
f3 += f2;
92+
return float_utils.add(b1, b2);
93+
94+
case MINUS:
95+
f3 -= f2;
96+
return float_utils.sub(b1, b2);
97+
98+
case MULT:
99+
f3 *= f2;
100+
return float_utils.mul(b1, b2);
101+
102+
case DIV:
103+
f3 /= f2;
104+
return float_utils.div(b1, b2);
105+
}
106+
107+
return bvt();
108+
}
109+
110+
void print(
111+
unsigned i,
112+
const ieee_floatt &i1,
113+
const ieee_floatt &i2,
114+
const ieee_floatt &i3,
115+
const ieee_floatt &fres,
116+
const float &f1,
117+
const float &f2,
118+
const float &f3)
119+
{
120+
const unsigned op = i % 3;
121+
const char *opsym = binopsyms[op];
122+
123+
std::cout << i1 << opsym << i2 << " != " << fres << '\n';
124+
std::cout << f1 << opsym << f2 << " == " << f3 << '\n';
125+
std::cout << integer2binary(i1.get_exponent(), i1.spec.e) << " "
126+
<< integer2binary(i1.get_fraction(), i1.spec.f + 1) << opsym
127+
<< integer2binary(i2.get_exponent(), i1.spec.e) << " "
128+
<< integer2binary(i2.get_fraction(), i1.spec.f + 1) << " != "
129+
<< integer2binary(fres.get_exponent(), i1.spec.e) << " "
130+
<< integer2binary(fres.get_fraction(), i1.spec.f + 1) << '\n';
131+
std::cout << integer2binary(i1.get_exponent(), i1.spec.e) << " "
132+
<< integer2binary(i1.get_fraction(), i1.spec.f + 1) << opsym
133+
<< integer2binary(i2.get_exponent(), i1.spec.e) << " "
134+
<< integer2binary(i2.get_fraction(), i1.spec.f + 1) << " == "
135+
<< integer2binary(i3.get_exponent(), i1.spec.e) << " "
136+
<< integer2binary(i3.get_fraction(), i1.spec.f + 1) << '\n';
137+
}
138+
139+
SCENARIO("float_utils", "[core][solvers][floatbv][float_utils]")
140+
{
141+
ieee_floatt i1, i2, i3;
142+
float f1, f2, f3;
143+
144+
for(unsigned i = 0; i < 200; i++)
145+
{
146+
satcheckt satcheck;
147+
float_utilst float_utils(satcheck);
148+
149+
GIVEN("Two random floating point numbers")
150+
{
151+
f3 = set_values(float_utils, f1, f2, i1, i2);
152+
bvt res = compute(i, float_utils, f2, f3, i1, i2);
153+
154+
THEN("Machine execution yields the same result as symbolic computation")
155+
{
156+
i3.from_float(f3);
157+
158+
const satcheckt::resultt result = satcheck.prop_solve();
159+
REQUIRE(result == satcheckt::resultt::P_SATISFIABLE);
160+
161+
const ieee_floatt fres = float_utils.get(res);
162+
163+
if(!eq(fres, i3))
164+
print(i, i1, i2, i3, fres, f1, f2, f3);
165+
166+
REQUIRE(eq(fres, i3));
167+
}
168+
}
169+
}
170+
}
171+
172+
SCENARIO("float_approximation", "[core][solvers][floatbv][float_approximation]")
173+
{
174+
ieee_floatt i1, i2, i3;
175+
float f1, f2, f3;
176+
177+
for(unsigned i = 0; i < 200; i++)
178+
{
179+
satcheckt satcheck;
180+
float_approximationt float_utils(satcheck);
181+
182+
GIVEN("Two random floating point numbers")
183+
{
184+
f3 = set_values(float_utils, f1, f2, i1, i2);
185+
bvt res = compute(i, float_utils, f2, f3, i1, i2);
186+
187+
THEN("Machine execution yields the same result as symbolic computation")
188+
{
189+
i3.from_float(f3);
190+
191+
const satcheckt::resultt result = satcheck.prop_solve();
192+
REQUIRE(result == satcheckt::resultt::P_SATISFIABLE);
193+
194+
const ieee_floatt fres = float_utils.get(res);
195+
196+
if(!eq(fres, i3))
197+
print(i, i1, i2, i3, fres, f1, f2, f3);
198+
199+
REQUIRE(eq(fres, i3));
200+
}
201+
}
202+
}
203+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
solvers/floatbv
2+
solvers/sat
3+
testing-utils
4+
util

0 commit comments

Comments
 (0)