glib/tsrc/BC/tests/patterntest.c
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /* GLIB - Library of useful routines for C programming
       
     2  * Copyright (C) 2001 Matthias Clasen <matthiasc@poet.de>
       
     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 #undef G_DISABLE_ASSERT
       
    21 #undef G_LOG_DOMAIN
       
    22 
       
    23 #include <string.h>
       
    24 #include <stdio.h>
       
    25 
       
    26 #include "glib.h"
       
    27 #include "glib/gpattern.h"
       
    28 
       
    29 #ifdef SYMBIAN
       
    30 #include "mrt2_glib2_test.h"
       
    31 #endif /*SYMBIAN*/
       
    32 
       
    33 
       
    34 static gboolean noisy = FALSE;
       
    35 
       
    36 static void
       
    37 verbose (const gchar *format, ...)
       
    38 {
       
    39   gchar *msg;
       
    40   va_list args;
       
    41 
       
    42   va_start (args, format);
       
    43   msg = g_strdup_vprintf (format, args);
       
    44   va_end (args);
       
    45 
       
    46   if (noisy) 
       
    47     g_print (msg);
       
    48   g_free (msg);
       
    49 }
       
    50 
       
    51 /* keep enum and structure of gpattern.c and patterntest.c in sync */
       
    52 typedef enum
       
    53 {
       
    54   G_MATCH_ALL,       /* "*A?A*" */
       
    55   G_MATCH_ALL_TAIL,  /* "*A?AA" */
       
    56   G_MATCH_HEAD,      /* "AAAA*" */
       
    57   G_MATCH_TAIL,      /* "*AAAA" */
       
    58   G_MATCH_EXACT,     /* "AAAAA" */
       
    59   G_MATCH_LAST
       
    60 } GMatchType;
       
    61 
       
    62 struct _GPatternSpec
       
    63 {
       
    64   GMatchType match_type;
       
    65   guint      pattern_length;
       
    66   guint      min_length;
       
    67   guint      max_length;
       
    68   gchar     *pattern;
       
    69 };
       
    70 
       
    71 
       
    72 static gchar *
       
    73 match_type_name (GMatchType match_type)
       
    74 {
       
    75   switch (match_type)
       
    76     {
       
    77     case G_MATCH_ALL: 
       
    78       return "G_MATCH_ALL";
       
    79       break;
       
    80     case G_MATCH_ALL_TAIL:
       
    81       return "G_MATCH_ALL_TAIL";
       
    82       break;
       
    83     case G_MATCH_HEAD:
       
    84       return "G_MATCH_HEAD";
       
    85       break;
       
    86     case G_MATCH_TAIL:
       
    87       return "G_MATCH_TAIL";
       
    88       break;
       
    89     case G_MATCH_EXACT:
       
    90       return "G_MATCH_EXACT";
       
    91       break;
       
    92     default:
       
    93       return "unknown GMatchType";
       
    94       break;
       
    95     }
       
    96 }
       
    97 
       
    98 static gboolean
       
    99 test_compilation (gchar *src, 
       
   100 		  GMatchType match_type, 
       
   101 		  gchar *pattern,
       
   102 		  guint min)
       
   103 {
       
   104   GPatternSpec *spec; 
       
   105 
       
   106   verbose ("compiling \"%s\" \t", src);
       
   107   spec = g_pattern_spec_new (src);
       
   108 
       
   109   if (spec->match_type != match_type)
       
   110     {
       
   111       g_print ("failed \t(match_type: %s, expected %s)\n",
       
   112 	       match_type_name (spec->match_type), 
       
   113 	       match_type_name (match_type));
       
   114 	  g_assert(FALSE && "patterntest");
       
   115       return FALSE;
       
   116     }
       
   117   
       
   118   if (strcmp (spec->pattern, pattern) != 0)
       
   119     {
       
   120       g_print ("failed \t(pattern: \"%s\", expected \"%s\")\n",
       
   121 	       spec->pattern,
       
   122 	       pattern);
       
   123 	g_assert(FALSE && "patterntest");	       
       
   124       return FALSE;
       
   125     }
       
   126   
       
   127   if (spec->pattern_length != strlen (spec->pattern))
       
   128     {
       
   129       g_print ("failed \t(pattern_length: %d, expected %d)\n",
       
   130 	       spec->pattern_length,
       
   131 	       (gint)strlen (spec->pattern));
       
   132 	  g_assert(FALSE && "patterntest");
       
   133       return FALSE;
       
   134     }
       
   135   
       
   136   if (spec->min_length != min)
       
   137     {
       
   138       g_print ("failed \t(min_length: %d, expected %d)\n",
       
   139 	       spec->min_length,
       
   140 	       min);
       
   141 	  g_assert(FALSE && "patterntest");
       
   142       return FALSE;
       
   143     }
       
   144   
       
   145   verbose ("passed (%s: \"%s\")\n",
       
   146 	   match_type_name (spec->match_type),
       
   147 	   spec->pattern);
       
   148   
       
   149   return TRUE;
       
   150 }
       
   151 
       
   152 static gboolean
       
   153 test_match (gchar *pattern, 
       
   154 	    gchar *string, 
       
   155 	    gboolean match)
       
   156 {
       
   157   verbose ("matching \"%s\" against \"%s\" \t", string, pattern);
       
   158   
       
   159   if (g_pattern_match_simple (pattern, string) != match)
       
   160     {
       
   161       g_print ("failed \t(unexpected %s)\n", (match ? "mismatch" : "match"));
       
   162       g_assert(FALSE && "patterntest");
       
   163       return FALSE;
       
   164     }
       
   165   
       
   166   verbose ("passed (%s)\n", match ? "match" : "nomatch");
       
   167 
       
   168   return TRUE;
       
   169 }
       
   170 
       
   171 static gboolean
       
   172 test_equal (gchar *pattern1,
       
   173 	    gchar *pattern2,
       
   174 	    gboolean expected)
       
   175 {
       
   176   GPatternSpec *p1 = g_pattern_spec_new (pattern1);
       
   177   GPatternSpec *p2 = g_pattern_spec_new (pattern2);
       
   178   gboolean equal = g_pattern_spec_equal (p1, p2);
       
   179 
       
   180   verbose ("comparing \"%s\" with \"%s\" \t", pattern1, pattern2);
       
   181 
       
   182   if (expected != equal)
       
   183     {
       
   184       g_print ("failed \t{%s, %u, \"%s\"} %s {%s, %u, \"%s\"}\n",
       
   185 	       match_type_name (p1->match_type), p1->pattern_length, p1->pattern,
       
   186 	       expected ? "!=" : "==",
       
   187 	       match_type_name (p2->match_type), p2->pattern_length, p2->pattern);
       
   188     g_assert(FALSE && "patterntest");
       
   189     }
       
   190   else
       
   191     verbose ("passed (%s)\n", equal ? "equal" : "unequal");
       
   192   
       
   193   g_pattern_spec_free (p1);
       
   194   g_pattern_spec_free (p2);
       
   195 
       
   196   return expected == equal;
       
   197 }
       
   198 
       
   199 #define TEST_COMPILATION(src, type, pattern, min) { \
       
   200   total++; \
       
   201   if (test_compilation (src, type, pattern, min)) \
       
   202     passed++; \
       
   203   else \
       
   204     failed++; \
       
   205 }
       
   206 
       
   207 #define TEST_MATCH(pattern, string, match) { \
       
   208   total++; \
       
   209   if (test_match (pattern, string, match)) \
       
   210     passed++; \
       
   211   else \
       
   212     failed++; \
       
   213 }
       
   214 
       
   215 #define TEST_EQUAL(pattern1, pattern2, match) { \
       
   216   total++; \
       
   217   if (test_equal (pattern1, pattern2, match)) \
       
   218     passed++; \
       
   219   else \
       
   220     failed++; \
       
   221 }
       
   222 
       
   223 int
       
   224 main (int argc, char** argv)
       
   225 {
       
   226   gint total = 0;
       
   227   gint passed = 0;
       
   228   gint failed = 0;
       
   229   gint i;
       
   230 
       
   231   #ifdef SYMBIAN
       
   232   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);
       
   233   g_set_print_handler(mrtPrintHandler);
       
   234   #endif /*SYMBIAN*/
       
   235 	  
       
   236 
       
   237   for (i = 1; i < argc; i++) 
       
   238       if (strcmp ("--noisy", argv[i]) == 0)
       
   239 	noisy = TRUE;
       
   240 
       
   241   TEST_COMPILATION("*A?B*", G_MATCH_ALL, "*A?B*", 3);
       
   242   TEST_COMPILATION("ABC*DEFGH", G_MATCH_ALL_TAIL, "HGFED*CBA", 8);
       
   243   TEST_COMPILATION("ABCDEF*GH", G_MATCH_ALL, "ABCDEF*GH", 8);
       
   244   TEST_COMPILATION("ABC**?***??**DEF*GH", G_MATCH_ALL, "ABC*???DEF*GH", 11);
       
   245   TEST_COMPILATION("*A?AA", G_MATCH_ALL_TAIL, "AA?A*", 4);
       
   246   TEST_COMPILATION("ABCD*", G_MATCH_HEAD, "ABCD", 4);
       
   247   TEST_COMPILATION("*ABCD", G_MATCH_TAIL, "ABCD", 4);
       
   248   TEST_COMPILATION("ABCDE", G_MATCH_EXACT, "ABCDE", 5);
       
   249   TEST_COMPILATION("A?C?E", G_MATCH_ALL, "A?C?E", 5);
       
   250   TEST_COMPILATION("*?x", G_MATCH_ALL_TAIL, "x?*", 2);
       
   251   TEST_COMPILATION("?*x", G_MATCH_ALL_TAIL, "x?*", 2);
       
   252   TEST_COMPILATION("*?*x", G_MATCH_ALL_TAIL, "x?*", 2);
       
   253   TEST_COMPILATION("x*??", G_MATCH_ALL_TAIL, "??*x", 3);
       
   254 
       
   255   TEST_EQUAL("*A?B*", "*A?B*", TRUE);
       
   256   TEST_EQUAL("A*BCD", "A*BCD", TRUE);
       
   257   TEST_EQUAL("ABCD*", "ABCD****", TRUE);
       
   258   TEST_EQUAL("A1*", "A1*", TRUE);
       
   259   TEST_EQUAL("*YZ", "*YZ", TRUE);
       
   260   TEST_EQUAL("A1x", "A1x", TRUE);
       
   261   TEST_EQUAL("AB*CD", "AB**CD", TRUE);
       
   262   TEST_EQUAL("AB*?*CD", "AB*?CD", TRUE);
       
   263   TEST_EQUAL("AB*?CD", "AB?*CD", TRUE);
       
   264   TEST_EQUAL("AB*CD", "AB*?*CD", FALSE);
       
   265   TEST_EQUAL("ABC*", "ABC?", FALSE);
       
   266 
       
   267   TEST_MATCH("*x", "x", TRUE);
       
   268   TEST_MATCH("*x", "xx", TRUE);
       
   269   TEST_MATCH("*x", "yyyx", TRUE);
       
   270   TEST_MATCH("*x", "yyxy", FALSE);
       
   271   TEST_MATCH("?x", "x", FALSE);
       
   272   TEST_MATCH("?x", "xx", TRUE);
       
   273   TEST_MATCH("?x", "yyyx", FALSE);
       
   274   TEST_MATCH("?x", "yyxy", FALSE);
       
   275   TEST_MATCH("*?x", "xx", TRUE);
       
   276   TEST_MATCH("?*x", "xx", TRUE);
       
   277   TEST_MATCH("*?x", "x", FALSE);
       
   278   TEST_MATCH("?*x", "x", FALSE);
       
   279   TEST_MATCH("*?*x", "yx", TRUE);
       
   280   TEST_MATCH("*?*x", "xxxx", TRUE);
       
   281   TEST_MATCH("x*??", "xyzw", TRUE);
       
   282   TEST_MATCH("*x", "\xc3\x84x", TRUE);
       
   283   TEST_MATCH("?x", "\xc3\x84x", TRUE);
       
   284   TEST_MATCH("??x", "\xc3\x84x", FALSE);
       
   285   TEST_MATCH("ab\xc3\xa4\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE);
       
   286   TEST_MATCH("ab\xc3\xa4\xc3\xb6", "abao", FALSE);
       
   287   TEST_MATCH("ab?\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE);
       
   288   TEST_MATCH("ab?\xc3\xb6", "abao", FALSE);
       
   289   TEST_MATCH("ab\xc3\xa4?", "ab\xc3\xa4\xc3\xb6", TRUE);
       
   290   TEST_MATCH("ab\xc3\xa4?", "abao", FALSE);
       
   291   TEST_MATCH("ab??", "ab\xc3\xa4\xc3\xb6", TRUE);
       
   292   TEST_MATCH("ab*", "ab\xc3\xa4\xc3\xb6", TRUE);
       
   293   TEST_MATCH("ab*\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE);
       
   294   TEST_MATCH("ab*\xc3\xb6", "aba\xc3\xb6x\xc3\xb6", TRUE);
       
   295   TEST_MATCH("", "", TRUE);
       
   296   TEST_MATCH("", "abc", FALSE);
       
   297   
       
   298   verbose ("\n%u tests passed, %u failed\n", passed, failed);
       
   299 #ifdef SYMBIAN
       
   300   testResultXml("patterntest");
       
   301 #endif /* EMULATOR */
       
   302 
       
   303   return failed;
       
   304 }
       
   305 
       
   306