ocarina/tests/core/string.c

139 lines
4.0 KiB
C

/*
* Copyright 2015 (c) Anna Schumaker.
* Test the string formatting functions.
*/
#include <core/string.h>
#include <tests/test.h>
#include <locale.h>
#define str_test_equal(lhs, rhs) \
test_strings_equal(lhs, g_strdup(rhs), __LINE__)
void test_sec2str()
{
str_test_equal(string_sec2str(0), "0:00");
str_test_equal(string_sec2str(5), "0:05");
str_test_equal(string_sec2str(10), "0:10");
str_test_equal(string_sec2str(60), "1:00");
str_test_equal(string_sec2str(65), "1:05");
str_test_equal(string_sec2str(75), "1:15");
str_test_equal(string_sec2str(600), "10:00");
str_test_equal(string_sec2str(605), "10:05");
str_test_equal(string_sec2str(615), "10:15");
str_test_equal(string_sec2str(660), "11:00");
str_test_equal(string_sec2str(665), "11:05");
str_test_equal(string_sec2str(675), "11:15");
}
void test_sec2str_long()
{
unsigned int minute = 60;
unsigned int hour = 3600;
unsigned int day = 86400;
str_test_equal(string_sec2str_long(day), "1 day");
str_test_equal(string_sec2str_long(2 * day), "2 days");
str_test_equal(string_sec2str_long(day + hour), "1 day, 1 hour");
str_test_equal(string_sec2str_long(hour), "1 hour");
str_test_equal(string_sec2str_long(2 * hour), "2 hours");
str_test_equal(string_sec2str_long(hour + minute), "1 hour, 1 minute");
str_test_equal(string_sec2str_long(minute), "1 minute");
str_test_equal(string_sec2str_long(2 * minute), "2 minutes");
str_test_equal(string_sec2str_long(minute + 1), "1 minute, 1 second");
str_test_equal(string_sec2str_long(0), "");
str_test_equal(string_sec2str_long(1), "1 second");
str_test_equal(string_sec2str_long(2), "2 seconds");
str_test_equal(
string_sec2str_long((22 * day) + (4 * hour) + (12 * minute) + 32),
"22 days, 4 hours, 12 minutes, 32 seconds");
}
void test_date()
{
struct tm tm = {
.tm_year = 92,
.tm_mon = 9,
.tm_mday = 5,
};
gchar *str;
setlocale(LC_TIME, "C");
str = string_tm2str(&tm);
test_equal(str, "10/05/92");
g_free(str);
setlocale(LC_TIME, "en_US");
str = string_tm2str(&tm);
test_equal(str, "10/05/1992");
g_free(str);
}
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 };
test_equal(string_compare_tokens(A, A), 0);
test_equal(string_compare_tokens(a, a), 0);
test_equal(string_compare_tokens(A, B), -1);
test_equal(string_compare_tokens(B, A), 1);
test_equal(string_compare_tokens(null, null), 0);
test_equal(string_compare_tokens(null, A), 1);
test_equal(string_compare_tokens(A, null), -1);
test_equal(string_compare_tokens(aa, aa), 0);
test_equal(string_compare_tokens(a, aa), -1);
test_equal(string_compare_tokens(aa, a), 1);
test_equal(string_compare_tokens(ab, ba), -1);
test_equal(string_compare_tokens(ba, ab), 1);
}
void test_match()
{
test_equal(string_match(NULL, NULL), (bool)false);
test_equal(string_match(NULL, "A"), (bool)false);
test_equal(string_match("A", NULL), (bool)false);
test_equal(string_match("A", "A"), (bool)true);
test_equal(string_match("A", "B"), (bool)false);
}
void test_match_tokens()
{
gchar *tokens[3] = { "hyrule", "symphony", NULL };
test_equal(string_match_token("hyr", tokens), (bool)true);
test_equal(string_match_token("sym", tokens), (bool)true);
test_equal(string_match_token("rule", tokens), (bool)false);
}
void test_length()
{
test_equal(string_length(NULL), 0);
test_equal(string_length(""), 0);
test_equal(string_length("a"), 1);
test_equal(string_length("abcdefghijklmnopqrstuvwxyz"), 26);
}
DECLARE_UNIT_TESTS(
UNIT_TEST("Seconds to String", test_sec2str),
UNIT_TEST("Seconds to String (Long)", test_sec2str_long),
UNIT_TEST("Date to String", test_date),
UNIT_TEST("String Comparison (Tokens)", test_compare_tokens),
UNIT_TEST("String Match", test_match),
UNIT_TEST("String Match (Tokens)", test_match_tokens),
UNIT_TEST("String Length", test_length),
);