Move checksum tests to the test framework
[platform/upstream/glib.git] / tests / shell-test.c
index 4787733..437f20e 100644 (file)
@@ -56,6 +56,7 @@ test_command_lines[] =
   /*  9 */ "foo \\\" la la la",
   /* 10 */ "foo \\ foo woo woo\\ ",
   /* 11 */ "foo \"yada yada \\$\\\"\"",
+  /* 12 */ "foo \"c:\\\\\"",
   NULL
 };
 
@@ -71,6 +72,7 @@ static const gchar *result8[] = { "foo", "", "", NULL };
 static const gchar *result9[] = { "foo", "\"", "la", "la", "la", NULL };
 static const gchar *result10[] = { "foo", " foo", "woo", "woo ", NULL };
 static const gchar *result11[] = { "foo", "yada yada $\"", NULL };
+static const gchar *result12[] = { "foo", "c:\\", NULL };
 
 static const TestResult
 correct_results[] =
@@ -86,7 +88,8 @@ correct_results[] =
   { G_N_ELEMENTS (result8) - 1, result8 },
   { G_N_ELEMENTS (result9) - 1, result9 },
   { G_N_ELEMENTS (result10) - 1, result10 },
-  { G_N_ELEMENTS (result11) - 1, result11 }
+  { G_N_ELEMENTS (result11) - 1, result11 },
+  { G_N_ELEMENTS (result12) - 1, result12 }
 };
 
 static void
@@ -95,25 +98,23 @@ print_test (const gchar *cmdline, gint argc, gchar **argv,
 {
   gint i;
   
-  printf ("\nCommand line was: '%s'\n", cmdline);
+  fprintf (stderr, "Command line was: '%s'\n", cmdline);
 
-  printf ("Expected result (%d args):\n", result->argc);
+  fprintf (stderr, "Expected result (%d args):\n", result->argc);
   
   i = 0;
   while (result->argv[i])
     {
-      printf (" %3d '%s'\n", i, result->argv[i]);
-
+      fprintf (stderr, " %3d '%s'\n", i, result->argv[i]);
       ++i;
     }  
 
-  printf ("Actual result (%d args):\n", argc);
+  fprintf (stderr, "Actual result (%d args):\n", argc);
   
   i = 0;
   while (argv[i])
     {
-      printf (" %3d '%s'\n", i, argv[i]);
-
+      fprintf (stderr, " %3d '%s'\n", i, argv[i]);
       ++i;
     }
 }
@@ -160,6 +161,7 @@ do_argv_test (const gchar *cmdline, const TestResult *result)
       fprintf (stderr, "argv didn't get NULL-terminated\n");
       exit (1);
     }
+  g_strfreev (argv);
 }
 
 static void
@@ -170,10 +172,7 @@ run_tests (void)
   i = 0;
   while (test_command_lines[i])
     {
-      printf ("g_shell_parse_argv() test %d - ", i);
       do_argv_test (test_command_lines[i], &correct_results[i]);
-      printf ("ok (%s)\n", test_command_lines[i]);
-      
       ++i;
     }
 }
@@ -190,27 +189,27 @@ check_string_result (const char *expression,
                     char *result,
                     const char *expected)
 {
-       gboolean match;
-       
-       if (expected == NULL)
-         match = result == NULL;
-       else
-         match = result != NULL && strcmp (result, expected) == 0;
-
-       if (!match)
-         {
-           if (!any_test_failed)
-             fprintf (stderr, "\n");
-         
-           fprintf (stderr, "FAIL: check failed in %s, line %d\n", file_name, line_number);
-           fprintf (stderr, "      evaluated: %s\n", expression);
-           fprintf (stderr, "       expected: %s\n", expected == NULL ? "NULL" : expected);
-           fprintf (stderr, "            got: %s\n", result == NULL ? "NULL" : result);
-           
-           any_test_failed = TRUE;
-         }
-
-       g_free (result);
+  gboolean match;
+  
+  if (expected == NULL)
+    match = result == NULL;
+  else
+    match = result != NULL && strcmp (result, expected) == 0;
+  
+  if (!match)
+    {
+      if (!any_test_failed)
+       fprintf (stderr, "\n");
+      
+      fprintf (stderr, "FAIL: check failed in %s, line %d\n", file_name, line_number);
+      fprintf (stderr, "      evaluated: %s\n", expression);
+      fprintf (stderr, "       expected: %s\n", expected == NULL ? "NULL" : expected);
+      fprintf (stderr, "            got: %s\n", result == NULL ? "NULL" : result);
+      
+      any_test_failed = TRUE;
+    }
+  
+  g_free (result);
 }
 
 static char *
@@ -224,7 +223,9 @@ test_shell_unquote (const char *str)
   if (error == NULL)
     return result;
 
-  /* Leaks the error, which is no big deal. */
+  /* Leaks the error, which is no big deal and easy to fix if we
+   * decide it matters.
+   */
 
   if (error->domain != G_SHELL_ERROR)
     return g_strdup ("error in domain other than G_SHELL_ERROR");
@@ -272,19 +273,25 @@ main (int   argc,
   CHECK_STRING_RESULT (test_shell_unquote ("\\\n"), "");
 
   CHECK_STRING_RESULT (test_shell_unquote ("'\\''"), "G_SHELL_ERROR_BAD_QUOTING");
+
+#if defined (_MSC_VER) && (_MSC_VER <= 1200)
+  /* using \x22 instead of \" to work around a msvc 5.0, 6.0 compiler bug */
+  CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\x22\""), "\"");
+#else
   CHECK_STRING_RESULT (test_shell_unquote ("\"\\\"\""), "\"");
+#endif
 
   CHECK_STRING_RESULT (test_shell_unquote ("\""), "G_SHELL_ERROR_BAD_QUOTING");
   CHECK_STRING_RESULT (test_shell_unquote ("'"), "G_SHELL_ERROR_BAD_QUOTING");
 
-  CHECK_STRING_RESULT (test_shell_unquote ("\"\\\\\""), "\\");
-  CHECK_STRING_RESULT (test_shell_unquote ("\"\\`\""), "`");
-  CHECK_STRING_RESULT (test_shell_unquote ("\"\\$\""), "$");
-  CHECK_STRING_RESULT (test_shell_unquote ("\"\\\n\""), "\n");
+  CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\\\""), "\\");
+  CHECK_STRING_RESULT (test_shell_unquote ("\x22\\`\""), "`");
+  CHECK_STRING_RESULT (test_shell_unquote ("\x22\\$\""), "$");
+  CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\n\""), "\n");
 
   CHECK_STRING_RESULT (test_shell_unquote ("\"\\'\""), "\\'");
-  CHECK_STRING_RESULT (test_shell_unquote ("\"\\\r\""), "\\\r");
-  CHECK_STRING_RESULT (test_shell_unquote ("\"\\n\""), "\\n");
+  CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\r\""), "\\\r");
+  CHECK_STRING_RESULT (test_shell_unquote ("\x22\\n\""), "\\n");
 
   return any_test_failed ? 1 : 0;
 }