os/ossrv/glib/tsrc/BC/tests/patterntest.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/glib/tsrc/BC/tests/patterntest.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,306 @@
     1.4 +/* GLIB - Library of useful routines for C programming
     1.5 + * Copyright (C) 2001 Matthias Clasen <matthiasc@poet.de>
     1.6 + * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
     1.7 + * This library is free software; you can redistribute it and/or
     1.8 + * modify it under the terms of the GNU Lesser General Public
     1.9 + * License as published by the Free Software Foundation; either
    1.10 + * version 2 of the License, or (at your option) any later version.
    1.11 + *
    1.12 + * This library is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.15 + * Lesser General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU Lesser General Public
    1.18 + * License along with this library; if not, write to the
    1.19 + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    1.20 + * Boston, MA 02111-1307, USA.
    1.21 + */
    1.22 +
    1.23 +#undef G_DISABLE_ASSERT
    1.24 +#undef G_LOG_DOMAIN
    1.25 +
    1.26 +#include <string.h>
    1.27 +#include <stdio.h>
    1.28 +
    1.29 +#include "glib.h"
    1.30 +#include "glib/gpattern.h"
    1.31 +
    1.32 +#ifdef SYMBIAN
    1.33 +#include "mrt2_glib2_test.h"
    1.34 +#endif /*SYMBIAN*/
    1.35 +
    1.36 +
    1.37 +static gboolean noisy = FALSE;
    1.38 +
    1.39 +static void
    1.40 +verbose (const gchar *format, ...)
    1.41 +{
    1.42 +  gchar *msg;
    1.43 +  va_list args;
    1.44 +
    1.45 +  va_start (args, format);
    1.46 +  msg = g_strdup_vprintf (format, args);
    1.47 +  va_end (args);
    1.48 +
    1.49 +  if (noisy) 
    1.50 +    g_print (msg);
    1.51 +  g_free (msg);
    1.52 +}
    1.53 +
    1.54 +/* keep enum and structure of gpattern.c and patterntest.c in sync */
    1.55 +typedef enum
    1.56 +{
    1.57 +  G_MATCH_ALL,       /* "*A?A*" */
    1.58 +  G_MATCH_ALL_TAIL,  /* "*A?AA" */
    1.59 +  G_MATCH_HEAD,      /* "AAAA*" */
    1.60 +  G_MATCH_TAIL,      /* "*AAAA" */
    1.61 +  G_MATCH_EXACT,     /* "AAAAA" */
    1.62 +  G_MATCH_LAST
    1.63 +} GMatchType;
    1.64 +
    1.65 +struct _GPatternSpec
    1.66 +{
    1.67 +  GMatchType match_type;
    1.68 +  guint      pattern_length;
    1.69 +  guint      min_length;
    1.70 +  guint      max_length;
    1.71 +  gchar     *pattern;
    1.72 +};
    1.73 +
    1.74 +
    1.75 +static gchar *
    1.76 +match_type_name (GMatchType match_type)
    1.77 +{
    1.78 +  switch (match_type)
    1.79 +    {
    1.80 +    case G_MATCH_ALL: 
    1.81 +      return "G_MATCH_ALL";
    1.82 +      break;
    1.83 +    case G_MATCH_ALL_TAIL:
    1.84 +      return "G_MATCH_ALL_TAIL";
    1.85 +      break;
    1.86 +    case G_MATCH_HEAD:
    1.87 +      return "G_MATCH_HEAD";
    1.88 +      break;
    1.89 +    case G_MATCH_TAIL:
    1.90 +      return "G_MATCH_TAIL";
    1.91 +      break;
    1.92 +    case G_MATCH_EXACT:
    1.93 +      return "G_MATCH_EXACT";
    1.94 +      break;
    1.95 +    default:
    1.96 +      return "unknown GMatchType";
    1.97 +      break;
    1.98 +    }
    1.99 +}
   1.100 +
   1.101 +static gboolean
   1.102 +test_compilation (gchar *src, 
   1.103 +		  GMatchType match_type, 
   1.104 +		  gchar *pattern,
   1.105 +		  guint min)
   1.106 +{
   1.107 +  GPatternSpec *spec; 
   1.108 +
   1.109 +  verbose ("compiling \"%s\" \t", src);
   1.110 +  spec = g_pattern_spec_new (src);
   1.111 +
   1.112 +  if (spec->match_type != match_type)
   1.113 +    {
   1.114 +      g_print ("failed \t(match_type: %s, expected %s)\n",
   1.115 +	       match_type_name (spec->match_type), 
   1.116 +	       match_type_name (match_type));
   1.117 +	  g_assert(FALSE && "patterntest");
   1.118 +      return FALSE;
   1.119 +    }
   1.120 +  
   1.121 +  if (strcmp (spec->pattern, pattern) != 0)
   1.122 +    {
   1.123 +      g_print ("failed \t(pattern: \"%s\", expected \"%s\")\n",
   1.124 +	       spec->pattern,
   1.125 +	       pattern);
   1.126 +	g_assert(FALSE && "patterntest");	       
   1.127 +      return FALSE;
   1.128 +    }
   1.129 +  
   1.130 +  if (spec->pattern_length != strlen (spec->pattern))
   1.131 +    {
   1.132 +      g_print ("failed \t(pattern_length: %d, expected %d)\n",
   1.133 +	       spec->pattern_length,
   1.134 +	       (gint)strlen (spec->pattern));
   1.135 +	  g_assert(FALSE && "patterntest");
   1.136 +      return FALSE;
   1.137 +    }
   1.138 +  
   1.139 +  if (spec->min_length != min)
   1.140 +    {
   1.141 +      g_print ("failed \t(min_length: %d, expected %d)\n",
   1.142 +	       spec->min_length,
   1.143 +	       min);
   1.144 +	  g_assert(FALSE && "patterntest");
   1.145 +      return FALSE;
   1.146 +    }
   1.147 +  
   1.148 +  verbose ("passed (%s: \"%s\")\n",
   1.149 +	   match_type_name (spec->match_type),
   1.150 +	   spec->pattern);
   1.151 +  
   1.152 +  return TRUE;
   1.153 +}
   1.154 +
   1.155 +static gboolean
   1.156 +test_match (gchar *pattern, 
   1.157 +	    gchar *string, 
   1.158 +	    gboolean match)
   1.159 +{
   1.160 +  verbose ("matching \"%s\" against \"%s\" \t", string, pattern);
   1.161 +  
   1.162 +  if (g_pattern_match_simple (pattern, string) != match)
   1.163 +    {
   1.164 +      g_print ("failed \t(unexpected %s)\n", (match ? "mismatch" : "match"));
   1.165 +      g_assert(FALSE && "patterntest");
   1.166 +      return FALSE;
   1.167 +    }
   1.168 +  
   1.169 +  verbose ("passed (%s)\n", match ? "match" : "nomatch");
   1.170 +
   1.171 +  return TRUE;
   1.172 +}
   1.173 +
   1.174 +static gboolean
   1.175 +test_equal (gchar *pattern1,
   1.176 +	    gchar *pattern2,
   1.177 +	    gboolean expected)
   1.178 +{
   1.179 +  GPatternSpec *p1 = g_pattern_spec_new (pattern1);
   1.180 +  GPatternSpec *p2 = g_pattern_spec_new (pattern2);
   1.181 +  gboolean equal = g_pattern_spec_equal (p1, p2);
   1.182 +
   1.183 +  verbose ("comparing \"%s\" with \"%s\" \t", pattern1, pattern2);
   1.184 +
   1.185 +  if (expected != equal)
   1.186 +    {
   1.187 +      g_print ("failed \t{%s, %u, \"%s\"} %s {%s, %u, \"%s\"}\n",
   1.188 +	       match_type_name (p1->match_type), p1->pattern_length, p1->pattern,
   1.189 +	       expected ? "!=" : "==",
   1.190 +	       match_type_name (p2->match_type), p2->pattern_length, p2->pattern);
   1.191 +    g_assert(FALSE && "patterntest");
   1.192 +    }
   1.193 +  else
   1.194 +    verbose ("passed (%s)\n", equal ? "equal" : "unequal");
   1.195 +  
   1.196 +  g_pattern_spec_free (p1);
   1.197 +  g_pattern_spec_free (p2);
   1.198 +
   1.199 +  return expected == equal;
   1.200 +}
   1.201 +
   1.202 +#define TEST_COMPILATION(src, type, pattern, min) { \
   1.203 +  total++; \
   1.204 +  if (test_compilation (src, type, pattern, min)) \
   1.205 +    passed++; \
   1.206 +  else \
   1.207 +    failed++; \
   1.208 +}
   1.209 +
   1.210 +#define TEST_MATCH(pattern, string, match) { \
   1.211 +  total++; \
   1.212 +  if (test_match (pattern, string, match)) \
   1.213 +    passed++; \
   1.214 +  else \
   1.215 +    failed++; \
   1.216 +}
   1.217 +
   1.218 +#define TEST_EQUAL(pattern1, pattern2, match) { \
   1.219 +  total++; \
   1.220 +  if (test_equal (pattern1, pattern2, match)) \
   1.221 +    passed++; \
   1.222 +  else \
   1.223 +    failed++; \
   1.224 +}
   1.225 +
   1.226 +int
   1.227 +main (int argc, char** argv)
   1.228 +{
   1.229 +  gint total = 0;
   1.230 +  gint passed = 0;
   1.231 +  gint failed = 0;
   1.232 +  gint i;
   1.233 +
   1.234 +  #ifdef SYMBIAN
   1.235 +  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);
   1.236 +  g_set_print_handler(mrtPrintHandler);
   1.237 +  #endif /*SYMBIAN*/
   1.238 +	  
   1.239 +
   1.240 +  for (i = 1; i < argc; i++) 
   1.241 +      if (strcmp ("--noisy", argv[i]) == 0)
   1.242 +	noisy = TRUE;
   1.243 +
   1.244 +  TEST_COMPILATION("*A?B*", G_MATCH_ALL, "*A?B*", 3);
   1.245 +  TEST_COMPILATION("ABC*DEFGH", G_MATCH_ALL_TAIL, "HGFED*CBA", 8);
   1.246 +  TEST_COMPILATION("ABCDEF*GH", G_MATCH_ALL, "ABCDEF*GH", 8);
   1.247 +  TEST_COMPILATION("ABC**?***??**DEF*GH", G_MATCH_ALL, "ABC*???DEF*GH", 11);
   1.248 +  TEST_COMPILATION("*A?AA", G_MATCH_ALL_TAIL, "AA?A*", 4);
   1.249 +  TEST_COMPILATION("ABCD*", G_MATCH_HEAD, "ABCD", 4);
   1.250 +  TEST_COMPILATION("*ABCD", G_MATCH_TAIL, "ABCD", 4);
   1.251 +  TEST_COMPILATION("ABCDE", G_MATCH_EXACT, "ABCDE", 5);
   1.252 +  TEST_COMPILATION("A?C?E", G_MATCH_ALL, "A?C?E", 5);
   1.253 +  TEST_COMPILATION("*?x", G_MATCH_ALL_TAIL, "x?*", 2);
   1.254 +  TEST_COMPILATION("?*x", G_MATCH_ALL_TAIL, "x?*", 2);
   1.255 +  TEST_COMPILATION("*?*x", G_MATCH_ALL_TAIL, "x?*", 2);
   1.256 +  TEST_COMPILATION("x*??", G_MATCH_ALL_TAIL, "??*x", 3);
   1.257 +
   1.258 +  TEST_EQUAL("*A?B*", "*A?B*", TRUE);
   1.259 +  TEST_EQUAL("A*BCD", "A*BCD", TRUE);
   1.260 +  TEST_EQUAL("ABCD*", "ABCD****", TRUE);
   1.261 +  TEST_EQUAL("A1*", "A1*", TRUE);
   1.262 +  TEST_EQUAL("*YZ", "*YZ", TRUE);
   1.263 +  TEST_EQUAL("A1x", "A1x", TRUE);
   1.264 +  TEST_EQUAL("AB*CD", "AB**CD", TRUE);
   1.265 +  TEST_EQUAL("AB*?*CD", "AB*?CD", TRUE);
   1.266 +  TEST_EQUAL("AB*?CD", "AB?*CD", TRUE);
   1.267 +  TEST_EQUAL("AB*CD", "AB*?*CD", FALSE);
   1.268 +  TEST_EQUAL("ABC*", "ABC?", FALSE);
   1.269 +
   1.270 +  TEST_MATCH("*x", "x", TRUE);
   1.271 +  TEST_MATCH("*x", "xx", TRUE);
   1.272 +  TEST_MATCH("*x", "yyyx", TRUE);
   1.273 +  TEST_MATCH("*x", "yyxy", FALSE);
   1.274 +  TEST_MATCH("?x", "x", FALSE);
   1.275 +  TEST_MATCH("?x", "xx", TRUE);
   1.276 +  TEST_MATCH("?x", "yyyx", FALSE);
   1.277 +  TEST_MATCH("?x", "yyxy", FALSE);
   1.278 +  TEST_MATCH("*?x", "xx", TRUE);
   1.279 +  TEST_MATCH("?*x", "xx", TRUE);
   1.280 +  TEST_MATCH("*?x", "x", FALSE);
   1.281 +  TEST_MATCH("?*x", "x", FALSE);
   1.282 +  TEST_MATCH("*?*x", "yx", TRUE);
   1.283 +  TEST_MATCH("*?*x", "xxxx", TRUE);
   1.284 +  TEST_MATCH("x*??", "xyzw", TRUE);
   1.285 +  TEST_MATCH("*x", "\xc3\x84x", TRUE);
   1.286 +  TEST_MATCH("?x", "\xc3\x84x", TRUE);
   1.287 +  TEST_MATCH("??x", "\xc3\x84x", FALSE);
   1.288 +  TEST_MATCH("ab\xc3\xa4\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE);
   1.289 +  TEST_MATCH("ab\xc3\xa4\xc3\xb6", "abao", FALSE);
   1.290 +  TEST_MATCH("ab?\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE);
   1.291 +  TEST_MATCH("ab?\xc3\xb6", "abao", FALSE);
   1.292 +  TEST_MATCH("ab\xc3\xa4?", "ab\xc3\xa4\xc3\xb6", TRUE);
   1.293 +  TEST_MATCH("ab\xc3\xa4?", "abao", FALSE);
   1.294 +  TEST_MATCH("ab??", "ab\xc3\xa4\xc3\xb6", TRUE);
   1.295 +  TEST_MATCH("ab*", "ab\xc3\xa4\xc3\xb6", TRUE);
   1.296 +  TEST_MATCH("ab*\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE);
   1.297 +  TEST_MATCH("ab*\xc3\xb6", "aba\xc3\xb6x\xc3\xb6", TRUE);
   1.298 +  TEST_MATCH("", "", TRUE);
   1.299 +  TEST_MATCH("", "abc", FALSE);
   1.300 +  
   1.301 +  verbose ("\n%u tests passed, %u failed\n", passed, failed);
   1.302 +#ifdef SYMBIAN
   1.303 +  testResultXml("patterntest");
   1.304 +#endif /* EMULATOR */
   1.305 +
   1.306 +  return failed;
   1.307 +}
   1.308 +
   1.309 +