os/ossrv/glib/tests/utf8-pointer.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200 (2014-06-10)
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
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
#include <string.h>
sl@0
    28
#include <glib.h>
sl@0
    29
sl@0
    30
#ifdef __SYMBIAN32__
sl@0
    31
#include <glib_global.h>
sl@0
    32
#include "mrt2_glib2_test.h"
sl@0
    33
#endif //__SYMBIAN32__
sl@0
    34
sl@0
    35
/* Test conversions between offsets and pointers */
sl@0
    36
sl@0
    37
static void test_utf8 (gchar *string)
sl@0
    38
{
sl@0
    39
  gint num_chars;
sl@0
    40
  gchar **p;
sl@0
    41
  gint i, j;
sl@0
    42
  
sl@0
    43
  g_assert (g_utf8_validate (string, -1, NULL));
sl@0
    44
  
sl@0
    45
  num_chars = g_utf8_strlen (string, -1);
sl@0
    46
  
sl@0
    47
  p = (gchar **) g_malloc (num_chars * sizeof (gchar *));
sl@0
    48
  
sl@0
    49
  p[0] = string;
sl@0
    50
  for (i = 1; i < num_chars; i++)
sl@0
    51
    p[i] = g_utf8_next_char (p[i-1]);
sl@0
    52
  
sl@0
    53
  for (i = 0; i < num_chars; i++)
sl@0
    54
    for (j = 0; j < num_chars; j++) 
sl@0
    55
      {
sl@0
    56
	g_assert (g_utf8_offset_to_pointer (p[i], j - i) == p[j]);	
sl@0
    57
	g_assert (g_utf8_pointer_to_offset (p[i], p[j]) == j - i);	
sl@0
    58
      }
sl@0
    59
  
sl@0
    60
  g_free (p);
sl@0
    61
}
sl@0
    62
sl@0
    63
gchar *longline = "asdasdas dsaf asfd as fdasdf asfd asdf as dfas dfasdf a"
sl@0
    64
"asd fasdf asdf asdf asd fasfd as fdasfd asdf as fdççççççççças ffsd asfd as fdASASASAs As"
sl@0
    65
"Asfdsf sdfg sdfg dsfg dfg sdfgsdfgsdfgsdfg sdfgsdfg sdfg sdfg sdf gsdfg sdfg sd"
sl@0
    66
"asd fasdf asdf asdf asd fasfd as fdaèèèèèèè òòòòòòòòòòòòsfd asdf as fdas ffsd asfd as fdASASASAs D"
sl@0
    67
"Asfdsf sdfg sdfg dsfg dfg sdfgsdfgsdfgsdfg sdfgsdfg sdfgùùùùùùùùùùùùùù sdfg sdf gsdfg sdfg sd"
sl@0
    68
"asd fasdf asdf asdf asd fasfd as fdasfd asd@@@@@@@f as fdas ffsd asfd as fdASASASAs D "
sl@0
    69
"Asfdsf sdfg sdfg dsfg dfg sdfgsdfgsdfgsdfg sdfgsdf€€€€€€€€€€€€€€€€€€g sdfg sdfg sdf gsdfg sdfg sd"
sl@0
    70
"asd fasdf asdf asdf asd fasfd as fdasfd asdf as fdas ffsd asfd as fdASASASAs D"
sl@0
    71
"Asfdsf sdfg sdfg dsfg dfg sdfgsdfgsdfgsdfg sdfgsdfg sdfg sdfg sdf gsdfg sdfg sd\n\nlalala\n";
sl@0
    72
sl@0
    73
static void
sl@0
    74
test_length (void)
sl@0
    75
{
sl@0
    76
  g_assert (g_utf8_strlen ("1234", -1) == 4);
sl@0
    77
  g_assert (g_utf8_strlen ("1234", 0) == 0);
sl@0
    78
  g_assert (g_utf8_strlen ("1234", 1) == 1);
sl@0
    79
  g_assert (g_utf8_strlen ("1234", 2) == 2);
sl@0
    80
  g_assert (g_utf8_strlen ("1234", 3) == 3);
sl@0
    81
  g_assert (g_utf8_strlen ("1234", 4) == 4);
sl@0
    82
  g_assert (g_utf8_strlen ("1234", 5) == 4);
sl@0
    83
sl@0
    84
  g_assert (g_utf8_strlen (longline, -1) == 762);
sl@0
    85
  g_assert (g_utf8_strlen (longline, strlen (longline)) == 762);
sl@0
    86
  g_assert (g_utf8_strlen (longline, 1024) == 762);
sl@0
    87
sl@0
    88
  g_assert (g_utf8_strlen (NULL, 0) == 0);
sl@0
    89
sl@0
    90
  g_assert (g_utf8_strlen ("a\340\250\201c", -1) == 3);
sl@0
    91
  g_assert (g_utf8_strlen ("a\340\250\201c", 1) == 1);
sl@0
    92
  g_assert (g_utf8_strlen ("a\340\250\201c", 2) == 1);
sl@0
    93
  g_assert (g_utf8_strlen ("a\340\250\201c", 3) == 1);
sl@0
    94
  g_assert (g_utf8_strlen ("a\340\250\201c", 4) == 2);
sl@0
    95
  g_assert (g_utf8_strlen ("a\340\250\201c", 5) == 3);
sl@0
    96
}
sl@0
    97
sl@0
    98
static void
sl@0
    99
test_misc (void)
sl@0
   100
{
sl@0
   101
  char *s;
sl@0
   102
  s = g_utf8_strreverse ("1234", -1);
sl@0
   103
  g_assert (strcmp (s, "4321") == 0);
sl@0
   104
  g_free (s);
sl@0
   105
  s = g_utf8_strreverse ("1234", 3);
sl@0
   106
  g_assert (strcmp (s, "321") == 0);
sl@0
   107
  g_free (s);
sl@0
   108
}
sl@0
   109
sl@0
   110
int main (int argc, char *argv[])
sl@0
   111
{
sl@0
   112
sl@0
   113
  #ifdef __SYMBIAN32__
sl@0
   114
  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
   115
  g_set_print_handler(mrtPrintHandler);
sl@0
   116
  #endif /*__SYMBIAN32__*/
sl@0
   117
  
sl@0
   118
  test_utf8 (longline);
sl@0
   119
  test_length ();
sl@0
   120
  test_misc ();
sl@0
   121
 #ifdef __SYMBIAN32__
sl@0
   122
  testResultXml("utf8-pointer");
sl@0
   123
  #endif //__SYMBIAN32__
sl@0
   124
  
sl@0
   125
  return 0;
sl@0
   126
}