tests: Build string test with CTest
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
2f559b2fb3
commit
9f2811a343
|
@ -12,6 +12,13 @@
|
||||||
#include <string.h>
|
#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
|
#ifndef __cplusplus
|
||||||
static inline gchar *lutos(long unsigned int u)
|
static inline gchar *lutos(long unsigned int u)
|
||||||
{ return g_strdup_printf("%lu", u); }
|
{ return g_strdup_printf("%lu", u); }
|
||||||
|
|
|
@ -5,3 +5,4 @@ function(core_unit_test name)
|
||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
core_unit_test(Version)
|
core_unit_test(Version)
|
||||||
|
core_unit_test(String)
|
||||||
|
|
|
@ -24,7 +24,7 @@ def CoreTest(name):
|
||||||
Export("core_objs", "CoreTest")
|
Export("core_objs", "CoreTest")
|
||||||
|
|
||||||
|
|
||||||
res += [ CoreTest("string") ]
|
core_objs += [ env.Object("../../core/string.c") ]
|
||||||
res += [ CoreTest("file") ]
|
res += [ CoreTest("file") ]
|
||||||
res += [ CoreTest("date") ]
|
res += [ CoreTest("date") ]
|
||||||
res += [ CoreTest("idle") ]
|
res += [ CoreTest("idle") ]
|
||||||
|
|
|
@ -6,73 +6,62 @@
|
||||||
#include <tests/test.h>
|
#include <tests/test.h>
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
|
|
||||||
#define str_test_equal(lhs, rhs) \
|
|
||||||
test_strings_equal(lhs, g_strdup(rhs), __LINE__)
|
|
||||||
|
|
||||||
|
|
||||||
void test_sec2str()
|
void test_sec2str()
|
||||||
{
|
{
|
||||||
str_test_equal(string_sec2str(0), "0:00");
|
g_assert_cmpstr_free(string_sec2str(0), ==, "0:00");
|
||||||
str_test_equal(string_sec2str(5), "0:05");
|
g_assert_cmpstr_free(string_sec2str(5), ==, "0:05");
|
||||||
str_test_equal(string_sec2str(10), "0:10");
|
g_assert_cmpstr_free(string_sec2str(10), ==, "0:10");
|
||||||
str_test_equal(string_sec2str(60), "1:00");
|
g_assert_cmpstr_free(string_sec2str(60), ==, "1:00");
|
||||||
str_test_equal(string_sec2str(65), "1:05");
|
g_assert_cmpstr_free(string_sec2str(65), ==, "1:05");
|
||||||
str_test_equal(string_sec2str(75), "1:15");
|
g_assert_cmpstr_free(string_sec2str(75), ==, "1:15");
|
||||||
str_test_equal(string_sec2str(600), "10:00");
|
g_assert_cmpstr_free(string_sec2str(600), ==, "10:00");
|
||||||
str_test_equal(string_sec2str(605), "10:05");
|
g_assert_cmpstr_free(string_sec2str(605), ==, "10:05");
|
||||||
str_test_equal(string_sec2str(615), "10:15");
|
g_assert_cmpstr_free(string_sec2str(615), ==, "10:15");
|
||||||
str_test_equal(string_sec2str(660), "11:00");
|
g_assert_cmpstr_free(string_sec2str(660), ==, "11:00");
|
||||||
str_test_equal(string_sec2str(665), "11:05");
|
g_assert_cmpstr_free(string_sec2str(665), ==, "11:05");
|
||||||
str_test_equal(string_sec2str(675), "11:15");
|
g_assert_cmpstr_free(string_sec2str(675), ==, "11:15");
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_sec2str_long()
|
void test_sec2str_long()
|
||||||
{
|
{
|
||||||
|
|
||||||
unsigned int minute = 60;
|
unsigned int minute = 60;
|
||||||
unsigned int hour = 3600;
|
unsigned int hour = 60 * minute;
|
||||||
unsigned int day = 86400;
|
unsigned int day = 24 * hour;
|
||||||
|
|
||||||
str_test_equal(string_sec2str_long(day), "1 day");
|
g_assert_cmpstr_free(string_sec2str_long(0), ==, "");
|
||||||
str_test_equal(string_sec2str_long(2 * day), "2 days");
|
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");
|
g_assert_cmpstr_free(string_sec2str_long(minute), ==, "1 minute");
|
||||||
str_test_equal(string_sec2str_long(hour), "1 hour");
|
g_assert_cmpstr_free(string_sec2str_long(2 * minute), ==, "2 minutes");
|
||||||
str_test_equal(string_sec2str_long(2 * hour), "2 hours");
|
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");
|
g_assert_cmpstr_free(string_sec2str_long(hour), ==, "1 hour");
|
||||||
str_test_equal(string_sec2str_long(minute), "1 minute");
|
g_assert_cmpstr_free(string_sec2str_long(2 * hour), ==, "2 hours");
|
||||||
str_test_equal(string_sec2str_long(2 * minute), "2 minutes");
|
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");
|
g_assert_cmpstr_free(string_sec2str_long(day), ==, "1 day");
|
||||||
str_test_equal(string_sec2str_long(0), "");
|
g_assert_cmpstr_free(string_sec2str_long(2 * day), ==, "2 days");
|
||||||
str_test_equal(string_sec2str_long(1), "1 second");
|
g_assert_cmpstr_free(string_sec2str_long(day + hour), ==, "1 day, 1 hour");
|
||||||
str_test_equal(string_sec2str_long(2), "2 seconds");
|
|
||||||
|
|
||||||
str_test_equal(
|
g_assert_cmpstr_free(
|
||||||
string_sec2str_long((22 * day) + (4 * hour) + (12 * minute) + 32),
|
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 = {
|
struct tm tm = {
|
||||||
.tm_year = 92,
|
.tm_year = 92,
|
||||||
.tm_mon = 9,
|
.tm_mon = 9,
|
||||||
.tm_mday = 5,
|
.tm_mday = 5,
|
||||||
};
|
};
|
||||||
gchar *str;
|
|
||||||
|
|
||||||
setlocale(LC_TIME, "C");
|
setlocale(LC_TIME, "C");
|
||||||
str = string_tm2str(&tm);
|
g_assert_cmpstr_free(string_tm2str(&tm), ==, "10/05/92");
|
||||||
test_equal(str, "10/05/92");
|
|
||||||
g_free(str);
|
|
||||||
|
|
||||||
setlocale(LC_TIME, "en_US");
|
setlocale(LC_TIME, "en_US");
|
||||||
str = string_tm2str(&tm);
|
g_assert_cmpstr_free(string_tm2str(&tm), ==, "10/05/1992");
|
||||||
test_equal(str, "10/05/1992");
|
|
||||||
g_free(str);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_compare_tokens()
|
void test_compare_tokens()
|
||||||
|
@ -85,54 +74,57 @@ void test_compare_tokens()
|
||||||
gchar *ba[3] = { "b", "a", NULL };
|
gchar *ba[3] = { "b", "a", NULL };
|
||||||
gchar *null[1] = { NULL };
|
gchar *null[1] = { NULL };
|
||||||
|
|
||||||
test_equal(string_compare_tokens(A, A), 0);
|
g_assert_cmpint(string_compare_tokens(A, A), ==, 0);
|
||||||
test_equal(string_compare_tokens(a, a), 0);
|
g_assert_cmpint(string_compare_tokens(a, a), ==, 0);
|
||||||
test_equal(string_compare_tokens(A, B), -1);
|
g_assert_cmpint(string_compare_tokens(A, B), ==, -1);
|
||||||
test_equal(string_compare_tokens(B, A), 1);
|
g_assert_cmpint(string_compare_tokens(B, A), ==, 1);
|
||||||
|
|
||||||
test_equal(string_compare_tokens(null, null), 0);
|
g_assert_cmpint(string_compare_tokens(null, null), ==, 0);
|
||||||
test_equal(string_compare_tokens(null, A), 1);
|
g_assert_cmpint(string_compare_tokens(null, A), ==, 1);
|
||||||
test_equal(string_compare_tokens(A, null), -1);
|
g_assert_cmpint(string_compare_tokens(A, null), ==, -1);
|
||||||
|
|
||||||
test_equal(string_compare_tokens(aa, aa), 0);
|
g_assert_cmpint(string_compare_tokens(aa, aa), ==, 0);
|
||||||
test_equal(string_compare_tokens(a, aa), -1);
|
g_assert_cmpint(string_compare_tokens(a, aa), ==, -1);
|
||||||
test_equal(string_compare_tokens(aa, a), 1);
|
g_assert_cmpint(string_compare_tokens(aa, a), ==, 1);
|
||||||
test_equal(string_compare_tokens(ab, ba), -1);
|
g_assert_cmpint(string_compare_tokens(ab, ba), ==, -1);
|
||||||
test_equal(string_compare_tokens(ba, ab), 1);
|
g_assert_cmpint(string_compare_tokens(ba, ab), ==, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_match()
|
void test_match()
|
||||||
{
|
{
|
||||||
test_equal(string_match(NULL, NULL), (bool)false);
|
g_assert_false(string_match(NULL, NULL));
|
||||||
test_equal(string_match(NULL, "A"), (bool)false);
|
g_assert_false(string_match(NULL, "A"));
|
||||||
test_equal(string_match("A", NULL), (bool)false);
|
g_assert_false(string_match("A", NULL));
|
||||||
test_equal(string_match("A", "A"), (bool)true);
|
g_assert_true( string_match("A", "A"));
|
||||||
test_equal(string_match("A", "B"), (bool)false);
|
g_assert_false(string_match("A", "B"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_match_tokens()
|
void test_match_tokens()
|
||||||
{
|
{
|
||||||
gchar *tokens[3] = { "hyrule", "symphony", NULL };
|
gchar *tokens[3] = { "hyrule", "symphony", NULL };
|
||||||
|
|
||||||
test_equal(string_match_token("hyr", tokens), (bool)true);
|
g_assert_true( string_match_token("hyr", tokens));
|
||||||
test_equal(string_match_token("sym", tokens), (bool)true);
|
g_assert_true( string_match_token("sym", tokens));
|
||||||
test_equal(string_match_token("rule", tokens), (bool)false);
|
g_assert_false(string_match_token("rule", tokens));
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_length()
|
void test_length()
|
||||||
{
|
{
|
||||||
test_equal(string_length(NULL), 0);
|
g_assert_cmpint(string_length(NULL), ==, 0);
|
||||||
test_equal(string_length(""), 0);
|
g_assert_cmpint(string_length(""), ==, 0);
|
||||||
test_equal(string_length("a"), 1);
|
g_assert_cmpint(string_length("a"), ==, 1);
|
||||||
test_equal(string_length("abcdefghijklmnopqrstuvwxyz"), 26);
|
g_assert_cmpint(string_length("abcdefghijklmnopqrstuvwxyz"), ==, 26);
|
||||||
}
|
}
|
||||||
|
|
||||||
DECLARE_UNIT_TESTS(
|
int main(int argc, char **argv)
|
||||||
UNIT_TEST("Seconds to String", test_sec2str),
|
{
|
||||||
UNIT_TEST("Seconds to String (Long)", test_sec2str_long),
|
g_test_init(&argc, &argv, NULL);
|
||||||
UNIT_TEST("Date to String", test_date),
|
g_test_add_func("/Core/String/Seconds to String", test_sec2str);
|
||||||
UNIT_TEST("String Comparison (Tokens)", test_compare_tokens),
|
g_test_add_func("/Core/String/Seconds to String (Long)", test_sec2str_long);
|
||||||
UNIT_TEST("String Match", test_match),
|
g_test_add_func("/Core/String/Date to String", test_date2str);
|
||||||
UNIT_TEST("String Match (Tokens)", test_match_tokens),
|
g_test_add_func("/Core/String/Comparison/Tokens", test_compare_tokens);
|
||||||
UNIT_TEST("String Length", test_length),
|
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();
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue