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