From 633ecbe3659456c294dd9cc37c6c50cf80b77a66 Mon Sep 17 00:00:00 2001 From: Richard Russon Date: Tue, 23 Apr 2019 00:05:39 +0100 Subject: [PATCH] test: mutt_str_atos() --- test/string/mutt_str_atos.c | 79 ++++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/test/string/mutt_str_atos.c b/test/string/mutt_str_atos.c index e25dc4a74..dea7cb3aa 100644 --- a/test/string/mutt_str_atos.c +++ b/test/string/mutt_str_atos.c @@ -25,11 +25,88 @@ #include "config.h" #include "mutt/mutt.h" +struct TestValue +{ + const char *str; ///< String to test + int retval; ///< Expected return value + int 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 }, + + { "32765", 0, 32765 }, + { "32766", 0, 32766 }, + { "32767", 0, 32767 }, + + { "-1", 0, -1 }, + { "-2", 0, -2 }, + { "-3", 0, -3 }, + { " -3", 0, -3 }, + { " -3", 0, -3 }, + + { "-32766", 0, -32766 }, + { "-32767", 0, -32767 }, + { "-32768", 0, -32768 }, + + // Out of range tests + { "32768", -2, 0 }, + { "32769", -2, 0 }, + { "32770", -2, 0 }, + + { "-32769", -2, 0 }, + { "-32770", -2, 0 }, + { "-32771", -2, 0 }, + + // Invalid tests + { "abc", -1, 0 }, + { "a123", -1, 0 }, + { "a-123", -1, 0 }, + { "0a", -1, 0 }, + { "123a", -1, 0 }, + { "-123a", -1, 0 }, + { "1,234", -1, 0 }, + { "-1,234", -1, 0 }, + { "1.234", -1, 0 }, + { "-1.234", -1, 0 }, + { ".123", -1, 0 }, + { "-.123", -1, 0 }, + { "3 ", -1, 0 }, + { "-3 ", -1, 0 }, +}; +// clang-format on + +static const int UNEXPECTED = -9999; + void test_mutt_str_atos(void) { // int mutt_str_atos(const char *str, short *dst); + short result = UNEXPECTED; + int retval = 0; + + // Degenerate tests + TEST_CHECK(mutt_str_atos(NULL, &result) == 0); + TEST_CHECK(mutt_str_atos("42", NULL) == 0); + + // Normal tests + for (size_t i = 0; i < mutt_array_size(tests); i++) { - TEST_CHECK(mutt_str_atos(NULL, NULL) == 0); + result = UNEXPECTED; + retval = mutt_str_atos(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: %d, Got: %d\n", tests[i].result, result); + } } } -- 2.40.0