tests: Build string test with CTest

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2016-03-03 10:09:01 -05:00 committed by Anna Schumaker
parent 2f559b2fb3
commit 9f2811a343
4 changed files with 76 additions and 76 deletions

View File

@ -12,6 +12,13 @@
#include <string.h>
#define g_assert_cmpstr_free(lhs, cmp, rhs) \
do { \
g_assert_cmpstr(lhs, cmp, rhs); \
g_free(lhs); \
} while (0)
#ifndef __cplusplus
static inline gchar *lutos(long unsigned int u)
{ return g_strdup_printf("%lu", u); }

View File

@ -5,3 +5,4 @@ function(core_unit_test name)
endfunction()
core_unit_test(Version)
core_unit_test(String)

View File

@ -24,7 +24,7 @@ def CoreTest(name):
Export("core_objs", "CoreTest")
res += [ CoreTest("string") ]
core_objs += [ env.Object("../../core/string.c") ]
res += [ CoreTest("file") ]
res += [ CoreTest("date") ]
res += [ CoreTest("idle") ]

View File

@ -6,73 +6,62 @@
#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");
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 = 3600;
unsigned int day = 86400;
unsigned int hour = 60 * minute;
unsigned int day = 24 * hour;
str_test_equal(string_sec2str_long(day), "1 day");
str_test_equal(string_sec2str_long(2 * day), "2 days");
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");
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");
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");
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");
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");
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");
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");
str_test_equal(
g_assert_cmpstr_free(
string_sec2str_long((22 * day) + (4 * hour) + (12 * minute) + 32),
"22 days, 4 hours, 12 minutes, 32 seconds");
==, "22 days, 4 hours, 12 minutes, 32 seconds");
}
void test_date()
void test_date2str()
{
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);
g_assert_cmpstr_free(string_tm2str(&tm), ==, "10/05/92");
setlocale(LC_TIME, "en_US");
str = string_tm2str(&tm);
test_equal(str, "10/05/1992");
g_free(str);
g_assert_cmpstr_free(string_tm2str(&tm), ==, "10/05/1992");
}
void test_compare_tokens()
@ -85,54 +74,57 @@ void test_compare_tokens()
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);
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);
test_equal(string_compare_tokens(null, null), 0);
test_equal(string_compare_tokens(null, A), 1);
test_equal(string_compare_tokens(A, null), -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);
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);
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()
{
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);
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 };
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);
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()
{
test_equal(string_length(NULL), 0);
test_equal(string_length(""), 0);
test_equal(string_length("a"), 1);
test_equal(string_length("abcdefghijklmnopqrstuvwxyz"), 26);
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);
}
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),
);
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();
}