Skip to content

Commit

Permalink
chore(hexfloat): add some fixed known-good cases
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Dec 1, 2023
1 parent 760aa85 commit 2e1b9b5
Showing 1 changed file with 200 additions and 0 deletions.
200 changes: 200 additions & 0 deletions src/test-hexfloat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

#include <array>
#include <cstdio>
#include <thread>
#include <vector>
Expand Down Expand Up @@ -262,3 +263,202 @@ class ManyDoublesRoundtripTest : public ThreadedTest {
TEST_F(ManyDoublesRoundtripTest, Run) {
RunThreads();
}

class SpecificFloatsTest : public ::testing::Test {
protected:
uint32_t ConstructUint32(const std::array<uint8_t, 4>& bytes) {
uint32_t result = 0;
for (int i = 0; i < 4; ++i) {
result |= static_cast<uint32_t>(bytes[i]) << (i * 8);
}
return result;
}

uint64_t ConstructUint64(const std::array<uint8_t, 8>& bytes) {
uint64_t result = 0;
for (int i = 0; i < 8; ++i) {
result |= static_cast<uint64_t>(bytes[i]) << (i * 8);
}
return result;
}

void TestFloat(const std::array<uint8_t, 4>& bytes,
const std::string& expected_output) {
uint32_t float_bits = ConstructUint32(bytes);
char buffer[100];
WriteFloatHex(buffer, sizeof(buffer), float_bits);
std::cout << "buffer: " << buffer << std::endl;
ASSERT_STREQ(buffer, expected_output.c_str());
uint32_t parsed_bits;
ASSERT_EQ(Result::Ok, ParseFloat(LiteralType::Hexfloat, buffer,
buffer + strlen(buffer), &parsed_bits));
ASSERT_EQ(parsed_bits, float_bits);
}

void TestDouble(const std::array<uint8_t, 8>& bytes,
const std::string& expected_output) {
uint64_t float_bits = ConstructUint64(bytes);
char buffer[100];
WriteDoubleHex(buffer, sizeof(buffer), float_bits);
std::cout << "buffer: " << buffer << std::endl;
ASSERT_STREQ(buffer, expected_output.c_str());
uint64_t parsed_bits;
ASSERT_EQ(Result::Ok, ParseDouble(LiteralType::Hexfloat, buffer,
buffer + strlen(buffer), &parsed_bits));
ASSERT_EQ(parsed_bits, float_bits);
}
};

TEST_F(SpecificFloatsTest, Run) {
TestFloat({0x00, 0x00, 0x00, 0x80}, "-0x0p+0");
TestFloat({0x00, 0x00, 0x00, 0x00}, "0x0p+0");
TestFloat({0x01, 0x00, 0x80, 0xd8}, "-0x1.000002p+50");
TestFloat({0x01, 0x00, 0x80, 0xa6}, "-0x1.000002p-50");
TestFloat({0x01, 0x00, 0x80, 0x58}, "0x1.000002p+50");
TestFloat({0x01, 0x00, 0x80, 0x26}, "0x1.000002p-50");
TestFloat({0x01, 0x00, 0x00, 0x7f}, "0x1.000002p+127");
TestFloat({0x02, 0x00, 0x80, 0xd8}, "-0x1.000004p+50");
TestFloat({0x02, 0x00, 0x80, 0xa6}, "-0x1.000004p-50");
TestFloat({0x02, 0x00, 0x80, 0x58}, "0x1.000004p+50");
TestFloat({0x02, 0x00, 0x80, 0x26}, "0x1.000004p-50");
TestFloat({0x03, 0x00, 0x80, 0xd8}, "-0x1.000006p+50");
TestFloat({0x03, 0x00, 0x80, 0xa6}, "-0x1.000006p-50");
TestFloat({0x03, 0x00, 0x80, 0x58}, "0x1.000006p+50");
TestFloat({0x03, 0x00, 0x80, 0x26}, "0x1.000006p-50");
TestFloat({0xb4, 0xa2, 0x11, 0x52}, "0x1.234568p+37");
TestFloat({0xb4, 0xa2, 0x91, 0x5b}, "0x1.234568p+56");
TestFloat({0xb4, 0xa2, 0x11, 0x65}, "0x1.234568p+75");
TestFloat({0x99, 0x76, 0x96, 0xfe}, "-0x1.2ced32p+126");
TestFloat({0x99, 0x76, 0x96, 0x7e}, "0x1.2ced32p+126");
TestFloat({0x03, 0x00, 0x00, 0x80}, "-0x1.8p-148");
TestFloat({0x03, 0x00, 0x00, 0x00}, "0x1.8p-148");
TestFloat({0xff, 0x2f, 0x59, 0x2d}, "0x1.b25ffep-37");
TestFloat({0xa3, 0x79, 0xeb, 0x4c}, "0x1.d6f346p+26");
TestFloat({0x7b, 0x4d, 0x7f, 0x6c}, "0x1.fe9af6p+89");
TestFloat({0x00, 0x00, 0x00, 0xff}, "-0x1p+127");
TestFloat({0x00, 0x00, 0x00, 0x7f}, "0x1p+127");
TestFloat({0x02, 0x00, 0x00, 0x80}, "-0x1p-148");
TestFloat({0x02, 0x00, 0x00, 0x00}, "0x1p-148");
TestFloat({0x01, 0x00, 0x00, 0x80}, "-0x1p-149");
TestFloat({0x01, 0x00, 0x00, 0x00}, "0x1p-149");
TestFloat({0x00, 0x00, 0x80, 0xd8}, "-0x1p+50");
TestFloat({0x00, 0x00, 0x80, 0xa6}, "-0x1p-50");
TestFloat({0x00, 0x00, 0x80, 0x58}, "0x1p+50");
TestFloat({0x00, 0x00, 0x80, 0x26}, "0x1p-50");
TestFloat({0x00, 0x00, 0x80, 0x7f}, "inf");
TestFloat({0x00, 0x00, 0x80, 0xff}, "-inf");
TestFloat({0x00, 0x00, 0xc0, 0x7f}, "nan");
TestFloat({0x01, 0x00, 0x80, 0x7f}, "nan:0x1");
TestFloat({0xff, 0xff, 0xff, 0x7f}, "nan:0x7fffff");
TestFloat({0x00, 0x00, 0x80, 0x3f}, "0x1p+0");
TestFloat({0x00, 0x00, 0x80, 0xbf}, "-0x1p+0");
TestFloat({0xff, 0xff, 0x7f, 0x7f}, "0x1.fffffep+127");
TestFloat({0xff, 0xff, 0x7f, 0xff}, "-0x1.fffffep+127");
TestFloat({0x00, 0x00, 0x80, 0x4b}, "0x1p+24");
TestFloat({0x00, 0x00, 0x80, 0xcb}, "-0x1p+24");
TestFloat({0xa4, 0x70, 0x9d, 0x3f}, "0x1.3ae148p+0");

TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80}, "-0x0p+0");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, "0x0p+0");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, 0xc3},
"-0x1.0000000000001p+60");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, 0x43},
"0x1.0000000000001p+60");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0xe5},
"-0x1.0000000000001p+600");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x9a},
"-0x1.0000000000001p-600");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x65},
"0x1.0000000000001p+600");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x1a},
"0x1.0000000000001p-600");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6},
"-0x1.0000000000001p+97");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46},
"0x1.0000000000001p+97");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xfe},
"-0x1.0000000000001p+999");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x7e},
"0x1.0000000000001p+999");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, 0xc3},
"-0x1.0000000000002p+60");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, 0x43},
"0x1.0000000000002p+60");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0xe5},
"-0x1.0000000000002p+600");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x9a},
"-0x1.0000000000002p-600");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x65},
"0x1.0000000000002p+600");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x1a},
"0x1.0000000000002p-600");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6},
"-0x1.0000000000002p+97");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46},
"0x1.0000000000002p+97");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xfe},
"-0x1.0000000000002p+999");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x7e},
"0x1.0000000000002p+999");
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x80},
"-0x1.0000000000003p-1022");
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00},
"0x1.0000000000003p-1022");
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0xe5},
"-0x1.0000000000003p+600");
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x9a},
"-0x1.0000000000003p-600");
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x65},
"0x1.0000000000003p+600");
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x1a},
"0x1.0000000000003p-600");
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6},
"-0x1.0000000000003p+97");
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46},
"0x1.0000000000003p+97");
TestDouble({0xa0, 0xc8, 0xeb, 0x85, 0xf3, 0xcc, 0xe1, 0xff},
"-0x1.1ccf385ebc8ap+1023");
TestDouble({0xa0, 0xc8, 0xeb, 0x85, 0xf3, 0xcc, 0xe1, 0x7f},
"0x1.1ccf385ebc8ap+1023");
TestDouble({0xdf, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0xc2, 0x43},
"0x1.23456789abcdfp+61");
TestDouble({0xdf, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0xf2, 0x44},
"0x1.23456789abcdfp+80");
TestDouble({0xdf, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x22, 0x46},
"0x1.23456789abcdfp+99");
TestDouble({0x11, 0x43, 0x2b, 0xd6, 0xff, 0x25, 0xab, 0x3d},
"0x1.b25ffd62b4311p-37");
TestDouble({0x12, 0xec, 0x36, 0xd6, 0xff, 0x25, 0xab, 0x3d},
"0x1.b25ffd636ec12p-37");
TestDouble({0x58, 0xa4, 0x0c, 0x54, 0x34, 0x6f, 0x9d, 0x41},
"0x1.d6f34540ca458p+26");
TestDouble({0x00, 0x00, 0x00, 0x54, 0x34, 0x6f, 0x9d, 0x41},
"0x1.d6f3454p+26");
TestDouble({0xfa, 0x16, 0x5e, 0x5b, 0xaf, 0xe9, 0x8f, 0x45},
"0x1.fe9af5b5e16fap+89");
TestDouble({0xd5, 0xcb, 0x6b, 0x5b, 0xaf, 0xe9, 0x8f, 0x45},
"0x1.fe9af5b6bcbd5p+89");
TestDouble({0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0xff},
"-0x1.fffffffffffffp+1023");
TestDouble({0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0x7f},
"0x1.fffffffffffffp+1023");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff}, "-0x1p+1023");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x7f}, "0x1p+1023");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80}, "-0x1p-1073");
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, "0x1p-1073");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80}, "-0x1p-1074");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, "0x1p-1074");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, 0xc3}, "-0x1p+60");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, 0x43}, "0x1p+60");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0xe5}, "-0x1p+600");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x9a}, "-0x1p-600");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x65}, "0x1p+600");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x1a}, "0x1p-600");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6}, "-0x1p+97");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}, "0x1p+97");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xfe}, "-0x1p+999");
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x7e}, "0x1p+999");
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x7f}, "nan:0x1");
TestDouble({0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f},
"nan:0xfffffffffffff");
}

0 comments on commit 2e1b9b5

Please sign in to comment.