glib/tests/utf8-pointer.c
changeset 18 47c74d1534e1
equal deleted inserted replaced
0:e4d67989cc36 18:47c74d1534e1
       
     1 /* GLIB - Library of useful routines for C programming
       
     2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
       
     3  * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
       
     4  * This library is free software; you can redistribute it and/or
       
     5  * modify it under the terms of the GNU Lesser General Public
       
     6  * License as published by the Free Software Foundation; either
       
     7  * version 2 of the License, or (at your option) any later version.
       
     8  *
       
     9  * This library is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    12  * Lesser General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU Lesser General Public
       
    15  * License along with this library; if not, write to the
       
    16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    17  * Boston, MA 02111-1307, USA.
       
    18  */
       
    19 
       
    20 /*
       
    21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
       
    22  * file for a list of people on the GLib Team.  See the ChangeLog
       
    23  * files for a list of changes.  These files are distributed with
       
    24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
       
    25  */
       
    26 
       
    27 #include <string.h>
       
    28 #include <glib.h>
       
    29 
       
    30 #ifdef __SYMBIAN32__
       
    31 #include <glib_global.h>
       
    32 #include "mrt2_glib2_test.h"
       
    33 #endif //__SYMBIAN32__
       
    34 
       
    35 /* Test conversions between offsets and pointers */
       
    36 
       
    37 static void test_utf8 (gchar *string)
       
    38 {
       
    39   gint num_chars;
       
    40   gchar **p;
       
    41   gint i, j;
       
    42   
       
    43   g_assert (g_utf8_validate (string, -1, NULL));
       
    44   
       
    45   num_chars = g_utf8_strlen (string, -1);
       
    46   
       
    47   p = (gchar **) g_malloc (num_chars * sizeof (gchar *));
       
    48   
       
    49   p[0] = string;
       
    50   for (i = 1; i < num_chars; i++)
       
    51     p[i] = g_utf8_next_char (p[i-1]);
       
    52   
       
    53   for (i = 0; i < num_chars; i++)
       
    54     for (j = 0; j < num_chars; j++) 
       
    55       {
       
    56 	g_assert (g_utf8_offset_to_pointer (p[i], j - i) == p[j]);	
       
    57 	g_assert (g_utf8_pointer_to_offset (p[i], p[j]) == j - i);	
       
    58       }
       
    59   
       
    60   g_free (p);
       
    61 }
       
    62 
       
    63 gchar *longline = "asdasdas dsaf asfd as fdasdf asfd asdf as dfas dfasdf a"
       
    64 "asd fasdf asdf asdf asd fasfd as fdasfd asdf as fdççççççççças ffsd asfd as fdASASASAs As"
       
    65 "Asfdsf sdfg sdfg dsfg dfg sdfgsdfgsdfgsdfg sdfgsdfg sdfg sdfg sdf gsdfg sdfg sd"
       
    66 "asd fasdf asdf asdf asd fasfd as fdaèèèèèèè òòòòòòòòòòòòsfd asdf as fdas ffsd asfd as fdASASASAs D"
       
    67 "Asfdsf sdfg sdfg dsfg dfg sdfgsdfgsdfgsdfg sdfgsdfg sdfgùùùùùùùùùùùùùù sdfg sdf gsdfg sdfg sd"
       
    68 "asd fasdf asdf asdf asd fasfd as fdasfd asd@@@@@@@f as fdas ffsd asfd as fdASASASAs D "
       
    69 "Asfdsf sdfg sdfg dsfg dfg sdfgsdfgsdfgsdfg sdfgsdf€€€€€€€€€€€€€€€€€€g sdfg sdfg sdf gsdfg sdfg sd"
       
    70 "asd fasdf asdf asdf asd fasfd as fdasfd asdf as fdas ffsd asfd as fdASASASAs D"
       
    71 "Asfdsf sdfg sdfg dsfg dfg sdfgsdfgsdfgsdfg sdfgsdfg sdfg sdfg sdf gsdfg sdfg sd\n\nlalala\n";
       
    72 
       
    73 static void
       
    74 test_length (void)
       
    75 {
       
    76   g_assert (g_utf8_strlen ("1234", -1) == 4);
       
    77   g_assert (g_utf8_strlen ("1234", 0) == 0);
       
    78   g_assert (g_utf8_strlen ("1234", 1) == 1);
       
    79   g_assert (g_utf8_strlen ("1234", 2) == 2);
       
    80   g_assert (g_utf8_strlen ("1234", 3) == 3);
       
    81   g_assert (g_utf8_strlen ("1234", 4) == 4);
       
    82   g_assert (g_utf8_strlen ("1234", 5) == 4);
       
    83 
       
    84   g_assert (g_utf8_strlen (longline, -1) == 762);
       
    85   g_assert (g_utf8_strlen (longline, strlen (longline)) == 762);
       
    86   g_assert (g_utf8_strlen (longline, 1024) == 762);
       
    87 
       
    88   g_assert (g_utf8_strlen (NULL, 0) == 0);
       
    89 
       
    90   g_assert (g_utf8_strlen ("a\340\250\201c", -1) == 3);
       
    91   g_assert (g_utf8_strlen ("a\340\250\201c", 1) == 1);
       
    92   g_assert (g_utf8_strlen ("a\340\250\201c", 2) == 1);
       
    93   g_assert (g_utf8_strlen ("a\340\250\201c", 3) == 1);
       
    94   g_assert (g_utf8_strlen ("a\340\250\201c", 4) == 2);
       
    95   g_assert (g_utf8_strlen ("a\340\250\201c", 5) == 3);
       
    96 }
       
    97 
       
    98 static void
       
    99 test_misc (void)
       
   100 {
       
   101   char *s;
       
   102   s = g_utf8_strreverse ("1234", -1);
       
   103   g_assert (strcmp (s, "4321") == 0);
       
   104   g_free (s);
       
   105   s = g_utf8_strreverse ("1234", 3);
       
   106   g_assert (strcmp (s, "321") == 0);
       
   107   g_free (s);
       
   108 }
       
   109 
       
   110 int main (int argc, char *argv[])
       
   111 {
       
   112 
       
   113   #ifdef __SYMBIAN32__
       
   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);
       
   115   g_set_print_handler(mrtPrintHandler);
       
   116   #endif /*__SYMBIAN32__*/
       
   117   
       
   118   test_utf8 (longline);
       
   119   test_length ();
       
   120   test_misc ();
       
   121  #ifdef __SYMBIAN32__
       
   122   testResultXml("utf8-pointer");
       
   123   #endif //__SYMBIAN32__
       
   124   
       
   125   return 0;
       
   126 }