]> git.ipfire.org Git - pakfire.git/commitdiff
tests/string.c: implemented a test for the pakfire_format_speed function
authorRico Hoppe <rico.hoppe@ipfire.org>
Mon, 20 Jan 2025 16:46:56 +0000 (16:46 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 23 Jan 2025 21:12:31 +0000 (21:12 +0000)
Signed-off-by: Rico Hoppe <rico.hoppe@ipfire.org>
tests/libpakfire/string.c

index 56a093cf994e7238e8dc979beaf2e263e82768db..a4a56ff450ea960a6c855463ddddd26b16a149e6 100644 (file)
@@ -278,6 +278,31 @@ FAIL:
        return EXIT_FAILURE;
 }
 
+static int test_format_speed(const struct test* t) {
+       char buffer[128];
+       char small_buffer[2];
+       int r;
+
+       ASSERT_SUCCESS(pakfire_format_speed(buffer, 0));
+       ASSERT_STRING_EQUALS(buffer, "   0B/s");
+
+       ASSERT_SUCCESS(pakfire_format_speed(buffer, 1024));
+       ASSERT_STRING_EQUALS(buffer, "   1kB/s");
+
+       ASSERT_SUCCESS(pakfire_format_speed(buffer, 1536));
+       ASSERT_STRING_EQUALS(buffer, "   2kB/s");
+
+       ASSERT_SUCCESS(pakfire_format_speed(buffer, 1024 * 1024) );
+       ASSERT_STRING_EQUALS(buffer, " 1.0MB/s");
+
+       ASSERT_ERROR(pakfire_format_speed(small_buffer, 0), ENOBUFS);
+
+       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);
@@ -398,6 +423,7 @@ int main(int argc, const char* argv[]) {
        testsuite_add_test(test_string_replace, 0);
        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_parse_bytes, 0);
        testsuite_add_test(test_intervals, 0);
        testsuite_add_test(test_string_contains_whitespace, 0);