From ed05e2f1aceb7fc9eb464869de75bfa39d89a7f0 Mon Sep 17 00:00:00 2001 From: Richard Russon Date: Wed, 24 Apr 2019 15:26:04 +0100 Subject: [PATCH] test: mutt_str_atol() --- test/string/mutt_str_atol.c | 83 ++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/test/string/mutt_str_atol.c b/test/string/mutt_str_atol.c index e3a727a41..8be49b2ae 100644 --- a/test/string/mutt_str_atol.c +++ b/test/string/mutt_str_atol.c @@ -23,13 +23,94 @@ #define TEST_NO_MAIN #include "acutest.h" #include "config.h" +#include #include "mutt/mutt.h" +struct TestValue +{ + const char *str; ///< String to test + int retval; ///< Expected return value + long result; ///< Expected result (outparam) +}; + +// clang-format off +static const struct TestValue tests[] = { + // Valid tests + { "0", 0, 0 }, + { "1", 0, 1 }, + { "2", 0, 2 }, + { "3", 0, 3 }, + { " 3", 0, 3 }, + { " 3", 0, 3 }, + + { "9223372036854775805", 0, 9223372036854775805 }, + { "9223372036854775806", 0, 9223372036854775806 }, + { "9223372036854775807", 0, LONG_MAX }, + + { "-1", 0, -1 }, + { "-2", 0, -2 }, + { "-3", 0, -3 }, + { " -3", 0, -3 }, + { " -3", 0, -3 }, + + { "-9223372036854775806", 0, -9223372036854775806 }, + { "-9223372036854775807", 0, -9223372036854775807 }, + { "-9223372036854775808", 0, LONG_MIN }, + + // Out of range tests + { "9223372036854775808", -2, LONG_MAX }, + { "9223372036854775809", -2, LONG_MAX }, + { "9223372036854775810", -2, LONG_MAX }, + + { "-9223372036854775809", -2, LONG_MIN }, + { "-9223372036854775810", -2, LONG_MIN }, + { "-9223372036854775811", -2, LONG_MIN }, + + // Invalid tests + { "abc", -1, 0 }, + { "a123", -1, 0 }, + { "a-123", -1, 0 }, + { "0a", -1, 0 }, + + { "123a", -1, 123 }, + { "-123a", -1, -123 }, + + { "1,234", -1, 1 }, + { "-1,234", -1, -1 }, + { "1.234", -1, 1 }, + { "-1.234", -1, -1 }, + + { ".123", -1, 0 }, + { "-.123", -1, 0 }, + { "3 ", -1, 3 }, + { "-3 ", -1, -3 }, +}; +// clang-format on + +static const long UNEXPECTED = -9999; + void test_mutt_str_atol(void) { // int mutt_str_atol(const char *str, long *dst); + long result = UNEXPECTED; + int retval = 0; + + // Degenerate tests + TEST_CHECK(mutt_str_atol(NULL, &result) == 0); + TEST_CHECK(mutt_str_atol("42", NULL) == 0); + + // Normal tests + for (size_t i = 0; i < mutt_array_size(tests); i++) { - TEST_CHECK(mutt_str_atol(NULL, NULL) == 0); + result = UNEXPECTED; + retval = mutt_str_atol(tests[i].str, &result); + + const bool success = (retval == tests[i].retval) && (result == tests[i].result); + if (!TEST_CHECK_(success, "Testing '%s'\n", tests[i].str)) + { + TEST_MSG("retval: Expected: %d, Got: %d\n", tests[i].retval, retval); + TEST_MSG("result: Expected: %ld, Got: %ld\n", tests[i].result, result); + } } } -- 2.40.0