sl@0: /* GLIB - Library of useful routines for C programming sl@0: * Copyright (C) 2001 Matthias Clasen sl@0: * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. sl@0: * This library is free software; you can redistribute it and/or sl@0: * modify it under the terms of the GNU Lesser General Public sl@0: * License as published by the Free Software Foundation; either sl@0: * version 2 of the License, or (at your option) any later version. sl@0: * sl@0: * This library is distributed in the hope that it will be useful, sl@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of sl@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU sl@0: * Lesser General Public License for more details. sl@0: * sl@0: * You should have received a copy of the GNU Lesser General Public sl@0: * License along with this library; if not, write to the sl@0: * Free Software Foundation, Inc., 59 Temple Place - Suite 330, sl@0: * Boston, MA 02111-1307, USA. sl@0: */ sl@0: sl@0: #undef G_DISABLE_ASSERT sl@0: #undef G_LOG_DOMAIN sl@0: sl@0: #include sl@0: #include sl@0: #ifdef __SYMBIAN32__ sl@0: #include "mrt2_glib2_test.h" sl@0: #endif /*__SYMBIAN32__*/ sl@0: sl@0: sl@0: static gboolean noisy = FALSE; sl@0: sl@0: static void sl@0: verbose (const gchar *format, ...) sl@0: { sl@0: gchar *msg; sl@0: va_list args; sl@0: sl@0: va_start (args, format); sl@0: msg = g_strdup_vprintf (format, args); sl@0: va_end (args); sl@0: sl@0: if (noisy) sl@0: g_print (msg); sl@0: g_free (msg); sl@0: } sl@0: sl@0: /* keep enum and structure of gpattern.c and patterntest.c in sync */ sl@0: typedef enum sl@0: { sl@0: G_MATCH_ALL, /* "*A?A*" */ sl@0: G_MATCH_ALL_TAIL, /* "*A?AA" */ sl@0: G_MATCH_HEAD, /* "AAAA*" */ sl@0: G_MATCH_TAIL, /* "*AAAA" */ sl@0: G_MATCH_EXACT, /* "AAAAA" */ sl@0: G_MATCH_LAST sl@0: } GMatchType; sl@0: sl@0: struct _GPatternSpec sl@0: { sl@0: GMatchType match_type; sl@0: guint pattern_length; sl@0: guint min_length; sl@0: guint max_length; sl@0: gchar *pattern; sl@0: }; sl@0: sl@0: sl@0: static gchar * sl@0: match_type_name (GMatchType match_type) sl@0: { sl@0: switch (match_type) sl@0: { sl@0: case G_MATCH_ALL: sl@0: return "G_MATCH_ALL"; sl@0: break; sl@0: case G_MATCH_ALL_TAIL: sl@0: return "G_MATCH_ALL_TAIL"; sl@0: break; sl@0: case G_MATCH_HEAD: sl@0: return "G_MATCH_HEAD"; sl@0: break; sl@0: case G_MATCH_TAIL: sl@0: return "G_MATCH_TAIL"; sl@0: break; sl@0: case G_MATCH_EXACT: sl@0: return "G_MATCH_EXACT"; sl@0: break; sl@0: default: sl@0: return "unknown GMatchType"; sl@0: break; sl@0: } sl@0: } sl@0: sl@0: static gboolean sl@0: test_compilation (gchar *src, sl@0: GMatchType match_type, sl@0: gchar *pattern, sl@0: guint min) sl@0: { sl@0: GPatternSpec *spec; sl@0: sl@0: verbose ("compiling \"%s\" \t", src); sl@0: spec = g_pattern_spec_new (src); sl@0: sl@0: if (spec->match_type != match_type) sl@0: { sl@0: g_print ("failed \t(match_type: %s, expected %s)\n", sl@0: match_type_name (spec->match_type), sl@0: match_type_name (match_type)); sl@0: g_pattern_spec_free (spec); sl@0: return FALSE; sl@0: } sl@0: sl@0: if (strcmp (spec->pattern, pattern) != 0) sl@0: { sl@0: g_print ("failed \t(pattern: \"%s\", expected \"%s\")\n", sl@0: spec->pattern, sl@0: pattern); sl@0: g_pattern_spec_free (spec); sl@0: return FALSE; sl@0: } sl@0: sl@0: if (spec->pattern_length != strlen (spec->pattern)) sl@0: { sl@0: g_print ("failed \t(pattern_length: %d, expected %d)\n", sl@0: spec->pattern_length, sl@0: (gint)strlen (spec->pattern)); sl@0: g_pattern_spec_free (spec); sl@0: return FALSE; sl@0: } sl@0: sl@0: if (spec->min_length != min) sl@0: { sl@0: g_print ("failed \t(min_length: %d, expected %d)\n", sl@0: spec->min_length, sl@0: min); sl@0: g_pattern_spec_free (spec); sl@0: return FALSE; sl@0: } sl@0: sl@0: verbose ("passed (%s: \"%s\")\n", sl@0: match_type_name (spec->match_type), sl@0: spec->pattern); sl@0: sl@0: g_pattern_spec_free (spec); sl@0: sl@0: return TRUE; sl@0: } sl@0: sl@0: static gboolean sl@0: test_match (gchar *pattern, sl@0: gchar *string, sl@0: gboolean match) sl@0: { sl@0: verbose ("matching \"%s\" against \"%s\" \t", string, pattern); sl@0: sl@0: if (g_pattern_match_simple (pattern, string) != match) sl@0: { sl@0: g_print ("failed \t(unexpected %s)\n", (match ? "mismatch" : "match")); sl@0: return FALSE; sl@0: } sl@0: sl@0: verbose ("passed (%s)\n", match ? "match" : "nomatch"); sl@0: sl@0: return TRUE; sl@0: } sl@0: sl@0: static gboolean sl@0: test_equal (gchar *pattern1, sl@0: gchar *pattern2, sl@0: gboolean expected) sl@0: { sl@0: GPatternSpec *p1 = g_pattern_spec_new (pattern1); sl@0: GPatternSpec *p2 = g_pattern_spec_new (pattern2); sl@0: gboolean equal = g_pattern_spec_equal (p1, p2); sl@0: sl@0: verbose ("comparing \"%s\" with \"%s\" \t", pattern1, pattern2); sl@0: sl@0: if (expected != equal) sl@0: { sl@0: g_print ("failed \t{%s, %u, \"%s\"} %s {%s, %u, \"%s\"}\n", sl@0: match_type_name (p1->match_type), p1->pattern_length, p1->pattern, sl@0: expected ? "!=" : "==", sl@0: match_type_name (p2->match_type), p2->pattern_length, p2->pattern); sl@0: } sl@0: else sl@0: verbose ("passed (%s)\n", equal ? "equal" : "unequal"); sl@0: sl@0: g_pattern_spec_free (p1); sl@0: g_pattern_spec_free (p2); sl@0: sl@0: return expected == equal; sl@0: } sl@0: sl@0: #define TEST_COMPILATION(src, type, pattern, min) { \ sl@0: total++; \ sl@0: if (test_compilation (src, type, pattern, min)) \ sl@0: passed++; \ sl@0: else \ sl@0: failed++; \ sl@0: } sl@0: sl@0: #define TEST_MATCH(pattern, string, match) { \ sl@0: total++; \ sl@0: if (test_match (pattern, string, match)) \ sl@0: passed++; \ sl@0: else \ sl@0: failed++; \ sl@0: } sl@0: sl@0: #define TEST_EQUAL(pattern1, pattern2, match) { \ sl@0: total++; \ sl@0: if (test_equal (pattern1, pattern2, match)) \ sl@0: passed++; \ sl@0: else \ sl@0: failed++; \ sl@0: } sl@0: sl@0: int sl@0: main (int argc, char** argv) sl@0: { sl@0: gint total = 0; sl@0: gint passed = 0; sl@0: gint failed = 0; sl@0: gint i; sl@0: sl@0: #ifdef __SYMBIAN32__ sl@0: 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: g_set_print_handler(mrtPrintHandler); sl@0: #endif /*__SYMBIAN32__*/ sl@0: sl@0: sl@0: for (i = 1; i < argc; i++) sl@0: if (strcmp ("--noisy", argv[i]) == 0) sl@0: noisy = TRUE; sl@0: sl@0: TEST_COMPILATION("*A?B*", G_MATCH_ALL, "*A?B*", 3); sl@0: TEST_COMPILATION("ABC*DEFGH", G_MATCH_ALL_TAIL, "HGFED*CBA", 8); sl@0: TEST_COMPILATION("ABCDEF*GH", G_MATCH_ALL, "ABCDEF*GH", 8); sl@0: TEST_COMPILATION("ABC**?***??**DEF*GH", G_MATCH_ALL, "ABC*???DEF*GH", 11); sl@0: TEST_COMPILATION("*A?AA", G_MATCH_ALL_TAIL, "AA?A*", 4); sl@0: TEST_COMPILATION("ABCD*", G_MATCH_HEAD, "ABCD", 4); sl@0: TEST_COMPILATION("*ABCD", G_MATCH_TAIL, "ABCD", 4); sl@0: TEST_COMPILATION("ABCDE", G_MATCH_EXACT, "ABCDE", 5); sl@0: TEST_COMPILATION("A?C?E", G_MATCH_ALL, "A?C?E", 5); sl@0: TEST_COMPILATION("*?x", G_MATCH_ALL_TAIL, "x?*", 2); sl@0: TEST_COMPILATION("?*x", G_MATCH_ALL_TAIL, "x?*", 2); sl@0: TEST_COMPILATION("*?*x", G_MATCH_ALL_TAIL, "x?*", 2); sl@0: TEST_COMPILATION("x*??", G_MATCH_ALL_TAIL, "??*x", 3); sl@0: sl@0: TEST_EQUAL("*A?B*", "*A?B*", TRUE); sl@0: TEST_EQUAL("A*BCD", "A*BCD", TRUE); sl@0: TEST_EQUAL("ABCD*", "ABCD****", TRUE); sl@0: TEST_EQUAL("A1*", "A1*", TRUE); sl@0: TEST_EQUAL("*YZ", "*YZ", TRUE); sl@0: TEST_EQUAL("A1x", "A1x", TRUE); sl@0: TEST_EQUAL("AB*CD", "AB**CD", TRUE); sl@0: TEST_EQUAL("AB*?*CD", "AB*?CD", TRUE); sl@0: TEST_EQUAL("AB*?CD", "AB?*CD", TRUE); sl@0: TEST_EQUAL("AB*CD", "AB*?*CD", FALSE); sl@0: TEST_EQUAL("ABC*", "ABC?", FALSE); sl@0: sl@0: TEST_MATCH("*x", "x", TRUE); sl@0: TEST_MATCH("*x", "xx", TRUE); sl@0: TEST_MATCH("*x", "yyyx", TRUE); sl@0: TEST_MATCH("*x", "yyxy", FALSE); sl@0: TEST_MATCH("?x", "x", FALSE); sl@0: TEST_MATCH("?x", "xx", TRUE); sl@0: TEST_MATCH("?x", "yyyx", FALSE); sl@0: TEST_MATCH("?x", "yyxy", FALSE); sl@0: TEST_MATCH("*?x", "xx", TRUE); sl@0: TEST_MATCH("?*x", "xx", TRUE); sl@0: TEST_MATCH("*?x", "x", FALSE); sl@0: TEST_MATCH("?*x", "x", FALSE); sl@0: TEST_MATCH("*?*x", "yx", TRUE); sl@0: TEST_MATCH("*?*x", "xxxx", TRUE); sl@0: TEST_MATCH("x*??", "xyzw", TRUE); sl@0: TEST_MATCH("*x", "\xc3\x84x", TRUE); sl@0: TEST_MATCH("?x", "\xc3\x84x", TRUE); sl@0: TEST_MATCH("??x", "\xc3\x84x", FALSE); sl@0: TEST_MATCH("ab\xc3\xa4\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE); sl@0: TEST_MATCH("ab\xc3\xa4\xc3\xb6", "abao", FALSE); sl@0: TEST_MATCH("ab?\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE); sl@0: TEST_MATCH("ab?\xc3\xb6", "abao", FALSE); sl@0: TEST_MATCH("ab\xc3\xa4?", "ab\xc3\xa4\xc3\xb6", TRUE); sl@0: TEST_MATCH("ab\xc3\xa4?", "abao", FALSE); sl@0: TEST_MATCH("ab??", "ab\xc3\xa4\xc3\xb6", TRUE); sl@0: TEST_MATCH("ab*", "ab\xc3\xa4\xc3\xb6", TRUE); sl@0: TEST_MATCH("ab*\xc3\xb6", "ab\xc3\xa4\xc3\xb6", TRUE); sl@0: TEST_MATCH("ab*\xc3\xb6", "aba\xc3\xb6x\xc3\xb6", TRUE); sl@0: TEST_MATCH("", "abc", FALSE); sl@0: sl@0: TEST_MATCH("", "", TRUE); sl@0: TEST_MATCH("abc", "abc", TRUE); sl@0: TEST_MATCH("*fo1*bar", "yyyfoxfo1bar", TRUE); sl@0: TEST_MATCH("12*fo1g*bar", "12yyyfoxfo1gbar", TRUE); sl@0: TEST_MATCH("__________:*fo1g*bar", "__________:yyyfoxfo1gbar", TRUE); sl@0: TEST_MATCH("*abc*cde", "abcde", FALSE); sl@0: TEST_MATCH("*abc*cde", "abccde", TRUE); sl@0: TEST_MATCH("*abc*cde", "abcxcde", TRUE); sl@0: TEST_MATCH("*abc*?cde", "abccde", FALSE); sl@0: TEST_MATCH("*abc*?cde", "abcxcde", TRUE); sl@0: TEST_MATCH("*abc*def", "abababcdededef", TRUE); sl@0: TEST_MATCH("*abc*def", "abcbcbcdededef", TRUE); sl@0: TEST_MATCH("*acbc*def", "acbcbcbcdededef", TRUE); sl@0: TEST_MATCH("*a?bc*def", "acbcbcbcdededef", TRUE); sl@0: TEST_MATCH("*abc*def", "bcbcbcdefdef", FALSE); sl@0: TEST_MATCH("*abc*def*ghi", "abcbcbcbcbcbcdefefdefdefghi", TRUE); sl@0: TEST_MATCH("*abc*def*ghi", "bcbcbcbcbcbcdefdefdefdefghi", FALSE); sl@0: 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); sl@0: TEST_MATCH("fooooooo*a*bc", "fooooooo_a_bd_a_bc", TRUE); sl@0: sl@0: verbose ("\n%u tests passed, %u failed\n", passed, failed); sl@0: #ifdef __SYMBIAN32__ sl@0: testResultXml("patterntest"); sl@0: #endif /* EMULATOR */ sl@0: sl@0: return failed; sl@0: } sl@0: sl@0: