mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-02-04 02:06:18 +01:00
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
This commit is contained in:
parent
e701ea96b6
commit
e6eb809599
19
ChangeLog
19
ChangeLog
@ -1,3 +1,22 @@
|
||||
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
|
||||
|
||||
2008-08-31 Emmanuele Bassi <ebassi@gnome.org>
|
||||
|
||||
Bug 550096 – GBookmarkFile parser is not forward compatible
|
||||
|
@ -22,10 +22,22 @@ TEST_PROGS += fileutils
|
||||
fileutils_SOURCES = fileutils.c
|
||||
fileutils_LDADD = $(progs_ldadd)
|
||||
|
||||
TEST_PROGS += printf
|
||||
printf_SOURCES = printf.c
|
||||
printf_LDADD = $(progs_ldadd) -lm
|
||||
|
||||
TEST_PROGS += rand
|
||||
rand_SOURCES = rand.c
|
||||
rand_LDADD = $(progs_ldadd) -lm
|
||||
|
||||
TEST_PROGS += strfuncs
|
||||
strfuncs_SOURCES = strfuncs.c
|
||||
strfuncs_LDADD = $(progs_ldadd) -lm
|
||||
|
||||
TEST_PROGS += string
|
||||
string_SOURCES = string.c
|
||||
string_LDADD = $(progs_ldadd) -lm
|
||||
|
||||
TEST_PROGS += markup-subparser
|
||||
markup_subparser_LDADD = $(progs_ldadd)
|
||||
|
||||
|
703
glib/tests/printf.c
Normal file
703
glib/tests/printf.c
Normal file
@ -0,0 +1,703 @@
|
||||
/* Unit tests for gstring
|
||||
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
||||
*
|
||||
* This work is provided "as is"; redistribution and modification
|
||||
* in whole or in part, in any medium, physical or electronic is
|
||||
* permitted without restriction.
|
||||
*
|
||||
* This work 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.
|
||||
*
|
||||
* In no event shall the authors or contributors be liable for any
|
||||
* direct, indirect, incidental, special, exemplary, or consequential
|
||||
* damages (including, but not limited to, procurement of substitute
|
||||
* goods or services; loss of use, data, or profits; or business
|
||||
* interruption) however caused and on any theory of liability, whether
|
||||
* in contract, strict liability, or tort (including negligence or
|
||||
* otherwise) arising in any way out of the use of this software, even
|
||||
* if advised of the possibility of such damage.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "glib.h"
|
||||
|
||||
static void
|
||||
test_retval_and_trunc (void)
|
||||
{
|
||||
gchar buf[128];
|
||||
gint res;
|
||||
|
||||
res = g_snprintf (buf, 0, "abc");
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
|
||||
res = g_snprintf (NULL, 0, "abc");
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
|
||||
res = g_snprintf (buf, 5, "abc");
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
|
||||
res = g_snprintf (buf, 1, "abc");
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
g_assert (buf[0] == '\0');
|
||||
g_assert_cmpstr (buf, ==, "");
|
||||
|
||||
res = g_snprintf (buf, 2, "abc");
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
g_assert (buf[1] == '\0');
|
||||
g_assert_cmpstr (buf, ==, "a");
|
||||
|
||||
res = g_snprintf (buf, 3, "abc");
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
g_assert (buf[2] == '\0');
|
||||
g_assert_cmpstr (buf, ==, "ab");
|
||||
|
||||
res = g_snprintf (buf, 4, "abc");
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
g_assert (buf[3] == '\0');
|
||||
g_assert_cmpstr (buf, ==, "abc");
|
||||
|
||||
res = g_snprintf (buf, 5, "abc");
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
g_assert (buf[3] == '\0');
|
||||
g_assert_cmpstr (buf, ==, "abc");
|
||||
}
|
||||
|
||||
static void
|
||||
test_d (void)
|
||||
{
|
||||
gchar buf[128];
|
||||
gint res;
|
||||
|
||||
/* %d basic formatting */
|
||||
|
||||
res = g_snprintf (buf, 128, "%d", 5);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "5");
|
||||
|
||||
res = g_snprintf (buf, 128, "%d", 0);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "0");
|
||||
|
||||
res = g_snprintf (buf, 128, "%.0d", 0);
|
||||
g_assert_cmpint (res, ==, 0);
|
||||
g_assert_cmpstr (buf, ==, "");
|
||||
|
||||
res = g_snprintf (buf, 128, "%.0d", 1);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "1");
|
||||
|
||||
res = g_snprintf (buf, 128, "%.d", 2);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "2");
|
||||
|
||||
res = g_snprintf (buf, 128, "%d", -1);
|
||||
g_assert_cmpint (res, ==, 2);
|
||||
g_assert_cmpstr (buf, ==, "-1");
|
||||
|
||||
res = g_snprintf (buf, 128, "%.3d", 5);
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
g_assert_cmpstr (buf, ==, "005");
|
||||
|
||||
res = g_snprintf (buf, 128, "%.3d", -5);
|
||||
g_assert_cmpint (res, ==, 4);
|
||||
g_assert_cmpstr (buf, ==, "-005");
|
||||
|
||||
res = g_snprintf (buf, 128, "%5.3d", 5);
|
||||
g_assert_cmpint (res, ==, 5);
|
||||
g_assert_cmpstr (buf, ==, " 005");
|
||||
|
||||
res = g_snprintf (buf, 128, "%-5.3d", -5);
|
||||
g_assert_cmpint (res, ==, 5);
|
||||
g_assert_cmpstr (buf, ==, "-005 ");
|
||||
|
||||
/* %d, length modifiers */
|
||||
|
||||
res = g_snprintf (buf, 128, "%" G_GINT16_FORMAT, (gint16)-5);
|
||||
g_assert_cmpint (res, ==, 2);
|
||||
g_assert_cmpstr (buf, ==, "-5");
|
||||
|
||||
res = g_snprintf (buf, 128, "%" G_GUINT16_FORMAT, (guint16)5);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "5");
|
||||
|
||||
res = g_snprintf (buf, 128, "%" G_GINT32_FORMAT, (gint32)-5);
|
||||
g_assert_cmpint (res, ==, 2);
|
||||
g_assert_cmpstr (buf, ==, "-5");
|
||||
|
||||
res = g_snprintf (buf, 128, "%" G_GUINT32_FORMAT, (guint32)5);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "5");
|
||||
|
||||
res = g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)-5);
|
||||
g_assert_cmpint (res, ==, 2);
|
||||
g_assert_cmpstr (buf, ==, "-5");
|
||||
|
||||
res = g_snprintf (buf, 128, "%" G_GUINT64_FORMAT, (guint64)5);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "5");
|
||||
|
||||
res = g_snprintf (buf, 128, "%" G_GSSIZE_FORMAT, (gssize)-5);
|
||||
g_assert_cmpint (res, ==, 2);
|
||||
g_assert_cmpstr (buf, ==, "-5");
|
||||
|
||||
res = g_snprintf (buf, 128, "%" G_GSIZE_FORMAT, (gsize)5);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "5");
|
||||
|
||||
/* %d, flags */
|
||||
|
||||
res = g_snprintf (buf, 128, "%-d", 5);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "5");
|
||||
|
||||
res = g_snprintf (buf, 128, "%-+d", 5);
|
||||
g_assert_cmpint (res, ==, 2);
|
||||
g_assert_cmpstr (buf, ==, "+5");
|
||||
|
||||
res = g_snprintf (buf, 128, "%+-d", 5);
|
||||
g_assert_cmpint (res, ==, 2);
|
||||
g_assert_cmpstr (buf, ==, "+5");
|
||||
|
||||
res = g_snprintf (buf, 128, "%+d", -5);
|
||||
g_assert_cmpint (res, ==, 2);
|
||||
g_assert_cmpstr (buf, ==, "-5");
|
||||
|
||||
res = g_snprintf (buf, 128, "% d", 5);
|
||||
g_assert_cmpint (res, ==, 2);
|
||||
g_assert_cmpstr (buf, ==, " 5");
|
||||
|
||||
res = g_snprintf (buf, 128, "% .0d", 0);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, " ");
|
||||
|
||||
res = g_snprintf (buf, 128, "% +d", 5);
|
||||
g_assert_cmpint (res, ==, 2);
|
||||
g_assert_cmpstr (buf, ==, "+5");
|
||||
|
||||
res = g_snprintf (buf, 128, "%03d", 5);
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
g_assert_cmpstr (buf, ==, "005");
|
||||
|
||||
res = g_snprintf (buf, 128, "%-03d", -5);
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
g_assert_cmpstr (buf, ==, "-5 ");
|
||||
|
||||
res = g_snprintf (buf, 128, "%03d", -5);
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
g_assert_cmpstr (buf, ==, "-05");
|
||||
}
|
||||
|
||||
static void
|
||||
test_o (void)
|
||||
{
|
||||
gchar buf[128];
|
||||
gint res;
|
||||
|
||||
/* %o basic formatting */
|
||||
|
||||
res = g_snprintf (buf, 128, "%o", 5);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "5");
|
||||
|
||||
res = g_snprintf (buf, 128, "%o", 8);
|
||||
g_assert_cmpint (res, ==, 2);
|
||||
g_assert_cmpstr (buf, ==, "10");
|
||||
|
||||
res = g_snprintf (buf, 128, "%o", 0);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "0");
|
||||
|
||||
res = g_snprintf (buf, 128, "%.0o", 0);
|
||||
g_assert_cmpint (res, ==, 0);
|
||||
g_assert_cmpstr (buf, ==, "");
|
||||
|
||||
res = g_snprintf (buf, 128, "%.0o", 1);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "1");
|
||||
|
||||
res = g_snprintf (buf, 128, "%.3o", 5);
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
g_assert_cmpstr (buf, ==, "005");
|
||||
|
||||
res = g_snprintf (buf, 128, "%.3o", 8);
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
g_assert_cmpstr (buf, ==, "010");
|
||||
|
||||
res = g_snprintf (buf, 128, "%5.3o", 5);
|
||||
g_assert_cmpint (res, ==, 5);
|
||||
g_assert_cmpstr (buf, ==, " 005");
|
||||
}
|
||||
|
||||
static void
|
||||
test_u (void)
|
||||
{
|
||||
gchar buf[128];
|
||||
gint res;
|
||||
|
||||
/* %u, basic formatting */
|
||||
|
||||
res = g_snprintf (buf, 128, "%u", 5);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "5");
|
||||
|
||||
res = g_snprintf (buf, 128, "%u", 0);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "0");
|
||||
|
||||
res = g_snprintf (buf, 128, "%.0u", 0);
|
||||
g_assert_cmpint (res, ==, 0);
|
||||
g_assert_cmpstr (buf, ==, "");
|
||||
|
||||
res = g_snprintf (buf, 128, "%.0u", 1);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "1");
|
||||
|
||||
res = g_snprintf (buf, 128, "%.3u", 5);
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
g_assert_cmpstr (buf, ==, "005");
|
||||
|
||||
res = g_snprintf (buf, 128, "%5.3u", 5);
|
||||
g_assert_cmpint (res, ==, 5);
|
||||
g_assert_cmpstr (buf, ==, " 005");
|
||||
}
|
||||
|
||||
static void
|
||||
test_x (void)
|
||||
{
|
||||
gchar buf[128];
|
||||
gint res;
|
||||
|
||||
/* %x, basic formatting */
|
||||
|
||||
res = g_snprintf (buf, 128, "%x", 5);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "5");
|
||||
|
||||
res = g_snprintf (buf, 128, "%x", 31);
|
||||
g_assert_cmpint (res, ==, 2);
|
||||
g_assert_cmpstr (buf, ==, "1f");
|
||||
|
||||
res = g_snprintf (buf, 128, "%x", 0);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "0");
|
||||
|
||||
res = g_snprintf (buf, 128, "%.0x", 0);
|
||||
g_assert_cmpint (res, ==, 0);
|
||||
g_assert_cmpstr (buf, ==, "");
|
||||
|
||||
res = g_snprintf (buf, 128, "%.0x", 1);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "1");
|
||||
|
||||
res = g_snprintf (buf, 128, "%.3x", 5);
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
g_assert_cmpstr (buf, ==, "005");
|
||||
|
||||
res = g_snprintf (buf, 128, "%.3x", 31);
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
g_assert_cmpstr (buf, ==, "01f");
|
||||
|
||||
res = g_snprintf (buf, 128, "%5.3x", 5);
|
||||
g_assert_cmpint (res, ==, 5);
|
||||
g_assert_cmpstr (buf, ==, " 005");
|
||||
|
||||
/* %x, flags */
|
||||
|
||||
res = g_snprintf (buf, 128, "%-x", 5);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "5");
|
||||
|
||||
res = g_snprintf (buf, 128, "%03x", 5);
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
g_assert_cmpstr (buf, ==, "005");
|
||||
|
||||
res = g_snprintf (buf, 128, "%#x", 31);
|
||||
g_assert_cmpint (res, ==, 4);
|
||||
g_assert_cmpstr (buf, ==, "0x1f");
|
||||
|
||||
res = g_snprintf (buf, 128, "%#x", 0);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "0");
|
||||
}
|
||||
|
||||
static void
|
||||
test_X (void)
|
||||
{
|
||||
gchar buf[128];
|
||||
gint res;
|
||||
|
||||
/* %X, basic formatting */
|
||||
|
||||
res = g_snprintf (buf, 128, "%X", 5);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "5");
|
||||
|
||||
res = g_snprintf (buf, 128, "%X", 31);
|
||||
g_assert_cmpint (res, ==, 2);
|
||||
g_assert_cmpstr (buf, ==, "1F");
|
||||
|
||||
res = g_snprintf (buf, 128, "%X", 0);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "0");
|
||||
|
||||
res = g_snprintf (buf, 128, "%.0X", 0);
|
||||
g_assert_cmpint (res, ==, 0);
|
||||
g_assert_cmpstr (buf, ==, "");
|
||||
|
||||
res = g_snprintf (buf, 128, "%.0X", 1);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "1");
|
||||
|
||||
res = g_snprintf (buf, 128, "%.3X", 5);
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
g_assert_cmpstr (buf, ==, "005");
|
||||
|
||||
res = g_snprintf (buf, 128, "%.3X", 31);
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
g_assert_cmpstr (buf, ==, "01F");
|
||||
|
||||
res = g_snprintf (buf, 128, "%5.3X", 5);
|
||||
g_assert_cmpint (res, ==, 5);
|
||||
g_assert_cmpstr (buf, ==, " 005");
|
||||
|
||||
/* %X, flags */
|
||||
|
||||
res = g_snprintf (buf, 128, "%-X", 5);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "5");
|
||||
|
||||
res = g_snprintf (buf, 128, "%03X", 5);
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
g_assert_cmpstr (buf, ==, "005");
|
||||
|
||||
res = g_snprintf (buf, 128, "%#X", 31);
|
||||
g_assert_cmpint (res, ==, 4);
|
||||
g_assert_cmpstr (buf, ==, "0X1F");
|
||||
|
||||
res = g_snprintf (buf, 128, "%#X", 0);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "0");
|
||||
}
|
||||
|
||||
static void
|
||||
test_f (void)
|
||||
{
|
||||
gchar buf[128];
|
||||
gint res;
|
||||
|
||||
/* %f, basic formattting */
|
||||
|
||||
res = g_snprintf (buf, 128, "%f", G_PI);
|
||||
g_assert_cmpint (res, ==, 8);
|
||||
g_assert (0 == strncmp (buf, "3.14159", 7));
|
||||
|
||||
res = g_snprintf (buf, 128, "%.8f", G_PI);
|
||||
g_assert_cmpint (res, ==, 10);
|
||||
g_assert (0 == strncmp (buf, "3.1415926", 9));
|
||||
|
||||
res = g_snprintf (buf, 128, "%.0f", G_PI);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "3");
|
||||
|
||||
res = g_snprintf (buf, 128, "%1.f", G_PI);
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "3");
|
||||
|
||||
res = g_snprintf (buf, 128, "%3.f", G_PI);
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
g_assert_cmpstr (buf, ==, " 3");
|
||||
|
||||
/* %f, flags */
|
||||
|
||||
res = g_snprintf (buf, 128, "%+f", G_PI);
|
||||
g_assert_cmpint (res, ==, 9);
|
||||
g_assert (0 == strncmp (buf, "+3.14159", 8));
|
||||
|
||||
res = g_snprintf (buf, 128, "% f", G_PI);
|
||||
g_assert_cmpint (res, ==, 9);
|
||||
g_assert (0 == strncmp (buf, " 3.14159", 8));
|
||||
|
||||
res = g_snprintf (buf, 128, "%#.0f", G_PI);
|
||||
g_assert_cmpint (res, ==, 2);
|
||||
g_assert_cmpstr (buf, ==, "3.");
|
||||
|
||||
res = g_snprintf (buf, 128, "%05.2f", G_PI);
|
||||
g_assert_cmpint (res, ==, 5);
|
||||
g_assert_cmpstr (buf, ==, "03.14");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static void
|
||||
test_e (void)
|
||||
{
|
||||
gchar buf[128];
|
||||
gint res;
|
||||
|
||||
/* %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.
|
||||
*/
|
||||
|
||||
res = g_snprintf (buf, 128, "%e", G_PI);
|
||||
g_assert_cmpint (res, >=, 12);
|
||||
g_assert (same_value (buf, "3.141593e+00"));
|
||||
|
||||
res = g_snprintf (buf, 128, "%.8e", G_PI);
|
||||
g_assert_cmpint (res, >=, 14);
|
||||
g_assert (same_value (buf, "3.14159265e+00"));
|
||||
|
||||
res = g_snprintf (buf, 128, "%.0e", G_PI);
|
||||
g_assert_cmpint (res, >=, 5);
|
||||
g_assert (same_value (buf, "3e+00"));
|
||||
|
||||
res = g_snprintf (buf, 128, "%.1e", 0.0);
|
||||
g_assert_cmpint (res, >=, 7);
|
||||
g_assert (same_value (buf, "0.0e+00"));
|
||||
|
||||
res = g_snprintf (buf, 128, "%.1e", 0.00001);
|
||||
g_assert_cmpint (res, >=, 7);
|
||||
g_assert (same_value (buf, "1.0e-05"));
|
||||
|
||||
res = g_snprintf (buf, 128, "%.1e", 10000.0);
|
||||
g_assert_cmpint (res, >=, 7);
|
||||
g_assert (same_value (buf, "1.0e+04"));
|
||||
|
||||
/* %e, flags */
|
||||
|
||||
res = g_snprintf (buf, 128, "%+e", G_PI);
|
||||
g_assert_cmpint (res, >=, 13);
|
||||
g_assert (same_value (buf, "+3.141593e+00"));
|
||||
|
||||
res = g_snprintf (buf, 128, "% e", G_PI);
|
||||
g_assert_cmpint (res, >=, 13);
|
||||
g_assert (same_value (buf, " 3.141593e+00"));
|
||||
|
||||
res = g_snprintf (buf, 128, "%#.0e", G_PI);
|
||||
g_assert_cmpint (res, >=, 6);
|
||||
g_assert (same_value (buf, "3.e+00"));
|
||||
|
||||
res = g_snprintf (buf, 128, "%09.2e", G_PI);
|
||||
g_assert_cmpint (res, >=, 9);
|
||||
g_assert (same_value (buf, "03.14e+00"));
|
||||
}
|
||||
|
||||
static void
|
||||
test_c (void)
|
||||
{
|
||||
gchar buf[128];
|
||||
gint res;
|
||||
|
||||
res = g_snprintf (buf, 128, "%c", 'a');
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "a");
|
||||
}
|
||||
|
||||
static void
|
||||
test_s (void)
|
||||
{
|
||||
gchar buf[128];
|
||||
gint res;
|
||||
|
||||
res = g_snprintf (buf, 128, "%.2s", "abc");
|
||||
g_assert_cmpint (res, ==, 2);
|
||||
g_assert_cmpstr (buf, ==, "ab");
|
||||
|
||||
res = g_snprintf (buf, 128, "%.6s", "abc");
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
g_assert_cmpstr (buf, ==, "abc");
|
||||
|
||||
res = g_snprintf (buf, 128, "%5s", "abc");
|
||||
g_assert_cmpint (res, ==, 5);
|
||||
g_assert_cmpstr (buf, ==, " abc");
|
||||
|
||||
res = g_snprintf (buf, 128, "%-5s", "abc");
|
||||
g_assert_cmpint (res, ==, 5);
|
||||
g_assert_cmpstr (buf, ==, "abc ");
|
||||
|
||||
res = g_snprintf (buf, 128, "%5.2s", "abc");
|
||||
g_assert_cmpint (res, ==, 5);
|
||||
g_assert_cmpstr (buf, ==, " ab");
|
||||
|
||||
res = g_snprintf (buf, 128, "%*s", 5, "abc");
|
||||
g_assert_cmpint (res, ==, 5);
|
||||
g_assert_cmpstr (buf, ==, " abc");
|
||||
|
||||
#if 0 /* HP-UX doesn't get this right */
|
||||
res = g_snprintf (buf, 128, "%*s", -5, "abc");
|
||||
g_assert_cmpint (res, ==, 5);
|
||||
g_assert_cmpstr (buf, ==, "abc ");
|
||||
#endif
|
||||
|
||||
res = g_snprintf (buf, 128, "%*.*s", 5, 2, "abc");
|
||||
g_assert_cmpint (res, ==, 5);
|
||||
g_assert_cmpstr (buf, ==, " ab");
|
||||
}
|
||||
|
||||
static void
|
||||
test_n (void)
|
||||
{
|
||||
gchar buf[128];
|
||||
gint res;
|
||||
gint i;
|
||||
glong l;
|
||||
|
||||
res = g_snprintf (buf, 128, "abc%n", &i);
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
g_assert_cmpstr (buf, ==, "abc");
|
||||
g_assert_cmpint (i, ==, 3);
|
||||
|
||||
res = g_snprintf (buf, 128, "abc%ln", &l);
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
g_assert_cmpstr (buf, ==, "abc");
|
||||
g_assert_cmpint (l, ==, 3);
|
||||
}
|
||||
|
||||
static void
|
||||
test_percent (void)
|
||||
{
|
||||
gchar buf[128];
|
||||
gint res;
|
||||
|
||||
res = g_snprintf (buf, 128, "%%");
|
||||
g_assert_cmpint (res, ==, 1);
|
||||
g_assert_cmpstr (buf, ==, "%");
|
||||
}
|
||||
|
||||
static void
|
||||
test_positional_params (void)
|
||||
{
|
||||
gchar buf[128];
|
||||
gint res;
|
||||
|
||||
res = g_snprintf (buf, 128, "%2$c %1$c", 'b', 'a');
|
||||
g_assert_cmpint (res, ==, 3);
|
||||
g_assert_cmpstr (buf, ==, "a b");
|
||||
|
||||
res = g_snprintf (buf, 128, "%1$*2$.*3$s", "abc", 5, 2);
|
||||
g_assert_cmpint (res, ==, 5);
|
||||
g_assert_cmpstr (buf, ==, " ab");
|
||||
|
||||
res = g_snprintf (buf, 128, "%1$s%1$s", "abc");
|
||||
g_assert_cmpint (res, ==, 6);
|
||||
g_assert_cmpstr (buf, ==, "abcabc");
|
||||
}
|
||||
|
||||
static void
|
||||
test_64bit (void)
|
||||
{
|
||||
gchar buf[128];
|
||||
gint res;
|
||||
|
||||
res = g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)123456);
|
||||
g_assert_cmpint (res, ==, 6);
|
||||
g_assert_cmpstr (buf, ==, "123456");
|
||||
|
||||
res = g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)-123456);
|
||||
g_assert_cmpint (res, ==, 7);
|
||||
g_assert_cmpstr (buf, ==, "-123456");
|
||||
|
||||
res = g_snprintf (buf, 128, "%" G_GUINT64_FORMAT, (guint64)123456);
|
||||
g_assert_cmpint (res, ==, 6);
|
||||
g_assert_cmpstr (buf, ==, "123456");
|
||||
|
||||
res = g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "o", (gint64)123456);
|
||||
g_assert_cmpint (res, ==, 6);
|
||||
g_assert_cmpstr (buf, ==, "361100");
|
||||
|
||||
res = g_snprintf (buf, 128, "%#" G_GINT64_MODIFIER "o", (gint64)123456);
|
||||
g_assert_cmpint (res, ==, 7);
|
||||
g_assert_cmpstr (buf, ==, "0361100");
|
||||
|
||||
res = g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "x", (gint64)123456);
|
||||
g_assert_cmpint (res, ==, 5);
|
||||
g_assert_cmpstr (buf, ==, "1e240");
|
||||
|
||||
res = g_snprintf (buf, 128, "%#" G_GINT64_MODIFIER "x", (gint64)123456);
|
||||
g_assert_cmpint (res, ==, 7);
|
||||
g_assert_cmpstr (buf, ==, "0x1e240");
|
||||
|
||||
res = g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "X", (gint64)123456);
|
||||
g_assert_cmpint (res, ==, 5);
|
||||
g_assert_cmpstr (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.
|
||||
*/
|
||||
|
||||
res = g_snprintf (buf, 128, "%" "lli", (gint64)123456);
|
||||
g_assert_cmpint (res, ==, 6);
|
||||
g_assert_cmpstr (buf, ==, "123456");
|
||||
|
||||
res = g_snprintf (buf, 128, "%" "lli", (gint64)-123456);
|
||||
g_assert_cmpint (res, ==, 7);
|
||||
g_assert_cmpstr (buf, ==, "-123456");
|
||||
|
||||
res = g_snprintf (buf, 128, "%" "llu", (guint64)123456);
|
||||
g_assert_cmpint (res, ==, 6);
|
||||
g_assert_cmpstr (buf, ==, "123456");
|
||||
|
||||
res = g_snprintf (buf, 128, "%" "ll" "o", (gint64)123456);
|
||||
g_assert_cmpint (res, ==, 6);
|
||||
g_assert_cmpstr (buf, ==, "361100");
|
||||
|
||||
res = g_snprintf (buf, 128, "%#" "ll" "o", (gint64)123456);
|
||||
g_assert_cmpint (res, ==, 7);
|
||||
g_assert_cmpstr (buf, ==, "0361100");
|
||||
|
||||
res = g_snprintf (buf, 128, "%" "ll" "x", (gint64)123456);
|
||||
g_assert_cmpint (res, ==, 5);
|
||||
g_assert_cmpstr (buf, ==, "1e240");
|
||||
|
||||
res = g_snprintf (buf, 128, "%#" "ll" "x", (gint64)123456);
|
||||
g_assert_cmpint (res, ==, 7);
|
||||
g_assert_cmpstr (buf, ==, "0x1e240");
|
||||
|
||||
res = g_snprintf (buf, 128, "%" "ll" "X", (gint64)123456);
|
||||
g_assert_cmpint (res, ==, 5);
|
||||
g_assert_cmpstr (buf, ==, "1E240");
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char *argv[])
|
||||
{
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
|
||||
g_test_add_func ("/printf/test-retval-and-trunc", test_retval_and_trunc);
|
||||
g_test_add_func ("/printf/test-d", test_d);
|
||||
g_test_add_func ("/printf/test-o", test_o);
|
||||
g_test_add_func ("/printf/test-u", test_u);
|
||||
g_test_add_func ("/printf/test-x", test_x);
|
||||
g_test_add_func ("/printf/test-X", test_X);
|
||||
g_test_add_func ("/printf/test-f", test_f);
|
||||
g_test_add_func ("/printf/test-e", test_e);
|
||||
g_test_add_func ("/printf/test-c", test_c);
|
||||
g_test_add_func ("/printf/test-s", test_s);
|
||||
g_test_add_func ("/printf/test-n", test_n);
|
||||
g_test_add_func ("/printf/test-percent", test_percent);
|
||||
g_test_add_func ("/printf/test-positional-params", test_positional_params);
|
||||
g_test_add_func ("/printf/test-64bit", test_64bit);
|
||||
|
||||
return g_test_run();
|
||||
}
|
@ -1,7 +1,25 @@
|
||||
#undef G_DISABLE_ASSERT
|
||||
#undef G_LOG_DOMAIN
|
||||
/* Unit tests for gstring
|
||||
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
||||
*
|
||||
* This work is provided "as is"; redistribution and modification
|
||||
* in whole or in part, in any medium, physical or electronic is
|
||||
* permitted without restriction.
|
||||
*
|
||||
* This work 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.
|
||||
*
|
||||
* In no event shall the authors or contributors be liable for any
|
||||
* direct, indirect, incidental, special, exemplary, or consequential
|
||||
* damages (including, but not limited to, procurement of substitute
|
||||
* goods or services; loss of use, data, or profits; or business
|
||||
* interruption) however caused and on any theory of liability, whether
|
||||
* in contract, strict liability, or tort (including negligence or
|
||||
* otherwise) arising in any way out of the use of this software, even
|
||||
* if advised of the possibility of such damage.
|
||||
*/
|
||||
|
||||
#include <glib.h>
|
||||
#include "glib.h"
|
||||
|
||||
/* Outputs tested against the reference implementation mt19937ar.c from
|
||||
http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html */
|
||||
@ -56,26 +74,24 @@ const guint32 array_outputs[] =
|
||||
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()
|
||||
static void
|
||||
test_rand (void)
|
||||
{
|
||||
guint n;
|
||||
guint ones;
|
||||
double proportion;
|
||||
GRand *rand;
|
||||
GRand *copy;
|
||||
|
||||
GRand* rand = g_rand_new_with_seed (first_numbers[0]);
|
||||
GRand* copy;
|
||||
rand = g_rand_new_with_seed (first_numbers[0]);
|
||||
|
||||
for (n = 1; n < length; n++)
|
||||
for (n = 1; n < G_N_ELEMENTS (first_numbers); 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);
|
||||
g_rand_set_seed_array (rand, seed_array, G_N_ELEMENTS (seed_array));
|
||||
|
||||
for (n = 0; n < array_length; n++)
|
||||
for (n = 0; n < G_N_ELEMENTS (array_outputs); n++)
|
||||
g_assert (array_outputs[n] == g_rand_int (rand));
|
||||
|
||||
copy = g_rand_copy (rand);
|
||||
@ -123,6 +139,7 @@ int main()
|
||||
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);
|
||||
@ -133,3 +150,13 @@ int main()
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char *argv[])
|
||||
{
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
|
||||
g_test_add_func ("/rand/test-rand", test_rand);
|
||||
|
||||
return g_test_run();
|
||||
}
|
394
glib/tests/string.c
Normal file
394
glib/tests/string.c
Normal file
@ -0,0 +1,394 @@
|
||||
/* Unit tests for gstring
|
||||
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
||||
*
|
||||
* This work is provided "as is"; redistribution and modification
|
||||
* in whole or in part, in any medium, physical or electronic is
|
||||
* permitted without restriction.
|
||||
*
|
||||
* This work 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.
|
||||
*
|
||||
* In no event shall the authors or contributors be liable for any
|
||||
* direct, indirect, incidental, special, exemplary, or consequential
|
||||
* damages (including, but not limited to, procurement of substitute
|
||||
* goods or services; loss of use, data, or profits; or business
|
||||
* interruption) however caused and on any theory of liability, whether
|
||||
* in contract, strict liability, or tort (including negligence or
|
||||
* otherwise) arising in any way out of the use of this software, even
|
||||
* if advised of the possibility of such damage.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "glib.h"
|
||||
|
||||
|
||||
static void
|
||||
test_string_chunks (void)
|
||||
{
|
||||
GStringChunk *string_chunk;
|
||||
gchar *tmp_string, *tmp_string_2;
|
||||
gint i;
|
||||
|
||||
string_chunk = g_string_chunk_new (1024);
|
||||
|
||||
for (i = 0; i < 100000; i ++)
|
||||
{
|
||||
tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
|
||||
g_assert_cmpstr ("hi pete", ==, tmp_string);
|
||||
}
|
||||
|
||||
tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
|
||||
g_assert (tmp_string_2 != tmp_string);
|
||||
g_assert_cmpstr (tmp_string_2, ==, tmp_string);
|
||||
|
||||
tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
|
||||
g_assert_cmpstr (tmp_string_2, ==, tmp_string);
|
||||
|
||||
g_string_chunk_free (string_chunk);
|
||||
}
|
||||
|
||||
static void
|
||||
test_string_new (void)
|
||||
{
|
||||
GString *string1, *string2;
|
||||
|
||||
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_cmpstr ("hi pete!", ==, string1->str);
|
||||
g_assert_cmpstr ("", ==, string2->str);
|
||||
|
||||
g_string_free (string1, TRUE);
|
||||
g_string_free (string2, TRUE);
|
||||
}
|
||||
|
||||
static void
|
||||
test_string_printf (void)
|
||||
{
|
||||
GString *string;
|
||||
|
||||
string = g_string_new (NULL);
|
||||
|
||||
#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 (string, "%s|%0100d|%s|%0*d|%*.*f|%10000.10000f",
|
||||
"this pete guy sure is a wuss, like he's the number ",
|
||||
1,
|
||||
" wuss. everyone agrees.\n",
|
||||
10, 666, 15, 15, 666.666666666, 666.666666666);
|
||||
#else
|
||||
g_string_printf (string, "%s|%0100d|%s|%0*d|%*.*f|%100.100f",
|
||||
"this pete guy sure is a wuss, like he's the number ",
|
||||
1,
|
||||
" wuss. everyone agrees.\n",
|
||||
10, 666, 15, 15, 666.666666666, 666.666666666);
|
||||
#endif
|
||||
|
||||
g_string_free (string, TRUE);
|
||||
}
|
||||
|
||||
static void
|
||||
test_string_assign (void)
|
||||
{
|
||||
GString *string;
|
||||
|
||||
string = g_string_new (NULL);
|
||||
g_string_assign (string, "boring text");
|
||||
g_assert_cmpstr (string->str, ==, "boring text");
|
||||
g_string_free (string, TRUE);
|
||||
|
||||
/* assign with string overlap */
|
||||
string = g_string_new ("textbeforetextafter");
|
||||
g_string_assign (string, string->str + 10);
|
||||
g_assert_cmpstr (string->str, ==, "textafter");
|
||||
g_string_free (string, TRUE);
|
||||
|
||||
string = g_string_new ("boring text");
|
||||
g_string_assign (string, string->str);
|
||||
g_assert_cmpstr (string->str, ==, "boring text");
|
||||
g_string_free (string, TRUE);
|
||||
}
|
||||
|
||||
static void
|
||||
test_string_append_c (void)
|
||||
{
|
||||
GString *string;
|
||||
gint i;
|
||||
|
||||
string = g_string_new ("hi pete!");
|
||||
|
||||
for (i = 0; i < 10000; i++)
|
||||
g_string_append_c (string, 'a'+(i%26));
|
||||
|
||||
g_assert((strlen("hi pete!") + 10000) == string->len);
|
||||
g_assert((strlen("hi pete!") + 10000) == strlen(string->str));
|
||||
|
||||
g_string_free (string, TRUE);
|
||||
}
|
||||
|
||||
static void
|
||||
test_string_append (void)
|
||||
{
|
||||
GString *string;
|
||||
|
||||
/* append */
|
||||
string = g_string_new ("firsthalf");
|
||||
g_string_append (string, "lasthalf");
|
||||
g_assert_cmpstr (string->str, ==, "firsthalflasthalf");
|
||||
g_string_free (string, TRUE);
|
||||
|
||||
/* append_len */
|
||||
string = g_string_new ("firsthalf");
|
||||
g_string_append_len (string, "lasthalfjunkjunk", strlen ("lasthalf"));
|
||||
g_assert_cmpstr (string->str, ==, "firsthalflasthalf");
|
||||
g_string_free (string, TRUE);
|
||||
}
|
||||
|
||||
static void
|
||||
test_string_prepend_c (void)
|
||||
{
|
||||
GString *string;
|
||||
gint i;
|
||||
|
||||
string = g_string_new ("hi pete!");
|
||||
|
||||
for (i = 0; i < 10000; i++)
|
||||
g_string_prepend_c (string, 'a'+(i%26));
|
||||
|
||||
g_assert((strlen("hi pete!") + 10000) == string->len);
|
||||
g_assert((strlen("hi pete!") + 10000) == strlen(string->str));
|
||||
|
||||
g_string_free (string, TRUE);
|
||||
}
|
||||
|
||||
static void
|
||||
test_string_prepend (void)
|
||||
{
|
||||
GString *string;
|
||||
|
||||
/* prepend */
|
||||
string = g_string_new ("lasthalf");
|
||||
g_string_prepend (string, "firsthalf");
|
||||
g_assert_cmpstr (string->str, ==, "firsthalflasthalf");
|
||||
g_string_free (string, TRUE);
|
||||
|
||||
/* prepend_len */
|
||||
string = g_string_new ("lasthalf");
|
||||
g_string_prepend_len (string, "firsthalfjunkjunk", strlen ("firsthalf"));
|
||||
g_assert_cmpstr (string->str, ==, "firsthalflasthalf");
|
||||
g_string_free (string, TRUE);
|
||||
}
|
||||
|
||||
static void
|
||||
test_string_insert (void)
|
||||
{
|
||||
GString *string;
|
||||
|
||||
/* insert */
|
||||
string = g_string_new ("firstlast");
|
||||
g_string_insert (string, 5, "middle");
|
||||
g_assert_cmpstr (string->str, ==, "firstmiddlelast");
|
||||
g_string_free (string, TRUE);
|
||||
|
||||
/* insert with pos == end of the string */
|
||||
string = g_string_new ("firstmiddle");
|
||||
g_string_insert (string, strlen ("firstmiddle"), "last");
|
||||
g_assert_cmpstr (string->str, ==, "firstmiddlelast");
|
||||
g_string_free (string, TRUE);
|
||||
|
||||
/* insert_len */
|
||||
string = g_string_new ("firstlast");
|
||||
g_string_insert_len (string, 5, "middlejunkjunk", strlen ("middle"));
|
||||
g_assert_cmpstr (string->str, ==, "firstmiddlelast");
|
||||
g_string_free (string, TRUE);
|
||||
|
||||
/* insert_len with magic -1 pos for append */
|
||||
string = g_string_new ("first");
|
||||
g_string_insert_len (string, -1, "lastjunkjunk", strlen ("last"));
|
||||
g_assert_cmpstr (string->str, ==, "firstlast");
|
||||
g_string_free (string, TRUE);
|
||||
|
||||
/* insert_len with magic -1 len for strlen-the-string */
|
||||
string = g_string_new ("first");
|
||||
g_string_insert_len (string, 5, "last", -1);
|
||||
g_assert_cmpstr (string->str, ==, "firstlast");
|
||||
g_string_free (string, TRUE);
|
||||
|
||||
/* insert_len with string overlap */
|
||||
string = g_string_new ("textbeforetextafter");
|
||||
g_string_insert_len (string, 10, string->str + 8, 5);
|
||||
g_assert_cmpstr (string->str, ==, "textbeforeretextextafter");
|
||||
g_string_free (string, TRUE);
|
||||
}
|
||||
|
||||
static void
|
||||
test_string_insert_unichar (void)
|
||||
{
|
||||
GString *string;
|
||||
|
||||
/* insert_unichar with insertion in middle */
|
||||
string = g_string_new ("firsthalf");
|
||||
g_string_insert_unichar (string, 5, 0x0041);
|
||||
g_assert_cmpstr (string->str, ==, "first\x41half");
|
||||
g_string_free (string, TRUE);
|
||||
|
||||
string = g_string_new ("firsthalf");
|
||||
g_string_insert_unichar (string, 5, 0x0298);
|
||||
g_assert_cmpstr (string->str, ==, "first\xCA\x98half");
|
||||
g_string_free (string, TRUE);
|
||||
|
||||
string = g_string_new ("firsthalf");
|
||||
g_string_insert_unichar (string, 5, 0xFFFD);
|
||||
g_assert_cmpstr (string->str, ==, "first\xEF\xBF\xBDhalf");
|
||||
g_string_free (string, TRUE);
|
||||
|
||||
string = g_string_new ("firsthalf");
|
||||
g_string_insert_unichar (string, 5, 0x1D100);
|
||||
g_assert_cmpstr (string->str, ==, "first\xF0\x9D\x84\x80half");
|
||||
g_string_free (string, TRUE);
|
||||
|
||||
/* insert_unichar with insertion at end */
|
||||
string = g_string_new ("start");
|
||||
g_string_insert_unichar (string, -1, 0x0041);
|
||||
g_assert_cmpstr (string->str, ==, "start\x41");
|
||||
g_string_free (string, TRUE);
|
||||
|
||||
string = g_string_new ("start");
|
||||
g_string_insert_unichar (string, -1, 0x0298);
|
||||
g_assert_cmpstr (string->str, ==, "start\xCA\x98");
|
||||
g_string_free (string, TRUE);
|
||||
|
||||
string = g_string_new ("start");
|
||||
g_string_insert_unichar (string, -1, 0xFFFD);
|
||||
g_assert_cmpstr (string->str, ==, "start\xEF\xBF\xBD");
|
||||
g_string_free (string, TRUE);
|
||||
|
||||
string = g_string_new ("start");
|
||||
g_string_insert_unichar (string, -1, 0x1D100);
|
||||
g_assert_cmpstr (string->str, ==, "start\xF0\x9D\x84\x80");
|
||||
g_string_free (string, TRUE);
|
||||
}
|
||||
|
||||
static void
|
||||
test_string_equal (void)
|
||||
{
|
||||
GString *string1, *string2;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
static void
|
||||
test_string_truncate (void)
|
||||
{
|
||||
GString *string;
|
||||
|
||||
string = g_string_new ("testing");
|
||||
|
||||
g_string_truncate (string, 1000);
|
||||
g_assert (string->len == strlen("testing"));
|
||||
g_assert_cmpstr (string->str, ==, "testing");
|
||||
|
||||
g_string_truncate (string, 4);
|
||||
g_assert (string->len == 4);
|
||||
g_assert_cmpstr (string->str, ==, "test");
|
||||
|
||||
g_string_truncate (string, 0);
|
||||
g_assert (string->len == 0);
|
||||
g_assert_cmpstr (string->str, ==, "");
|
||||
|
||||
g_string_free (string, TRUE);
|
||||
}
|
||||
|
||||
static void
|
||||
test_string_overwrite (void)
|
||||
{
|
||||
GString *string;
|
||||
|
||||
/* overwriting functions */
|
||||
string = g_string_new ("testing");
|
||||
|
||||
g_string_overwrite (string, 4, " and expand");
|
||||
g_assert (15 == string->len);
|
||||
g_assert ('\0' == string->str[15]);
|
||||
g_assert (g_str_equal ("test and expand", string->str));
|
||||
|
||||
g_string_overwrite (string, 5, "NOT-");
|
||||
g_assert (15 == string->len);
|
||||
g_assert ('\0' == string->str[15]);
|
||||
g_assert (g_str_equal ("test NOT-expand", string->str));
|
||||
|
||||
g_string_overwrite_len (string, 9, "blablabla", 6);
|
||||
g_assert (15 == string->len);
|
||||
g_assert ('\0' == string->str[15]);
|
||||
g_assert (g_str_equal ("test NOT-blabla", string->str));
|
||||
|
||||
g_string_free (string, TRUE);
|
||||
}
|
||||
|
||||
static void
|
||||
test_string_nul_handling (void)
|
||||
{
|
||||
GString *string1, *string2;
|
||||
|
||||
/* 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);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char *argv[])
|
||||
{
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
|
||||
g_test_add_func ("/string/test-string-chunks", test_string_chunks);
|
||||
g_test_add_func ("/string/test-string-new", test_string_new);
|
||||
g_test_add_func ("/string/test-string-printf", test_string_printf);
|
||||
g_test_add_func ("/string/test-string-assign", test_string_assign);
|
||||
g_test_add_func ("/string/test-string-append-c", test_string_append_c);
|
||||
g_test_add_func ("/string/test-string-append", test_string_append);
|
||||
g_test_add_func ("/string/test-string-prepend-c", test_string_prepend_c);
|
||||
g_test_add_func ("/string/test-string-prepend", test_string_prepend);
|
||||
g_test_add_func ("/string/test-string-insert", test_string_insert);
|
||||
g_test_add_func ("/string/test-string-insert-unichar", test_string_insert_unichar);
|
||||
g_test_add_func ("/string/test-string-equal", test_string_equal);
|
||||
g_test_add_func ("/string/test-string-truncate", test_string_truncate);
|
||||
g_test_add_func ("/string/test-string-overwrite", test_string_overwrite);
|
||||
g_test_add_func ("/string/test-string-nul-handling", test_string_nul_handling);
|
||||
|
||||
return g_test_run();
|
||||
}
|
@ -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)
|
||||
|
@ -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;
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user