]> git.ipfire.org Git - pakfire.git/commitdiff
tests/string.c: add test function for time formatting
authorRico Hoppe <rico.hoppe@ipfire.org>
Fri, 7 Feb 2025 16:11:58 +0000 (16:11 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 10 Feb 2025 17:08:39 +0000 (17:08 +0000)
Signed-off-by: Rico Hoppe <rico.hoppe@ipfire.org>
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
tests/libpakfire/string.c

index 56e01a96bffbf111313f4005344e4deddd02ac17..706c0b4a2fd9e26598b8b9b306558ab77d0540d8 100644 (file)
@@ -303,6 +303,35 @@ FAIL:
        return EXIT_FAILURE;
 }
 
+static int test_format_time(const struct test* t) {
+       char buffer[128];
+
+       // Values smaller than zero
+       ASSERT_SUCCESS(pakfire_format_time(buffer, -1));
+       ASSERT_STRING_EQUALS(buffer, NULL);
+
+       // Values greater than a day
+       ASSERT_SUCCESS(pakfire_format_time(buffer, 86400));
+       ASSERT_STRING_EQUALS(buffer, "1d0h0m");
+
+       // Values greater than an hour
+       ASSERT_SUCCESS(pakfire_format_time(buffer, 3600));
+       ASSERT_STRING_EQUALS(buffer, "1h0m0s");
+
+       // Values greater than a minute
+       ASSERT_SUCCESS(pakfire_format_time(buffer, 60));
+       ASSERT_STRING_EQUALS(buffer, "1m0s");
+
+       // Values below a minute
+       ASSERT_SUCCESS(pakfire_format_time(buffer, 1));
+       ASSERT_STRING_EQUALS(buffer, "1s");
+
+       return EXIT_SUCCESS;
+
+FAIL:
+       return EXIT_FAILURE;
+}
+
 static int test_parse_bytes(const struct test* t) {
        // Zero without unit
        ASSERT(pakfire_string_parse_bytes("0") == (size_t)0);
@@ -449,6 +478,7 @@ int main(int argc, const char* argv[]) {
        testsuite_add_test(test_string_join, 0);
        testsuite_add_test(test_format_size, 0);
        testsuite_add_test(test_format_speed, 0);
+       testsuite_add_test(test_format_time, 0);
        testsuite_add_test(test_parse_bytes, 0);
        testsuite_add_test(test_intervals, 0);
        testsuite_add_test(test_string_contains_whitespace, 0);