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