From 6b7411fdb50ef3970371390972eb411f6854075f Mon Sep 17 00:00:00 2001 From: Richard Russon Date: Wed, 24 Apr 2019 15:56:48 +0100 Subject: [PATCH] test: mutt_str_atoui() --- test/string/mutt_str_atoui.c | 64 +++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/test/string/mutt_str_atoui.c b/test/string/mutt_str_atoui.c index 8c83ef2c0..ec0eb7ec7 100644 --- a/test/string/mutt_str_atoui.c +++ b/test/string/mutt_str_atoui.c @@ -25,11 +25,73 @@ #include "config.h" #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 }, + + { "4294967293", 0, 4294967293 }, + { "4294967294", 0, 4294967294 }, + { "4294967295", 0, 4294967295 }, + + // Out of range tests + { "4294967296", -2, 0 }, + { "4294967297", -2, 0 }, + { "4294967298", -2, 0 }, + { "18446744073709551616", -1, 0 }, + + // Invalid tests + { "-3", -2, 0 }, + { " -3", -2, 0 }, + { " -3", -2, 0 }, + { "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_atoui(void) { // int mutt_str_atoui(const char *str, unsigned int *dst); + unsigned int result = UNEXPECTED; + int retval = 0; + + // Degenerate tests + TEST_CHECK(mutt_str_atoui(NULL, &result) == 0); + TEST_CHECK(mutt_str_atoui("42", NULL) == 0); + + // Normal tests + for (size_t i = 0; i < mutt_array_size(tests); i++) { - TEST_CHECK(mutt_str_atoui(NULL, NULL) == 0); + result = UNEXPECTED; + retval = mutt_str_atoui(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); + } } } -- 2.40.0