Skip to content

Commit d676e0f

Browse files
committed
chore(hexfloat): add some fixed known-good cases
1 parent 353280b commit d676e0f

File tree

1 file changed

+200
-0
lines changed

1 file changed

+200
-0
lines changed

src/test-hexfloat.cc

+200
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
#include <array>
1718
#include <cstdio>
1819
#include <thread>
1920
#include <vector>
@@ -262,3 +263,202 @@ class ManyDoublesRoundtripTest : public ThreadedTest {
262263
TEST_F(ManyDoublesRoundtripTest, Run) {
263264
RunThreads();
264265
}
266+
267+
class SpecificFloatsTest : public ::testing::Test {
268+
protected:
269+
uint32_t ConstructUint32(const std::array<uint8_t, 4>& bytes) {
270+
uint32_t result = 0;
271+
for (int i = 0; i < 4; ++i) {
272+
result |= static_cast<uint32_t>(bytes[i]) << (i * 8);
273+
}
274+
return result;
275+
}
276+
277+
uint64_t ConstructUint64(const std::array<uint8_t, 8>& bytes) {
278+
uint64_t result = 0;
279+
for (int i = 0; i < 8; ++i) {
280+
result |= static_cast<uint64_t>(bytes[i]) << (i * 8);
281+
}
282+
return result;
283+
}
284+
285+
void TestFloat(const std::array<uint8_t, 4>& bytes,
286+
const std::string& expected_output) {
287+
uint32_t float_bits = ConstructUint32(bytes);
288+
char buffer[100];
289+
WriteFloatHex(buffer, sizeof(buffer), float_bits);
290+
std::cout << "buffer: " << buffer << std::endl;
291+
ASSERT_STREQ(buffer, expected_output.c_str());
292+
uint32_t parsed_bits;
293+
ASSERT_EQ(Result::Ok, ParseFloat(LiteralType::Hexfloat, buffer,
294+
buffer + strlen(buffer), &parsed_bits));
295+
ASSERT_EQ(parsed_bits, float_bits);
296+
}
297+
298+
void TestDouble(const std::array<uint8_t, 8>& bytes,
299+
const std::string& expected_output) {
300+
uint64_t float_bits = ConstructUint64(bytes);
301+
char buffer[100];
302+
WriteDoubleHex(buffer, sizeof(buffer), float_bits);
303+
std::cout << "buffer: " << buffer << std::endl;
304+
ASSERT_STREQ(buffer, expected_output.c_str());
305+
uint64_t parsed_bits;
306+
ASSERT_EQ(Result::Ok, ParseDouble(LiteralType::Hexfloat, buffer,
307+
buffer + strlen(buffer), &parsed_bits));
308+
ASSERT_EQ(parsed_bits, float_bits);
309+
}
310+
};
311+
312+
TEST_F(SpecificFloatsTest, Run) {
313+
TestFloat({0x00, 0x00, 0x00, 0x80}, "-0x0p+0");
314+
TestFloat({0x00, 0x00, 0x00, 0x00}, "0x0p+0");
315+
TestFloat({0x01, 0x00, 0x80, 0xd8}, "-0x1.000002p+50");
316+
TestFloat({0x01, 0x00, 0x80, 0xa6}, "-0x1.000002p-50");
317+
TestFloat({0x01, 0x00, 0x80, 0x58}, "0x1.000002p+50");
318+
TestFloat({0x01, 0x00, 0x80, 0x26}, "0x1.000002p-50");
319+
TestFloat({0x01, 0x00, 0x00, 0x7f}, "0x1.000002p+127");
320+
TestFloat({0x02, 0x00, 0x80, 0xd8}, "-0x1.000004p+50");
321+
TestFloat({0x02, 0x00, 0x80, 0xa6}, "-0x1.000004p-50");
322+
TestFloat({0x02, 0x00, 0x80, 0x58}, "0x1.000004p+50");
323+
TestFloat({0x02, 0x00, 0x80, 0x26}, "0x1.000004p-50");
324+
TestFloat({0x03, 0x00, 0x80, 0xd8}, "-0x1.000006p+50");
325+
TestFloat({0x03, 0x00, 0x80, 0xa6}, "-0x1.000006p-50");
326+
TestFloat({0x03, 0x00, 0x80, 0x58}, "0x1.000006p+50");
327+
TestFloat({0x03, 0x00, 0x80, 0x26}, "0x1.000006p-50");
328+
TestFloat({0xb4, 0xa2, 0x11, 0x52}, "0x1.234568p+37");
329+
TestFloat({0xb4, 0xa2, 0x91, 0x5b}, "0x1.234568p+56");
330+
TestFloat({0xb4, 0xa2, 0x11, 0x65}, "0x1.234568p+75");
331+
TestFloat({0x99, 0x76, 0x96, 0xfe}, "-0x1.2ced32p+126");
332+
TestFloat({0x99, 0x76, 0x96, 0x7e}, "0x1.2ced32p+126");
333+
TestFloat({0x03, 0x00, 0x00, 0x80}, "-0x1.8p-148");
334+
TestFloat({0x03, 0x00, 0x00, 0x00}, "0x1.8p-148");
335+
TestFloat({0xff, 0x2f, 0x59, 0x2d}, "0x1.b25ffep-37");
336+
TestFloat({0xa3, 0x79, 0xeb, 0x4c}, "0x1.d6f346p+26");
337+
TestFloat({0x7b, 0x4d, 0x7f, 0x6c}, "0x1.fe9af6p+89");
338+
TestFloat({0x00, 0x00, 0x00, 0xff}, "-0x1p+127");
339+
TestFloat({0x00, 0x00, 0x00, 0x7f}, "0x1p+127");
340+
TestFloat({0x02, 0x00, 0x00, 0x80}, "-0x1p-148");
341+
TestFloat({0x02, 0x00, 0x00, 0x00}, "0x1p-148");
342+
TestFloat({0x01, 0x00, 0x00, 0x80}, "-0x1p-149");
343+
TestFloat({0x01, 0x00, 0x00, 0x00}, "0x1p-149");
344+
TestFloat({0x00, 0x00, 0x80, 0xd8}, "-0x1p+50");
345+
TestFloat({0x00, 0x00, 0x80, 0xa6}, "-0x1p-50");
346+
TestFloat({0x00, 0x00, 0x80, 0x58}, "0x1p+50");
347+
TestFloat({0x00, 0x00, 0x80, 0x26}, "0x1p-50");
348+
TestFloat({0x00, 0x00, 0x80, 0x7f}, "inf");
349+
TestFloat({0x00, 0x00, 0x80, 0xff}, "-inf");
350+
TestFloat({0x00, 0x00, 0xc0, 0x7f}, "nan");
351+
TestFloat({0x01, 0x00, 0x80, 0x7f}, "nan:0x1");
352+
TestFloat({0xff, 0xff, 0xff, 0x7f}, "nan:0x7fffff");
353+
TestFloat({0x00, 0x00, 0x80, 0x3f}, "0x1p+0");
354+
TestFloat({0x00, 0x00, 0x80, 0xbf}, "-0x1p+0");
355+
TestFloat({0xff, 0xff, 0x7f, 0x7f}, "0x1.fffffep+127");
356+
TestFloat({0xff, 0xff, 0x7f, 0xff}, "-0x1.fffffep+127");
357+
TestFloat({0x00, 0x00, 0x80, 0x4b}, "0x1p+24");
358+
TestFloat({0x00, 0x00, 0x80, 0xcb}, "-0x1p+24");
359+
TestFloat({0xa4, 0x70, 0x9d, 0x3f}, "0x1.3ae148p+0");
360+
361+
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80}, "-0x0p+0");
362+
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, "0x0p+0");
363+
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, 0xc3},
364+
"-0x1.0000000000001p+60");
365+
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, 0x43},
366+
"0x1.0000000000001p+60");
367+
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0xe5},
368+
"-0x1.0000000000001p+600");
369+
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x9a},
370+
"-0x1.0000000000001p-600");
371+
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x65},
372+
"0x1.0000000000001p+600");
373+
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x1a},
374+
"0x1.0000000000001p-600");
375+
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6},
376+
"-0x1.0000000000001p+97");
377+
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46},
378+
"0x1.0000000000001p+97");
379+
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xfe},
380+
"-0x1.0000000000001p+999");
381+
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x7e},
382+
"0x1.0000000000001p+999");
383+
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, 0xc3},
384+
"-0x1.0000000000002p+60");
385+
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, 0x43},
386+
"0x1.0000000000002p+60");
387+
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0xe5},
388+
"-0x1.0000000000002p+600");
389+
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x9a},
390+
"-0x1.0000000000002p-600");
391+
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x65},
392+
"0x1.0000000000002p+600");
393+
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x1a},
394+
"0x1.0000000000002p-600");
395+
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6},
396+
"-0x1.0000000000002p+97");
397+
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46},
398+
"0x1.0000000000002p+97");
399+
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xfe},
400+
"-0x1.0000000000002p+999");
401+
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x7e},
402+
"0x1.0000000000002p+999");
403+
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x80},
404+
"-0x1.0000000000003p-1022");
405+
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00},
406+
"0x1.0000000000003p-1022");
407+
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0xe5},
408+
"-0x1.0000000000003p+600");
409+
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x9a},
410+
"-0x1.0000000000003p-600");
411+
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x65},
412+
"0x1.0000000000003p+600");
413+
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x1a},
414+
"0x1.0000000000003p-600");
415+
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6},
416+
"-0x1.0000000000003p+97");
417+
TestDouble({0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46},
418+
"0x1.0000000000003p+97");
419+
TestDouble({0xa0, 0xc8, 0xeb, 0x85, 0xf3, 0xcc, 0xe1, 0xff},
420+
"-0x1.1ccf385ebc8ap+1023");
421+
TestDouble({0xa0, 0xc8, 0xeb, 0x85, 0xf3, 0xcc, 0xe1, 0x7f},
422+
"0x1.1ccf385ebc8ap+1023");
423+
TestDouble({0xdf, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0xc2, 0x43},
424+
"0x1.23456789abcdfp+61");
425+
TestDouble({0xdf, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0xf2, 0x44},
426+
"0x1.23456789abcdfp+80");
427+
TestDouble({0xdf, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x22, 0x46},
428+
"0x1.23456789abcdfp+99");
429+
TestDouble({0x11, 0x43, 0x2b, 0xd6, 0xff, 0x25, 0xab, 0x3d},
430+
"0x1.b25ffd62b4311p-37");
431+
TestDouble({0x12, 0xec, 0x36, 0xd6, 0xff, 0x25, 0xab, 0x3d},
432+
"0x1.b25ffd636ec12p-37");
433+
TestDouble({0x58, 0xa4, 0x0c, 0x54, 0x34, 0x6f, 0x9d, 0x41},
434+
"0x1.d6f34540ca458p+26");
435+
TestDouble({0x00, 0x00, 0x00, 0x54, 0x34, 0x6f, 0x9d, 0x41},
436+
"0x1.d6f3454p+26");
437+
TestDouble({0xfa, 0x16, 0x5e, 0x5b, 0xaf, 0xe9, 0x8f, 0x45},
438+
"0x1.fe9af5b5e16fap+89");
439+
TestDouble({0xd5, 0xcb, 0x6b, 0x5b, 0xaf, 0xe9, 0x8f, 0x45},
440+
"0x1.fe9af5b6bcbd5p+89");
441+
TestDouble({0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0xff},
442+
"-0x1.fffffffffffffp+1023");
443+
TestDouble({0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0x7f},
444+
"0x1.fffffffffffffp+1023");
445+
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff}, "-0x1p+1023");
446+
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x7f}, "0x1p+1023");
447+
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80}, "-0x1p-1073");
448+
TestDouble({0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, "0x1p-1073");
449+
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80}, "-0x1p-1074");
450+
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, "0x1p-1074");
451+
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, 0xc3}, "-0x1p+60");
452+
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, 0x43}, "0x1p+60");
453+
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0xe5}, "-0x1p+600");
454+
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x9a}, "-0x1p-600");
455+
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x65}, "0x1p+600");
456+
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x1a}, "0x1p-600");
457+
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6}, "-0x1p+97");
458+
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}, "0x1p+97");
459+
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xfe}, "-0x1p+999");
460+
TestDouble({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x7e}, "0x1p+999");
461+
TestDouble({0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x7f}, "nan:0x1");
462+
TestDouble({0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f},
463+
"nan:0xfffffffffffff");
464+
}

0 commit comments

Comments
 (0)