Commit 1da9fe3d by Patrick Steinhardt

tests: core::strtol: test for some more edge-cases

Some edge cases were currently completely untested, e.g. parsing numbers
greater than INT64_{MIN,MAX}, truncating buffers by length and invalid
characters. Add tests to verify that the system under test performs as
expected.

(cherry picked from commit 39087ab8)
parent d4a4e6be
...@@ -20,13 +20,24 @@ static void assert_l64_parses(const char *string, int64_t expected, int base) ...@@ -20,13 +20,24 @@ static void assert_l64_parses(const char *string, int64_t expected, int base)
cl_assert_equal_i(i, expected); cl_assert_equal_i(i, expected);
} }
static void assert_l64_fails(const char *string, int base)
{
int64_t i;
cl_git_fail(git__strntol64(&i, string, strlen(string), NULL, base));
}
void test_core_strtol__int32(void) void test_core_strtol__int32(void)
{ {
assert_l32_parses("123", 123, 10); assert_l32_parses("123", 123, 10);
assert_l32_parses(" +123 ", 123, 10); assert_l32_parses(" +123 ", 123, 10);
assert_l32_parses(" +2147483647 ", 2147483647, 10); assert_l32_parses(" +2147483647 ", 2147483647, 10);
assert_l32_parses(" -2147483648 ", -2147483648LL, 10); assert_l32_parses(" -2147483648 ", -2147483648LL, 10);
assert_l32_parses("A", 10, 16);
assert_l32_parses("1x1", 1, 10);
assert_l32_fails("", 10);
assert_l32_fails("a", 10);
assert_l32_fails("x10x", 10);
assert_l32_fails(" 2147483657 ", 10); assert_l32_fails(" 2147483657 ", 10);
assert_l32_fails(" -2147483657 ", 10); assert_l32_fails(" -2147483657 ", 10);
} }
...@@ -43,4 +54,24 @@ void test_core_strtol__int64(void) ...@@ -43,4 +54,24 @@ void test_core_strtol__int64(void)
assert_l64_parses(" -9223372036854775808 ", INT64_MIN, 10); assert_l64_parses(" -9223372036854775808 ", INT64_MIN, 10);
assert_l64_parses(" 0x7fffffffffffffff ", INT64_MAX, 16); assert_l64_parses(" 0x7fffffffffffffff ", INT64_MAX, 16);
assert_l64_parses(" -0x8000000000000000 ", INT64_MIN, 16); assert_l64_parses(" -0x8000000000000000 ", INT64_MIN, 16);
assert_l64_parses("1a", 26, 16);
assert_l64_parses("1A", 26, 16);
assert_l64_fails("", 10);
assert_l64_fails("a", 10);
assert_l64_fails("x10x", 10);
assert_l64_fails("0x8000000000000000", 16);
assert_l64_fails("-0x8000000000000001", 16);
}
void test_core_strtol__buffer_length_truncates(void)
{
int32_t i32;
int64_t i64;
cl_git_pass(git__strntol32(&i32, "11", 1, NULL, 10));
cl_assert_equal_i(i32, 1);
cl_git_pass(git__strntol64(&i64, "11", 1, NULL, 10));
cl_assert_equal_i(i64, 1);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment