From: Richard Russon Date: Wed, 24 Apr 2019 18:37:23 +0000 (+0100) Subject: test: mutt_str_atoul() X-Git-Tag: 2019-10-25~242^2~3 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8790010ed228aad52ef25cccb02010675e8bf4c6;p=neomutt test: mutt_str_atoul() --- diff --git a/test/string/mutt_str_atoul.c b/test/string/mutt_str_atoul.c index 5ec4b8889..622508acb 100644 --- a/test/string/mutt_str_atoul.c +++ b/test/string/mutt_str_atoul.c @@ -23,13 +23,75 @@ #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 + unsigned 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 }, + + { "18446744073709551613", 0, 18446744073709551613UL }, + { "18446744073709551614", 0, 18446744073709551614UL }, + { "18446744073709551615", 0, 18446744073709551615UL }, + + // Out of range tests + { "18446744073709551616", -1, ULONG_MAX }, + { "18446744073709551617", -1, ULONG_MAX }, + { "18446744073709551618", -1, ULONG_MAX }, + + // Invalid tests + { "-3", 0, 18446744073709551613UL }, + { " -3", 0, 18446744073709551613UL }, + { " -3", 0, 18446744073709551613UL }, + { "abc", 1, 0 }, + { "a123", 1, 0 }, + { "a-123", 1, 0 }, + { "0a", 1, 0 }, + { "123a", 1, 123 }, + { "1,234", 1, 1 }, + { "1.234", 1, 1 }, + { ".123", 1, 0 }, + { "3 ", 1, 3 }, +}; +// clang-format on + +static const int UNEXPECTED = -9999; + void test_mutt_str_atoul(void) { // int mutt_str_atoul(const char *str, unsigned long *dst); + unsigned long result = UNEXPECTED; + int retval = 0; + + // Degenerate tests + TEST_CHECK(mutt_str_atoul(NULL, &result) == 0); + TEST_CHECK(mutt_str_atoul("42", NULL) == 0); + + // Normal tests + for (size_t i = 0; i < mutt_array_size(tests); i++) { - TEST_CHECK(mutt_str_atoul(NULL, NULL) == 0); + result = UNEXPECTED; + retval = mutt_str_atoul(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: %lu, Got: %lu\n", tests[i].result, result); + } } }