1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tests/patterntest.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,321 @@
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 <glib.h>
1.28 +#ifdef __SYMBIAN32__
1.29 +#include "mrt2_glib2_test.h"
1.30 +#endif /*__SYMBIAN32__*/
1.31 +
1.32 +
1.33 +static gboolean noisy = FALSE;
1.34 +
1.35 +static void
1.36 +verbose (const gchar *format, ...)
1.37 +{
1.38 + gchar *msg;
1.39 + va_list args;
1.40 +
1.41 + va_start (args, format);
1.42 + msg = g_strdup_vprintf (format, args);
1.43 + va_end (args);
1.44 +
1.45 + if (noisy)
1.46 + g_print (msg);
1.47 + g_free (msg);
1.48 +}
1.49 +
1.50 +/* keep enum and structure of gpattern.c and patterntest.c in sync */
1.51 +typedef enum
1.52 +{
1.53 + G_MATCH_ALL, /* "*A?A*" */
1.54 + G_MATCH_ALL_TAIL, /* "*A?AA" */
1.55 + G_MATCH_HEAD, /* "AAAA*" */
1.56 + G_MATCH_TAIL, /* "*AAAA" */
1.57 + G_MATCH_EXACT, /* "AAAAA" */
1.58 + G_MATCH_LAST
1.59 +} GMatchType;
1.60 +
1.61 +struct _GPatternSpec
1.62 +{
1.63 + GMatchType match_type;
1.64 + guint pattern_length;
1.65 + guint min_length;
1.66 + guint max_length;
1.67 + gchar *pattern;
1.68 +};
1.69 +
1.70 +
1.71 +static gchar *
1.72 +match_type_name (GMatchType match_type)
1.73 +{
1.74 + switch (match_type)
1.75 + {
1.76 + case G_MATCH_ALL:
1.77 + return "G_MATCH_ALL";
1.78 + break;
1.79 + case G_MATCH_ALL_TAIL:
1.80 + return "G_MATCH_ALL_TAIL";
1.81 + break;
1.82 + case G_MATCH_HEAD:
1.83 + return "G_MATCH_HEAD";
1.84 + break;
1.85 + case G_MATCH_TAIL:
1.86 + return "G_MATCH_TAIL";
1.87 + break;
1.88 + case G_MATCH_EXACT:
1.89 + return "G_MATCH_EXACT";
1.90 + break;
1.91 + default:
1.92 + return "unknown GMatchType";
1.93 + break;
1.94 + }
1.95 +}
1.96 +
1.97 +static gboolean
1.98 +test_compilation (gchar *src,
1.99 + GMatchType match_type,
1.100 + gchar *pattern,
1.101 + guint min)
1.102 +{
1.103 + GPatternSpec *spec;
1.104 +
1.105 + verbose ("compiling \"%s\" \t", src);
1.106 + spec = g_pattern_spec_new (src);
1.107 +
1.108 + if (spec->match_type != match_type)
1.109 + {
1.110 + g_print ("failed \t(match_type: %s, expected %s)\n",
1.111 + match_type_name (spec->match_type),
1.112 + match_type_name (match_type));
1.113 + g_pattern_spec_free (spec);
1.114 + return FALSE;
1.115 + }
1.116 +
1.117 + if (strcmp (spec->pattern, pattern) != 0)
1.118 + {
1.119 + g_print ("failed \t(pattern: \"%s\", expected \"%s\")\n",
1.120 + spec->pattern,
1.121 + pattern);
1.122 + g_pattern_spec_free (spec);
1.123 + return FALSE;
1.124 + }
1.125 +
1.126 + if (spec->pattern_length != strlen (spec->pattern))
1.127 + {
1.128 + g_print ("failed \t(pattern_length: %d, expected %d)\n",
1.129 + spec->pattern_length,
1.130 + (gint)strlen (spec->pattern));
1.131 + g_pattern_spec_free (spec);
1.132 + return FALSE;
1.133 + }
1.134 +
1.135 + if (spec->min_length != min)
1.136 + {
1.137 + g_print ("failed \t(min_length: %d, expected %d)\n",
1.138 + spec->min_length,
1.139 + min);
1.140 + g_pattern_spec_free (spec);
1.141 + return FALSE;
1.142 + }
1.143 +
1.144 + verbose ("passed (%s: \"%s\")\n",
1.145 + match_type_name (spec->match_type),
1.146 + spec->pattern);
1.147 +
1.148 + g_pattern_spec_free (spec);
1.149 +
1.150 + return TRUE;
1.151 +}
1.152 +
1.153 +static gboolean
1.154 +test_match (gchar *pattern,
1.155 + gchar *string,
1.156 + gboolean match)
1.157 +{
1.158 + verbose ("matching \"%s\" against \"%s\" \t", string, pattern);
1.159 +
1.160 + if (g_pattern_match_simple (pattern, string) != match)
1.161 + {
1.162 + g_print ("failed \t(unexpected %s)\n", (match ? "mismatch" : "match"));
1.163 + return FALSE;
1.164 + }
1.165 +
1.166 + verbose ("passed (%s)\n", match ? "match" : "nomatch");
1.167 +
1.168 + return TRUE;
1.169 +}
1.170 +
1.171 +static gboolean
1.172 +test_equal (gchar *pattern1,
1.173 + gchar *pattern2,
1.174 + gboolean expected)
1.175 +{
1.176 + GPatternSpec *p1 = g_pattern_spec_new (pattern1);
1.177 + GPatternSpec *p2 = g_pattern_spec_new (pattern2);
1.178 + gboolean equal = g_pattern_spec_equal (p1, p2);
1.179 +
1.180 + verbose ("comparing \"%s\" with \"%s\" \t", pattern1, pattern2);
1.181 +
1.182 + if (expected != equal)
1.183 + {
1.184 + g_print ("failed \t{%s, %u, \"%s\"} %s {%s, %u, \"%s\"}\n",
1.185 + match_type_name (p1->match_type), p1->pattern_length, p1->pattern,
1.186 + expected ? "!=" : "==",
1.187 + match_type_name (p2->match_type), p2->pattern_length, p2->pattern);
1.188 + }
1.189 + else
1.190 + verbose ("passed (%s)\n", equal ? "equal" : "unequal");
1.191 +
1.192 + g_pattern_spec_free (p1);
1.193 + g_pattern_spec_free (p2);
1.194 +
1.195 + return expected == equal;
1.196 +}
1.197 +
1.198 +#define TEST_COMPILATION(src, type, pattern, min) { \
1.199 + total++; \
1.200 + if (test_compilation (src, type, pattern, min)) \
1.201 + passed++; \
1.202 + else \
1.203 + failed++; \
1.204 +}
1.205 +
1.206 +#define TEST_MATCH(pattern, string, match) { \
1.207 + total++; \
1.208 + if (test_match (pattern, string, match)) \
1.209 + passed++; \
1.210 + else \
1.211 + failed++; \
1.212 +}
1.213 +
1.214 +#define TEST_EQUAL(pattern1, pattern2, match) { \
1.215 + total++; \
1.216 + if (test_equal (pattern1, pattern2, match)) \
1.217 + passed++; \
1.218 + else \
1.219 + failed++; \
1.220 +}
1.221 +
1.222 +int
1.223 +main (int argc, char** argv)
1.224 +{
1.225 + gint total = 0;
1.226 + gint passed = 0;
1.227 + gint failed = 0;
1.228 + gint i;
1.229 +
1.230 + #ifdef __SYMBIAN32__
1.231 + 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.232 + g_set_print_handler(mrtPrintHandler);
1.233 + #endif /*__SYMBIAN32__*/
1.234 +
1.235 +
1.236 + for (i = 1; i < argc; i++)
1.237 + if (strcmp ("--noisy", argv[i]) == 0)
1.238 + noisy = TRUE;
1.239 +
1.240 + TEST_COMPILATION("*A?B*", G_MATCH_ALL, "*A?B*", 3);
1.241 + TEST_COMPILATION("ABC*DEFGH", G_MATCH_ALL_TAIL, "HGFED*CBA", 8);
1.242 + TEST_COMPILATION("ABCDEF*GH", G_MATCH_ALL, "ABCDEF*GH", 8);
1.243 + TEST_COMPILATION("ABC**?***??**DEF*GH", G_MATCH_ALL, "ABC*???DEF*GH", 11);
1.244 + TEST_COMPILATION("*A?AA", G_MATCH_ALL_TAIL, "AA?A*", 4);
1.245 + TEST_COMPILATION("ABCD*", G_MATCH_HEAD, "ABCD", 4);
1.246 + TEST_COMPILATION("*ABCD", G_MATCH_TAIL, "ABCD", 4);
1.247 + TEST_COMPILATION("ABCDE", G_MATCH_EXACT, "ABCDE", 5);
1.248 + TEST_COMPILATION("A?C?E", G_MATCH_ALL, "A?C?E", 5);
1.249 + TEST_COMPILATION("*?x", G_MATCH_ALL_TAIL, "x?*", 2);
1.250 + TEST_COMPILATION("?*x", G_MATCH_ALL_TAIL, "x?*", 2);
1.251 + TEST_COMPILATION("*?*x", G_MATCH_ALL_TAIL, "x?*", 2);
1.252 + TEST_COMPILATION("x*??", G_MATCH_ALL_TAIL, "??*x", 3);
1.253 +
1.254 + TEST_EQUAL("*A?B*", "*A?B*", TRUE);
1.255 + TEST_EQUAL("A*BCD", "A*BCD", TRUE);
1.256 + TEST_EQUAL("ABCD*", "ABCD****", TRUE);
1.257 + TEST_EQUAL("A1*", "A1*", TRUE);
1.258 + TEST_EQUAL("*YZ", "*YZ", TRUE);
1.259 + TEST_EQUAL("A1x", "A1x", TRUE);
1.260 + TEST_EQUAL("AB*CD", "AB**CD", TRUE);
1.261 + TEST_EQUAL("AB*?*CD", "AB*?CD", TRUE);
1.262 + TEST_EQUAL("AB*?CD", "AB?*CD", TRUE);
1.263 + TEST_EQUAL("AB*CD", "AB*?*CD", FALSE);
1.264 + TEST_EQUAL("ABC*", "ABC?", FALSE);
1.265 +
1.266 + TEST_MATCH("*x", "x", TRUE);
1.267 + TEST_MATCH("*x", "xx", TRUE);
1.268 + TEST_MATCH("*x", "yyyx", TRUE);
1.269 + TEST_MATCH("*x", "yyxy", FALSE);
1.270 + TEST_MATCH("?x", "x", FALSE);
1.271 + TEST_MATCH("?x", "xx", TRUE);
1.272 + TEST_MATCH("?x", "yyyx", FALSE);
1.273 + TEST_MATCH("?x", "yyxy", FALSE);
1.274 + TEST_MATCH("*?x", "xx", TRUE);
1.275 + TEST_MATCH("?*x", "xx", TRUE);
1.276 + TEST_MATCH("*?x", "x", FALSE);
1.277 + TEST_MATCH("?*x", "x", FALSE);
1.278 + TEST_MATCH("*?*x", "yx", TRUE);
1.279 + TEST_MATCH("*?*x", "xxxx", TRUE);
1.280 + TEST_MATCH("x*??", "xyzw", TRUE);
1.281 + TEST_MATCH("*x", "\xc3\x84x", TRUE);
1.282 + TEST_MATCH("?x", "\xc3\x84x", TRUE);
1.283 + TEST_MATCH("??x", "\xc3\x84x", FALSE);
1.284 + TEST_MATCH("ab\xc3\xa4\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE);
1.285 + TEST_MATCH("ab\xc3\xa4\xc3\xb6", "abao", FALSE);
1.286 + TEST_MATCH("ab?\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE);
1.287 + TEST_MATCH("ab?\xc3\xb6", "abao", FALSE);
1.288 + TEST_MATCH("ab\xc3\xa4?", "ab\xc3\xa4\xc3\xb6", TRUE);
1.289 + TEST_MATCH("ab\xc3\xa4?", "abao", FALSE);
1.290 + TEST_MATCH("ab??", "ab\xc3\xa4\xc3\xb6", TRUE);
1.291 + TEST_MATCH("ab*", "ab\xc3\xa4\xc3\xb6", TRUE);
1.292 + TEST_MATCH("ab*\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE);
1.293 + TEST_MATCH("ab*\xc3\xb6", "aba\xc3\xb6x\xc3\xb6", TRUE);
1.294 + TEST_MATCH("", "abc", FALSE);
1.295 +
1.296 + TEST_MATCH("", "", TRUE);
1.297 + TEST_MATCH("abc", "abc", TRUE);
1.298 + TEST_MATCH("*fo1*bar", "yyyfoxfo1bar", TRUE);
1.299 + TEST_MATCH("12*fo1g*bar", "12yyyfoxfo1gbar", TRUE);
1.300 + TEST_MATCH("__________:*fo1g*bar", "__________:yyyfoxfo1gbar", TRUE);
1.301 + TEST_MATCH("*abc*cde", "abcde", FALSE);
1.302 + TEST_MATCH("*abc*cde", "abccde", TRUE);
1.303 + TEST_MATCH("*abc*cde", "abcxcde", TRUE);
1.304 + TEST_MATCH("*abc*?cde", "abccde", FALSE);
1.305 + TEST_MATCH("*abc*?cde", "abcxcde", TRUE);
1.306 + TEST_MATCH("*abc*def", "abababcdededef", TRUE);
1.307 + TEST_MATCH("*abc*def", "abcbcbcdededef", TRUE);
1.308 + TEST_MATCH("*acbc*def", "acbcbcbcdededef", TRUE);
1.309 + TEST_MATCH("*a?bc*def", "acbcbcbcdededef", TRUE);
1.310 + TEST_MATCH("*abc*def", "bcbcbcdefdef", FALSE);
1.311 + TEST_MATCH("*abc*def*ghi", "abcbcbcbcbcbcdefefdefdefghi", TRUE);
1.312 + TEST_MATCH("*abc*def*ghi", "bcbcbcbcbcbcdefdefdefdefghi", FALSE);
1.313 + TEST_MATCH("_1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_*abc*def*ghi", "_1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_abcbcbcbcbcbcdefefdefdefghi", TRUE);
1.314 + TEST_MATCH("fooooooo*a*bc", "fooooooo_a_bd_a_bc", TRUE);
1.315 +
1.316 + verbose ("\n%u tests passed, %u failed\n", passed, failed);
1.317 +#ifdef __SYMBIAN32__
1.318 + testResultXml("patterntest");
1.319 +#endif /* EMULATOR */
1.320 +
1.321 + return failed;
1.322 +}
1.323 +
1.324 +