diff options
author | Paolo Borelli <pborelli@katamail.com> | 2008-09-01 09:31:40 +0000 |
---|---|---|
committer | Paolo Borelli <pborelli@src.gnome.org> | 2008-09-01 09:31:40 +0000 |
commit | e6eb8095995309484a963769b42f58c7f818f055 (patch) | |
tree | 5e33b14e7a4111578fa2e0d743b4b4f52e20309d /tests | |
parent | e701ea96b637bcb8cf21e72e0caa7c82e10e278c (diff) |
Bug 550040 - Move GString, rand and printf tests to the unit test
2008-09-01 Paolo Borelli <pborelli@katamail.com>
Bug 550040 - Move GString, rand and printf tests to the unit test
framework
* tests/printf-test.c:
* tests/rand-test.c:
* tests/string-test.c:
Removed
* glib/tests/printf.c:
* glib/tests/rand.c:
* glib/tests/string.c:
Added
* tests/Makefile.am:
* glib/tests/Makefile.am:
Updated for the above
svn path=/trunk/; revision=7419
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 6 | ||||
-rw-r--r-- | tests/printf-test.c | 244 | ||||
-rw-r--r-- | tests/rand-test.c | 135 | ||||
-rw-r--r-- | tests/string-test.c | 313 |
4 files changed, 0 insertions, 698 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index 644d2d94a..bba5f026a 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -119,11 +119,9 @@ test_programs = \ node-test \ onceinit \ patterntest \ - printf-test \ queue-test \ asyncqueue-test \ qsort-test \ - rand-test \ relation-test \ sequence-test \ shell-test \ @@ -134,7 +132,6 @@ test_programs = \ slice-threadinit \ spawn-test \ $(spawn_test_win32_gui) \ - string-test \ thread-test \ threadpool-test \ tree-test \ @@ -186,11 +183,9 @@ module_test_LDADD = $(module_ldadd) $(module_test_exp) module_test_LDFLAGS = $(G_MODULE_LDFLAGS) node_test_LDADD = $(progs_ldadd) onceinit_LDADD = $(thread_ldadd) -printf_test_LDADD = $(progs_ldadd) queue_test_LDADD = $(progs_ldadd) asyncqueue_test_LDADD = $(thread_ldadd) qsort_test_LDADD = $(progs_ldadd) -rand_test_LDADD = $(progs_ldadd) relation_test_LDADD = $(progs_ldadd) sequence_test_LDADD = $(progs_ldadd) shell_test_LDADD = $(progs_ldadd) @@ -204,7 +199,6 @@ slice_concurrent_LDADD = $(thread_ldadd) slice_threadinit_SOURCES = slice-threadinit.c slice_threadinit_LDADD = $(thread_ldadd) spawn_test_LDADD = $(progs_ldadd) -string_test_LDADD = $(progs_ldadd) thread_test_LDADD = $(thread_ldadd) threadpool_test_LDADD = $(thread_ldadd) tree_test_LDADD = $(progs_ldadd) diff --git a/tests/printf-test.c b/tests/printf-test.c deleted file mode 100644 index 99f480379..000000000 --- a/tests/printf-test.c +++ /dev/null @@ -1,244 +0,0 @@ -/* GLIB - Library of useful routines for C programming - * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -/* - * Modified by the GLib Team 2003. See the AUTHORS - * file for a list of people on the GLib Team. See the ChangeLog - * files for a list of changes. These files are distributed with - * GLib at ftp://ftp.gtk.org/pub/gtk/. - */ - -#include "glib.h" - -#include <stdio.h> -#include <string.h> - -static gboolean any_failed = FALSE; -static gboolean failed = FALSE; - -#define TEST(message,cond) G_STMT_START { failed = !(cond); \ -if (failed) \ - { if (!message) \ - g_print ("(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \ - else \ - g_print ("(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), message ? (gchar*)message : ""); \ - fflush (stdout); \ - any_failed = TRUE; \ - } \ -} G_STMT_END - -#define TEST_FAILED(message) \ - G_STMT_START { g_print ("Error: "); g_print message; g_print ("\n"); any_failed = TRUE; } G_STMT_END - -static gboolean -same_value (const gchar *actual, - const gchar *expected) -{ - gdouble actual_value, expected_value; - - actual_value = g_ascii_strtod (actual, NULL); - expected_value = g_ascii_strtod (expected, NULL); - - return actual_value == expected_value; -} - -int -main (int argc, - char *argv[]) -{ - gchar buf[128]; - int i; - long l; - - /* truncation and return value */ - TEST (NULL, g_snprintf (buf, 0, "abc") == 3); - TEST (NULL, g_snprintf (NULL, 0, "abc") == 3); - TEST (NULL, g_snprintf (buf, 5, "abc") == 3); - TEST (NULL, g_snprintf (buf, 1, "abc") == 3 && buf[0] == '\0' && !strcmp (buf, "")); - TEST (NULL, g_snprintf (buf, 2, "abc") == 3 && buf[1] == '\0' && !strcmp (buf, "a")); - TEST (NULL, g_snprintf (buf, 3, "abc") == 3 && buf[2] == '\0' && !strcmp (buf, "ab")); - TEST (NULL, g_snprintf (buf, 4, "abc") == 3 && buf[3] == '\0' && !strcmp (buf, "abc")); - TEST (NULL, g_snprintf (buf, 5, "abc") == 3 && buf[3] == '\0' && !strcmp (buf, "abc")); - - /* %d, basic formatting */ - TEST (NULL, g_snprintf (buf, 128, "%d", 5) == 1 && !strcmp (buf, "5")); - TEST (NULL, g_snprintf (buf, 128, "%d", 0) == 1 && !strcmp (buf, "0")); - TEST (NULL, g_snprintf (buf, 128, "%.0d", 0) == 0 && !strcmp (buf, "")); - TEST (NULL, g_snprintf (buf, 128, "%.0d", 1) == 1 && !strcmp (buf, "1")); - TEST (NULL, g_snprintf (buf, 128, "%.d", 2) == 1 && !strcmp (buf, "2")); - TEST (NULL, g_snprintf (buf, 128, "%d", -1) == 2 && !strcmp (buf, "-1")); - TEST (NULL, g_snprintf (buf, 128, "%.3d", 5) == 3 && !strcmp (buf, "005")); - TEST (NULL, g_snprintf (buf, 128, "%.3d", -5) == 4 && !strcmp (buf, "-005")); - TEST (NULL, g_snprintf (buf, 128, "%5.3d", 5) == 5 && !strcmp (buf, " 005")); - TEST (NULL, g_snprintf (buf, 128, "%-5.3d", -5) == 5 && !strcmp (buf, "-005 ")); - /* %d, length modifiers */ - TEST (NULL, g_snprintf (buf, 128, "%" G_GINT16_FORMAT, (gint16)-5) == 2 && !strcmp (buf, "-5")); - TEST (NULL, g_snprintf (buf, 128, "%" G_GUINT16_FORMAT, (guint16)5) == 1 && !strcmp (buf, "5")); - TEST (NULL, g_snprintf (buf, 128, "%" G_GINT32_FORMAT, (gint32)-5) == 2 && !strcmp (buf, "-5")); - TEST (NULL, g_snprintf (buf, 128, "%" G_GUINT32_FORMAT, (guint32)5) == 1 && !strcmp (buf, "5")); - TEST (NULL, g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)-5) == 2 && !strcmp (buf, "-5")); - TEST (NULL, g_snprintf (buf, 128, "%" G_GUINT64_FORMAT, (guint64)5) == 1 && !strcmp (buf, "5")); - TEST (NULL, g_snprintf (buf, 128, "%" G_GSSIZE_FORMAT, (gssize)-5) == 2 && !strcmp (buf, "-5")); - TEST (NULL, g_snprintf (buf, 128, "%" G_GSIZE_FORMAT, (gsize)5) == 1 && !strcmp (buf, "5")); - /* %d, flags */ - TEST (NULL, g_snprintf (buf, 128, "%-d", 5) == 1 && !strcmp (buf, "5")); - TEST (NULL, g_snprintf (buf, 128, "%-+d", 5) == 2 && !strcmp (buf, "+5")); - TEST (NULL, g_snprintf (buf, 128, "%+-d", 5) == 2 && !strcmp (buf, "+5")); - TEST (NULL, g_snprintf (buf, 128, "%+d", -5) == 2 && !strcmp (buf, "-5")); - TEST (NULL, g_snprintf (buf, 128, "% d", 5) == 2 && !strcmp (buf, " 5")); - TEST (NULL, g_snprintf (buf, 128, "% .0d", 0) == 1 && !strcmp (buf, " ")); - TEST (NULL, g_snprintf (buf, 128, "% +d", 5) == 2 && !strcmp (buf, "+5")); - TEST (NULL, g_snprintf (buf, 128, "%03d", 5) == 3 && !strcmp (buf, "005")); - TEST (NULL, g_snprintf (buf, 128, "%-03d", -5) == 3 && !strcmp (buf, "-5 ")); - TEST (NULL, g_snprintf (buf, 128, "%03d", -5) == 3 && !strcmp (buf, "-05")); - - /* %o, basic formatting */ - TEST (NULL, g_snprintf (buf, 128, "%o", 5) == 1 && !strcmp (buf, "5")); - TEST (NULL, g_snprintf (buf, 128, "%o", 8) == 2 && !strcmp (buf, "10")); - TEST (NULL, g_snprintf (buf, 128, "%o", 0) == 1 && !strcmp (buf, "0")); - TEST (NULL, g_snprintf (buf, 128, "%.0o", 0) == 0 && !strcmp (buf, "")); - TEST (NULL, g_snprintf (buf, 128, "%.0o", 1) == 1 && !strcmp (buf, "1")); - TEST (NULL, g_snprintf (buf, 128, "%.3o", 5) == 3 && !strcmp (buf, "005")); - TEST (NULL, g_snprintf (buf, 128, "%.3o", 8) == 3 && !strcmp (buf, "010")); - TEST (NULL, g_snprintf (buf, 128, "%5.3o", 5) == 5 && !strcmp (buf, " 005")); - - /* %u, basic formatting */ - TEST (NULL, g_snprintf (buf, 128, "%u", 5) == 1 && !strcmp (buf, "5")); - TEST (NULL, g_snprintf (buf, 128, "%u", 0) == 1 && !strcmp (buf, "0")); - TEST (NULL, g_snprintf (buf, 128, "%.0u", 0) == 0 && !strcmp (buf, "")); - TEST (NULL, g_snprintf (buf, 128, "%.0u", 1) == 1 && !strcmp (buf, "1")); - TEST (NULL, g_snprintf (buf, 128, "%.3u", 5) == 3 && !strcmp (buf, "005")); - TEST (NULL, g_snprintf (buf, 128, "%5.3u", 5) == 5 && !strcmp (buf, " 005")); - - /* %x, basic formatting */ - TEST (NULL, g_snprintf (buf, 128, "%x", 5) == 1 && !strcmp (buf, "5")); - TEST (buf, g_snprintf (buf, 128, "%x", 31) == 2 && !strcmp (buf, "1f")); - TEST (NULL, g_snprintf (buf, 128, "%x", 0) == 1 && !strcmp (buf, "0")); - TEST (NULL, g_snprintf (buf, 128, "%.0x", 0) == 0 && !strcmp (buf, "")); - TEST (NULL, g_snprintf (buf, 128, "%.0x", 1) == 1 && !strcmp (buf, "1")); - TEST (NULL, g_snprintf (buf, 128, "%.3x", 5) == 3 && !strcmp (buf, "005")); - TEST (NULL, g_snprintf (buf, 128, "%.3x", 31) == 3 && !strcmp (buf, "01f")); - TEST (NULL, g_snprintf (buf, 128, "%5.3x", 5) == 5 && !strcmp (buf, " 005")); - /* %x, flags */ - TEST (NULL, g_snprintf (buf, 128, "%-x", 5) == 1 && !strcmp (buf, "5")); - TEST (NULL, g_snprintf (buf, 128, "%03x", 5) == 3 && !strcmp (buf, "005")); - TEST (NULL, g_snprintf (buf, 128, "%#x", 31) == 4 && !strcmp (buf, "0x1f")); - TEST (NULL, g_snprintf (buf, 128, "%#x", 0) == 1 && !strcmp (buf, "0")); - - /* %X, basic formatting */ - TEST (NULL, g_snprintf (buf, 128, "%X", 5) == 1 && !strcmp (buf, "5")); - TEST (buf, g_snprintf (buf, 128, "%X", 31) == 2 && !strcmp (buf, "1F")); - TEST (NULL, g_snprintf (buf, 128, "%X", 0) == 1 && !strcmp (buf, "0")); - TEST (NULL, g_snprintf (buf, 128, "%.0X", 0) == 0 && !strcmp (buf, "")); - TEST (NULL, g_snprintf (buf, 128, "%.0X", 1) == 1 && !strcmp (buf, "1")); - TEST (NULL, g_snprintf (buf, 128, "%.3X", 5) == 3 && !strcmp (buf, "005")); - TEST (NULL, g_snprintf (buf, 128, "%.3X", 31) == 3 && !strcmp (buf, "01F")); - TEST (NULL, g_snprintf (buf, 128, "%5.3X", 5) == 5 && !strcmp (buf, " 005")); - /* %X, flags */ - TEST (NULL, g_snprintf (buf, 128, "%-X", 5) == 1 && !strcmp (buf, "5")); - TEST (NULL, g_snprintf (buf, 128, "%03X", 5) == 3 && !strcmp (buf, "005")); - TEST (NULL, g_snprintf (buf, 128, "%#X", 31) == 4 && !strcmp (buf, "0X1F")); - TEST (NULL, g_snprintf (buf, 128, "%#X", 0) == 1 && !strcmp (buf, "0")); - - /* %f, basic formattting */ - TEST (NULL, g_snprintf (buf, 128, "%f", G_PI) == 8 && !strncmp (buf, "3.14159", 7)); - TEST (NULL, g_snprintf (buf, 128, "%.8f", G_PI) == 10 && !strncmp (buf, "3.1415926", 9)); - TEST (NULL, g_snprintf (buf, 128, "%.0f", G_PI) == 1 && !strcmp (buf, "3")); - TEST (NULL, g_snprintf (buf, 128, "%1.f", G_PI) == 1 && !strcmp (buf, "3")); - TEST (NULL, g_snprintf (buf, 128, "%3.f", G_PI) == 3 && !strcmp (buf, " 3")); - /* %f, flags */ - TEST (NULL, g_snprintf (buf, 128, "%+f", G_PI) == 9 && !strncmp (buf, "+3.14159", 8)); - TEST (NULL, g_snprintf (buf, 128, "% f", G_PI) == 9 && !strncmp (buf, " 3.14159", 8)); - TEST (NULL, g_snprintf (buf, 128, "%#.0f", G_PI) == 2 && !strcmp (buf, "3.")); - TEST (NULL, g_snprintf (buf, 128, "%05.2f", G_PI) == 5 && !strcmp (buf, "03.14")); - - /* %e, basic formatting */ - /* for %e we can't expect to reproduce exact strings and lengths, since SUS - * only guarantees that the exponent shall always contain at least two - * digits. On Windows, it seems to be at least three digits long. - * Therefore, we compare the results of parsing the expected result and the - * actual result. - */ - TEST (buf, g_snprintf (buf, 128, "%e", G_PI) >= 12 && same_value (buf, "3.141593e+00")); - TEST (buf, g_snprintf (buf, 128, "%.8e", G_PI) >= 14 && same_value (buf, "3.14159265e+00")); - TEST (buf, g_snprintf (buf, 128, "%.0e", G_PI) >= 5 && same_value (buf, "3e+00")); - TEST (buf, g_snprintf (buf, 128, "%.1e", 0.0) >= 7 && same_value (buf, "0.0e+00")); - TEST (buf, g_snprintf (buf, 128, "%.1e", 0.00001) >= 7 && same_value (buf, "1.0e-05")); - TEST (buf, g_snprintf (buf, 128, "%.1e", 10000.0) >= 7 && same_value (buf, "1.0e+04")); - /* %e, flags */ - TEST (buf, g_snprintf (buf, 128, "%+e", G_PI) >= 13 && same_value (buf, "+3.141593e+00")); - TEST (buf, g_snprintf (buf, 128, "% e", G_PI) >= 13 && same_value (buf, " 3.141593e+00")); - TEST (buf, g_snprintf (buf, 128, "%#.0e", G_PI) >= 6 && same_value (buf, "3.e+00")); - TEST (buf, g_snprintf (buf, 128, "%09.2e", G_PI) >= 9 && same_value (buf, "03.14e+00")); - - /* %c */ - TEST (NULL, g_snprintf (buf, 128, "%c", 'a') == 1 && !strcmp (buf, "a")); - - /* %s */ - TEST (NULL, g_snprintf (buf, 128, "%.2s", "abc") == 2 && !strcmp (buf, "ab")); - TEST (NULL, g_snprintf (buf, 128, "%.6s", "abc") == 3 && !strcmp (buf, "abc")); - TEST (NULL, g_snprintf (buf, 128, "%5s", "abc") == 5 && !strcmp (buf, " abc")); - TEST (NULL, g_snprintf (buf, 128, "%-5s", "abc") == 5 && !strcmp (buf, "abc ")); - TEST (NULL, g_snprintf (buf, 128, "%5.2s", "abc") == 5 && !strcmp (buf, " ab")); - TEST (NULL, g_snprintf (buf, 128, "%*s", 5, "abc") == 5 && !strcmp (buf, " abc")); -#if 0 /* HP-UX doesn't get this right */ - TEST (NULL, g_snprintf (buf, 128, "%*s", -5, "abc") == 5 && !strcmp (buf, "abc ")); -#endif - TEST (NULL, g_snprintf (buf, 128, "%*.*s", 5, 2, "abc") == 5 && !strcmp (buf, " ab")); - - /* %n */ - TEST (NULL, g_snprintf (buf, 128, "abc%n", &i) == 3 && !strcmp (buf, "abc") && i == 3); - TEST (NULL, g_snprintf (buf, 128, "abc%ln", &l) == 3 && !strcmp (buf, "abc") && l == 3); - - /* %% */ - TEST (NULL, g_snprintf (buf, 128, "%%") == 1 && !strcmp (buf, "%")); - - /* positional parameters */ - TEST (NULL, g_snprintf (buf, 128, "%2$c %1$c", 'b', 'a') == 3 && !strcmp (buf, "a b")); - TEST (NULL, g_snprintf (buf, 128, "%1$*2$.*3$s", "abc", 5, 2) == 5 && !strcmp (buf, " ab")); - TEST (NULL, g_snprintf (buf, 128, "%1$s%1$s", "abc") == 6 && !strcmp (buf, "abcabc")); - - /* 64 bit support */ - TEST (NULL, g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)123456) == 6 && !strcmp (buf, "123456")); - TEST (NULL, g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)-123456) == 7 && !strcmp (buf, "-123456")); - TEST (NULL, g_snprintf (buf, 128, "%" G_GUINT64_FORMAT, (guint64)123456) == 6 && !strcmp (buf, "123456")); - TEST (NULL, g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "o", (gint64)123456) == 6 && !strcmp (buf, "361100")); - TEST (NULL, g_snprintf (buf, 128, "%#" G_GINT64_MODIFIER "o", (gint64)123456) == 7 && !strcmp (buf, "0361100")); - TEST (NULL, g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "x", (gint64)123456) == 5 && !strcmp (buf, "1e240")); - TEST (NULL, g_snprintf (buf, 128, "%#" G_GINT64_MODIFIER "x", (gint64)123456) == 7 && !strcmp (buf, "0x1e240")); - TEST (NULL, g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "X", (gint64)123456) == 5 && !strcmp (buf, "1E240")); -#ifdef G_OS_WIN32 - /* On Win32, test that the "ll" modifier also works, for backward - * compatibility. One really should use the G_GINT64_MODIFIER (which - * on Win32 is the "I64" that the (msvcrt) C library's printf uses), - * but "ll" used to work with the "trio" g_printf implementation in - * GLib 2.2, so it's best if it continues to work. - */ - TEST (NULL, g_snprintf (buf, 128, "%" "lli", (gint64)123456) == 6 && !strcmp (buf, "123456")); - TEST (NULL, g_snprintf (buf, 128, "%" "lli", (gint64)-123456) == 7 && !strcmp (buf, "-123456")); - TEST (NULL, g_snprintf (buf, 128, "%" "llu", (guint64)123456) == 6 && !strcmp (buf, "123456")); - TEST (NULL, g_snprintf (buf, 128, "%" "ll" "o", (gint64)123456) == 6 && !strcmp (buf, "361100")); - TEST (NULL, g_snprintf (buf, 128, "%#" "ll" "o", (gint64)123456) == 7 && !strcmp (buf, "0361100")); - TEST (NULL, g_snprintf (buf, 128, "%" "ll" "x", (gint64)123456) == 5 && !strcmp (buf, "1e240")); - TEST (NULL, g_snprintf (buf, 128, "%#" "ll" "x", (gint64)123456) == 7 && !strcmp (buf, "0x1e240")); - TEST (NULL, g_snprintf (buf, 128, "%" "ll" "X", (gint64)123456) == 5 && !strcmp (buf, "1E240")); -#endif - - return any_failed; -} diff --git a/tests/rand-test.c b/tests/rand-test.c deleted file mode 100644 index 141883eb8..000000000 --- a/tests/rand-test.c +++ /dev/null @@ -1,135 +0,0 @@ -#undef G_DISABLE_ASSERT -#undef G_LOG_DOMAIN - -#include <glib.h> - -/* Outputs tested against the reference implementation mt19937ar.c from - http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html */ - -/* Tests for a simple seed, first number is the seed */ -const guint32 first_numbers[] = -{ - 0x7a7a7a7a, - 0xfdcc2d54, - 0x3a279ceb, - 0xc4d39c33, - 0xf31895cd, - 0x46ca0afc, - 0x3f5484ff, - 0x54bc9557, - 0xed2c24b1, - 0x84062503, - 0x8f6404b3, - 0x599a94b3, - 0xe46d03d5, - 0x310beb78, - 0x7bee5d08, - 0x760d09be, - 0x59b6e163, - 0xbf6d16ec, - 0xcca5fb54, - 0x5de7259b, - 0x1696330c, -}; - -/* array seed */ -const guint32 seed_array[] = -{ - 0x6553375f, - 0xd6b8d43b, - 0xa1e7667f, - 0x2b10117c -}; - -/* tests for the array seed */ -const guint32 array_outputs[] = -{ - 0xc22b7dc3, - 0xfdecb8ae, - 0xb4af0738, - 0x516bc6e1, - 0x7e372e91, - 0x2d38ff80, - 0x6096494a, - 0xd162d5a8, - 0x3c0aaa0d, - 0x10e736ae -}; - -const gint length = sizeof (first_numbers) / sizeof (first_numbers[0]); -const gint seed_length = sizeof (seed_array) / sizeof (seed_array[0]); -const gint array_length = sizeof (array_outputs) / sizeof (array_outputs[0]); - -int main() -{ - guint n; - guint ones; - double proportion; - - GRand* rand = g_rand_new_with_seed (first_numbers[0]); - GRand* copy; - - for (n = 1; n < length; n++) - g_assert (first_numbers[n] == g_rand_int (rand)); - - g_rand_set_seed (rand, 2); - g_rand_set_seed_array (rand, seed_array, seed_length); - - for (n = 0; n < array_length; n++) - g_assert (array_outputs[n] == g_rand_int (rand)); - - copy = g_rand_copy (rand); - for (n = 0; n < 100; n++) - g_assert (g_rand_int (copy) == g_rand_int (rand)); - - for (n = 1; n < 100000; n++) - { - gint32 i; - gdouble d; - gboolean b; - - i = g_rand_int_range (rand, 8,16); - g_assert (i >= 8 && i < 16); - - i = g_random_int_range (8,16); - g_assert (i >= 8 && i < 16); - - d = g_rand_double (rand); - g_assert (d >= 0 && d < 1); - - d = g_random_double (); - g_assert (d >= 0 && d < 1); - - d = g_rand_double_range (rand, -8, 32); - g_assert (d >= -8 && d < 32); - - d = g_random_double_range (-8, 32); - g_assert (d >= -8 && d < 32); - - b = g_random_boolean (); - g_assert (b == TRUE || b == FALSE); - - b = g_rand_boolean (rand); - g_assert (b == TRUE || b == FALSE); - } - - /* Statistical sanity check, count the number of ones - * when getting random numbers in range [0,3) and see - * that it must be semi-close to 0.25 with a VERY large - * probability */ - ones = 0; - for (n = 1; n < 100000; n++) - { - if (g_random_int_range (0, 4) == 1) - ones ++; - } - proportion = (double)ones / (double)100000; - /* 0.025 is overkill, but should suffice to test for some unreasonability */ - g_assert (ABS (proportion - 0.25) < 0.025); - - g_rand_free (rand); - g_rand_free (copy); - - return 0; -} - diff --git a/tests/string-test.c b/tests/string-test.c deleted file mode 100644 index 7da4128f1..000000000 --- a/tests/string-test.c +++ /dev/null @@ -1,313 +0,0 @@ -/* GLIB - Library of useful routines for C programming - * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -/* - * Modified by the GLib Team and others 1997-2000. See the AUTHORS - * file for a list of people on the GLib Team. See the ChangeLog - * files for a list of changes. These files are distributed with - * GLib at ftp://ftp.gtk.org/pub/gtk/. - */ - -#undef G_DISABLE_ASSERT -#undef G_LOG_DOMAIN - -#include <stdio.h> -#include <string.h> -#include "glib.h" -#include "glib/gprintf.h" - -int array[10000]; -gboolean failed = FALSE; - -#define TEST(m,cond) G_STMT_START { failed = !(cond); \ -if (failed) \ - { if (!m) \ - g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \ - else \ - g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \ - } \ -else \ - g_print ("."); fflush (stdout); \ -} G_STMT_END - -#define C2P(c) ((gpointer) ((long) (c))) -#define P2C(p) ((gchar) ((long) (p))) - -#define GLIB_TEST_STRING "el dorado " -#define GLIB_TEST_STRING_5 "el do" - -typedef struct { - guint age; - gchar name[40]; -} GlibTestInfo; - -int -main (int argc, - char *argv[]) -{ - GStringChunk *string_chunk; - - gchar *tmp_string = NULL, *tmp_string_2; - gint i; - GString *string1, *string2; - - string_chunk = g_string_chunk_new (1024); - - for (i = 0; i < 100000; i ++) - { - tmp_string = g_string_chunk_insert (string_chunk, "hi pete"); - - if (strcmp ("hi pete", tmp_string) != 0) - g_error ("string chunks are broken.\n"); - } - - tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string); - - g_assert (tmp_string_2 != tmp_string && - strcmp(tmp_string_2, tmp_string) == 0); - - tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string); - - g_assert (tmp_string_2 == tmp_string); - - g_string_chunk_free (string_chunk); - - string1 = g_string_new ("hi pete!"); - string2 = g_string_new (NULL); - - g_assert (string1 != NULL); - g_assert (string2 != NULL); - g_assert (strlen (string1->str) == string1->len); - g_assert (strlen (string2->str) == string2->len); - g_assert (string2->len == 0); - g_assert (strcmp ("hi pete!", string1->str) == 0); - g_assert (strcmp ("", string2->str) == 0); - - for (i = 0; i < 10000; i++) - g_string_append_c (string1, 'a'+(i%26)); - - g_assert((strlen("hi pete!") + 10000) == string1->len); - g_assert((strlen("hi pete!") + 10000) == strlen(string1->str)); - -#ifndef G_OS_WIN32 - /* MSVC and mingw32 use the same run-time C library, which doesn't like - the %10000.10000f format... */ - g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f", - "this pete guy sure is a wuss, like he's the number ", - 1, - " wuss. everyone agrees.\n", - string1->str, - 10, 666, 15, 15, 666.666666666, 666.666666666); -#else - g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f", - "this pete guy sure is a wuss, like he's the number ", - 1, - " wuss. everyone agrees.\n", - string1->str, - 10, 666, 15, 15, 666.666666666, 666.666666666); -#endif - - g_string_free (string1, TRUE); - g_string_free (string2, TRUE); - - /* append */ - string1 = g_string_new ("firsthalf"); - g_string_append (string1, "lasthalf"); - g_assert (strcmp (string1->str, "firsthalflasthalf") == 0); - g_string_free (string1, TRUE); - - /* append_len */ - string1 = g_string_new ("firsthalf"); - g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf")); - g_assert (strcmp (string1->str, "firsthalflasthalf") == 0); - g_string_free (string1, TRUE); - - /* prepend */ - string1 = g_string_new ("lasthalf"); - g_string_prepend (string1, "firsthalf"); - g_assert (strcmp (string1->str, "firsthalflasthalf") == 0); - g_string_free (string1, TRUE); - - /* prepend_len */ - string1 = g_string_new ("lasthalf"); - g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf")); - g_assert (strcmp (string1->str, "firsthalflasthalf") == 0); - g_string_free (string1, TRUE); - - /* insert */ - string1 = g_string_new ("firstlast"); - g_string_insert (string1, 5, "middle"); - g_assert (strcmp (string1->str, "firstmiddlelast") == 0); - g_string_free (string1, TRUE); - - /* insert with pos == end of the string */ - string1 = g_string_new ("firstmiddle"); - g_string_insert (string1, strlen ("firstmiddle"), "last"); - g_assert (strcmp (string1->str, "firstmiddlelast") == 0); - g_string_free (string1, TRUE); - - /* insert_len */ - string1 = g_string_new ("firstlast"); - g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle")); - g_assert (strcmp (string1->str, "firstmiddlelast") == 0); - g_string_free (string1, TRUE); - - /* insert_len with magic -1 pos for append */ - string1 = g_string_new ("first"); - g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last")); - g_assert (strcmp (string1->str, "firstlast") == 0); - g_string_free (string1, TRUE); - - /* insert_len with magic -1 len for strlen-the-string */ - string1 = g_string_new ("first"); - g_string_insert_len (string1, 5, "last", -1); - g_assert (strcmp (string1->str, "firstlast") == 0); - g_string_free (string1, TRUE); - - /* insert_len with string overlap */ - string1 = g_string_new ("textbeforetextafter"); - g_string_insert_len (string1, 10, string1->str + 8, 5); - g_assert (strcmp (string1->str, "textbeforeretextextafter") == 0); - g_string_free (string1, TRUE); - - string1 = g_string_new ("boring text"); - g_string_insert_len (string1, 7, string1->str + 2, 4); - g_assert (strcmp (string1->str, "boring ringtext") == 0); - g_string_free (string1, TRUE); - - string1 = g_string_new ("boring text"); - g_string_insert_len (string1, 6, string1->str + 7, 4); - g_assert (strcmp (string1->str, "boringtext text") == 0); - g_string_free (string1, TRUE); - - /* assign_len with string overlap */ - string1 = g_string_new ("textbeforetextafter"); - g_string_assign (string1, string1->str + 10); - g_assert (strcmp (string1->str, "textafter") == 0); - g_string_free (string1, TRUE); - - string1 = g_string_new ("boring text"); - g_string_assign (string1, string1->str); - g_assert (strcmp (string1->str, "boring text") == 0); - g_string_free (string1, TRUE); - - /* insert_unichar with insertion in middle */ - string1 = g_string_new ("firsthalf"); - g_string_insert_unichar (string1, 5, 0x0041); - g_assert (strcmp (string1->str, "first\x41half") == 0); - g_string_free (string1, TRUE); - - string1 = g_string_new ("firsthalf"); - g_string_insert_unichar (string1, 5, 0x0298); - g_assert (strcmp (string1->str, "first\xCA\x98half") == 0); - g_string_free (string1, TRUE); - - string1 = g_string_new ("firsthalf"); - g_string_insert_unichar (string1, 5, 0xFFFD); - g_assert (strcmp (string1->str, "first\xEF\xBF\xBDhalf") == 0); - g_string_free (string1, TRUE); - - string1 = g_string_new ("firsthalf"); - g_string_insert_unichar (string1, 5, 0x1D100); - g_assert (strcmp (string1->str, "first\xF0\x9D\x84\x80half") == 0); - g_string_free (string1, TRUE); - - /* insert_unichar with insertion at end */ - string1 = g_string_new ("start"); - g_string_insert_unichar (string1, -1, 0x0041); - g_assert (strcmp (string1->str, "start\x41") == 0); - g_string_free (string1, TRUE); - - string1 = g_string_new ("start"); - g_string_insert_unichar (string1, -1, 0x0298); - g_assert (strcmp (string1->str, "start\xCA\x98") == 0); - g_string_free (string1, TRUE); - - string1 = g_string_new ("start"); - g_string_insert_unichar (string1, -1, 0xFFFD); - g_assert (strcmp (string1->str, "start\xEF\xBF\xBD") == 0); - g_string_free (string1, TRUE); - - string1 = g_string_new ("start"); - g_string_insert_unichar (string1, -1, 0x1D100); - g_assert (strcmp (string1->str, "start\xF0\x9D\x84\x80") == 0); - g_string_free (string1, TRUE); - - /* g_string_equal */ - string1 = g_string_new ("test"); - string2 = g_string_new ("te"); - g_assert (! g_string_equal(string1, string2)); - g_string_append (string2, "st"); - g_assert (g_string_equal(string1, string2)); - g_string_free (string1, TRUE); - g_string_free (string2, TRUE); - - /* overwriting functions */ - string1 = g_string_new ("testing"); - - g_string_overwrite (string1, 4, " and expand"); - g_assert (15 == string1->len); - g_assert ('\0' == string1->str[15]); - g_assert (g_str_equal ("test and expand", string1->str)); - - g_string_overwrite (string1, 5, "NOT-"); - g_assert (15 == string1->len); - g_assert ('\0' == string1->str[15]); - g_assert (g_str_equal ("test NOT-expand", string1->str)); - - g_string_overwrite_len (string1, 9, "blablabla", 6); - g_assert (15 == string1->len); - g_assert ('\0' == string1->str[15]); - g_assert (g_str_equal ("test NOT-blabla", string1->str)); - - g_string_free (string1, TRUE); - - /* Check handling of embedded ASCII 0 (NUL) characters in GString. */ - string1 = g_string_new ("fiddle"); - string2 = g_string_new ("fiddle"); - g_assert (g_string_equal(string1, string2)); - g_string_append_c(string1, '\0'); - g_assert (! g_string_equal(string1, string2)); - g_string_append_c(string2, '\0'); - g_assert (g_string_equal(string1, string2)); - g_string_append_c(string1, 'x'); - g_string_append_c(string2, 'y'); - g_assert (! g_string_equal(string1, string2)); - g_assert (string1->len == 8); - g_string_append(string1, "yzzy"); - g_assert (string1->len == 12); - g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0); - g_string_insert(string1, 1, "QED"); - g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0); - g_string_printf (string1, "fiddle%cxyzzy", '\0'); - g_assert (string1->len == 12); - g_assert (memcmp (string1->str, "fiddle\0xyzzy", 13) == 0); - - g_string_free (string1, TRUE); - g_string_free (string2, TRUE); - - tmp_string = (gchar *) g_malloc (10); - g_snprintf (tmp_string, 10, "%2$s %1$s", "a", "b"); - g_assert (strcmp (tmp_string, "b a") == 0); - g_free (tmp_string); - - return 0; -} - - |