Skip to content

Commit a8c5ee0

Browse files
committed
Fixes to the byte_operator lowering unit test
Let's only test byte extracts within bounds for now (there is ample work to be done to get those right), but make sure there is possible combination for each pair of types. Endianness needs to be taken into account when constructing the expected value. Disable any non-POD tests as they all need more work.
1 parent 5530ca3 commit a8c5ee0

File tree

1 file changed

+146
-127
lines changed

1 file changed

+146
-127
lines changed

unit/solvers/lowering/byte_operators.cpp

Lines changed: 146 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -65,96 +65,104 @@ SCENARIO("byte_extract_lowering", "[core][solvers][lowering][byte_extract]")
6565
GIVEN("A collection of types")
6666
{
6767
unsignedbv_typet u8(8);
68+
signedbv_typet s8(8);
69+
unsignedbv_typet u16(16);
70+
signedbv_typet s16(16);
6871
unsignedbv_typet u32(32);
72+
signedbv_typet s32(32);
6973
unsignedbv_typet u64(64);
74+
signedbv_typet s64(64);
7075

7176
constant_exprt size = from_integer(8, size_type());
7277

7378
std::vector<typet> types = {
74-
struct_typet({{"comp1", u32}, {"comp2", u64}}),
79+
// struct_typet({{"comp1", u16}, {"comp2", u16}}),
80+
// struct_typet({{"comp1", u32}, {"comp2", u64}}),
7581
#if 0
7682
// not currently handled: components not byte aligned
7783
struct_typet({{"comp1", u32},
7884
{"compX", c_bit_field_typet(u8, 4)},
7985
{"pad", c_bit_field_typet(u8, 4)},
8086
{"comp2", u8}}),
8187
#endif
82-
union_typet({{"compA", u32}, {"compB", u64}}),
83-
c_enum_typet(unsignedbv_typet(16)),
84-
array_typet(u8, size),
85-
array_typet(u64, size),
88+
// union_typet({{"compA", u32}, {"compB", u64}}),
89+
// c_enum_typet(u16),
90+
// c_enum_typet(unsignedbv_typet(128)),
91+
// array_typet(u8, size),
92+
// array_typet(s32, size),
93+
// array_typet(u64, size),
8694
unsignedbv_typet(24),
95+
unsignedbv_typet(128),
96+
signedbv_typet(24),
8797
signedbv_typet(128),
88-
ieee_float_spect::single_precision().to_type(),
89-
pointer_typet(u64, 64),
90-
vector_typet(u8, size),
91-
vector_typet(u64, size),
92-
complex_typet(u32)
98+
// ieee_float_spect::single_precision().to_type(),
99+
// pointer_typet(u64, 64),
100+
// vector_typet(u8, size),
101+
// vector_typet(u64, size),
102+
// complex_typet(s16),
103+
// complex_typet(u64)
93104
};
94105

95106
simplify_exprt simp(ns);
96107

97108
THEN("byte_extract lowering yields the expected value")
98109
{
99-
for(const auto &t1 : types)
110+
for(const auto endianness :
111+
{ID_byte_extract_little_endian, ID_byte_extract_big_endian})
100112
{
101-
std::ostringstream oss;
102-
for(int i = 0; i < 64; ++i)
103-
oss << integer2binary(i, 8);
104-
105-
const auto type_bits = pointer_offset_bits(t1, ns);
106-
REQUIRE(type_bits);
107-
const auto type_bits_int = numeric_cast_v<std::size_t>(*type_bits);
108-
REQUIRE(type_bits_int <= oss.str().size());
109-
const exprt s =
110-
simp.bits2expr(oss.str().substr(0, type_bits_int), t1, true);
111-
REQUIRE(s.is_not_nil());
112-
113-
for(const auto &t2 : types)
113+
for(const auto &t1 : types)
114114
{
115-
oss.str("");
116-
for(int i = 2; i < 66; ++i)
115+
std::ostringstream oss;
116+
for(int i = 0; i < 64; ++i)
117117
oss << integer2binary(i, 8);
118118

119-
const auto type_bits_2 = pointer_offset_bits(t2, ns);
120-
REQUIRE(type_bits_2);
121-
const auto type_bits_2_int =
122-
numeric_cast_v<std::size_t>(*type_bits_2);
123-
REQUIRE(type_bits_2_int <= oss.str().size());
124-
const exprt r =
125-
simp.bits2expr(oss.str().substr(0, type_bits_2_int), t2, true);
126-
REQUIRE(r.is_not_nil());
127-
128-
const byte_extract_exprt be1(
129-
ID_byte_extract_little_endian,
130-
s,
131-
from_integer(2, index_type()),
132-
t2);
133-
134-
const exprt lower_be1 = lower_byte_extract(be1, ns);
135-
const exprt lower_be1_s = simplify_expr(lower_be1, ns);
136-
137-
// TODO: does not currently hold
138-
// REQUIRE(!has_subexpr(lower_be1, ID_byte_extract_little_endian));
139-
REQUIRE(lower_be1.type() == be1.type());
140-
// TODO: does not currently hold
141-
// REQUIRE(lower_be1 == r);
142-
// TODO: does not currently hold
143-
// REQUIRE(lower_be1_s == r);
144-
145-
const byte_extract_exprt be2(
146-
ID_byte_extract_big_endian, s, from_integer(2, index_type()), t2);
147-
148-
const exprt lower_be2 = lower_byte_extract(be2, ns);
149-
const exprt lower_be2_s = simplify_expr(lower_be2, ns);
150-
151-
// TODO: does not currently hold
152-
REQUIRE(!has_subexpr(lower_be2, ID_byte_extract_big_endian));
153-
REQUIRE(lower_be2.type() == be2.type());
154-
// TODO: does not currently hold
155-
// REQUIRE(lower_be2 == r);
156-
// TODO: does not currently hold
157-
// REQUIRE(lower_be2_s == r);
119+
const auto type_bits = pointer_offset_bits(t1, ns);
120+
REQUIRE(type_bits);
121+
const auto type_bits_int = numeric_cast_v<std::size_t>(*type_bits);
122+
REQUIRE(type_bits_int <= oss.str().size());
123+
const exprt s = simp.bits2expr(
124+
oss.str().substr(0, type_bits_int),
125+
t1,
126+
endianness == ID_byte_extract_little_endian);
127+
REQUIRE(s.is_not_nil());
128+
129+
for(const auto &t2 : types)
130+
{
131+
oss.str("");
132+
for(int i = 2; i < 64; ++i)
133+
oss << integer2binary(i, 8);
134+
135+
const auto type_bits_2 = pointer_offset_bits(t2, ns);
136+
REQUIRE(type_bits_2);
137+
138+
// for now only extract within bounds
139+
if(*type_bits_2 + 16 > *type_bits)
140+
continue;
141+
142+
const auto type_bits_2_int =
143+
numeric_cast_v<std::size_t>(*type_bits_2);
144+
REQUIRE(type_bits_2_int <= oss.str().size());
145+
const exprt r = simp.bits2expr(
146+
oss.str().substr(0, type_bits_2_int),
147+
t2,
148+
endianness == ID_byte_extract_little_endian);
149+
REQUIRE(r.is_not_nil());
150+
151+
const byte_extract_exprt be(
152+
endianness, s, from_integer(2, index_type()), t2);
153+
154+
const exprt lower_be = lower_byte_extract(be, ns);
155+
const exprt lower_be_s = simplify_expr(lower_be, ns);
156+
157+
// TODO: does not currently hold
158+
// REQUIRE(!has_subexpr(lower_be, ID_byte_extract_little_endian));
159+
// REQUIRE(!has_subexpr(lower_be, ID_byte_extract_big_endian));
160+
REQUIRE(lower_be.type() == be.type());
161+
// TODO: does not currently hold
162+
// REQUIRE(lower_be == r);
163+
// TODO: does not currently hold
164+
// REQUIRE(lower_be_s == r);
165+
}
158166
}
159167
}
160168
}
@@ -208,13 +216,19 @@ SCENARIO("byte_update_lowering", "[core][solvers][lowering][byte_update]")
208216
GIVEN("A collection of types")
209217
{
210218
unsignedbv_typet u8(8);
219+
signedbv_typet s8(8);
220+
unsignedbv_typet u16(16);
221+
signedbv_typet s16(16);
211222
unsignedbv_typet u32(32);
223+
signedbv_typet s32(32);
212224
unsignedbv_typet u64(64);
225+
signedbv_typet s64(64);
213226

214227
constant_exprt size = from_integer(8, size_type());
215228

216229
std::vector<typet> types = {
217230
// TODO: only arrays and scalars
231+
// struct_typet({{"comp1", u16}, {"comp2", u16}}),
218232
// struct_typet({{"comp1", u32}, {"comp2", u64}}),
219233
#if 0
220234
// not currently handled: components not byte aligned
@@ -225,83 +239,88 @@ SCENARIO("byte_update_lowering", "[core][solvers][lowering][byte_update]")
225239
#endif
226240
// TODO: only arrays and scalars
227241
// union_typet({{"compA", u32}, {"compB", u64}}),
228-
// c_enum_typet(unsignedbv_typet(16)),
229-
array_typet(u8, size),
230-
array_typet(u64, size),
242+
// c_enum_typet(u16),
243+
// c_enum_typet(unsignedbv_typet(128)),
244+
// array_typet(u8, size),
245+
// array_typet(s32, size),
246+
// array_typet(u64, size),
231247
unsignedbv_typet(24),
248+
unsignedbv_typet(128),
249+
signedbv_typet(24),
232250
signedbv_typet(128),
233-
ieee_float_spect::single_precision().to_type(),
234-
pointer_typet(u64, 64),
251+
// ieee_float_spect::single_precision().to_type(),
252+
// pointer_typet(u64, 64),
235253
// TODO: only arrays and scalars
236254
// vector_typet(u8, size),
237255
// vector_typet(u64, size),
238-
// complex_typet(u32)
256+
// complex_typet(s16),
257+
// complex_typet(u64)
239258
};
240259

241260
simplify_exprt simp(ns);
242261

243262
THEN("byte_update lowering yields the expected value")
244263
{
245-
for(const auto &t1 : types)
264+
for(const auto endianness :
265+
{ID_byte_update_little_endian, ID_byte_update_big_endian})
246266
{
247-
std::ostringstream oss;
248-
for(int i = 0; i < 64; ++i)
249-
oss << integer2binary(i, 8);
250-
251-
const auto type_bits = pointer_offset_bits(t1, ns);
252-
REQUIRE(type_bits);
253-
const auto type_bits_int = numeric_cast_v<std::size_t>(*type_bits);
254-
REQUIRE(type_bits_int <= oss.str().size());
255-
const exprt s =
256-
simp.bits2expr(oss.str().substr(0, type_bits_int), t1, true);
257-
REQUIRE(s.is_not_nil());
258-
259-
for(const auto &t2 : types)
267+
for(const auto &t1 : types)
260268
{
261-
oss.str("");
262-
for(int i = 64; i < 128; ++i)
269+
std::ostringstream oss;
270+
for(int i = 0; i < 64; ++i)
263271
oss << integer2binary(i, 8);
264272

265-
const auto type_bits_2 = pointer_offset_bits(t2, ns);
266-
REQUIRE(type_bits_2);
267-
const auto type_bits_2_int =
268-
numeric_cast_v<std::size_t>(*type_bits_2);
269-
REQUIRE(type_bits_2_int <= oss.str().size());
270-
const exprt u =
271-
simp.bits2expr(oss.str().substr(0, type_bits_2_int), t2, true);
272-
REQUIRE(u.is_not_nil());
273-
274-
// TODO: currently required
275-
if(type_bits < type_bits_2)
276-
continue;
277-
278-
const byte_update_exprt bu1(
279-
ID_byte_update_little_endian, s, from_integer(2, index_type()), u);
280-
281-
const exprt lower_bu1 = lower_byte_operators(bu1, ns);
282-
const exprt lower_bu1_s = simplify_expr(lower_bu1, ns);
283-
284-
REQUIRE(!has_subexpr(lower_bu1, ID_byte_update_little_endian));
285-
REQUIRE(!has_subexpr(lower_bu1, ID_byte_extract_little_endian));
286-
REQUIRE(lower_bu1.type() == bu1.type());
287-
// TODO: does not currently hold
288-
// REQUIRE(lower_bu1 == u);
289-
// TODO: does not currently hold
290-
// REQUIRE(lower_bu1_s == u);
291-
292-
const byte_update_exprt bu2(
293-
ID_byte_update_big_endian, s, from_integer(2, index_type()), u);
294-
295-
const exprt lower_bu2 = lower_byte_operators(bu2, ns);
296-
const exprt lower_bu2_s = simplify_expr(lower_bu2, ns);
297-
298-
REQUIRE(!has_subexpr(lower_bu2, ID_byte_update_big_endian));
299-
REQUIRE(!has_subexpr(lower_bu2, ID_byte_extract_big_endian));
300-
REQUIRE(lower_bu2.type() == bu2.type());
301-
// TODO: does not currently hold
302-
// REQUIRE(lower_bu2 == u);
303-
// TODO: does not currently hold
304-
// REQUIRE(lower_bu2_s == u);
273+
const auto type_bits = pointer_offset_bits(t1, ns);
274+
REQUIRE(type_bits);
275+
const auto type_bits_int = numeric_cast_v<std::size_t>(*type_bits);
276+
REQUIRE(type_bits_int <= oss.str().size());
277+
std::string s_string = oss.str().substr(0, type_bits_int);
278+
const exprt s = simp.bits2expr(
279+
s_string, t1, endianness == ID_byte_update_little_endian);
280+
REQUIRE(s.is_not_nil());
281+
282+
for(const auto &t2 : types)
283+
{
284+
oss.str("");
285+
for(int i = 64; i < 128; ++i)
286+
oss << integer2binary(i, 8);
287+
288+
const auto type_bits_2 = pointer_offset_bits(t2, ns);
289+
REQUIRE(type_bits_2);
290+
291+
// for now only update within bounds
292+
if(*type_bits_2 + 16 > *type_bits)
293+
continue;
294+
295+
const auto type_bits_2_int =
296+
numeric_cast_v<std::size_t>(*type_bits_2);
297+
REQUIRE(type_bits_2_int <= oss.str().size());
298+
std::string u_string = oss.str().substr(0, type_bits_2_int);
299+
const exprt u = simp.bits2expr(
300+
u_string, t2, endianness == ID_byte_update_little_endian);
301+
REQUIRE(u.is_not_nil());
302+
303+
const exprt r = simp.bits2expr(
304+
s_string.replace(16, u_string.size(), u_string),
305+
t1,
306+
endianness == ID_byte_update_little_endian);
307+
REQUIRE(r.is_not_nil());
308+
309+
const byte_update_exprt bu(
310+
endianness, s, from_integer(2, index_type()), u);
311+
312+
const exprt lower_bu = lower_byte_operators(bu, ns);
313+
const exprt lower_bu_s = simplify_expr(lower_bu, ns);
314+
315+
REQUIRE(!has_subexpr(lower_bu, endianness));
316+
REQUIRE(!has_subexpr(lower_bu, ID_byte_extract_big_endian));
317+
REQUIRE(!has_subexpr(lower_bu, ID_byte_extract_little_endian));
318+
REQUIRE(lower_bu.type() == bu.type());
319+
// TODO: does not currently hold
320+
// REQUIRE(lower_bu == r);
321+
// TODO: does not currently hold
322+
// REQUIRE(lower_bu_s == r);
323+
}
305324
}
306325
}
307326
}

0 commit comments

Comments
 (0)