summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorEmmanuel Fleury <emmanuel.fleury@gmail.com>2022-04-15 10:47:46 +0200
committerEmmanuel Fleury <emmanuel.fleury@gmail.com>2022-05-10 19:21:53 +0200
commita67e9cdbe0ab7ddae0f15377426c6817978de84f (patch)
treec8847a51ca8acfdc7d08b2a9292aeae3c9afc1d9 /tests
parent8f2a950362757b43e6d8b15e9f37bab50985df39 (diff)
downloadglib-a67e9cdbe0ab7ddae0f15377426c6817978de84f.tar.gz
Convert tests/mapping-test.c to glib test framework
Diffstat (limited to 'tests')
-rw-r--r--tests/mapping-test.c215
1 files changed, 107 insertions, 108 deletions
diff --git a/tests/mapping-test.c b/tests/mapping-test.c
index d4547deba..260134a91 100644
--- a/tests/mapping-test.c
+++ b/tests/mapping-test.c
@@ -14,13 +14,9 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-#include <signal.h>
-#include "glib.h"
-#include "gstdio.h"
+#include <glib.h>
+#include <glib/gstdio.h>
#ifdef G_OS_UNIX
#include <unistd.h>
@@ -29,12 +25,12 @@
#include <process.h>
#endif
-static gchar *dir, *global_filename, *global_displayname, *childname;
-
static gboolean stop = FALSE;
-
static gint parent_pid;
+/* Passing argc and argv through global variables */
+static char **local_argv;
+
#ifndef G_OS_WIN32
static void
@@ -66,15 +62,11 @@ write_or_die (const gchar *filename,
gssize length)
{
GError *error = NULL;
- gchar *displayname;
+ gboolean result;
- if (!g_file_set_contents (filename, contents, length, &error))
- {
- displayname = g_filename_display_name (childname);
- g_print ("failed to write '%s': %s\n",
- displayname, error->message);
- exit (1);
- }
+ result = g_file_set_contents (filename, contents, length, &error);
+ g_assert_no_error (error);
+ g_assert_true (result);
}
static GMappedFile *
@@ -83,20 +75,14 @@ map_or_die (const gchar *filename,
{
GError *error = NULL;
GMappedFile *map;
- gchar *displayname;
map = g_mapped_file_new (filename, writable, &error);
- if (!map)
- {
- displayname = g_filename_display_name (childname);
- g_print ("failed to map '%s' non-writable, shared: %s\n",
- displayname, error->message);
- exit (1);
- }
+ g_assert_no_error (error);
+ g_assert_nonnull (map);
return map;
}
-
+
static gboolean
signal_parent (gpointer data)
{
@@ -106,13 +92,18 @@ signal_parent (gpointer data)
return G_SOURCE_REMOVE;
}
-static int
-child_main (int argc, char *argv[])
+static void
+child_main (void)
{
GMappedFile *map;
GMainLoop *loop;
+ gchar *dir, *global_filename, *childname;
+
+ dir = g_get_current_dir ();
+ global_filename = g_build_filename (dir, "maptest", NULL);
+ childname = g_build_filename (dir, "mapchild", NULL);
- parent_pid = atoi (argv[2]);
+ parent_pid = atoi (local_argv[2]);
map = map_or_die (global_filename, FALSE);
#ifndef G_OS_WIN32
@@ -123,41 +114,58 @@ child_main (int argc, char *argv[])
g_idle_add (signal_parent, NULL);
g_main_loop_run (loop);
- g_message ("test_child_private: received parent signal");
+ g_test_message ("test_child_private: received parent signal");
- write_or_die (childname,
- g_mapped_file_get_contents (map),
- g_mapped_file_get_length (map));
+ write_or_die (childname,
+ g_mapped_file_get_contents (map),
+ g_mapped_file_get_length (map));
- signal_parent (NULL);
+ g_free (childname);
+ g_free (global_filename);
+ g_free (dir);
- return 0;
+ signal_parent (NULL);
}
static void
-test_mapping (void)
+test_mapping_flags (void)
{
GMappedFile *map;
+ gchar *dir, *global_filename;
+
+ dir = g_get_current_dir ();
+ global_filename = g_build_filename (dir, "maptest", NULL);
write_or_die (global_filename, "ABC", -1);
map = map_or_die (global_filename, FALSE);
- g_assert (g_mapped_file_get_length (map) == 3);
- g_mapped_file_free (map);
+ g_assert_cmpint (g_mapped_file_get_length (map), ==, 3);
+ g_mapped_file_unref (map);
map = map_or_die (global_filename, TRUE);
- g_assert (g_mapped_file_get_length (map) == 3);
- g_mapped_file_free (map);
- g_message ("test_mapping: ok");
+ g_assert_cmpint (g_mapped_file_get_length (map), ==, 3);
+ g_mapped_file_unref (map);
+ g_test_message ("test_mapping: ok");
+
+ /* Cleaning left over files */
+ g_remove ("maptest");
+
+ g_free (global_filename);
+ g_free (dir);
}
-static void
+static void
test_private (void)
{
GError *error = NULL;
GMappedFile *map;
+ gboolean result;
gchar *buffer;
gsize len;
+ gchar *dir, *global_filename;
+
+ dir = g_get_current_dir ();
+ global_filename = g_build_filename (dir, "maptest", NULL);
write_or_die (global_filename, "ABC", -1);
map = map_or_die (global_filename, TRUE);
@@ -166,27 +174,30 @@ test_private (void)
buffer[0] = '1';
buffer[1] = '2';
buffer[2] = '3';
- g_mapped_file_free (map);
+ g_mapped_file_unref (map);
- if (!g_file_get_contents (global_filename, &buffer, &len, &error))
- {
- g_print ("failed to read '%s': %s\n",
- global_displayname, error->message);
- exit (1);
-
- }
- g_assert (len == 3);
- g_assert (strcmp (buffer, "ABC") == 0);
+ result = g_file_get_contents (global_filename, &buffer, &len, &error);
+ g_assert_no_error (error);
+ g_assert_true (result);
+ g_assert_cmpint (len, ==, 3);
+ g_assert_cmpstr (buffer, ==, "ABC");
g_free (buffer);
- g_message ("test_private: ok");
+ g_free (global_filename);
+ g_free (dir);
+
+ /* Cleaning left over files */
+ g_remove ("maptest");
+
+ g_test_message ("test_private: ok");
}
static void
-test_child_private (gchar *argv0)
+test_child_private (void)
{
GError *error = NULL;
GMappedFile *map;
+ gboolean result;
gchar *buffer;
gsize len;
gchar *child_argv[4];
@@ -195,12 +206,17 @@ test_child_private (gchar *argv0)
GMainLoop *loop;
#endif
gchar pid[100];
-
+ gchar *dir, *global_filename, *childname;
+
#ifdef G_OS_WIN32
g_remove ("STOP");
- g_assert (!g_file_test ("STOP", G_FILE_TEST_EXISTS));
+ g_assert_false (g_file_test ("STOP", G_FILE_TEST_EXISTS));
#endif
+ dir = g_get_current_dir ();
+ global_filename = g_build_filename (dir, "maptest", NULL);
+ childname = g_build_filename (dir, "mapchild", NULL);
+
write_or_die (global_filename, "ABC", -1);
map = map_or_die (global_filename, TRUE);
@@ -209,18 +225,16 @@ test_child_private (gchar *argv0)
#endif
g_snprintf (pid, sizeof(pid), "%d", getpid ());
- child_argv[0] = argv0;
+ child_argv[0] = local_argv[0];
child_argv[1] = "mapchild";
child_argv[2] = pid;
child_argv[3] = NULL;
- if (!g_spawn_async (dir, child_argv, NULL,
- 0, NULL, NULL, &child_pid, &error))
- {
- g_print ("failed to spawn child: %s\n",
- error->message);
- exit (1);
- }
- g_message ("test_child_private: child spawned");
+
+ result = g_spawn_async (dir, child_argv, NULL,
+ 0, NULL, NULL, &child_pid, &error);
+ g_assert_no_error (error);
+ g_assert_true (result);
+ g_test_message ("test_child_private: child spawned");
#ifndef G_OS_WIN32
loop = g_main_loop_new (NULL, FALSE);
@@ -231,13 +245,13 @@ test_child_private (gchar *argv0)
g_usleep (2000000);
#endif
- g_message ("test_child_private: received first child signal");
+ g_test_message ("test_child_private: received first child signal");
buffer = (gchar *)g_mapped_file_get_contents (map);
buffer[0] = '1';
buffer[1] = '2';
buffer[2] = '3';
- g_mapped_file_free (map);
+ g_mapped_file_unref (map);
#ifndef G_OS_WIN32
kill (child_pid, SIGUSR1);
@@ -252,44 +266,30 @@ test_child_private (gchar *argv0)
g_usleep (2000000);
#endif
- g_message ("test_child_private: received second child signal");
+ g_test_message ("test_child_private: received second child signal");
- if (!g_file_get_contents (childname, &buffer, &len, &error))
- {
- gchar *name;
-
- name = g_filename_display_name (childname);
- g_print ("failed to read '%s': %s\n", name, error->message);
- exit (1);
- }
- g_assert (len == 3);
- g_assert (strcmp (buffer, "ABC") == 0);
+ result = g_file_get_contents (childname, &buffer, &len, &error);
+ g_assert_no_error (error);
+ g_assert_true (result);
+ g_assert_cmpint (len, ==, 3);
+ g_assert_cmpstr (buffer, ==, "ABC");
g_free (buffer);
- g_message ("test_child_private: ok");
-}
-
-static int
-parent_main (int argc,
- char *argv[])
-{
- /* test mapping with various flag combinations */
- test_mapping ();
-
- /* test private modification */
- test_private ();
+ g_free (childname);
+ g_free (global_filename);
+ g_free (dir);
- /* test multiple clients, non-shared */
- test_child_private (argv[0]);
+ /* Cleaning left over files */
+ g_remove ("mapchild");
+ g_remove ("maptest");
- return 0;
+ g_test_message ("test_child_private: ok");
}
int
-main (int argc,
+main (int argc,
char *argv[])
{
- int ret;
#ifndef G_OS_WIN32
sigset_t sig_mask, old_mask;
@@ -298,24 +298,23 @@ main (int argc,
if (sigprocmask (SIG_UNBLOCK, &sig_mask, &old_mask) == 0)
{
if (sigismember (&old_mask, SIGUSR1))
- g_message ("SIGUSR1 was blocked, unblocking it");
+ g_test_message ("SIGUSR1 was blocked, unblocking it");
}
#endif
- dir = g_get_current_dir ();
- global_filename = g_build_filename (dir, "maptest", NULL);
- global_displayname = g_filename_display_name (global_filename);
- childname = g_build_filename (dir, "mapchild", NULL);
+ local_argv = argv;
if (argc > 1)
- ret = child_main (argc, argv);
- else
- ret = parent_main (argc, argv);
+ {
+ child_main ();
+ return EXIT_SUCCESS;
+ }
- g_free (childname);
- g_free (global_filename);
- g_free (global_displayname);
- g_free (dir);
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/mapping/flags", test_mapping_flags);
+ g_test_add_func ("/mapping/private", test_private);
+ g_test_add_func ("/mapping/private-child", test_child_private);
- return ret;
+ return g_test_run ();
}