Improve test coverage

Various test additions, mainly in GObject
This commit is contained in:
Matthias Clasen
2011-02-13 23:47:42 -05:00
parent 1e48bff207
commit 309f5f978b
14 changed files with 1376 additions and 7 deletions

View File

@@ -151,6 +151,12 @@ gdatetime_LDADD = $(progs_ldadd)
TEST_PROGS += environment
environment_LDADD = $(progs_ldadd)
TEST_PROGS += mappedfile
mappedfile_LDADD = $(progs_ldadd)
TEST_PROGS += dataset
dataset_LDADD = $(progs_ldadd)
if OS_UNIX
# some testing of gtester funcitonality
@@ -169,7 +175,8 @@ EXTRA_DIST += \
4096-random-bytes \
keyfiletest.ini \
pages.ini \
bookmarks.xbel
bookmarks.xbel \
empty
dist-hook:
mkdir $(distdir)/markups; \

187
glib/tests/dataset.c Normal file
View File

@@ -0,0 +1,187 @@
#include <glib.h>
static void
test_quark_basic (void)
{
GQuark quark;
const gchar *orig = "blargh";
gchar *copy;
const gchar *str;
quark = g_quark_try_string ("no-such-quark");
g_assert (quark == 0);
copy = g_strdup (orig);
quark = g_quark_from_static_string (orig);
g_assert (quark != 0);
g_assert (g_quark_from_string (orig) == quark);
g_assert (g_quark_from_string (copy) == quark);
g_assert (g_quark_try_string (orig) == quark);
str = g_quark_to_string (quark);
g_assert_cmpstr (str, ==, orig);
g_free (copy);
}
static void
test_quark_string (void)
{
const gchar *orig = "string1";
gchar *copy;
const gchar *str1;
const gchar *str2;
copy = g_strdup (orig);
str1 = g_intern_static_string (orig);
str2 = g_intern_string (copy);
g_assert (str1 == str2);
g_assert (str1 == orig);
g_free (copy);
}
static void
test_dataset_basic (void)
{
gpointer location = (gpointer)test_dataset_basic;
gpointer other = (gpointer)test_quark_basic;
gpointer data = "test1";
gpointer ret;
g_dataset_set_data (location, "test1", data);
ret = g_dataset_get_data (location, "test1");
g_assert (ret == data);
ret = g_dataset_get_data (location, "test2");
g_assert (ret == NULL);
ret = g_dataset_get_data (other, "test1");
g_assert (ret == NULL);
g_dataset_set_data (location, "test1", "new-value");
ret = g_dataset_get_data (location, "test1");
g_assert (ret != data);
g_dataset_remove_data (location, "test1");
ret = g_dataset_get_data (location, "test1");
g_assert (ret == NULL);
}
static gint destroy_count;
static void
notify (gpointer data)
{
destroy_count++;
}
static void
test_dataset_full (void)
{
gpointer location = (gpointer)test_dataset_full;
g_dataset_set_data_full (location, "test1", "test1", notify);
destroy_count = 0;
g_dataset_set_data (location, "test1", NULL);
g_assert (destroy_count == 1);
g_dataset_set_data_full (location, "test1", "test1", notify);
destroy_count = 0;
g_dataset_remove_data (location, "test1");
g_assert (destroy_count == 1);
g_dataset_set_data_full (location, "test1", "test1", notify);
destroy_count = 0;
g_dataset_remove_no_notify (location, "test1");
g_assert (destroy_count == 0);
}
static void
foreach (GQuark id,
gpointer data,
gpointer user_data)
{
gint *counter = user_data;
*counter += 1;
}
static void
test_dataset_foreach (void)
{
gpointer location = (gpointer)test_dataset_foreach;
gint my_count;
my_count = 0;
g_dataset_set_data_full (location, "test1", "test1", notify);
g_dataset_set_data_full (location, "test2", "test2", notify);
g_dataset_set_data_full (location, "test3", "test3", notify);
g_dataset_foreach (location, foreach, &my_count);
g_assert (my_count == 3);
}
static void
test_dataset_destroy (void)
{
gpointer location = (gpointer)test_dataset_destroy;
destroy_count = 0;
g_dataset_set_data_full (location, "test1", "test1", notify);
g_dataset_set_data_full (location, "test2", "test2", notify);
g_dataset_set_data_full (location, "test3", "test3", notify);
g_dataset_destroy (location);
g_assert (destroy_count == 3);
}
static void
test_dataset_id (void)
{
gpointer location = (gpointer)test_dataset_id;
gpointer other = (gpointer)test_quark_basic;
gpointer data = "test1";
gpointer ret;
GQuark quark;
quark = g_quark_from_string ("test1");
g_dataset_id_set_data (location, quark, data);
ret = g_dataset_id_get_data (location, quark);
g_assert (ret == data);
ret = g_dataset_id_get_data (location, g_quark_from_string ("test2"));
g_assert (ret == NULL);
ret = g_dataset_id_get_data (other, quark);
g_assert (ret == NULL);
g_dataset_id_set_data (location, quark, "new-value");
ret = g_dataset_id_get_data (location, quark);
g_assert (ret != data);
g_dataset_id_remove_data (location, quark);
ret = g_dataset_id_get_data (location, quark);
g_assert (ret == NULL);
}
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/quark/basic", test_quark_basic);
g_test_add_func ("/quark/string", test_quark_string);
g_test_add_func ("/dataset/basic", test_dataset_basic);
g_test_add_func ("/dataset/id", test_dataset_id);
g_test_add_func ("/dataset/full", test_dataset_full);
g_test_add_func ("/dataset/foreach", test_dataset_foreach);
g_test_add_func ("/dataset/destroy", test_dataset_destroy);
return g_test_run ();
}

0
glib/tests/empty Normal file
View File

View File

@@ -624,6 +624,28 @@ test_GDateTime_now_utc (void)
g_date_time_unref (dt);
}
static void
test_GDateTime_new_from_unix_utc (void)
{
GDateTime *dt;
gint64 t;
t = g_get_real_time ();
dt = g_date_time_new_from_unix_utc (t);
g_assert (dt == NULL);
t = t / 1e6; /* oops, this was microseconds */
dt = g_date_time_new_from_unix_utc (t);
g_assert (dt != NULL);
g_assert (dt == g_date_time_ref (dt));
g_date_time_unref (dt);
g_assert_cmpint (g_date_time_to_unix (dt), ==, t);
g_date_time_unref (dt);
}
static void
test_GDateTime_get_utc_offset (void)
{
@@ -638,6 +660,9 @@ test_GDateTime_get_utc_offset (void)
ts = g_date_time_get_utc_offset (dt);
#ifdef HAVE_STRUCT_TM_TM_GMTOFF
g_assert_cmpint (ts, ==, (tm.tm_gmtoff * G_TIME_SPAN_SECOND));
#endif
#ifdef HAVE_STRUCT_TM___TM_GMTOFF
g_assert_cmpint (ts, ==, (tm.__tm_gmtoff * G_TIME_SPAN_SECOND));
#endif
g_date_time_unref (dt);
}
@@ -1029,6 +1054,7 @@ main (gint argc,
g_test_add_func ("/GDateTime/get_year", test_GDateTime_get_year);
g_test_add_func ("/GDateTime/hash", test_GDateTime_hash);
g_test_add_func ("/GDateTime/new_from_unix", test_GDateTime_new_from_unix);
g_test_add_func ("/GDateTime/new_from_unix_utc", test_GDateTime_new_from_unix_utc);
g_test_add_func ("/GDateTime/new_from_timeval", test_GDateTime_new_from_timeval);
g_test_add_func ("/GDateTime/new_full", test_GDateTime_new_full);
g_test_add_func ("/GDateTime/now", test_GDateTime_now);

73
glib/tests/mappedfile.c Normal file
View File

@@ -0,0 +1,73 @@
#include <glib.h>
#include <string.h>
static void
test_empty (void)
{
GMappedFile *file;
GError *error;
error = NULL;
file = g_mapped_file_new ("empty", FALSE, &error);
g_assert_no_error (error);
g_assert (g_mapped_file_get_contents (file) == NULL);
g_mapped_file_free (file);
}
static void
test_nonexisting (void)
{
GMappedFile *file;
GError *error;
error = NULL;
file = g_mapped_file_new ("no-such-file", FALSE, &error);
g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
g_clear_error (&error);
g_assert (file == NULL);
}
static void
test_writable (void)
{
GMappedFile *file;
GError *error;
gchar *contents;
const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
const gchar *new = "abcdefghijklmnopqrstuvxyz";
error = NULL;
file = g_mapped_file_new ("4096-random-bytes", TRUE, &error);
g_assert_no_error (error);
contents = g_mapped_file_get_contents (file);
g_assert (strncmp (contents, old, strlen (old)) == 0);
memcpy (contents, new, strlen (new));
g_assert (strncmp (contents, new, strlen (new)) == 0);
g_mapped_file_free (file);
error = NULL;
file = g_mapped_file_new ("4096-random-bytes", TRUE, &error);
g_assert_no_error (error);
contents = g_mapped_file_get_contents (file);
g_assert (strncmp (contents, old, strlen (old)) == 0);
g_mapped_file_free (file);
}
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/mappedfile/empty", test_empty);
g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
g_test_add_func ("/mappedfile/writable", test_writable);
return g_test_run ();
}

View File

@@ -151,6 +151,58 @@ test_tmpdir (void)
g_assert_cmpstr (g_get_tmp_dir (), !=, "");
}
static void
test_bits (void)
{
gulong mask;
gint max_bit;
gint i, pos;
pos = g_bit_nth_lsf (0, -1);
g_assert_cmpint (pos, ==, -1);
max_bit = sizeof (gulong) * 8;
for (i = 0; i < max_bit; i++)
{
mask = 1UL << i;
pos = g_bit_nth_lsf (mask, -1);
g_assert_cmpint (pos, ==, i);
pos = g_bit_nth_lsf (mask, i - 3);
g_assert_cmpint (pos , ==, i);
pos = g_bit_nth_lsf (mask, i);
g_assert_cmpint (pos , ==, -1);
pos = g_bit_nth_lsf (mask, i + 1);
g_assert_cmpint (pos , ==, -1);
}
pos = g_bit_nth_msf (0, -1);
g_assert_cmpint (pos, ==, -1);
for (i = 0; i < max_bit; i++)
{
mask = 1UL << i;
pos = g_bit_nth_msf (mask, -1);
g_assert_cmpint (pos, ==, i);
pos = g_bit_nth_msf (mask, i + 3);
g_assert_cmpint (pos , ==, i);
pos = g_bit_nth_msf (mask, i);
g_assert_cmpint (pos , ==, -1);
if (i > 0)
{
pos = g_bit_nth_msf (mask, i - 1);
g_assert_cmpint (pos , ==, -1);
}
}
}
int
main (int argc,
char *argv[])
@@ -170,6 +222,7 @@ main (int argc,
g_test_add_func ("/utils/version", test_version);
g_test_add_func ("/utils/appname", test_appname);
g_test_add_func ("/utils/tmpdir", test_tmpdir);
g_test_add_func ("/utils/bits", test_bits);
return g_test_run();
}