os/ossrv/glib/tsrc/BC/tests/string-test.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* GLIB - Library of useful routines for C programming
sl@0
     2
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
sl@0
     3
 * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
sl@0
     4
 * This library is free software; you can redistribute it and/or
sl@0
     5
 * modify it under the terms of the GNU Lesser General Public
sl@0
     6
 * License as published by the Free Software Foundation; either
sl@0
     7
 * version 2 of the License, or (at your option) any later version.
sl@0
     8
 *
sl@0
     9
 * This library is distributed in the hope that it will be useful,
sl@0
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
sl@0
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
sl@0
    12
 * Lesser General Public License for more details.
sl@0
    13
 *
sl@0
    14
 * You should have received a copy of the GNU Lesser General Public
sl@0
    15
 * License along with this library; if not, write to the
sl@0
    16
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
sl@0
    17
 * Boston, MA 02111-1307, USA.
sl@0
    18
 */
sl@0
    19
sl@0
    20
/*
sl@0
    21
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
sl@0
    22
 * file for a list of people on the GLib Team.  See the ChangeLog
sl@0
    23
 * files for a list of changes.  These files are distributed with
sl@0
    24
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
sl@0
    25
 */
sl@0
    26
sl@0
    27
#undef G_DISABLE_ASSERT
sl@0
    28
#undef G_LOG_DOMAIN
sl@0
    29
sl@0
    30
#include <stdio.h>
sl@0
    31
#include <string.h>
sl@0
    32
#include "glib.h"
sl@0
    33
#include "glib/gprintf.h"
sl@0
    34
sl@0
    35
#ifdef SYMBIAN
sl@0
    36
#include "mrt2_glib2_test.h"
sl@0
    37
#endif /*SYMBIAN*/
sl@0
    38
sl@0
    39
sl@0
    40
int array[10000];
sl@0
    41
gboolean failed = FALSE;
sl@0
    42
sl@0
    43
#define	TEST(m,cond)	G_STMT_START { failed = !(cond); \
sl@0
    44
if (failed) \
sl@0
    45
  { assert_failed = TRUE; \
sl@0
    46
  if (!m) \
sl@0
    47
      g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
sl@0
    48
    else \
sl@0
    49
      g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
sl@0
    50
  } \
sl@0
    51
else \
sl@0
    52
  g_print ("."); fflush (stdout); \
sl@0
    53
} G_STMT_END
sl@0
    54
sl@0
    55
#define	C2P(c)		((gpointer) ((long) (c)))
sl@0
    56
#define	P2C(p)		((gchar) ((long) (p)))
sl@0
    57
sl@0
    58
#define GLIB_TEST_STRING "el dorado "
sl@0
    59
#define GLIB_TEST_STRING_5 "el do"
sl@0
    60
sl@0
    61
typedef struct {
sl@0
    62
	guint age;
sl@0
    63
	gchar name[40];
sl@0
    64
} GlibTestInfo;
sl@0
    65
sl@0
    66
int
sl@0
    67
main (int   argc,
sl@0
    68
      char *argv[])
sl@0
    69
{
sl@0
    70
  GStringChunk *string_chunk;
sl@0
    71
sl@0
    72
  gchar *tmp_string = NULL, *tmp_string_2;
sl@0
    73
  gint i;
sl@0
    74
  GString *string1, *string2;
sl@0
    75
sl@0
    76
  #ifdef SYMBIAN
sl@0
    77
  g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
sl@0
    78
  g_set_print_handler(mrtPrintHandler);
sl@0
    79
  #endif /*SYMBIAN*/
sl@0
    80
	  
sl@0
    81
sl@0
    82
  string_chunk = g_string_chunk_new (1024);
sl@0
    83
sl@0
    84
  for (i = 0; i < 1000; i ++)
sl@0
    85
    {
sl@0
    86
      tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
sl@0
    87
sl@0
    88
      if (strcmp ("hi pete", tmp_string) != 0)
sl@0
    89
	g_error ("string chunks are broken.\n");
sl@0
    90
    }
sl@0
    91
sl@0
    92
  tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
sl@0
    93
sl@0
    94
  g_assert (tmp_string_2 != tmp_string &&
sl@0
    95
	    strcmp(tmp_string_2, tmp_string) == 0);
sl@0
    96
sl@0
    97
  tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
sl@0
    98
sl@0
    99
  g_assert (tmp_string_2 == tmp_string);
sl@0
   100
sl@0
   101
  g_string_chunk_free (string_chunk);
sl@0
   102
sl@0
   103
  string1 = g_string_new ("hi pete!");
sl@0
   104
  string2 = g_string_new (NULL);
sl@0
   105
sl@0
   106
  g_assert (string1 != NULL);
sl@0
   107
  g_assert (string2 != NULL);
sl@0
   108
  g_assert (strlen (string1->str) == string1->len);
sl@0
   109
  g_assert (strlen (string2->str) == string2->len);
sl@0
   110
  g_assert (string2->len == 0);
sl@0
   111
  g_assert (strcmp ("hi pete!", string1->str) == 0);
sl@0
   112
  g_assert (strcmp ("", string2->str) == 0);
sl@0
   113
sl@0
   114
  for (i = 0; i < 10000; i++)
sl@0
   115
    g_string_append_c (string1, 'a'+(i%26));
sl@0
   116
sl@0
   117
  g_assert((strlen("hi pete!") + 10000) == string1->len);
sl@0
   118
  g_assert((strlen("hi pete!") + 10000) == strlen(string1->str));
sl@0
   119
sl@0
   120
#ifndef G_OS_WIN32
sl@0
   121
  /* MSVC and mingw32 use the same run-time C library, which doesn't like
sl@0
   122
     the %10000.10000f format... */
sl@0
   123
  g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
sl@0
   124
		   "this pete guy sure is a wuss, like he's the number ",
sl@0
   125
		   1,
sl@0
   126
		   " wuss.  everyone agrees.\n",
sl@0
   127
		   string1->str,
sl@0
   128
		   10, 666, 15, 15, 666.666666666, 666.666666666);
sl@0
   129
#else
sl@0
   130
  g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
sl@0
   131
		   "this pete guy sure is a wuss, like he's the number ",
sl@0
   132
		   1,
sl@0
   133
		   " wuss.  everyone agrees.\n",
sl@0
   134
		   string1->str,
sl@0
   135
		   10, 666, 15, 15, 666.666666666, 666.666666666);
sl@0
   136
#endif
sl@0
   137
  
sl@0
   138
  g_string_free (string1, TRUE);
sl@0
   139
  g_string_free (string2, TRUE);
sl@0
   140
sl@0
   141
  /* append */
sl@0
   142
  string1 = g_string_new ("firsthalf");
sl@0
   143
  g_string_append (string1, "lasthalf");
sl@0
   144
  g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
sl@0
   145
  g_string_free (string1, TRUE);
sl@0
   146
sl@0
   147
  /* append_len */
sl@0
   148
  string1 = g_string_new ("firsthalf");
sl@0
   149
  g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
sl@0
   150
  g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
sl@0
   151
  g_string_free (string1, TRUE);  
sl@0
   152
  
sl@0
   153
  /* prepend */
sl@0
   154
  string1 = g_string_new ("lasthalf");
sl@0
   155
  g_string_prepend (string1, "firsthalf");
sl@0
   156
  g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
sl@0
   157
  g_string_free (string1, TRUE);
sl@0
   158
sl@0
   159
  /* prepend_len */
sl@0
   160
  string1 = g_string_new ("lasthalf");
sl@0
   161
  g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
sl@0
   162
  g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
sl@0
   163
  g_string_free (string1, TRUE);
sl@0
   164
  
sl@0
   165
  /* insert */
sl@0
   166
  string1 = g_string_new ("firstlast");
sl@0
   167
  g_string_insert (string1, 5, "middle");
sl@0
   168
  g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
sl@0
   169
  g_string_free (string1, TRUE);
sl@0
   170
sl@0
   171
  /* insert with pos == end of the string */
sl@0
   172
  string1 = g_string_new ("firstmiddle");
sl@0
   173
  g_string_insert (string1, strlen ("firstmiddle"), "last");
sl@0
   174
  g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
sl@0
   175
  g_string_free (string1, TRUE);
sl@0
   176
  
sl@0
   177
  /* insert_len */
sl@0
   178
  string1 = g_string_new ("firstlast");
sl@0
   179
  g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
sl@0
   180
  g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
sl@0
   181
  g_string_free (string1, TRUE);
sl@0
   182
sl@0
   183
  /* insert_len with magic -1 pos for append */
sl@0
   184
  string1 = g_string_new ("first");
sl@0
   185
  g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
sl@0
   186
  g_assert (strcmp (string1->str, "firstlast") == 0);
sl@0
   187
  g_string_free (string1, TRUE);
sl@0
   188
  
sl@0
   189
  /* insert_len with magic -1 len for strlen-the-string */
sl@0
   190
  string1 = g_string_new ("first");
sl@0
   191
  g_string_insert_len (string1, 5, "last", -1);
sl@0
   192
  g_assert (strcmp (string1->str, "firstlast") == 0);
sl@0
   193
  g_string_free (string1, TRUE);
sl@0
   194
sl@0
   195
  /* insert_len with string overlap */
sl@0
   196
  string1 = g_string_new ("textbeforetextafter");
sl@0
   197
  g_string_insert_len (string1, 10, string1->str + 8, 5);
sl@0
   198
  g_assert (strcmp (string1->str, "textbeforeretextextafter") == 0);
sl@0
   199
  g_string_free (string1, TRUE);
sl@0
   200
sl@0
   201
  string1 = g_string_new ("boring text");
sl@0
   202
  g_string_insert_len (string1, 7, string1->str + 2, 4);
sl@0
   203
  g_assert (strcmp (string1->str, "boring ringtext") == 0);
sl@0
   204
  g_string_free (string1, TRUE);
sl@0
   205
sl@0
   206
  string1 = g_string_new ("boring text");
sl@0
   207
  g_string_insert_len (string1, 6, string1->str + 7, 4);
sl@0
   208
  g_assert (strcmp (string1->str, "boringtext text") == 0);
sl@0
   209
  g_string_free (string1, TRUE);
sl@0
   210
sl@0
   211
  /* assign_len with string overlap */
sl@0
   212
  string1 = g_string_new ("textbeforetextafter");
sl@0
   213
  g_string_assign (string1, string1->str + 10);
sl@0
   214
  g_assert (strcmp (string1->str, "textafter") == 0);
sl@0
   215
  g_string_free (string1, TRUE);
sl@0
   216
sl@0
   217
  string1 = g_string_new ("boring text");
sl@0
   218
  g_string_assign (string1, string1->str);
sl@0
   219
  g_assert (strcmp (string1->str, "boring text") == 0);
sl@0
   220
  g_string_free (string1, TRUE);
sl@0
   221
sl@0
   222
  /* insert_unichar with insertion in middle */
sl@0
   223
  string1 = g_string_new ("firsthalf");
sl@0
   224
  g_string_insert_unichar (string1, 5, 0x0041);
sl@0
   225
  g_assert (strcmp (string1->str, "first\x41half") == 0);
sl@0
   226
  g_string_free (string1, TRUE);
sl@0
   227
sl@0
   228
  string1 = g_string_new ("firsthalf");
sl@0
   229
  g_string_insert_unichar (string1, 5, 0x0298);
sl@0
   230
  g_assert (strcmp (string1->str, "first\xCA\x98half") == 0);
sl@0
   231
  g_string_free (string1, TRUE);
sl@0
   232
sl@0
   233
  string1 = g_string_new ("firsthalf");
sl@0
   234
  g_string_insert_unichar (string1, 5, 0xFFFD);
sl@0
   235
  g_assert (strcmp (string1->str, "first\xEF\xBF\xBDhalf") == 0);
sl@0
   236
  g_string_free (string1, TRUE);
sl@0
   237
sl@0
   238
  string1 = g_string_new ("firsthalf");
sl@0
   239
  g_string_insert_unichar (string1, 5, 0x1D100);
sl@0
   240
  g_assert (strcmp (string1->str, "first\xF0\x9D\x84\x80half") == 0);
sl@0
   241
  g_string_free (string1, TRUE);
sl@0
   242
sl@0
   243
  /* insert_unichar with insertion at end */
sl@0
   244
  string1 = g_string_new ("start");
sl@0
   245
  g_string_insert_unichar (string1, -1, 0x0041);
sl@0
   246
  g_assert (strcmp (string1->str, "start\x41") == 0);
sl@0
   247
  g_string_free (string1, TRUE);
sl@0
   248
sl@0
   249
  string1 = g_string_new ("start");
sl@0
   250
  g_string_insert_unichar (string1, -1, 0x0298);
sl@0
   251
  g_assert (strcmp (string1->str, "start\xCA\x98") == 0);
sl@0
   252
  g_string_free (string1, TRUE);
sl@0
   253
sl@0
   254
  string1 = g_string_new ("start");
sl@0
   255
  g_string_insert_unichar (string1, -1, 0xFFFD);
sl@0
   256
  g_assert (strcmp (string1->str, "start\xEF\xBF\xBD") == 0);
sl@0
   257
  g_string_free (string1, TRUE);
sl@0
   258
sl@0
   259
  string1 = g_string_new ("start");
sl@0
   260
  g_string_insert_unichar (string1, -1, 0x1D100);
sl@0
   261
  g_assert (strcmp (string1->str, "start\xF0\x9D\x84\x80") == 0);
sl@0
   262
  g_string_free (string1, TRUE);
sl@0
   263
sl@0
   264
  /* g_string_equal */
sl@0
   265
  string1 = g_string_new ("test");
sl@0
   266
  string2 = g_string_new ("te");
sl@0
   267
  g_assert (! g_string_equal(string1, string2));
sl@0
   268
  g_string_append (string2, "st");
sl@0
   269
  g_assert (g_string_equal(string1, string2));
sl@0
   270
  g_string_free (string1, TRUE);
sl@0
   271
  g_string_free (string2, TRUE);
sl@0
   272
  
sl@0
   273
  /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
sl@0
   274
  string1 = g_string_new ("fiddle");
sl@0
   275
  string2 = g_string_new ("fiddle");
sl@0
   276
  g_assert (g_string_equal(string1, string2));
sl@0
   277
  g_string_append_c(string1, '\0');
sl@0
   278
  g_assert (! g_string_equal(string1, string2));
sl@0
   279
  g_string_append_c(string2, '\0');
sl@0
   280
  g_assert (g_string_equal(string1, string2));
sl@0
   281
  g_string_append_c(string1, 'x');
sl@0
   282
  g_string_append_c(string2, 'y');
sl@0
   283
  g_assert (! g_string_equal(string1, string2));
sl@0
   284
  g_assert (string1->len == 8);
sl@0
   285
  g_string_append(string1, "yzzy");
sl@0
   286
  g_assert (string1->len == 12);
sl@0
   287
  g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
sl@0
   288
  g_string_insert(string1, 1, "QED");
sl@0
   289
  g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
sl@0
   290
  g_string_printf (string1, "fiddle%cxyzzy", '\0');
sl@0
   291
  g_assert (string1->len == 12);
sl@0
   292
  g_assert (memcmp (string1->str, "fiddle\0xyzzy", 13) == 0);
sl@0
   293
sl@0
   294
  g_string_free (string1, TRUE);
sl@0
   295
  g_string_free (string2, TRUE);
sl@0
   296
  
sl@0
   297
  g_assert (g_str_has_prefix("foobar", "gazonk") == FALSE);
sl@0
   298
  g_assert (g_str_has_prefix("xyzzy", "xyzzy") == TRUE);
sl@0
   299
  g_assert (g_str_has_prefix("xyzzy", "xy") == TRUE);
sl@0
   300
  g_assert (g_str_has_prefix("xyzzy", "") == TRUE);
sl@0
   301
  g_assert (g_str_has_prefix("xyz", "xyzzy") == FALSE);
sl@0
   302
  g_assert (g_str_has_prefix("", "xyzzy") == FALSE);
sl@0
   303
  g_assert (g_str_has_prefix("", "") == TRUE);
sl@0
   304
sl@0
   305
  g_assert (g_str_has_suffix("foobar", "gazonk") == FALSE);
sl@0
   306
  g_assert (g_str_has_suffix("xyzzy", "xyzzy") == TRUE);
sl@0
   307
  g_assert (g_str_has_suffix("xyzzy", "zy") == TRUE);
sl@0
   308
  g_assert (g_str_has_suffix("xyzzy", "") == TRUE);
sl@0
   309
  g_assert (g_str_has_suffix("zzy", "xyzzy") == FALSE);
sl@0
   310
  g_assert (g_str_has_suffix("", "xyzzy") == FALSE);
sl@0
   311
  g_assert (g_str_has_suffix("", "") == TRUE);
sl@0
   312
sl@0
   313
  tmp_string = (gchar *) g_malloc (10);
sl@0
   314
  g_snprintf (tmp_string, 10, "%2$s %1$s", "a", "b");
sl@0
   315
  g_assert (strcmp (tmp_string, "b a") == 0);
sl@0
   316
  g_free (tmp_string);
sl@0
   317
#ifdef SYMBIAN
sl@0
   318
  testResultXml("string-test");
sl@0
   319
#endif /* EMULATOR */
sl@0
   320
  return 0;
sl@0
   321
}
sl@0
   322
sl@0
   323