Skip to content

Commit 31ff67a

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 b00794c commit 31ff67a

File tree

1 file changed

+147
-127
lines changed

1 file changed

+147
-127
lines changed

unit/solvers/lowering/byte_operators.cpp

Lines changed: 147 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -65,96 +65,103 @@ 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(lower_be.type() == be.type());
160+
// TODO: does not currently hold
161+
// REQUIRE(lower_be == r);
162+
// TODO: does not currently hold
163+
// REQUIRE(lower_be_s == r);
164+
}
158165
}
159166
}
160167
}
@@ -208,13 +215,19 @@ SCENARIO("byte_update_lowering", "[core][solvers][lowering][byte_update]")
208215
GIVEN("A collection of types")
209216
{
210217
unsignedbv_typet u8(8);
218+
signedbv_typet s8(8);
219+
unsignedbv_typet u16(16);
220+
signedbv_typet s16(16);
211221
unsignedbv_typet u32(32);
222+
signedbv_typet s32(32);
212223
unsignedbv_typet u64(64);
224+
signedbv_typet s64(64);
213225

214226
constant_exprt size = from_integer(8, size_type());
215227

216228
std::vector<typet> types = {
217229
// TODO: only arrays and scalars
230+
// struct_typet({{"comp1", u16}, {"comp2", u16}}),
218231
// struct_typet({{"comp1", u32}, {"comp2", u64}}),
219232
#if 0
220233
// not currently handled: components not byte aligned
@@ -225,83 +238,90 @@ SCENARIO("byte_update_lowering", "[core][solvers][lowering][byte_update]")
225238
#endif
226239
// TODO: only arrays and scalars
227240
// union_typet({{"compA", u32}, {"compB", u64}}),
228-
// c_enum_typet(unsignedbv_typet(16)),
229-
array_typet(u8, size),
230-
array_typet(u64, size),
241+
// c_enum_typet(u16),
242+
// c_enum_typet(unsignedbv_typet(128)),
243+
// array_typet(u8, size),
244+
// array_typet(s32, size),
245+
// array_typet(u64, size),
231246
unsignedbv_typet(24),
247+
unsignedbv_typet(128),
248+
signedbv_typet(24),
232249
signedbv_typet(128),
233-
ieee_float_spect::single_precision().to_type(),
234-
pointer_typet(u64, 64),
250+
// ieee_float_spect::single_precision().to_type(),
251+
// pointer_typet(u64, 64),
235252
// TODO: only arrays and scalars
236253
// vector_typet(u8, size),
237254
// vector_typet(u64, size),
238-
// complex_typet(u32)
255+
// complex_typet(s16),
256+
// complex_typet(u64)
239257
};
240258

241259
simplify_exprt simp(ns);
242260

243261
THEN("byte_update lowering yields the expected value")
244262
{
245-
for(const auto &t1 : types)
263+
for(const auto endianness :
264+
{ID_byte_update_little_endian, ID_byte_update_big_endian})
246265
{
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)
266+
for(const auto &t1 : types)
260267
{
261-
oss.str("");
262-
for(int i = 64; i < 128; ++i)
268+
std::ostringstream oss;
269+
for(int i = 0; i < 64; ++i)
263270
oss << integer2binary(i, 8);
264271

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);
272+
const auto type_bits = pointer_offset_bits(t1, ns);
273+
REQUIRE(type_bits);
274+
const auto type_bits_int = numeric_cast_v<std::size_t>(*type_bits);
275+
REQUIRE(type_bits_int <= oss.str().size());
276+
std::string s_string = oss.str().substr(0, type_bits_int);
277+
const exprt s = simp.bits2expr(s_string,
278+
t1,
279+
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(u_string,
300+
t2,
301+
endianness == ID_byte_update_little_endian);
302+
REQUIRE(u.is_not_nil());
303+
304+
const exprt r = simp.bits2expr(
305+
s_string.replace(16, u_string.size(), u_string),
306+
t1,
307+
endianness == ID_byte_update_little_endian);
308+
REQUIRE(r.is_not_nil());
309+
310+
const byte_update_exprt bu(
311+
endianness, s, from_integer(2, index_type()), u);
312+
313+
const exprt lower_bu = lower_byte_operators(bu, ns);
314+
const exprt lower_bu_s = simplify_expr(lower_bu, ns);
315+
316+
REQUIRE(!has_subexpr(lower_bu, endianness));
317+
REQUIRE(!has_subexpr(lower_bu, ID_byte_extract_big_endian));
318+
REQUIRE(!has_subexpr(lower_bu, ID_byte_extract_little_endian));
319+
REQUIRE(lower_bu.type() == bu.type());
320+
// TODO: does not currently hold
321+
// REQUIRE(lower_bu == r);
322+
// TODO: does not currently hold
323+
// REQUIRE(lower_bu_s == r);
324+
}
305325
}
306326
}
307327
}

0 commit comments

Comments
 (0)