Skip to content

Commit eaac0e9

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 eaac0e9

File tree

5 files changed

+222
-135
lines changed

5 files changed

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