|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | + Module: Unit tests for expr-to-java string conversion |
| 4 | +
|
| 5 | + Author: DiffBlue Limited. All rights reserved. |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +#include <testing-utils/catch.hpp> |
| 10 | +#include <java_bytecode/expr2java.h> |
| 11 | + |
| 12 | +TEST_CASE( |
| 13 | + "expr2java tests", |
| 14 | + "[core][java_bytecode][expr2java][floating_point_to_java_string]") |
| 15 | +{ |
| 16 | + SECTION("0.0 double to string") |
| 17 | + { |
| 18 | + REQUIRE(floating_point_to_java_string(0.0) == "0.0"); |
| 19 | + } |
| 20 | + |
| 21 | + SECTION("0.0 float to string") |
| 22 | + { |
| 23 | + REQUIRE(floating_point_to_java_string(0.0f) == "0.0f"); |
| 24 | + } |
| 25 | + |
| 26 | + SECTION("-0.0 double to string") |
| 27 | + { |
| 28 | + REQUIRE(floating_point_to_java_string(-0.0) == "-0.0"); |
| 29 | + } |
| 30 | + |
| 31 | + SECTION("-0.0 float to string") |
| 32 | + { |
| 33 | + REQUIRE(floating_point_to_java_string(-0.0f) == "-0.0f"); |
| 34 | + } |
| 35 | + |
| 36 | + SECTION("1.0 double to string") |
| 37 | + { |
| 38 | + REQUIRE(floating_point_to_java_string(1.0) == "1.0"); |
| 39 | + } |
| 40 | + |
| 41 | + SECTION("1.0 float to string") |
| 42 | + { |
| 43 | + REQUIRE(floating_point_to_java_string(1.0f) == "1.0f"); |
| 44 | + } |
| 45 | + |
| 46 | + SECTION("-1.0 double to string") |
| 47 | + { |
| 48 | + REQUIRE(floating_point_to_java_string(-1.0) == "-1.0"); |
| 49 | + } |
| 50 | + |
| 51 | + SECTION("-1.0 float to string") |
| 52 | + { |
| 53 | + REQUIRE(floating_point_to_java_string(-1.0f) == "-1.0f"); |
| 54 | + } |
| 55 | + |
| 56 | + SECTION("Infinity double to string") |
| 57 | + { |
| 58 | + REQUIRE( |
| 59 | + floating_point_to_java_string(static_cast<double>(INFINITY)) == |
| 60 | + "Double.POSITIVE_INFINITY"); |
| 61 | + } |
| 62 | + |
| 63 | + SECTION("Infinity float to string") |
| 64 | + { |
| 65 | + REQUIRE( |
| 66 | + floating_point_to_java_string(static_cast<float>(INFINITY)) == |
| 67 | + "Float.POSITIVE_INFINITY"); |
| 68 | + } |
| 69 | + |
| 70 | + SECTION("Negative infinity double to string") |
| 71 | + { |
| 72 | + REQUIRE( |
| 73 | + floating_point_to_java_string(static_cast<double>(-INFINITY)) == |
| 74 | + "Double.NEGATIVE_INFINITY"); |
| 75 | + } |
| 76 | + |
| 77 | + SECTION("Negative infinity float to string") |
| 78 | + { |
| 79 | + REQUIRE( |
| 80 | + floating_point_to_java_string(static_cast<float>(-INFINITY)) == |
| 81 | + "Float.NEGATIVE_INFINITY"); |
| 82 | + } |
| 83 | + |
| 84 | + SECTION("Float NaN to string") |
| 85 | + { |
| 86 | + REQUIRE( |
| 87 | + floating_point_to_java_string(static_cast<float>(NAN)) == "Float.NaN"); |
| 88 | + } |
| 89 | + |
| 90 | + SECTION("Double NaN to string") |
| 91 | + { |
| 92 | + REQUIRE( |
| 93 | + floating_point_to_java_string(static_cast<double>(NAN)) == "Double.NaN"); |
| 94 | + } |
| 95 | + |
| 96 | + SECTION("Hex float to string (print a comment)") |
| 97 | + { |
| 98 | + const float value = std::strtod("0x1p+37f", nullptr); |
| 99 | + REQUIRE( |
| 100 | + floating_point_to_java_string(value) == "0x1p+37f /* 1.37439e+11 */"); |
| 101 | + } |
| 102 | + |
| 103 | + SECTION("Hex double to string (print a comment)") |
| 104 | + { |
| 105 | + const double value = std::strtod("0x1p+37f", nullptr); |
| 106 | + REQUIRE( |
| 107 | + floating_point_to_java_string(value) == "0x1p+37 /* 1.37439e+11 */"); |
| 108 | + } |
| 109 | + |
| 110 | + SECTION("Beyond numeric limits") |
| 111 | + { |
| 112 | + REQUIRE( |
| 113 | + floating_point_to_java_string(-5.56268e-309) |
| 114 | + .find("/* -5.56268e-309 */") != std::string::npos); |
| 115 | + } |
| 116 | +} |
0 commit comments