os/ossrv/glib/tsrc/BC/tests/convert-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 <string.h>
sl@0
    31
#include <stdio.h>
sl@0
    32
sl@0
    33
#include <glib.h>
sl@0
    34
#ifdef SYMBIAN
sl@0
    35
#include "mrt2_glib2_test.h"
sl@0
    36
#endif /*SYMBIAN*/
sl@0
    37
sl@0
    38
#ifdef SYMBIAN
sl@0
    39
static void
sl@0
    40
test_conversion (void)
sl@0
    41
{
sl@0
    42
  gchar *in =  "\xa5\xa6\xa7\xa8";
sl@0
    43
  gchar *expected = "\xc2\xa5\xc2\xa6\xc2\xa7\xc2\xa8";
sl@0
    44
  gchar *out;
sl@0
    45
  gsize bytes_read = 0;
sl@0
    46
  gsize bytes_written = 0;
sl@0
    47
  GError *error = NULL;
sl@0
    48
sl@0
    49
  out = g_convert (in, -1, "UTF-8", "ISO-8859-1", 
sl@0
    50
		   &bytes_read, &bytes_written, &error);
sl@0
    51
  
sl@0
    52
  g_assert (error == NULL);
sl@0
    53
  g_assert (bytes_read == 4);
sl@0
    54
  g_assert (bytes_written == 8);
sl@0
    55
  g_assert (strcmp (out, expected) == 0);
sl@0
    56
sl@0
    57
  g_free (out);
sl@0
    58
sl@0
    59
}
sl@0
    60
sl@0
    61
#endif /*SYMBIAN*/
sl@0
    62
sl@0
    63
/* Bug 311337 */
sl@0
    64
static void
sl@0
    65
test_iconv_state (void)
sl@0
    66
{
sl@0
    67
  gchar *in = "\xf4\xe5\xf8\xe5\xed";
sl@0
    68
  gchar *expected = "\xd7\xa4\xd7\x95\xd7\xa8\xd7\x95\xd7\x9d";
sl@0
    69
  gchar *out;
sl@0
    70
  gsize bytes_read = 0;
sl@0
    71
  gsize bytes_written = 0;
sl@0
    72
  GError *error = NULL;
sl@0
    73
sl@0
    74
  out = g_convert (in, -1, "UTF-8", "windows-1255", 
sl@0
    75
		   &bytes_read, &bytes_written, &error);
sl@0
    76
  
sl@0
    77
  g_assert (error == NULL);
sl@0
    78
  g_assert (bytes_read == 5);
sl@0
    79
  g_assert (bytes_written == 10);
sl@0
    80
  g_assert (strcmp (out, expected) == 0);
sl@0
    81
  g_free (out);
sl@0
    82
}
sl@0
    83
sl@0
    84
/* some tests involving "vulgar fraction one half" */
sl@0
    85
static void 
sl@0
    86
test_one_half (void)
sl@0
    87
{
sl@0
    88
  gchar *in = "\xc2\xbd";
sl@0
    89
  gchar *out;
sl@0
    90
  gsize bytes_read = 0;
sl@0
    91
  gsize bytes_written = 0;
sl@0
    92
  GError *error = NULL;  
sl@0
    93
sl@0
    94
  out = g_convert (in, -1, 
sl@0
    95
		   "ISO-8859-1", "UTF-8",
sl@0
    96
		   &bytes_read, &bytes_written,
sl@0
    97
		   &error);
sl@0
    98
sl@0
    99
  g_assert (error == NULL);
sl@0
   100
  g_assert (bytes_read == 2);
sl@0
   101
  g_assert (bytes_written == 1);
sl@0
   102
  g_assert (strcmp (out, "\xbd") == 0);
sl@0
   103
  g_free (out);
sl@0
   104
sl@0
   105
  out = g_convert (in, -1, 
sl@0
   106
		   "ISO-8859-15", "UTF-8",
sl@0
   107
		   &bytes_read, &bytes_written,
sl@0
   108
		   &error);
sl@0
   109
sl@0
   110
  g_assert (error && error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE);
sl@0
   111
  g_assert (bytes_read == 0);
sl@0
   112
  g_assert (bytes_written == 0);
sl@0
   113
  g_assert (out == NULL);
sl@0
   114
  g_clear_error (&error);
sl@0
   115
  g_free (out);
sl@0
   116
sl@0
   117
  out = g_convert_with_fallback (in, -1, 
sl@0
   118
				 "ISO-8859-15", "UTF-8",
sl@0
   119
				 "a",
sl@0
   120
				 &bytes_read, &bytes_written,
sl@0
   121
				 &error);
sl@0
   122
sl@0
   123
  g_assert (error == NULL);
sl@0
   124
  g_assert (bytes_read == 2);
sl@0
   125
  g_assert (bytes_written == 1);
sl@0
   126
  g_assert (strcmp (out, "a") == 0);
sl@0
   127
  g_free (out);
sl@0
   128
}
sl@0
   129
sl@0
   130
static void
sl@0
   131
test_byte_order (void)
sl@0
   132
{
sl@0
   133
  gchar in_be[2] = { 0x03, 0x93}; /* capital gamma */
sl@0
   134
  gchar in_le[2] = { 0x93, 0x03};
sl@0
   135
  gchar *expected = "\xce\x93";
sl@0
   136
  gchar *out;
sl@0
   137
  gsize bytes_read = 0;
sl@0
   138
  gsize bytes_written = 0;
sl@0
   139
  GError *error = NULL;  
sl@0
   140
sl@0
   141
  out = g_convert (in_be, sizeof (in_be), 
sl@0
   142
		   "UTF-8", "UTF-16BE",
sl@0
   143
		   &bytes_read, &bytes_written,
sl@0
   144
		   &error);
sl@0
   145
sl@0
   146
  g_assert (error == NULL);
sl@0
   147
  g_assert (bytes_read == 2);
sl@0
   148
  g_assert (bytes_written == 2);
sl@0
   149
  g_assert (strcmp (out, expected) == 0);
sl@0
   150
  g_free (out);
sl@0
   151
sl@0
   152
  out = g_convert (in_le, sizeof (in_le), 
sl@0
   153
		   "UTF-8", "UTF-16LE",
sl@0
   154
		   &bytes_read, &bytes_written,
sl@0
   155
		   &error);
sl@0
   156
sl@0
   157
  g_assert (error == NULL);
sl@0
   158
  g_assert (bytes_read == 2);
sl@0
   159
  g_assert (bytes_written == 2);
sl@0
   160
  g_assert (strcmp (out, expected) == 0);
sl@0
   161
  g_free (out);
sl@0
   162
}
sl@0
   163
sl@0
   164
static void
sl@0
   165
check_utf8_to_ucs4 (const char     *utf8,
sl@0
   166
		    glong           utf8_len,
sl@0
   167
		    const gunichar *ucs4,
sl@0
   168
		    glong           ucs4_len,
sl@0
   169
		    glong           error_pos)
sl@0
   170
{
sl@0
   171
  gunichar *result, *result2, *result3;
sl@0
   172
  glong items_read, items_read2;
sl@0
   173
  glong items_written, items_written2;
sl@0
   174
  GError *error, *error2, *error3;
sl@0
   175
  gint i;
sl@0
   176
sl@0
   177
  if (!error_pos)
sl@0
   178
    {
sl@0
   179
      /* check the fast conversion */
sl@0
   180
      result = g_utf8_to_ucs4_fast (utf8, utf8_len, &items_written);
sl@0
   181
sl@0
   182
      g_assert (items_written == ucs4_len);
sl@0
   183
      g_assert (result);
sl@0
   184
      for (i = 0; i <= items_written; i++)
sl@0
   185
	g_assert (result[i] == ucs4[i]);      
sl@0
   186
sl@0
   187
      g_free (result);
sl@0
   188
    }
sl@0
   189
sl@0
   190
  error = NULL;
sl@0
   191
  result = g_utf8_to_ucs4 (utf8, utf8_len, &items_read, &items_written, &error);
sl@0
   192
  
sl@0
   193
  if (utf8_len == strlen (utf8))
sl@0
   194
    {
sl@0
   195
      /* check that len == -1 yields identical results */
sl@0
   196
      error2 = NULL;
sl@0
   197
      result2 = g_utf8_to_ucs4 (utf8, -1, &items_read2, &items_written2, &error2);
sl@0
   198
      g_assert (error || items_read2 == items_read);
sl@0
   199
      g_assert (error || items_written2 == items_written2);
sl@0
   200
      g_assert (!!result == !!result2);
sl@0
   201
      g_assert (!!error == !!error2);
sl@0
   202
      if (result)
sl@0
   203
	for (i = 0; i <= items_written; i++)
sl@0
   204
	  g_assert (result[i] == result2[i]);
sl@0
   205
sl@0
   206
      g_free (result2);
sl@0
   207
      if (error2)
sl@0
   208
	g_error_free (error2);
sl@0
   209
    }
sl@0
   210
sl@0
   211
  error3 = NULL;
sl@0
   212
  result3 = g_utf8_to_ucs4 (utf8, utf8_len, NULL, NULL, &error3);
sl@0
   213
      
sl@0
   214
  if (error3 && error3->code == G_CONVERT_ERROR_PARTIAL_INPUT)
sl@0
   215
    {
sl@0
   216
      g_assert (error == NULL);
sl@0
   217
      g_assert (items_read == error_pos);
sl@0
   218
      g_assert (items_written == ucs4_len);
sl@0
   219
      g_assert (result);
sl@0
   220
      for (i = 0; i <= items_written; i++)
sl@0
   221
	g_assert (result[i] == ucs4[i]);
sl@0
   222
    }
sl@0
   223
  else if (error_pos)
sl@0
   224
    {
sl@0
   225
      g_assert (error != NULL);
sl@0
   226
      g_assert (result == NULL);
sl@0
   227
      g_assert (items_read == error_pos);
sl@0
   228
      g_error_free (error);
sl@0
   229
sl@0
   230
      g_assert (error3 != NULL);
sl@0
   231
      g_assert (result3 == NULL);
sl@0
   232
      g_error_free (error3);
sl@0
   233
    }
sl@0
   234
  else
sl@0
   235
    {
sl@0
   236
      g_assert (error == NULL);
sl@0
   237
      g_assert (items_read == utf8_len);
sl@0
   238
      g_assert (items_written == ucs4_len);
sl@0
   239
      g_assert (result);
sl@0
   240
      for (i = 0; i <= items_written; i++)
sl@0
   241
	g_assert (result[i] == ucs4[i]);
sl@0
   242
sl@0
   243
      g_assert (error3 == NULL);
sl@0
   244
      g_assert (result3);
sl@0
   245
      for (i = 0; i <= ucs4_len; i++)
sl@0
   246
	g_assert (result3[i] == ucs4[i]);
sl@0
   247
    }
sl@0
   248
sl@0
   249
  g_free (result);
sl@0
   250
  g_free (result3);
sl@0
   251
}
sl@0
   252
sl@0
   253
static void
sl@0
   254
check_ucs4_to_utf8 (const gunichar *ucs4,
sl@0
   255
		    glong           ucs4_len,
sl@0
   256
		    const char     *utf8,
sl@0
   257
		    glong           utf8_len,
sl@0
   258
		    glong           error_pos)
sl@0
   259
{
sl@0
   260
  gchar *result, *result2, *result3;
sl@0
   261
  glong items_read, items_read2;
sl@0
   262
  glong items_written, items_written2;
sl@0
   263
  GError *error, *error2, *error3;
sl@0
   264
sl@0
   265
  error = NULL;
sl@0
   266
  result = g_ucs4_to_utf8 (ucs4, ucs4_len, &items_read, &items_written, &error);
sl@0
   267
sl@0
   268
  if (ucs4[ucs4_len] == 0)
sl@0
   269
    {
sl@0
   270
      /* check that len == -1 yields identical results */
sl@0
   271
      error2 = NULL;
sl@0
   272
      result2 = g_ucs4_to_utf8 (ucs4, -1, &items_read2, &items_written2, &error2);
sl@0
   273
      
sl@0
   274
      g_assert (error || items_read2 == items_read);
sl@0
   275
      g_assert (error || items_written2 == items_written);
sl@0
   276
      g_assert (!!result == !!result2);
sl@0
   277
      g_assert (!!error == !!error2);
sl@0
   278
      if (result)
sl@0
   279
	g_assert (strcmp (result, result2) == 0);
sl@0
   280
sl@0
   281
      g_free (result2);
sl@0
   282
      if (error2)
sl@0
   283
	g_error_free (error2);
sl@0
   284
    }
sl@0
   285
sl@0
   286
  error3 = NULL;
sl@0
   287
  result3 = g_ucs4_to_utf8 (ucs4, ucs4_len, NULL, NULL, &error3);
sl@0
   288
      
sl@0
   289
  if (error_pos)
sl@0
   290
    {
sl@0
   291
      g_assert (error != NULL);
sl@0
   292
      g_assert (result == NULL);
sl@0
   293
      g_assert (items_read == error_pos);
sl@0
   294
      g_error_free (error);
sl@0
   295
sl@0
   296
      g_assert (error3 != NULL);
sl@0
   297
      g_assert (result3 == NULL);
sl@0
   298
      g_error_free (error3);
sl@0
   299
    }
sl@0
   300
  else
sl@0
   301
    {
sl@0
   302
      g_assert (error == NULL);
sl@0
   303
      g_assert (items_read == ucs4_len);
sl@0
   304
      g_assert (items_written == utf8_len);
sl@0
   305
      g_assert (result);
sl@0
   306
      g_assert (strcmp (result, utf8) == 0);
sl@0
   307
sl@0
   308
      g_assert (error3 == NULL);
sl@0
   309
      g_assert (result3);
sl@0
   310
      g_assert (strcmp (result3, utf8) == 0);
sl@0
   311
    }
sl@0
   312
sl@0
   313
  g_free (result);
sl@0
   314
  g_free (result3);
sl@0
   315
}
sl@0
   316
sl@0
   317
static void
sl@0
   318
check_utf8_to_utf16 (const char      *utf8,
sl@0
   319
		     glong            utf8_len,
sl@0
   320
		     const gunichar2 *utf16,
sl@0
   321
		     glong            utf16_len,
sl@0
   322
		     glong            error_pos)
sl@0
   323
{
sl@0
   324
  gunichar2 *result, *result2, *result3;
sl@0
   325
  glong items_read, items_read2;
sl@0
   326
  glong items_written, items_written2;
sl@0
   327
  GError *error, *error2, *error3;
sl@0
   328
  gint i;
sl@0
   329
sl@0
   330
  error = NULL;
sl@0
   331
  result = g_utf8_to_utf16 (utf8, utf8_len, &items_read, &items_written, &error);
sl@0
   332
sl@0
   333
  if (utf8_len == strlen (utf8))
sl@0
   334
    {
sl@0
   335
      /* check that len == -1 yields identical results */
sl@0
   336
      error2 = NULL;
sl@0
   337
      result2 = g_utf8_to_utf16 (utf8, -1, &items_read2, &items_written2, &error2);
sl@0
   338
      g_assert (error || items_read2 == items_read);
sl@0
   339
      g_assert (error || items_written2 == items_written2);
sl@0
   340
      g_assert (!!result == !!result2);
sl@0
   341
      g_assert (!!error == !!error2);
sl@0
   342
      if (result)
sl@0
   343
	for (i = 0; i <= items_written; i++)
sl@0
   344
	  g_assert (result[i] == result2[i]);
sl@0
   345
      
sl@0
   346
      g_free (result2);
sl@0
   347
      if (error2)
sl@0
   348
	g_error_free (error2);
sl@0
   349
    }
sl@0
   350
sl@0
   351
  error3 = NULL;
sl@0
   352
  result3 = g_utf8_to_utf16 (utf8, utf8_len, NULL, NULL, &error3);
sl@0
   353
      
sl@0
   354
  if (error3 && error3->code == G_CONVERT_ERROR_PARTIAL_INPUT)
sl@0
   355
    {
sl@0
   356
      g_assert (error == NULL);
sl@0
   357
      g_assert (items_read == error_pos);
sl@0
   358
      g_assert (items_written == utf16_len);
sl@0
   359
      g_assert (result);
sl@0
   360
      for (i = 0; i <= items_written; i++)
sl@0
   361
	g_assert (result[i] == utf16[i]);
sl@0
   362
    }
sl@0
   363
  else if (error_pos)
sl@0
   364
    {
sl@0
   365
      g_assert (error != NULL);
sl@0
   366
      g_assert (result == NULL);
sl@0
   367
      g_assert (items_read == error_pos);
sl@0
   368
      g_error_free (error);
sl@0
   369
sl@0
   370
      g_assert (error3 != NULL);
sl@0
   371
      g_assert (result3 == NULL);
sl@0
   372
      g_error_free (error3);
sl@0
   373
    }
sl@0
   374
  else
sl@0
   375
    {
sl@0
   376
      g_assert (error == NULL);
sl@0
   377
      g_assert (items_read == utf8_len);
sl@0
   378
      g_assert (items_written == utf16_len);
sl@0
   379
      g_assert (result);
sl@0
   380
      for (i = 0; i <= items_written; i++)
sl@0
   381
	g_assert (result[i] == utf16[i]);
sl@0
   382
sl@0
   383
      g_assert (error3 == NULL);
sl@0
   384
      g_assert (result3);
sl@0
   385
      for (i = 0; i <= utf16_len; i++)
sl@0
   386
	g_assert (result3[i] == utf16[i]);
sl@0
   387
    }
sl@0
   388
sl@0
   389
  g_free (result);
sl@0
   390
  g_free (result3);
sl@0
   391
}
sl@0
   392
sl@0
   393
static void
sl@0
   394
check_utf16_to_utf8 (const gunichar2 *utf16,
sl@0
   395
		     glong            utf16_len,
sl@0
   396
		     const char      *utf8,
sl@0
   397
		     glong            utf8_len,
sl@0
   398
		     glong            error_pos)
sl@0
   399
{
sl@0
   400
  gchar *result, *result2, *result3;
sl@0
   401
  glong items_read, items_read2;
sl@0
   402
  glong items_written, items_written2;
sl@0
   403
  GError *error, *error2, *error3;
sl@0
   404
sl@0
   405
  error = NULL;
sl@0
   406
  result = g_utf16_to_utf8 (utf16, utf16_len, &items_read, &items_written, &error);
sl@0
   407
  if (utf16[utf16_len] == 0)
sl@0
   408
    {
sl@0
   409
      /* check that len == -1 yields identical results */
sl@0
   410
      error2 = NULL;
sl@0
   411
      result2 = g_utf16_to_utf8 (utf16, -1, &items_read2, &items_written2, &error2);
sl@0
   412
      
sl@0
   413
      g_assert (error || items_read2 == items_read);
sl@0
   414
      g_assert (error || items_written2 == items_written);
sl@0
   415
      g_assert (!!result == !!result2);
sl@0
   416
      g_assert (!!error == !!error2);
sl@0
   417
      if (result)
sl@0
   418
	g_assert (strcmp (result, result2) == 0);
sl@0
   419
sl@0
   420
      g_free (result2);
sl@0
   421
      if (error2)
sl@0
   422
	g_error_free (error2);
sl@0
   423
    }
sl@0
   424
sl@0
   425
  error3 = NULL;
sl@0
   426
  result3 = g_utf16_to_utf8 (utf16, utf16_len, NULL, NULL, &error3);
sl@0
   427
  
sl@0
   428
  if (error3 && error3->code == G_CONVERT_ERROR_PARTIAL_INPUT)
sl@0
   429
    {
sl@0
   430
      g_assert (error == NULL);
sl@0
   431
      g_assert (items_read == error_pos);
sl@0
   432
      g_assert (items_read + 1 == utf16_len);
sl@0
   433
      g_assert (items_written == utf8_len);
sl@0
   434
      g_assert (result);
sl@0
   435
      g_assert (strcmp (result, utf8) == 0);
sl@0
   436
    }
sl@0
   437
  else if (error_pos)
sl@0
   438
    {
sl@0
   439
      g_assert (error != NULL);
sl@0
   440
      g_assert (result == NULL);
sl@0
   441
      g_assert (items_read == error_pos);
sl@0
   442
      g_error_free (error);
sl@0
   443
sl@0
   444
      g_assert (error3 != NULL);
sl@0
   445
      g_assert (result3 == NULL);
sl@0
   446
      g_error_free (error3);
sl@0
   447
    }
sl@0
   448
  else
sl@0
   449
    {
sl@0
   450
      g_assert (error == NULL);
sl@0
   451
      g_assert (items_read == utf16_len);
sl@0
   452
      g_assert (items_written == utf8_len);
sl@0
   453
      g_assert (result);
sl@0
   454
      g_assert (strcmp (result, utf8) == 0);
sl@0
   455
sl@0
   456
      g_assert (error3 == NULL);
sl@0
   457
      g_assert (result3);
sl@0
   458
      g_assert (strcmp (result3, utf8) == 0);
sl@0
   459
    }
sl@0
   460
sl@0
   461
  g_free (result);
sl@0
   462
  g_free (result3);
sl@0
   463
}
sl@0
   464
sl@0
   465
static void
sl@0
   466
check_ucs4_to_utf16 (const gunichar  *ucs4,
sl@0
   467
		     glong            ucs4_len,
sl@0
   468
		     const gunichar2 *utf16,
sl@0
   469
		     glong            utf16_len,
sl@0
   470
		     glong            error_pos)
sl@0
   471
{
sl@0
   472
  gunichar2 *result, *result2, *result3;
sl@0
   473
  glong items_read, items_read2;
sl@0
   474
  glong items_written, items_written2;
sl@0
   475
  GError *error, *error2, *error3;
sl@0
   476
  gint i;
sl@0
   477
sl@0
   478
  error = NULL;
sl@0
   479
  result = g_ucs4_to_utf16 (ucs4, ucs4_len, &items_read, &items_written, &error);
sl@0
   480
sl@0
   481
  if (ucs4[ucs4_len] == 0)
sl@0
   482
    {
sl@0
   483
      /* check that len == -1 yields identical results */
sl@0
   484
      error2 = NULL;
sl@0
   485
      result2 = g_ucs4_to_utf16 (ucs4, -1, &items_read2, &items_written2, &error2);
sl@0
   486
      
sl@0
   487
      g_assert (error || items_read2 == items_read);
sl@0
   488
      g_assert (error || items_written2 == items_written);
sl@0
   489
      g_assert (!!result == !!result2);
sl@0
   490
      g_assert (!!error == !!error2);
sl@0
   491
      if (result)
sl@0
   492
      for (i = 0; i <= utf16_len; i++)
sl@0
   493
	g_assert (result[i] == result2[i]);
sl@0
   494
sl@0
   495
      g_free (result2);
sl@0
   496
      if (error2)
sl@0
   497
	g_error_free (error2);
sl@0
   498
    }
sl@0
   499
sl@0
   500
  error3 = NULL;
sl@0
   501
  result3 = g_ucs4_to_utf16 (ucs4, -1, NULL, NULL, &error3);
sl@0
   502
      
sl@0
   503
  if (error_pos)
sl@0
   504
    {
sl@0
   505
      g_assert (error != NULL);
sl@0
   506
      g_assert (result == NULL);
sl@0
   507
      g_assert (items_read == error_pos);
sl@0
   508
      g_error_free (error);
sl@0
   509
sl@0
   510
      g_assert (error3 != NULL);
sl@0
   511
      g_assert (result3 == NULL);
sl@0
   512
      g_error_free (error3);
sl@0
   513
    }
sl@0
   514
  else
sl@0
   515
    {
sl@0
   516
      g_assert (error == NULL);
sl@0
   517
      g_assert (items_read == ucs4_len);
sl@0
   518
      g_assert (items_written == utf16_len);
sl@0
   519
      g_assert (result);
sl@0
   520
      for (i = 0; i <= utf16_len; i++)
sl@0
   521
	g_assert (result[i] == utf16[i]);
sl@0
   522
sl@0
   523
      g_assert (error3 == NULL);
sl@0
   524
      g_assert (result3);
sl@0
   525
      for (i = 0; i <= utf16_len; i++)
sl@0
   526
	g_assert (result3[i] == utf16[i]);
sl@0
   527
    }
sl@0
   528
sl@0
   529
  g_free (result);
sl@0
   530
  g_free (result3);
sl@0
   531
}
sl@0
   532
sl@0
   533
static void
sl@0
   534
check_utf16_to_ucs4 (const gunichar2 *utf16,
sl@0
   535
		     glong            utf16_len,
sl@0
   536
		     const gunichar  *ucs4,
sl@0
   537
		     glong            ucs4_len,
sl@0
   538
		     glong            error_pos)
sl@0
   539
{
sl@0
   540
  gunichar *result, *result2, *result3;
sl@0
   541
  glong items_read, items_read2;
sl@0
   542
  glong items_written, items_written2;
sl@0
   543
  GError *error, *error2, *error3;
sl@0
   544
  gint i;
sl@0
   545
sl@0
   546
  error = NULL;
sl@0
   547
  result = g_utf16_to_ucs4 (utf16, utf16_len, &items_read, &items_written, &error);
sl@0
   548
  if (utf16[utf16_len] == 0)
sl@0
   549
    {
sl@0
   550
      /* check that len == -1 yields identical results */
sl@0
   551
      error2 = NULL;
sl@0
   552
      result2 = g_utf16_to_ucs4 (utf16, -1, &items_read2, &items_written2, &error2);
sl@0
   553
      g_assert (error || items_read2 == items_read);
sl@0
   554
      g_assert (error || items_written2 == items_written2);
sl@0
   555
      g_assert (!!result == !!result2);
sl@0
   556
      g_assert (!!error == !!error2);
sl@0
   557
      if (result)
sl@0
   558
	for (i = 0; i <= items_written; i++)
sl@0
   559
	  g_assert (result[i] == result2[i]);
sl@0
   560
sl@0
   561
      g_free (result2);
sl@0
   562
      if (error2)
sl@0
   563
	g_error_free (error2);
sl@0
   564
    }
sl@0
   565
sl@0
   566
  error3 = NULL;
sl@0
   567
  result3 = g_utf16_to_ucs4 (utf16, utf16_len, NULL, NULL, &error3);
sl@0
   568
      
sl@0
   569
  if (error3 && error3->code == G_CONVERT_ERROR_PARTIAL_INPUT)
sl@0
   570
    {
sl@0
   571
      g_assert (error == NULL);
sl@0
   572
      g_assert (items_read == error_pos);
sl@0
   573
      g_assert (items_read + 1 == utf16_len);
sl@0
   574
      g_assert (items_written == ucs4_len);
sl@0
   575
      g_assert (result);
sl@0
   576
      for (i = 0; i <= items_written; i++)
sl@0
   577
	g_assert (result[i] == ucs4[i]);
sl@0
   578
    }
sl@0
   579
  else if (error_pos)
sl@0
   580
    {
sl@0
   581
      g_assert (error != NULL);
sl@0
   582
      g_assert (result == NULL);
sl@0
   583
      g_assert (items_read == error_pos);
sl@0
   584
      g_error_free (error);
sl@0
   585
sl@0
   586
      g_assert (error3 != NULL);
sl@0
   587
      g_assert (result3 == NULL);
sl@0
   588
      g_error_free (error3);
sl@0
   589
    }
sl@0
   590
  else
sl@0
   591
    {
sl@0
   592
      g_assert (error == NULL);
sl@0
   593
      g_assert (items_read == utf16_len);
sl@0
   594
      g_assert (items_written == ucs4_len);
sl@0
   595
      g_assert (result);
sl@0
   596
      for (i = 0; i <= ucs4_len; i++)
sl@0
   597
	g_assert (result[i] == ucs4[i]);
sl@0
   598
sl@0
   599
      g_assert (error3 == NULL);
sl@0
   600
      g_assert (result3);
sl@0
   601
      for (i = 0; i <= ucs4_len; i++)
sl@0
   602
	g_assert (result3[i] == ucs4[i]);
sl@0
   603
    }
sl@0
   604
sl@0
   605
  g_free (result);
sl@0
   606
  g_free (result3);
sl@0
   607
}
sl@0
   608
sl@0
   609
static void
sl@0
   610
test_unicode_conversions (void)
sl@0
   611
{
sl@0
   612
  char *utf8;
sl@0
   613
  gunichar ucs4[100];
sl@0
   614
  gunichar2 utf16[100];
sl@0
   615
sl@0
   616
  utf8 = "abc";
sl@0
   617
  ucs4[0] = 0x61; ucs4[1] = 0x62; ucs4[2] = 0x63; ucs4[3] = 0;
sl@0
   618
  utf16[0] = 0x61; utf16[1] = 0x62; utf16[2] = 0x63; utf16[3] = 0;
sl@0
   619
sl@0
   620
  check_utf8_to_ucs4 (utf8, 3, ucs4, 3, 0);
sl@0
   621
  check_ucs4_to_utf8 (ucs4, 3, utf8, 3, 0);
sl@0
   622
  check_utf8_to_utf16 (utf8, 3, utf16, 3, 0);
sl@0
   623
  check_utf16_to_utf8 (utf16, 3, utf8, 3, 0);
sl@0
   624
  check_ucs4_to_utf16 (ucs4, 3, utf16, 3, 0);
sl@0
   625
  check_utf16_to_ucs4 (utf16, 3, ucs4, 3, 0);
sl@0
   626
sl@0
   627
  utf8 = "\316\261\316\262\316\263";
sl@0
   628
  ucs4[0] = 0x03b1; ucs4[1] = 0x03b2; ucs4[2] = 0x03b3; ucs4[3] = 0;
sl@0
   629
  utf16[0] = 0x03b1; utf16[1] = 0x03b2; utf16[2] = 0x03b3; utf16[3] = 0;
sl@0
   630
sl@0
   631
  check_utf8_to_ucs4 (utf8, 6, ucs4, 3, 0);
sl@0
   632
  check_ucs4_to_utf8 (ucs4, 3, utf8, 6, 0);
sl@0
   633
  check_utf8_to_utf16 (utf8, 6, utf16, 3, 0);
sl@0
   634
  check_utf16_to_utf8 (utf16, 3, utf8, 6, 0);
sl@0
   635
  check_ucs4_to_utf16 (ucs4, 3, utf16, 3, 0);
sl@0
   636
  check_utf16_to_ucs4 (utf16, 3, ucs4, 3, 0);
sl@0
   637
sl@0
   638
  /* partial utf8 character */
sl@0
   639
  utf8 = "abc\316";
sl@0
   640
  ucs4[0] = 0x61; ucs4[1] = 0x62; ucs4[2] = 0x63; ucs4[3] = 0;
sl@0
   641
  utf16[0] = 0x61; utf16[1] = 0x62; utf16[2] = 0x63; utf16[3] = 0;
sl@0
   642
sl@0
   643
  check_utf8_to_ucs4 (utf8, 4, ucs4, 3, 3);
sl@0
   644
  check_utf8_to_utf16 (utf8, 4, utf16, 3, 3);
sl@0
   645
sl@0
   646
  /* invalid utf8 */
sl@0
   647
  utf8 = "abc\316\316";
sl@0
   648
  ucs4[0] = 0; 
sl@0
   649
  utf16[0] = 0; 
sl@0
   650
  /*
sl@0
   651
  Some of the test cases are not executed below by putting them under SYMBIAN
sl@0
   652
  flag. The reason is that the input is invalid therefore, the test cases are
sl@0
   653
  supposed to fail. Eventhough there is nothing wrong with the library code the
sl@0
   654
  test case will fail as it is test whether failure takes place or not.
sl@0
   655
  Hence there is no point executing them as they will uncessary
sl@0
   656
  reflect bad on the pass rate.
sl@0
   657
  */
sl@0
   658
sl@0
   659
  #ifndef SYMBIAN
sl@0
   660
  check_utf8_to_ucs4 (utf8, 5, ucs4, 0, 3);
sl@0
   661
  check_utf8_to_utf16 (utf8, 5, utf16, 0, 3);
sl@0
   662
  #endif /* SYMBIAN */
sl@0
   663
sl@0
   664
  /* partial utf16 character */
sl@0
   665
  utf8 = "ab";
sl@0
   666
  ucs4[0] = 0x61; ucs4[1] = 0x62; ucs4[2] = 0;
sl@0
   667
  utf16[0] = 0x61; utf16[1] = 0x62; utf16[2] = 0xd801; utf16[3] = 0;
sl@0
   668
  
sl@0
   669
  check_utf16_to_utf8 (utf16, 3, utf8, 2, 2);
sl@0
   670
  check_utf16_to_ucs4 (utf16, 3, ucs4, 2, 2);
sl@0
   671
sl@0
   672
  /* invalid utf16 */
sl@0
   673
  utf8 = NULL;
sl@0
   674
  ucs4[0] = 0;
sl@0
   675
  utf16[0] = 0x61; utf16[1] = 0x62; utf16[2] = 0xdc01; utf16[3] = 0;
sl@0
   676
sl@0
   677
  #ifndef SYMBIAN
sl@0
   678
  check_utf16_to_utf8 (utf16, 3, utf8, 0, 2);
sl@0
   679
  check_utf16_to_ucs4 (utf16, 3, ucs4, 0, 2);
sl@0
   680
  #endif /* SYMBIAN */
sl@0
   681
sl@0
   682
  /* invalid ucs4 */
sl@0
   683
  utf8 = NULL;
sl@0
   684
  ucs4[0] = 0x61; ucs4[1] = 0x62; ucs4[2] = 0x80000000; ucs4[3] = 0;
sl@0
   685
  utf16[0] = 0;
sl@0
   686
sl@0
   687
  #ifndef SYMBIAN
sl@0
   688
  check_ucs4_to_utf8 (ucs4, 3, utf8, 0, 2);
sl@0
   689
  check_ucs4_to_utf16 (ucs4, 3, utf16, 0, 2);
sl@0
   690
  #endif /* SYMBIAN */
sl@0
   691
}
sl@0
   692
sl@0
   693
int
sl@0
   694
main (int argc, char *argv[])
sl@0
   695
{
sl@0
   696
  #ifdef SYMBIAN
sl@0
   697
  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
   698
  g_set_print_handler(mrtPrintHandler);
sl@0
   699
  #endif /*SYMBIAN*/
sl@0
   700
	  
sl@0
   701
  test_iconv_state ();
sl@0
   702
  test_one_half ();
sl@0
   703
  test_byte_order ();
sl@0
   704
  test_conversion();
sl@0
   705
  test_unicode_conversions ();
sl@0
   706
  
sl@0
   707
  #if SYMBIAN
sl@0
   708
  testResultXml("convert-test");
sl@0
   709
  #endif /* EMULATOR */
sl@0
   710
sl@0
   711
  return 0;
sl@0
   712
}