Home | History | Annotate | Download | only in tests
      1 /* GLIB - Library of useful routines for C programming
      2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
      3  *
      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 /* Test conversions between offsets and pointers */
     31 
     32 static void test_utf8 (gchar *string)
     33 {
     34   gint num_chars;
     35   gchar **p;
     36   gint i, j;
     37 
     38   g_assert (g_utf8_validate (string, -1, NULL));
     39 
     40   num_chars = g_utf8_strlen (string, -1);
     41 
     42   p = (gchar **) g_malloc (num_chars * sizeof (gchar *));
     43 
     44   p[0] = string;
     45   for (i = 1; i < num_chars; i++)
     46     p[i] = g_utf8_next_char (p[i-1]);
     47 
     48   for (i = 0; i < num_chars; i++)
     49     for (j = 0; j < num_chars; j++)
     50       {
     51 	g_assert (g_utf8_offset_to_pointer (p[i], j - i) == p[j]);
     52 	g_assert (g_utf8_pointer_to_offset (p[i], p[j]) == j - i);
     53       }
     54 
     55   g_free (p);
     56 }
     57 
     58 gchar *longline = "asdasdas dsaf asfd as fdasdf asfd asdf as dfas dfasdf a"
     59 "asd fasdf asdf asdf asd fasfd as fdasfd asdf as fdas ffsd asfd as fdASASASAs As"
     60 "Asfdsf sdfg sdfg dsfg dfg sdfgsdfgsdfgsdfg sdfgsdfg sdfg sdfg sdf gsdfg sdfg sd"
     61 "asd fasdf asdf asdf asd fasfd as fda sfd asdf as fdas ffsd asfd as fdASASASAs D"
     62 "Asfdsf sdfg sdfg dsfg dfg sdfgsdfgsdfgsdfg sdfgsdfg sdfg sdfg sdf gsdfg sdfg sd"
     63 "asd fasdf asdf asdf asd fasfd as fdasfd asd@@@@@@@f as fdas ffsd asfd as fdASASASAs D "
     64 "Asfdsf sdfg sdfg dsfg dfg sdfgsdfgsdfgsdfg sdfgsdfg sdfg sdfg sdf gsdfg sdfg sd"
     65 "asd fasdf asdf asdf asd fasfd as fdasfd asdf as fdas ffsd asfd as fdASASASAs D"
     66 "Asfdsf sdfg sdfg dsfg dfg sdfgsdfgsdfgsdfg sdfgsdfg sdfg sdfg sdf gsdfg sdfg sd\n\nlalala\n";
     67 
     68 static void
     69 test_length (void)
     70 {
     71   g_assert (g_utf8_strlen ("1234", -1) == 4);
     72   g_assert (g_utf8_strlen ("1234", 0) == 0);
     73   g_assert (g_utf8_strlen ("1234", 1) == 1);
     74   g_assert (g_utf8_strlen ("1234", 2) == 2);
     75   g_assert (g_utf8_strlen ("1234", 3) == 3);
     76   g_assert (g_utf8_strlen ("1234", 4) == 4);
     77   g_assert (g_utf8_strlen ("1234", 5) == 4);
     78 
     79   g_assert (g_utf8_strlen (longline, -1) == 762);
     80   g_assert (g_utf8_strlen (longline, strlen (longline)) == 762);
     81   g_assert (g_utf8_strlen (longline, 1024) == 762);
     82 
     83   g_assert (g_utf8_strlen (NULL, 0) == 0);
     84 
     85   g_assert (g_utf8_strlen ("a\340\250\201c", -1) == 3);
     86   g_assert (g_utf8_strlen ("a\340\250\201c", 1) == 1);
     87   g_assert (g_utf8_strlen ("a\340\250\201c", 2) == 1);
     88   g_assert (g_utf8_strlen ("a\340\250\201c", 3) == 1);
     89   g_assert (g_utf8_strlen ("a\340\250\201c", 4) == 2);
     90   g_assert (g_utf8_strlen ("a\340\250\201c", 5) == 3);
     91 }
     92 
     93 static void
     94 test_misc (void)
     95 {
     96   char *s;
     97   s = g_utf8_strreverse ("1234", -1);
     98   g_assert (strcmp (s, "4321") == 0);
     99   g_free (s);
    100   s = g_utf8_strreverse ("1234", 3);
    101   g_assert (strcmp (s, "321") == 0);
    102   g_free (s);
    103 }
    104 
    105 int main (int argc, char *argv[])
    106 {
    107   test_utf8 (longline);
    108   test_length ();
    109   test_misc ();
    110 
    111   return 0;
    112 }
    113