os/ossrv/glib/tsrc/BC/tests/shell-test.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200 (2012-06-15)
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /* GLIB - Library of useful routines for C programming
     2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
     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 /*
    21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
    22  * file for a list of people on the GLib Team.  See the ChangeLog
    23  * files for a list of changes.  These files are distributed with
    24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
    25  */
    26 
    27 #undef G_DISABLE_ASSERT
    28 #undef G_LOG_DOMAIN
    29 
    30 #include <glib.h>
    31 #include <stdio.h>
    32 #include <string.h>
    33 #include <stdlib.h>
    34 
    35 #ifdef SYMBIAN
    36 #include "mrt2_glib2_test.h"
    37 #endif /*SYMBIAN*/
    38 
    39 
    40 
    41 typedef struct _TestResult TestResult;
    42 
    43 
    44 
    45 
    46 struct _TestResult
    47 {
    48   gint argc;
    49   const gchar **argv;
    50 };
    51 
    52 static const gchar *
    53 test_command_lines[] =
    54 {
    55   /*  0 */ "foo bar",
    56   /*  1 */ "foo 'bar'",
    57   /*  2 */ "foo \"bar\"",
    58   /*  3 */ "foo '' 'bar'",
    59   /*  4 */ "foo \"bar\"'baz'blah'foo'\\''blah'\"boo\"",
    60   /*  5 */ "foo \t \tblah\tfoo\t\tbar  baz",
    61   /*  6 */ "foo '    spaces more spaces lots of     spaces in this   '  \t",
    62   /*  7 */ "foo \\\nbar",
    63   /*  8 */ "foo '' ''",
    64   /*  9 */ "foo \\\" la la la",
    65   /* 10 */ "foo \\ foo woo woo\\ ",
    66   /* 11 */ "foo \"yada yada \\$\\\"\"",
    67   /* 12 */ "foo \"c:\\\\\"",
    68   NULL
    69 };
    70 
    71 static const gchar *result0[] = { "foo", "bar", NULL };
    72 static const gchar *result1[] = { "foo", "bar", NULL };
    73 static const gchar *result2[] = { "foo", "bar", NULL };
    74 static const gchar *result3[] = { "foo", "", "bar", NULL };
    75 static const gchar *result4[] = { "foo", "barbazblahfoo'blahboo", NULL };
    76 static const gchar *result5[] = { "foo", "blah", "foo", "bar", "baz", NULL };
    77 static const gchar *result6[] = { "foo", "    spaces more spaces lots of     spaces in this   ", NULL };
    78 static const gchar *result7[] = { "foo", "bar", NULL };
    79 static const gchar *result8[] = { "foo", "", "", NULL };
    80 static const gchar *result9[] = { "foo", "\"", "la", "la", "la", NULL };
    81 static const gchar *result10[] = { "foo", " foo", "woo", "woo ", NULL };
    82 static const gchar *result11[] = { "foo", "yada yada $\"", NULL };
    83 static const gchar *result12[] = { "foo", "c:\\", NULL };
    84 
    85 static const TestResult
    86 correct_results[] =
    87 {
    88   { G_N_ELEMENTS (result0) - 1, result0 },
    89   { G_N_ELEMENTS (result1) - 1, result1 },
    90   { G_N_ELEMENTS (result2) - 1, result2 },
    91   { G_N_ELEMENTS (result3) - 1, result3 },
    92   { G_N_ELEMENTS (result4) - 1, result4 },
    93   { G_N_ELEMENTS (result5) - 1, result5 },
    94   { G_N_ELEMENTS (result6) - 1, result6 },
    95   { G_N_ELEMENTS (result7) - 1, result7 },
    96   { G_N_ELEMENTS (result8) - 1, result8 },
    97   { G_N_ELEMENTS (result9) - 1, result9 },
    98   { G_N_ELEMENTS (result10) - 1, result10 },
    99   { G_N_ELEMENTS (result11) - 1, result11 },
   100   { G_N_ELEMENTS (result12) - 1, result12 }
   101 };
   102 
   103 static void
   104 print_test (const gchar *cmdline, gint argc, gchar **argv,
   105             const TestResult *result)
   106 {
   107   gint i;
   108   
   109   g_print("Command line was: '%s'\n", cmdline);
   110 
   111   g_print("Expected result (%d args):\n", result->argc);
   112   
   113   i = 0;
   114   while (result->argv[i])
   115     {
   116       g_print(" %3d '%s'\n", i, result->argv[i]);
   117       ++i;
   118     }  
   119 
   120   g_print("Actual result (%d args):\n", argc);
   121   
   122   i = 0;
   123   while (argv[i])
   124     {
   125       g_print(" %3d '%s'\n", i, argv[i]);
   126       ++i;
   127     }
   128 }
   129 
   130 static void
   131 do_argv_test (const gchar *cmdline, const TestResult *result)
   132 {
   133   gint argc;
   134   gchar **argv;
   135   GError *err;
   136   gint i;
   137 
   138   err = NULL;
   139   if (!g_shell_parse_argv (cmdline, &argc, &argv, &err))
   140     {
   141       g_print("Error parsing command line that should work fine: %s\n",
   142                err->message);
   143       g_assert(FALSE && "shell-test");
   144       exit (1);
   145     }
   146   
   147   if (argc != result->argc)
   148     {
   149       g_print("Expected and actual argc don't match\n");
   150       print_test (cmdline, argc, argv, result);
   151       exit (1);
   152     }
   153 
   154   i = 0;
   155   while (argv[i])
   156     {
   157       if (strcmp (argv[i], result->argv[i]) != 0)
   158         {
   159           g_print("Expected and actual arg %d do not match\n", i);
   160           print_test (cmdline, argc, argv, result);
   161           exit (1);
   162         }
   163       
   164       ++i;
   165     }
   166 
   167   if (argv[i] != NULL)
   168     {
   169       g_print("argv didn't get NULL-terminated\n");
   170       exit (1);
   171     }
   172 }
   173 
   174 static void
   175 run_tests (void)
   176 {
   177   gint i;
   178   
   179   i = 0;
   180   while (test_command_lines[i])
   181     {
   182       do_argv_test (test_command_lines[i], &correct_results[i]);
   183       ++i;
   184     }
   185 }
   186 
   187 static gboolean any_test_failed = FALSE;
   188 
   189 #define CHECK_STRING_RESULT(expression, expected_value) \
   190  check_string_result (#expression, __FILE__, __LINE__, expression, expected_value)
   191 
   192 static void
   193 check_string_result (const char *expression,
   194 		     const char *file_name,
   195 		     int line_number,
   196 		     char *result,
   197 		     const char *expected)
   198 {
   199   gboolean match;
   200   
   201   if (expected == NULL)
   202     match = result == NULL;
   203   else
   204     match = result != NULL && strcmp (result, expected) == 0;
   205   
   206   if (!match)
   207     {
   208       if (!any_test_failed)
   209 	g_print("\n");
   210       
   211       g_print("FAIL: check failed in %s, line %d\n", file_name, line_number);
   212       g_print("evaluated: %s\n", expression);
   213       g_print("expected: %s\n", expected == NULL ? "NULL" : expected);
   214       g_print("got: %s\n", result == NULL ? "NULL" : result);
   215       g_assert(FALSE && "shell-test");
   216       any_test_failed = TRUE;
   217     }
   218   
   219   g_free (result);
   220 }
   221 
   222 static char *
   223 test_shell_unquote (const char *str)
   224 {
   225   char *result;
   226   GError *error;
   227 
   228   error = NULL;
   229   result = g_shell_unquote (str, &error);
   230   if (error == NULL)
   231     return result;
   232 
   233   /* Leaks the error, which is no big deal and easy to fix if we
   234    * decide it matters.
   235    */
   236 
   237   if (error->domain != G_SHELL_ERROR)
   238     return g_strdup ("error in domain other than G_SHELL_ERROR");
   239 
   240   /* It would be nice to check the error message too, but that's
   241    * localized, so it's too much of a pain.
   242    */
   243   switch (error->code)
   244     {
   245     case G_SHELL_ERROR_BAD_QUOTING:
   246       return g_strdup ("G_SHELL_ERROR_BAD_QUOTING");
   247     case G_SHELL_ERROR_EMPTY_STRING:
   248       return g_strdup ("G_SHELL_ERROR_EMPTY_STRING");
   249     case G_SHELL_ERROR_FAILED:
   250       return g_strdup ("G_SHELL_ERROR_FAILED");
   251     default:
   252       return g_strdup ("bad error code in G_SHELL_ERROR domain");
   253     }
   254 }
   255 
   256 int
   257 main (int   argc,
   258       char *argv[])
   259 {
   260   #ifdef SYMBIAN
   261   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);
   262   g_set_print_handler(mrtPrintHandler);
   263   #endif /*SYMBIAN*/
   264 	  
   265 
   266   run_tests ();
   267   
   268   CHECK_STRING_RESULT (g_shell_quote (""), "''");
   269   CHECK_STRING_RESULT (g_shell_quote ("a"), "'a'");
   270   CHECK_STRING_RESULT (g_shell_quote ("("), "'('");
   271   CHECK_STRING_RESULT (g_shell_quote ("'"), "''\\'''");
   272   CHECK_STRING_RESULT (g_shell_quote ("'a"), "''\\''a'");
   273   CHECK_STRING_RESULT (g_shell_quote ("a'"), "'a'\\'''");
   274   CHECK_STRING_RESULT (g_shell_quote ("a'a"), "'a'\\''a'");
   275   
   276   CHECK_STRING_RESULT (test_shell_unquote (""), "");
   277   CHECK_STRING_RESULT (test_shell_unquote ("a"), "a");
   278   CHECK_STRING_RESULT (test_shell_unquote ("'a'"), "a");
   279   CHECK_STRING_RESULT (test_shell_unquote ("'('"), "(");
   280   CHECK_STRING_RESULT (test_shell_unquote ("''\\'''"), "'");
   281   CHECK_STRING_RESULT (test_shell_unquote ("''\\''a'"), "'a");
   282   CHECK_STRING_RESULT (test_shell_unquote ("'a'\\'''"), "a'");
   283   CHECK_STRING_RESULT (test_shell_unquote ("'a'\\''a'"), "a'a");
   284 
   285   CHECK_STRING_RESULT (test_shell_unquote ("\\\\"), "\\");
   286   CHECK_STRING_RESULT (test_shell_unquote ("\\\n"), "");
   287 
   288   CHECK_STRING_RESULT (test_shell_unquote ("'\\''"), "G_SHELL_ERROR_BAD_QUOTING");
   289 
   290 #if defined (_MSC_VER) && (_MSC_VER <= 1200)
   291   /* using \x22 instead of \" to work around a msvc 5.0, 6.0 compiler bug */
   292   CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\x22\""), "\"");
   293 #else
   294   CHECK_STRING_RESULT (test_shell_unquote ("\"\\\"\""), "\"");
   295 #endif
   296 
   297   CHECK_STRING_RESULT (test_shell_unquote ("\""), "G_SHELL_ERROR_BAD_QUOTING");
   298   CHECK_STRING_RESULT (test_shell_unquote ("'"), "G_SHELL_ERROR_BAD_QUOTING");
   299 
   300   CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\\\""), "\\");
   301   CHECK_STRING_RESULT (test_shell_unquote ("\x22\\`\""), "`");
   302   CHECK_STRING_RESULT (test_shell_unquote ("\x22\\$\""), "$");
   303   CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\n\""), "\n");
   304 
   305   CHECK_STRING_RESULT (test_shell_unquote ("\"\\'\""), "\\'");
   306   CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\r\""), "\\\r");
   307   CHECK_STRING_RESULT (test_shell_unquote ("\x22\\n\""), "\\n");
   308 
   309 #ifdef SYMBIAN
   310   testResultXml("shell-test");
   311 #endif /* EMULATOR */	
   312   return any_test_failed ? 1 : 0;
   313 }