From 5629366c3d245b78ad148576f2dccabed83baa71 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 21 Jun 2010 21:22:09 -0400 Subject: [PATCH] Move checksum tests to the test framework --- glib/tests/Makefile.am | 3 + .../checksum-test.c => glib/tests/checksum.c | 103 +++++++++--------- tests/.gitignore | 1 - tests/Makefile.am | 2 - 4 files changed, 56 insertions(+), 53 deletions(-) rename tests/checksum-test.c => glib/tests/checksum.c (92%) diff --git a/glib/tests/Makefile.am b/glib/tests/Makefile.am index 74b578962..1e81f6006 100644 --- a/glib/tests/Makefile.am +++ b/glib/tests/Makefile.am @@ -84,6 +84,9 @@ utf8_validate_LDADD = $(progs_ldadd) TEST_PROGS += utf8-misc utf8_misc_LDADD = $(progs_ldadd) +TEST_PROGS += checksum +checksum_LDADD = $(progs_ldadd) + if OS_UNIX # some testing of gtester funcitonality diff --git a/tests/checksum-test.c b/glib/tests/checksum.c similarity index 92% rename from tests/checksum-test.c rename to glib/tests/checksum.c index dc1a61e48..b404a35ff 100644 --- a/tests/checksum-test.c +++ b/glib/tests/checksum.c @@ -582,95 +582,98 @@ const char *SHA256_sums[] = { "df3a0c35d5345d6d792415c1310bd4589cdf68bac96ed599d6bb0c1545ffc86c" }; +typedef struct { + GChecksumType checksum_type; + const gchar *sum; + int length; +} ChecksumTest; + static void -test_checksum (GChecksumType checksum_type, - const gchar *type, - const gchar *sum, - int length) +test_checksum (gconstpointer d) { + const ChecksumTest *test = d; GChecksum *checksum; const char *p; int chunk_length; - for (chunk_length = MIN (length, 1); chunk_length < length; chunk_length++) + for (chunk_length = MIN (test->length, 1); chunk_length < test->length; chunk_length++) { - checksum = g_checksum_new (checksum_type); - for (p = FIXED_STR; p < FIXED_STR + length; p += chunk_length) + checksum = g_checksum_new (test->checksum_type); + for (p = FIXED_STR; p < FIXED_STR + test->length; p += chunk_length) { g_checksum_update (checksum, (const guchar *)p, - MIN (chunk_length, length - (p - FIXED_STR))); - } - - if (strcmp (g_checksum_get_string (checksum), sum) != 0) - { - g_print ("Invalid %s checksum for `%.*s' (length %d) counting by %d:\n" - "%s (expecting: %s)\n", - type, - length, FIXED_STR, - length, chunk_length, - g_checksum_get_string (checksum), - sum); - exit (1); + MIN (chunk_length, test->length - (p - FIXED_STR))); } + g_assert_cmpstr (g_checksum_get_string (checksum), ==, test->sum); g_checksum_free (checksum); } } +typedef struct { + GChecksumType checksum_type; + const gchar **sums; +} ChecksumStringTest; + static void -test_checksum_string (GChecksumType checksum_type, - const gchar *type, - const gchar **sums) +test_checksum_string (gconstpointer d) { + const ChecksumStringTest *test = d; int length; for (length = 0; length <= FIXED_LEN; length++) { - const char *expected = sums[length]; char *checksum; - checksum = g_compute_checksum_for_string (checksum_type, + checksum = g_compute_checksum_for_string (test->checksum_type, FIXED_STR, length); - if (strcmp (checksum, expected) != 0) - { - g_print ("Invalid %s checksum for `%.*s' (length %d):\n" - "%s (expecting: %s)\n", - type, - length, FIXED_STR, length, - checksum, - expected); - exit (1); - } - + g_assert_cmpstr (checksum, ==, test->sums[length]); g_free (checksum); } } -#define test(type, length) test_checksum (G_CHECKSUM_##type, \ - #type, \ - type##_sums[length], \ - length) +#define test(type, length) { \ + ChecksumTest *test; \ + gchar *path; \ + test = g_new0 (ChecksumTest, 1); \ + test->checksum_type = G_CHECKSUM_##type; \ + test->sum = type##_sums[length]; \ + test->length = length; \ + path = g_strdup_printf ("/checksum/%s/%d", #type, length); \ + g_test_add_data_func (path, test, test_checksum); \ + g_free (path); \ +} -#define test_string(type) test_checksum_string (G_CHECKSUM_##type, \ - #type, \ - type##_sums) +#define test_string(type) { \ + ChecksumStringTest *test; \ + gchar *path; \ + test = g_new0 (ChecksumStringTest, 1); \ + test->checksum_type = G_CHECKSUM_##type; \ + test->sums = type##_sums; \ + path = g_strdup_printf ("/checksum/%s/string", #type); \ + g_test_add_data_func (path, test, test_checksum_string); \ + g_free (path); \ +} int main (int argc, char *argv[]) { int length; - for (length = 0; length <= FIXED_LEN; length++) - { - test (MD5, length); - test (SHA1, length); - test (SHA256, length); - } + g_test_init (&argc, &argv, NULL); + for (length = 0; length <= FIXED_LEN; length++) + test (MD5, length); test_string (MD5); + + for (length = 0; length <= FIXED_LEN; length++) + test (SHA1, length); test_string (SHA1); + + for (length = 0; length <= FIXED_LEN; length++) + test (SHA256, length); test_string (SHA256); - return EXIT_SUCCESS; + return g_test_run (); } diff --git a/tests/.gitignore b/tests/.gitignore index 81c9cc62f..c4dbf23d1 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -6,7 +6,6 @@ atomic-test base64-test bit-test bookmarkfile-test -checksum-test child-test closures collate.out diff --git a/tests/Makefile.am b/tests/Makefile.am index 967a88c02..fe1c18564 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -87,7 +87,6 @@ test_programs = \ atomic-test \ bit-test \ $(CXX_TEST) \ - checksum-test \ child-test \ completion-test \ convert-test \ @@ -146,7 +145,6 @@ module_ldadd = $(libgmodule) $(G_MODULE_LIBS) $(progs_ldadd) atomic_test_LDADD = $(progs_ldadd) bit_test_LDADD = $(progs_ldadd) bookmarkfile_test_LDADD = $(progs_ldadd) -checksum_test_LDADD = $(progs_ldadd) child_test_LDADD = $(thread_ldadd) completion_test_LDADD = $(progs_ldadd) convert_test_LDADD = $(progs_ldadd)