/* * Copyright 2015 (c) Anna Schumaker. * Test the string formatting functions. */ #include #include #include void test_sec2str() { g_assert_cmpstr_free(string_sec2str(0), ==, "0:00"); g_assert_cmpstr_free(string_sec2str(5), ==, "0:05"); g_assert_cmpstr_free(string_sec2str(10), ==, "0:10"); g_assert_cmpstr_free(string_sec2str(60), ==, "1:00"); g_assert_cmpstr_free(string_sec2str(65), ==, "1:05"); g_assert_cmpstr_free(string_sec2str(75), ==, "1:15"); g_assert_cmpstr_free(string_sec2str(600), ==, "10:00"); g_assert_cmpstr_free(string_sec2str(605), ==, "10:05"); g_assert_cmpstr_free(string_sec2str(615), ==, "10:15"); g_assert_cmpstr_free(string_sec2str(660), ==, "11:00"); g_assert_cmpstr_free(string_sec2str(665), ==, "11:05"); g_assert_cmpstr_free(string_sec2str(675), ==, "11:15"); } void test_sec2str_long() { unsigned int minute = 60; unsigned int hour = 60 * minute; unsigned int day = 24 * hour; g_assert_cmpstr_free(string_sec2str_long(0), ==, ""); g_assert_cmpstr_free(string_sec2str_long(1), ==, "1 second"); g_assert_cmpstr_free(string_sec2str_long(2), ==, "2 seconds"); g_assert_cmpstr_free(string_sec2str_long(minute), ==, "1 minute"); g_assert_cmpstr_free(string_sec2str_long(2 * minute), ==, "2 minutes"); g_assert_cmpstr_free(string_sec2str_long(minute + 1), ==, "1 minute, 1 second"); g_assert_cmpstr_free(string_sec2str_long(hour), ==, "1 hour"); g_assert_cmpstr_free(string_sec2str_long(2 * hour), ==, "2 hours"); g_assert_cmpstr_free(string_sec2str_long(hour + minute), ==, "1 hour, 1 minute"); g_assert_cmpstr_free(string_sec2str_long(day), ==, "1 day"); g_assert_cmpstr_free(string_sec2str_long(2 * day), ==, "2 days"); g_assert_cmpstr_free(string_sec2str_long(day + hour), ==, "1 day, 1 hour"); g_assert_cmpstr_free( string_sec2str_long((22 * day) + (4 * hour) + (12 * minute) + 32), ==, "22 days, 4 hours, 12 minutes, 32 seconds"); } void test_date2str() { struct tm tm = { .tm_year = 92, .tm_mon = 9, .tm_mday = 5, }; setlocale(LC_TIME, "C"); g_assert_cmpstr_free(string_tm2str(&tm), ==, "10/05/92"); setlocale(LC_TIME, "en_US"); g_assert_cmpstr_free(string_tm2str(&tm), ==, "10/05/1992"); } void test_compare_tokens() { gchar *a[2] = { "a", NULL }; gchar *A[2] = { "A", NULL }; gchar *B[2] = { "B", NULL }; gchar *aa[3] = { "a", "a", NULL }; gchar *ab[3] = { "a", "b", NULL }; gchar *ba[3] = { "b", "a", NULL }; gchar *null[1] = { NULL }; g_assert_cmpint(string_compare_tokens(A, A), ==, 0); g_assert_cmpint(string_compare_tokens(a, a), ==, 0); g_assert_cmpint(string_compare_tokens(A, B), ==, -1); g_assert_cmpint(string_compare_tokens(B, A), ==, 1); g_assert_cmpint(string_compare_tokens(null, null), ==, 0); g_assert_cmpint(string_compare_tokens(null, A), ==, 1); g_assert_cmpint(string_compare_tokens(A, null), ==, -1); g_assert_cmpint(string_compare_tokens(aa, aa), ==, 0); g_assert_cmpint(string_compare_tokens(a, aa), ==, -1); g_assert_cmpint(string_compare_tokens(aa, a), ==, 1); g_assert_cmpint(string_compare_tokens(ab, ba), ==, -1); g_assert_cmpint(string_compare_tokens(ba, ab), ==, 1); } void test_match() { g_assert_false(string_match(NULL, NULL)); g_assert_false(string_match(NULL, "A")); g_assert_false(string_match("A", NULL)); g_assert_true( string_match("A", "A")); g_assert_false(string_match("A", "B")); } void test_match_tokens() { gchar *tokens[3] = { "hyrule", "symphony", NULL }; g_assert_true( string_match_token("hyr", tokens)); g_assert_true( string_match_token("sym", tokens)); g_assert_false(string_match_token("rule", tokens)); } void test_length() { g_assert_cmpint(string_length(NULL), ==, 0); g_assert_cmpint(string_length(""), ==, 0); g_assert_cmpint(string_length("a"), ==, 1); g_assert_cmpint(string_length("abcdefghijklmnopqrstuvwxyz"), ==, 26); } int main(int argc, char **argv) { g_test_init(&argc, &argv, NULL); g_test_add_func("/Core/String/Seconds to String", test_sec2str); g_test_add_func("/Core/String/Seconds to String (Long)", test_sec2str_long); g_test_add_func("/Core/String/Date to String", test_date2str); g_test_add_func("/Core/String/Comparison/Tokens", test_compare_tokens); g_test_add_func("/Core/String/Matching", test_match); g_test_add_func("/Core/String/Matching/Tokens", test_match_tokens); g_test_add_func("/Core/String/Length", test_length); return g_test_run(); }