|
3 | 3 | #include <util/arith_tools.h>
|
4 | 4 | #include <util/bitvector_expr.h>
|
5 | 5 | #include <util/bitvector_types.h>
|
| 6 | +#include <util/byte_operators.h> |
6 | 7 | #include <util/c_types.h>
|
7 | 8 | #include <util/config.h>
|
8 | 9 | #include <util/expr_initializer.h>
|
@@ -189,6 +190,19 @@ TEST_CASE(
|
189 | 190 | // the init value is not 0 (NULL).
|
190 | 191 | pointer_typed_expected.type() = pointer_type;
|
191 | 192 | REQUIRE(result_with_pointer_type == pointer_typed_expected);
|
| 193 | + |
| 194 | + // Check that replicating to a float_value is same as unsigned_bv. |
| 195 | + const auto result_with_float_type = duplicate_per_byte( |
| 196 | + from_integer(init_expr_value, unsignedbv_typet{init_expr_size}), |
| 197 | + float_type()); |
| 198 | + const auto expected_unsigned_value = |
| 199 | + expr_try_dynamic_cast<constant_exprt>(duplicate_per_byte( |
| 200 | + from_integer(init_expr_value, unsignedbv_typet{init_expr_size}), |
| 201 | + unsignedbv_typet{float_type().get_width()})); |
| 202 | + REQUIRE(expected_unsigned_value); |
| 203 | + const auto float_typed_expected = |
| 204 | + constant_exprt{expected_unsigned_value->get_value(), float_type()}; |
| 205 | + REQUIRE(result_with_float_type == float_typed_expected); |
192 | 206 | }
|
193 | 207 | }
|
194 | 208 |
|
@@ -233,12 +247,31 @@ TEST_CASE(
|
233 | 247 | const auto pointer_unsigned_corr_type = unsignedbv_typet{output_size};
|
234 | 248 | const auto pointer_init_expr =
|
235 | 249 | typecast_exprt::conditional_cast(init_expr, pointer_unsigned_corr_type);
|
236 |
| - const auto pointer_expected = typecast_exprt::conditional_cast( |
| 250 | + const auto pointer_expected = make_byte_extract( |
237 | 251 | replicate_expression(
|
238 | 252 | pointer_init_expr, pointer_unsigned_corr_type, replication_count),
|
| 253 | + from_integer(0, char_type()), |
239 | 254 | pointer_type);
|
240 | 255 |
|
241 | 256 | REQUIRE(pointer_typed_result == pointer_expected);
|
| 257 | + |
| 258 | + // Check that replicating a float is same as unsigned_bv modulo a typecast |
| 259 | + // outside. |
| 260 | + const std::size_t float_replication_count = |
| 261 | + (float_type().get_width() + config.ansi_c.char_width - 1) / |
| 262 | + config.ansi_c.char_width; |
| 263 | + const auto float_typed_result = duplicate_per_byte(init_expr, float_type()); |
| 264 | + const auto float_unsigned_corr_type = |
| 265 | + unsignedbv_typet{float_type().get_width()}; |
| 266 | + const auto float_init_expr = |
| 267 | + typecast_exprt::conditional_cast(init_expr, float_unsigned_corr_type); |
| 268 | + const auto float_expected = make_byte_extract( |
| 269 | + replicate_expression( |
| 270 | + float_init_expr, float_unsigned_corr_type, float_replication_count), |
| 271 | + from_integer(0, char_type()), |
| 272 | + float_type()); |
| 273 | + |
| 274 | + REQUIRE(float_typed_result == float_expected); |
242 | 275 | }
|
243 | 276 | }
|
244 | 277 |
|
@@ -386,6 +419,47 @@ TEST_CASE(
|
386 | 419 | }
|
387 | 420 | }
|
388 | 421 |
|
| 422 | +TEST_CASE("expr_initializer on float type", "[core][util][expr_initializer]") |
| 423 | +{ |
| 424 | + auto test = expr_initializer_test_environmentt::make(); |
| 425 | + SECTION("Testing with expected type as float") |
| 426 | + { |
| 427 | + const typet input_type = float_type(); |
| 428 | + SECTION("nondet_initializer works") |
| 429 | + { |
| 430 | + const auto result = nondet_initializer(input_type, test.loc, test.ns); |
| 431 | + REQUIRE(result.has_value()); |
| 432 | + const auto expected = side_effect_expr_nondett{float_type(), test.loc}; |
| 433 | + REQUIRE(result.value() == expected); |
| 434 | + const auto expr_result = |
| 435 | + expr_initializer(input_type, test.loc, test.ns, exprt(ID_nondet)); |
| 436 | + REQUIRE(expr_result == result); |
| 437 | + } |
| 438 | + SECTION("zero_initializer works") |
| 439 | + { |
| 440 | + const auto result = zero_initializer(input_type, test.loc, test.ns); |
| 441 | + REQUIRE(result.has_value()); |
| 442 | + auto expected = |
| 443 | + from_integer(0, float_type()); |
| 444 | + REQUIRE(result.value() == expected); |
| 445 | + const auto expr_result = expr_initializer( |
| 446 | + input_type, test.loc, test.ns, constant_exprt(ID_0, char_type())); |
| 447 | + REQUIRE(expr_result == result); |
| 448 | + } |
| 449 | + SECTION("expr_initializer calls duplicate_per_byte") |
| 450 | + { |
| 451 | + const exprt init_value = |
| 452 | + from_integer(0x0A, unsignedbv_typet{config.ansi_c.char_width}); |
| 453 | + const auto result = |
| 454 | + expr_initializer(input_type, test.loc, test.ns, init_value); |
| 455 | + REQUIRE(result.has_value()); |
| 456 | + const auto expected = duplicate_per_byte( |
| 457 | + init_value, float_type()); |
| 458 | + REQUIRE(result.value() == expected); |
| 459 | + } |
| 460 | + } |
| 461 | +} |
| 462 | + |
389 | 463 | TEST_CASE(
|
390 | 464 | "expr_initializer on c_enum and c_enum_tag",
|
391 | 465 | "[core][util][expr_initializer]")
|
|
0 commit comments