Move checksum tests to the test framework

This commit is contained in:
Matthias Clasen 2010-06-21 21:22:09 -04:00
parent 839078ea63
commit 5629366c3d
4 changed files with 56 additions and 53 deletions

View File

@ -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

View File

@ -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 ();
}

1
tests/.gitignore vendored
View File

@ -6,7 +6,6 @@ atomic-test
base64-test
bit-test
bookmarkfile-test
checksum-test
child-test
closures
collate.out

View File

@ -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)