From fa5b1eed77d6b012ab440f0bd75e1b155985e671 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Fri, 29 Mar 2019 20:17:26 +0100 Subject: core: move parsing of /wait delay in a separate function, with unit tests --- tests/unit/core/test-core-util.cpp | 49 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'tests/unit') diff --git a/tests/unit/core/test-core-util.cpp b/tests/unit/core/test-core-util.cpp index 86627a3fe..0d29a832d 100644 --- a/tests/unit/core/test-core-util.cpp +++ b/tests/unit/core/test-core-util.cpp @@ -93,6 +93,55 @@ TEST(CoreUtil, GetTimeString) STRCMP_EQUAL("Sat, 01 Jan 2000 00:00:00", str_date); } +/* + * Tests functions: + * util_parse_delay + */ + +TEST(CoreUtil, ParseDelay) +{ + /* error: no string */ + LONGS_EQUAL(-1, util_parse_delay (NULL, -1)); + LONGS_EQUAL(-1, util_parse_delay (NULL, 0)); + LONGS_EQUAL(-1, util_parse_delay (NULL, 1)); + LONGS_EQUAL(-1, util_parse_delay ("", -1)); + LONGS_EQUAL(-1, util_parse_delay ("", 0)); + LONGS_EQUAL(-1, util_parse_delay ("", 1)); + + /* error: bad default_factor */ + LONGS_EQUAL(-1, util_parse_delay ("abcd", -1)); + LONGS_EQUAL(-1, util_parse_delay ("abcd", 0)); + LONGS_EQUAL(-1, util_parse_delay ("123", -1)); + LONGS_EQUAL(-1, util_parse_delay ("123", 0)); + + /* error: bad unit */ + LONGS_EQUAL(-1, util_parse_delay ("123a", 1)); + LONGS_EQUAL(-1, util_parse_delay ("123ss", 1)); + LONGS_EQUAL(-1, util_parse_delay ("123mss", 1)); + + /* error: bad number */ + LONGS_EQUAL(-1, util_parse_delay ("abcd", 1)); + + /* tests with delay == 0 */ + LONGS_EQUAL(0, util_parse_delay ("0", 1)); + LONGS_EQUAL(0, util_parse_delay ("0s", 1)); + LONGS_EQUAL(0, util_parse_delay ("0m", 1)); + LONGS_EQUAL(0, util_parse_delay ("0h", 1)); + + /* tests with delay == 123, default_factor = 1 */ + LONGS_EQUAL(123, util_parse_delay ("123", 1)); + LONGS_EQUAL(123, util_parse_delay ("123", 1)); + LONGS_EQUAL(123 * 1000, util_parse_delay ("123s", 1)); + LONGS_EQUAL(123 * 1000 * 60, util_parse_delay ("123m", 1)); + LONGS_EQUAL(123 * 1000 * 60 * 60, util_parse_delay ("123h", 1)); + + /* tests with delay == 123, default_factor = 1000 */ + LONGS_EQUAL(123 * 1000, util_parse_delay ("123", 1000)); + LONGS_EQUAL(123 * 1000, util_parse_delay ("123s", 1000)); + LONGS_EQUAL(123 * 1000 * 60, util_parse_delay ("123m", 1000)); + LONGS_EQUAL(123 * 1000 * 60 * 60, util_parse_delay ("123h", 1000)); +} + /* * Tests functions: * util_signal_search -- cgit v1.2.3