mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 16:32:11 +01:00
Add wuffs_base__parse_number_u64
This commit is contained in:
1 parent
b194c7fbb9
commit
70db96ac6d
11 files changed
+697
-4
No files matched your search
@@ -67,6 +67,87 @@ the first "./a.out" with "./a.out -bench". Combine these changes with the
|
||||
// No mimic library.
|
||||
#endif
|
||||
|
||||
// ---------------- String Conversions Tests
|
||||
|
||||
const char* test_strconv_parse_number_u64() {
|
||||
CHECK_FOCUS(__func__);
|
||||
|
||||
const uint64_t bad = 0xDEADBEEF;
|
||||
|
||||
struct {
|
||||
uint64_t want;
|
||||
const char* str;
|
||||
} test_cases[] = {
|
||||
{.want = 0x0000000000000000, .str = "0"},
|
||||
{.want = 0x0000000000000000, .str = "0_"},
|
||||
{.want = 0x0000000000000000, .str = "0d0"},
|
||||
{.want = 0x0000000000000000, .str = "0x000"},
|
||||
{.want = 0x0000000000000000, .str = "_0"},
|
||||
{.want = 0x0000000000000000, .str = "__0__"},
|
||||
{.want = 0x000000000000004A, .str = "0x4A"},
|
||||
{.want = 0x000000000000004B, .str = "0x__4_B_"},
|
||||
{.want = 0x000000000000007B, .str = "123"},
|
||||
{.want = 0x000000000000007C, .str = "12_4"},
|
||||
{.want = 0x000000000000007D, .str = "_1__2________5_"},
|
||||
{.want = 0x00000000000001F4, .str = "0d500"},
|
||||
{.want = 0x00000000000001F5, .str = "0D___5_01__"},
|
||||
{.want = 0x00000000FFFFFFFF, .str = "4294967295"},
|
||||
{.want = 0x0000000100000000, .str = "4294967296"},
|
||||
{.want = 0x0123456789ABCDEF, .str = "0x0123456789ABCDEF"},
|
||||
{.want = 0x0123456789ABCDEF, .str = "0x0123456789abcdef"},
|
||||
{.want = 0xFFFFFFFFFFFFFFF9, .str = "18446744073709551609"},
|
||||
{.want = 0xFFFFFFFFFFFFFFFA, .str = "18446744073709551610"},
|
||||
{.want = 0xFFFFFFFFFFFFFFFE, .str = "0xFFFFffffFFFFfffe"},
|
||||
{.want = 0xFFFFFFFFFFFFFFFE, .str = "18446744073709551614"},
|
||||
{.want = 0xFFFFFFFFFFFFFFFF, .str = "0xFFFF_FFFF_FFFF_FFFF"},
|
||||
{.want = 0xFFFFFFFFFFFFFFFF, .str = "18446744073709551615"},
|
||||
|
||||
{.want = bad, .str = " "},
|
||||
{.want = bad, .str = " 0"},
|
||||
{.want = bad, .str = " 12 "},
|
||||
{.want = bad, .str = ""},
|
||||
{.want = bad, .str = "+0"},
|
||||
{.want = bad, .str = "+1"},
|
||||
{.want = bad, .str = "-0"},
|
||||
{.want = bad, .str = "-1"},
|
||||
{.want = bad, .str = "0 "},
|
||||
{.want = bad, .str = "0_x1"},
|
||||
{.want = bad, .str = "0d___"},
|
||||
{.want = bad, .str = "0x"},
|
||||
{.want = bad, .str = "0x10000000000000000"}, // 1 << 64.
|
||||
{.want = bad, .str = "0x1_0000_0000_0000_0000"}, // 1 << 64.
|
||||
{.want = bad, .str = "1 23"},
|
||||
{.want = bad, .str = "1,23"},
|
||||
{.want = bad, .str = "1.23"},
|
||||
{.want = bad, .str = "123 "},
|
||||
{.want = bad, .str = "123456789012345678901234"},
|
||||
{.want = bad, .str = "12a3"},
|
||||
{.want = bad, .str = "18446744073709551616"}, // UINT64_MAX.
|
||||
{.want = bad, .str = "18446744073709551617"},
|
||||
{.want = bad, .str = "18446744073709551618"},
|
||||
{.want = bad, .str = "18446744073709551619"},
|
||||
{.want = bad, .str = "18446744073709551620"},
|
||||
{.want = bad, .str = "18446744073709551621"},
|
||||
{.want = bad, .str = "_"},
|
||||
{.want = bad, .str = "d"},
|
||||
{.want = bad, .str = "x"},
|
||||
};
|
||||
|
||||
int i;
|
||||
for (i = 0; i < WUFFS_TESTLIB_ARRAY_SIZE(test_cases); i++) {
|
||||
wuffs_base__result_u64 r =
|
||||
wuffs_base__parse_number_u64(wuffs_base__make_slice_u8(
|
||||
(void*)test_cases[i].str, strlen(test_cases[i].str)));
|
||||
uint64_t got = (r.status.repr == NULL) ? r.value : bad;
|
||||
if (got != test_cases[i].want) {
|
||||
RETURN_FAIL("\"%s\": got 0x%" PRIX64 ", want 0x%" PRIX64,
|
||||
test_cases[i].str, got, test_cases[i].want);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// ---------------- JSON Tests
|
||||
|
||||
const char* test_wuffs_json_decode_tokens() {
|
||||
@@ -131,6 +212,11 @@ const char* test_wuffs_json_decode_tokens() {
|
||||
// The empty comments forces clang-format to place one element per line.
|
||||
proc tests[] = {
|
||||
|
||||
// These strconv tests are really testing the Wuffs base library. They
|
||||
// aren't specific to the std/json code, but putting them here is as good
|
||||
// as any other place.
|
||||
test_strconv_parse_number_u64, //
|
||||
|
||||
test_wuffs_json_decode_tokens, //
|
||||
|
||||
#ifdef WUFFS_MIMIC
|
||||
|
||||
Reference in new issue
Block a user