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