diff options
author | Peter Hutterer <peter.hutterer@who-t.net> | 2018-06-01 10:21:18 +1000 |
---|---|---|
committer | Peter Hutterer <peter.hutterer@who-t.net> | 2018-06-08 13:47:33 +1000 |
commit | be647fbb0d32184636bd4bdd41e899b53ef59509 (patch) | |
tree | f0a6b13704ba4906423f1de1c2a1757dca2268f8 /test/test-misc.c | |
parent | e05fa8444a01c87395dae77844ac7e1352bd75b9 (diff) |
util: add a safe_atou() as unsigned equivalent to safe_atoi()
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Diffstat (limited to 'test/test-misc.c')
-rw-r--r-- | test/test-misc.c | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/test/test-misc.c b/test/test-misc.c index 779baf31..08aa1c8b 100644 --- a/test/test-misc.c +++ b/test/test-misc.c @@ -1198,6 +1198,118 @@ START_TEST(safe_atoi_base_8_test) } END_TEST +struct atou_test { + char *str; + bool success; + unsigned int val; +}; + +START_TEST(safe_atou_test) +{ + struct atou_test tests[] = { + { "10", true, 10 }, + { "20", true, 20 }, + { "-1", false, 0 }, + { "2147483647", true, 2147483647 }, + { "-2147483648", false, 0}, + { "4294967295", true, 4294967295 }, + { "0x0", false, 0 }, + { "-10x10", false, 0 }, + { "1x-99", false, 0 }, + { "", false, 0 }, + { "abd", false, 0 }, + { "xabd", false, 0 }, + { "0xaf", false, 0 }, + { "0x0x", false, 0 }, + { "x10", false, 0 }, + { NULL, false, 0 } + }; + unsigned int v; + bool success; + + for (int i = 0; tests[i].str != NULL; i++) { + v = 0xad; + success = safe_atou(tests[i].str, &v); + ck_assert(success == tests[i].success); + if (success) + ck_assert_int_eq(v, tests[i].val); + else + ck_assert_int_eq(v, 0xad); + } +} +END_TEST + +START_TEST(safe_atou_base_16_test) +{ + struct atou_test tests[] = { + { "10", true, 0x10 }, + { "20", true, 0x20 }, + { "-1", false, 0 }, + { "0x10", true, 0x10 }, + { "0xff", true, 0xff }, + { "abc", true, 0xabc }, + { "-10", false, 0 }, + { "0x0", true, 0 }, + { "0", true, 0 }, + { "0x-99", false, 0 }, + { "0xak", false, 0 }, + { "0x", false, 0 }, + { "x10", false, 0 }, + { NULL, false, 0 } + }; + + unsigned int v; + bool success; + + for (int i = 0; tests[i].str != NULL; i++) { + v = 0xad; + success = safe_atou_base(tests[i].str, &v, 16); + ck_assert(success == tests[i].success); + if (success) + ck_assert_int_eq(v, tests[i].val); + else + ck_assert_int_eq(v, 0xad); + } +} +END_TEST + +START_TEST(safe_atou_base_8_test) +{ + struct atou_test tests[] = { + { "7", true, 07 }, + { "10", true, 010 }, + { "20", true, 020 }, + { "-1", false, 0 }, + { "010", true, 010 }, + { "0ff", false, 0 }, + { "abc", false, 0}, + { "0xabc", false, 0}, + { "-10", false, 0 }, + { "0", true, 0 }, + { "00", true, 0 }, + { "0x0", false, 0 }, + { "0x-99", false, 0 }, + { "0xak", false, 0 }, + { "0x", false, 0 }, + { "x10", false, 0 }, + { NULL, false, 0 } + }; + + unsigned int v; + bool success; + + for (int i = 0; tests[i].str != NULL; i++) { + v = 0xad; + success = safe_atou_base(tests[i].str, &v, 8); + ck_assert(success == tests[i].success); + if (success) + ck_assert_int_eq(v, tests[i].val); + else + ck_assert_int_eq(v, 0xad); + } +} +END_TEST + struct atod_test { char *str; bool success; @@ -1675,6 +1787,9 @@ TEST_COLLECTION(misc) litest_add_no_device("misc:parser", safe_atoi_test); litest_add_no_device("misc:parser", safe_atoi_base_16_test); litest_add_no_device("misc:parser", safe_atoi_base_8_test); + litest_add_no_device("misc:parser", safe_atou_test); + litest_add_no_device("misc:parser", safe_atou_base_16_test); + litest_add_no_device("misc:parser", safe_atou_base_8_test); litest_add_no_device("misc:parser", safe_atod_test); litest_add_no_device("misc:parser", strsplit_test); litest_add_no_device("misc:parser", kvsplit_double_test); |