mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-11-10 03:16:17 +01:00
Some more hash, utils and checksum tests
This commit is contained in:
parent
01abbc4bd3
commit
464b5ff357
@ -689,17 +689,22 @@ test_checksum_string (gconstpointer d)
|
||||
{
|
||||
const ChecksumStringTest *test = d;
|
||||
int length;
|
||||
gchar *checksum;
|
||||
|
||||
for (length = 0; length <= FIXED_LEN; length++)
|
||||
{
|
||||
char *checksum;
|
||||
|
||||
checksum = g_compute_checksum_for_string (test->checksum_type,
|
||||
FIXED_STR,
|
||||
length);
|
||||
g_assert_cmpstr (checksum, ==, test->sums[length]);
|
||||
g_free (checksum);
|
||||
}
|
||||
|
||||
checksum = g_compute_checksum_for_string (test->checksum_type,
|
||||
FIXED_STR,
|
||||
-1);
|
||||
g_assert_cmpstr (checksum, ==, test->sums[FIXED_LEN]);
|
||||
g_free (checksum);
|
||||
}
|
||||
|
||||
#define test(type, length) { \
|
||||
@ -728,6 +733,13 @@ test_checksum_string (gconstpointer d)
|
||||
g_free (path); \
|
||||
}
|
||||
|
||||
static void
|
||||
test_unsupported (void)
|
||||
{
|
||||
g_assert_cmpint (g_checksum_type_get_length (20), ==, -1);
|
||||
g_assert (g_checksum_new (20) == NULL);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
@ -735,6 +747,8 @@ main (int argc, char *argv[])
|
||||
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
|
||||
g_test_add_func ("/checksum/unsupported", test_unsupported);
|
||||
|
||||
for (length = 0; length <= FIXED_LEN; length++)
|
||||
test (MD5, length);
|
||||
test_string (MD5);
|
||||
|
@ -379,6 +379,105 @@ static void direct_hash_test (void)
|
||||
g_hash_table_destroy (h);
|
||||
}
|
||||
|
||||
static void int64_hash_test (void)
|
||||
{
|
||||
gint i, rc;
|
||||
GHashTable *h;
|
||||
gint64 values[20];
|
||||
gint64 key;
|
||||
|
||||
h = g_hash_table_new (g_int64_hash, g_int64_equal);
|
||||
g_assert (h != NULL);
|
||||
for (i=0; i<20; i++)
|
||||
{
|
||||
values[i] = i + 42;
|
||||
g_hash_table_insert (h, &values[i], GINT_TO_POINTER (i + 42));
|
||||
}
|
||||
|
||||
g_assert (g_hash_table_size (h) == 20);
|
||||
|
||||
for (i=0; i<20; i++)
|
||||
{
|
||||
key = i + 42;
|
||||
rc = GPOINTER_TO_INT (g_hash_table_lookup (h, &key));
|
||||
|
||||
g_assert_cmpint (rc, ==, i + 42);
|
||||
}
|
||||
|
||||
g_hash_table_destroy (h);
|
||||
}
|
||||
|
||||
static void double_hash_test (void)
|
||||
{
|
||||
gint i, rc;
|
||||
GHashTable *h;
|
||||
gdouble values[20];
|
||||
gdouble key;
|
||||
|
||||
h = g_hash_table_new (g_double_hash, g_double_equal);
|
||||
g_assert (h != NULL);
|
||||
for (i=0; i<20; i++)
|
||||
{
|
||||
values[i] = i + 42.5;
|
||||
g_hash_table_insert (h, &values[i], GINT_TO_POINTER (i + 42));
|
||||
}
|
||||
|
||||
g_assert (g_hash_table_size (h) == 20);
|
||||
|
||||
for (i=0; i<20; i++)
|
||||
{
|
||||
key = i + 42.5;
|
||||
rc = GPOINTER_TO_INT (g_hash_table_lookup (h, &key));
|
||||
|
||||
g_assert_cmpint (rc, ==, i + 42);
|
||||
}
|
||||
|
||||
g_hash_table_destroy (h);
|
||||
}
|
||||
|
||||
static void
|
||||
string_free (gpointer data)
|
||||
{
|
||||
GString *s = data;
|
||||
|
||||
g_string_free (s, TRUE);
|
||||
}
|
||||
|
||||
static void string_hash_test (void)
|
||||
{
|
||||
gint i, rc;
|
||||
GHashTable *h;
|
||||
GString *s;
|
||||
|
||||
h = g_hash_table_new_full ((GHashFunc)g_string_hash, (GEqualFunc)g_string_equal, string_free, NULL);
|
||||
g_assert (h != NULL);
|
||||
for (i=0; i<20; i++)
|
||||
{
|
||||
s = g_string_new ("");
|
||||
g_string_append_printf (s, "%d", i + 42);
|
||||
g_string_append_c (s, '.');
|
||||
g_string_prepend_unichar (s, 0x2301);
|
||||
g_hash_table_insert (h, s, GINT_TO_POINTER (i + 42));
|
||||
}
|
||||
|
||||
g_assert (g_hash_table_size (h) == 20);
|
||||
|
||||
s = g_string_new ("");
|
||||
for (i=0; i<20; i++)
|
||||
{
|
||||
g_string_assign (s, "");
|
||||
g_string_append_printf (s, "%d", i + 42);
|
||||
g_string_append_c (s, '.');
|
||||
g_string_prepend_unichar (s, 0x2301);
|
||||
rc = GPOINTER_TO_INT (g_hash_table_lookup (h, s));
|
||||
|
||||
g_assert_cmpint (rc, ==, i + 42);
|
||||
}
|
||||
|
||||
g_string_free (s, TRUE);
|
||||
g_hash_table_destroy (h);
|
||||
}
|
||||
|
||||
static void
|
||||
test_hash_misc (void)
|
||||
{
|
||||
@ -518,6 +617,9 @@ main (int argc, char *argv[])
|
||||
g_test_add_data_func ("/hash/one", GINT_TO_POINTER (TRUE), second_hash_test);
|
||||
g_test_add_data_func ("/hash/honeyman", GINT_TO_POINTER (FALSE), second_hash_test);
|
||||
g_test_add_func ("/hash/direct", direct_hash_test);
|
||||
g_test_add_func ("/hash/int64", int64_hash_test);
|
||||
g_test_add_func ("/hash/double", double_hash_test);
|
||||
g_test_add_func ("/hash/string", string_hash_test);
|
||||
g_test_add_func ("/hash/ref", test_hash_ref);
|
||||
|
||||
return g_test_run ();
|
||||
|
@ -95,14 +95,45 @@ test_version (void)
|
||||
GLIB_MICRO_VERSION + 1) != NULL);
|
||||
}
|
||||
|
||||
static const gchar *argv0;
|
||||
|
||||
static void
|
||||
test_appname (void)
|
||||
{
|
||||
const gchar *prgname;
|
||||
const gchar *appname;
|
||||
|
||||
prgname = g_get_prgname ();
|
||||
appname = g_get_application_name ();
|
||||
g_assert_cmpstr (prgname, ==, argv0);
|
||||
g_assert_cmpstr (appname, ==, prgname);
|
||||
|
||||
g_set_prgname ("prgname");
|
||||
|
||||
prgname = g_get_prgname ();
|
||||
appname = g_get_application_name ();
|
||||
g_assert_cmpstr (prgname, ==, "prgname");
|
||||
g_assert_cmpstr (appname, ==, "prgname");
|
||||
|
||||
g_set_application_name ("appname");
|
||||
|
||||
prgname = g_get_prgname ();
|
||||
appname = g_get_application_name ();
|
||||
g_assert_cmpstr (prgname, ==, "prgname");
|
||||
g_assert_cmpstr (appname, ==, "appname");
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char *argv[])
|
||||
{
|
||||
argv0 = argv[0];
|
||||
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
|
||||
g_test_add_func ("/utils/language-names", test_language_names);
|
||||
g_test_add_func ("/utils/version", test_version);
|
||||
g_test_add_func ("/utils/appname", test_appname);
|
||||
|
||||
return g_test_run();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user