From 5b7383c54442a7d5592c3495a445b17d6414149c Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 17 Feb 2022 21:42:35 +0000 Subject: [PATCH] tests: Merge iochannel-test into io-channel tests in glib directory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We’re trying to eliminate the legacy `tests/` directory. This commit moves the code from `tests/iochannel-test.c` into `glib/tests/io-channel.c` and ports it to the latest GLib test coding standards: * Change `g_assert()` to `g_assert_*()` * Print verbose messages with `g_test_message()` * Rename some variables to conform to modern conventions * Use `GTest` Signed-off-by: Philip Withnall Helps: #1434 --- glib/tests/io-channel.c | 141 ++++++++++++++++ {tests => glib/tests}/iochannel-test-infile | 0 glib/tests/meson.build | 1 + tests/iochannel-test.c | 172 -------------------- tests/meson.build | 2 - 5 files changed, 142 insertions(+), 174 deletions(-) rename {tests => glib/tests}/iochannel-test-infile (100%) delete mode 100644 tests/iochannel-test.c diff --git a/glib/tests/io-channel.c b/glib/tests/io-channel.c index 4a1b10876..a04bcd098 100644 --- a/glib/tests/io-channel.c +++ b/glib/tests/io-channel.c @@ -1,5 +1,13 @@ /* GLib testing framework examples and tests * + * Copyright © 2001 Hidetoshi Tajima + * Copyright © 2001 Ron Steinke + * Copyright © 2001 Owen Taylor + * Copyright © 2002 Manish Singh + * Copyright © 2011 Sjoerd Simons + * Copyright © 2012 Simon McVittie + * Copyright © 2013 Stef Walter + * Copyright © 2005, 2006, 2008, 2012, 2013 Matthias Clasen * Copyright © 2020 Endless Mobile, Inc. * * This library is free software; you can redistribute it and/or @@ -21,6 +29,138 @@ #include #include +static void +test_small_writes (void) +{ + GIOChannel *io; + GIOStatus status = G_IO_STATUS_ERROR; + guint bytes_remaining; + gchar tmp; + GError *local_error = NULL; + + io = g_io_channel_new_file ("iochannel-test-outfile", "w", &local_error); + g_assert_no_error (local_error); + + g_io_channel_set_encoding (io, NULL, NULL); + g_io_channel_set_buffer_size (io, 1022); + + bytes_remaining = 2 * g_io_channel_get_buffer_size (io); + tmp = 0; + + while (bytes_remaining) + { + status = g_io_channel_write_chars (io, &tmp, 1, NULL, NULL); + if (status == G_IO_STATUS_ERROR) + break; + if (status == G_IO_STATUS_NORMAL) + bytes_remaining--; + } + + g_assert_cmpint (status, ==, G_IO_STATUS_NORMAL); + + g_io_channel_unref (io); +} + +static void +test_read_write (void) +{ + GIOChannel *gio_r, *gio_w ; + GError *local_error = NULL; + GString *buffer; + char *filename; + gint rlength = 0; + glong wlength = 0; + gsize length_out; + const gchar *encoding = "EUC-JP"; + GIOStatus status; + const gsize buffer_size_bytes = 1024; + + filename = g_test_build_filename (G_TEST_DIST, "iochannel-test-infile", NULL); + + setbuf (stdout, NULL); /* For debugging */ + + gio_r = g_io_channel_new_file (filename, "r", &local_error); + g_assert_no_error (local_error); + + gio_w = g_io_channel_new_file ("iochannel-test-outfile", "w", &local_error); + g_assert_no_error (local_error); + + g_io_channel_set_encoding (gio_r, encoding, &local_error); + g_assert_no_error (local_error); + + g_io_channel_set_buffer_size (gio_r, buffer_size_bytes); + + status = g_io_channel_set_flags (gio_r, G_IO_FLAG_NONBLOCK, &local_error); + if (status == G_IO_STATUS_ERROR) + { + /* Errors should not happen */ + g_assert_no_error (local_error); + g_clear_error (&local_error); + } + buffer = g_string_sized_new (buffer_size_bytes); + + while (TRUE) + { + do + status = g_io_channel_read_line_string (gio_r, buffer, NULL, &local_error); + while (status == G_IO_STATUS_AGAIN); + if (status != G_IO_STATUS_NORMAL) + break; + + rlength += buffer->len; + + do + status = g_io_channel_write_chars (gio_w, buffer->str, buffer->len, + &length_out, &local_error); + while (status == G_IO_STATUS_AGAIN); + if (status != G_IO_STATUS_NORMAL) + break; + + wlength += length_out; + + /* Ensure the whole line was written */ + g_assert_cmpuint (length_out, ==, buffer->len); + + g_test_message ("%s", buffer->str); + g_string_truncate (buffer, 0); + } + + switch (status) + { + case G_IO_STATUS_EOF: + break; + case G_IO_STATUS_ERROR: + /* Errors should not happen */ + g_assert_no_error (local_error); + g_clear_error (&local_error); + break; + default: + g_assert_not_reached (); + break; + } + + do + status = g_io_channel_flush (gio_w, &local_error); + while (status == G_IO_STATUS_AGAIN); + + if (status == G_IO_STATUS_ERROR) + { + /* Errors should not happen */ + g_assert_no_error (local_error); + g_clear_error (&local_error); + } + + g_test_message ("read %d bytes, wrote %ld bytes", rlength, wlength); + + g_io_channel_unref (gio_r); + g_io_channel_unref (gio_w); + + test_small_writes (); + + g_free (filename); + g_string_free (buffer, TRUE); +} + static void test_read_line_embedded_nuls (void) { @@ -75,6 +215,7 @@ main (int argc, { g_test_init (&argc, &argv, NULL); + g_test_add_func ("/io-channel/read-write", test_read_write); g_test_add_func ("/io-channel/read-line/embedded-nuls", test_read_line_embedded_nuls); return g_test_run (); diff --git a/tests/iochannel-test-infile b/glib/tests/iochannel-test-infile similarity index 100% rename from tests/iochannel-test-infile rename to glib/tests/iochannel-test-infile diff --git a/glib/tests/meson.build b/glib/tests/meson.build index 125d38ec9..869d5819b 100644 --- a/glib/tests/meson.build +++ b/glib/tests/meson.build @@ -190,6 +190,7 @@ if installed_tests_enabled 'echo-script', 'echo-script.bat', 'empty', + 'iochannel-test-infile', 'keyfile.c', 'keyfiletest.ini', 'pages.ini', diff --git a/tests/iochannel-test.c b/tests/iochannel-test.c deleted file mode 100644 index 06a85e5d0..000000000 --- a/tests/iochannel-test.c +++ /dev/null @@ -1,172 +0,0 @@ -#undef G_DISABLE_ASSERT -#undef G_LOG_DOMAIN - -#include -#include -#include -#include -#include -#include -#include - -#define BUFFER_SIZE 1024 - -static void -test_small_writes (void) -{ - GIOChannel *io; - GIOStatus status = G_IO_STATUS_ERROR; - guint cnt; - gchar tmp; - GError *error = NULL; - - io = g_io_channel_new_file ("iochannel-test-outfile", "w", &error); - if (error) - { - g_warning ("Unable to open file %s: %s", - "iochannel-test-outfile", - error->message); - g_clear_error (&error); - - exit (1); - } - - g_io_channel_set_encoding (io, NULL, NULL); - g_io_channel_set_buffer_size (io, 1022); - - cnt = 2 * g_io_channel_get_buffer_size (io); - tmp = 0; - - while (cnt) - { - status = g_io_channel_write_chars (io, &tmp, 1, NULL, NULL); - if (status == G_IO_STATUS_ERROR) - break; - if (status == G_IO_STATUS_NORMAL) - cnt--; - } - - g_assert (status == G_IO_STATUS_NORMAL); - - g_io_channel_unref (io); -} - - -gint main (gint argc, gchar * argv[]) -{ - GIOChannel *gio_r, *gio_w ; - GError *gerr = NULL; - GString *buffer; - char *filename; - gint rlength = 0; - glong wlength = 0; - gsize length_out; - const gchar encoding[] = "EUC-JP"; - GIOStatus status; - - g_test_init (&argc, &argv, NULL); - - filename = g_test_build_filename (G_TEST_DIST, "iochannel-test-infile", NULL); - - setbuf (stdout, NULL); /* For debugging */ - - gio_r = g_io_channel_new_file (filename, "r", &gerr); - if (gerr) - { - g_warning ("Unable to open file %s: %s", filename, gerr->message); - g_clear_error (&gerr); - return 1; - } - gio_w = g_io_channel_new_file ("iochannel-test-outfile", "w", &gerr); - if (gerr) - { - g_warning ("Unable to open file %s: %s", "iochannel-test-outfile", gerr->message); - g_clear_error (&gerr); - return 1; - } - - g_io_channel_set_encoding (gio_r, encoding, &gerr); - if (gerr) - { - g_warning ("%s", gerr->message); - /* Keep going if this is just a case of iconv not supporting EUC-JP, see bug 428048 */ - if (gerr->code != G_CONVERT_ERROR_NO_CONVERSION) - return 1; - g_clear_error (&gerr); - } - - g_io_channel_set_buffer_size (gio_r, BUFFER_SIZE); - - status = g_io_channel_set_flags (gio_r, G_IO_FLAG_NONBLOCK, &gerr); - if (status == G_IO_STATUS_ERROR) - { - g_warning ("%s", gerr->message); - g_clear_error (&gerr); - } - buffer = g_string_sized_new (BUFFER_SIZE); - - while (TRUE) - { - do - status = g_io_channel_read_line_string (gio_r, buffer, NULL, &gerr); - while (status == G_IO_STATUS_AGAIN); - if (status != G_IO_STATUS_NORMAL) - break; - - rlength += buffer->len; - - do - status = g_io_channel_write_chars (gio_w, buffer->str, buffer->len, - &length_out, &gerr); - while (status == G_IO_STATUS_AGAIN); - if (status != G_IO_STATUS_NORMAL) - break; - - wlength += length_out; - - if (length_out < buffer->len) - g_warning ("Only wrote part of the line."); - -#ifdef VERBOSE - g_print ("%s", buffer->str); -#endif - g_string_truncate (buffer, 0); - } - - switch (status) - { - case G_IO_STATUS_EOF: - break; - case G_IO_STATUS_ERROR: - g_warning ("%s", gerr->message); - g_clear_error (&gerr); - break; - default: - g_warning ("Abnormal exit from write loop."); - break; - } - - do - status = g_io_channel_flush (gio_w, &gerr); - while (status == G_IO_STATUS_AGAIN); - - if (status == G_IO_STATUS_ERROR) - { - g_warning ("%s", gerr->message); - g_clear_error (&gerr); - } - -#ifdef VERBOSE - g_print ("read %d bytes, wrote %ld bytes\n", rlength, wlength); -#endif - - g_io_channel_unref(gio_r); - g_io_channel_unref(gio_w); - - test_small_writes (); - - g_free (filename); - g_string_free (buffer, TRUE); - - return 0; -} diff --git a/tests/meson.build b/tests/meson.build index 84ae6b466..52b64d1c0 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -58,7 +58,6 @@ test_extra_programs = { if host_machine.system() != 'windows' tests += { 'timeloop' : {}, - 'iochannel-test' : {}, } else test_extra_programs += { @@ -68,7 +67,6 @@ endif if installed_tests_enabled install_data( - 'iochannel-test-infile', 'utf8.txt', install_dir : installed_tests_execdir, )