Fix warnings.
[platform/upstream/glib.git] / tests / shell-test.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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_LOG_DOMAIN
28
29 #include <glib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33
34
35 typedef struct _TestResult TestResult;
36
37 struct _TestResult
38 {
39   gint argc;
40   const gchar **argv;
41 };
42
43 static const gchar *
44 test_command_lines[] =
45 {
46   /*  0 */ "foo bar",
47   /*  1 */ "foo 'bar'",
48   /*  2 */ "foo \"bar\"",
49   /*  3 */ "foo '' 'bar'",
50   /*  4 */ "foo \"bar\"'baz'blah'foo'\\''blah'\"boo\"",
51   /*  5 */ "foo \t \tblah\tfoo\t\tbar  baz",
52   /*  6 */ "foo '    spaces more spaces lots of     spaces in this   '  \t",
53   /*  7 */ "foo \\\nbar",
54   /*  8 */ "foo '' ''",
55   /*  9 */ "foo \\\" la la la",
56   /* 10 */ "foo \\ foo woo woo\\ ",
57   /* 11 */ "foo \"yada yada \\$\\\"\"",
58   NULL
59 };
60
61 static const gchar *result0[] = { "foo", "bar", NULL };
62 static const gchar *result1[] = { "foo", "bar", NULL };
63 static const gchar *result2[] = { "foo", "bar", NULL };
64 static const gchar *result3[] = { "foo", "", "bar", NULL };
65 static const gchar *result4[] = { "foo", "barbazblahfoo'blahboo", NULL };
66 static const gchar *result5[] = { "foo", "blah", "foo", "bar", "baz", NULL };
67 static const gchar *result6[] = { "foo", "    spaces more spaces lots of     spaces in this   ", NULL };
68 static const gchar *result7[] = { "foo", "bar", NULL };
69 static const gchar *result8[] = { "foo", "", "", NULL };
70 static const gchar *result9[] = { "foo", "\"", "la", "la", "la", NULL };
71 static const gchar *result10[] = { "foo", " foo", "woo", "woo ", NULL };
72 static const gchar *result11[] = { "foo", "yada yada $\"", NULL };
73
74 static const TestResult
75 correct_results[] =
76 {
77   { G_N_ELEMENTS (result0) - 1, result0 },
78   { G_N_ELEMENTS (result1) - 1, result1 },
79   { G_N_ELEMENTS (result2) - 1, result2 },
80   { G_N_ELEMENTS (result3) - 1, result3 },
81   { G_N_ELEMENTS (result4) - 1, result4 },
82   { G_N_ELEMENTS (result5) - 1, result5 },
83   { G_N_ELEMENTS (result6) - 1, result6 },
84   { G_N_ELEMENTS (result7) - 1, result7 },
85   { G_N_ELEMENTS (result8) - 1, result8 },
86   { G_N_ELEMENTS (result9) - 1, result9 },
87   { G_N_ELEMENTS (result10) - 1, result10 },
88   { G_N_ELEMENTS (result11) - 1, result11 }
89 };
90
91 static void
92 print_test (const gchar *cmdline, gint argc, gchar **argv,
93             const TestResult *result)
94 {
95   gint i;
96   
97   printf ("\nCommand line was: '%s'\n", cmdline);
98
99   printf ("Expected result (%d args):\n", result->argc);
100   
101   i = 0;
102   while (result->argv[i])
103     {
104       printf (" %3d '%s'\n", i, result->argv[i]);
105
106       ++i;
107     }  
108
109   printf ("Actual result (%d args):\n", argc);
110   
111   i = 0;
112   while (argv[i])
113     {
114       printf (" %3d '%s'\n", i, argv[i]);
115
116       ++i;
117     }
118 }
119
120 static void
121 do_argv_test (const gchar *cmdline, const TestResult *result)
122 {
123   gint argc;
124   gchar **argv;
125   GError *err;
126   gint i;
127
128   err = NULL;
129   if (!g_shell_parse_argv (cmdline, &argc, &argv, &err))
130     {
131       fprintf (stderr, "Error parsing command line that should work fine: %s\n",
132                err->message);
133       
134       exit (1);
135     }
136   
137   if (argc != result->argc)
138     {
139       fprintf (stderr, "Expected and actual argc don't match\n");
140       print_test (cmdline, argc, argv, result);
141       exit (1);
142     }
143
144   i = 0;
145   while (argv[i])
146     {
147       if (strcmp (argv[i], result->argv[i]) != 0)
148         {
149           fprintf (stderr, "Expected and actual arg %d do not match\n", i);
150           print_test (cmdline, argc, argv, result);
151           exit (1);
152         }
153       
154       ++i;
155     }
156
157   if (argv[i] != NULL)
158     {
159       fprintf (stderr, "argv didn't get NULL-terminated\n");
160       exit (1);
161     }
162 }
163
164 static void
165 run_tests (void)
166 {
167   gint i;
168   
169   i = 0;
170   while (test_command_lines[i])
171     {
172       printf ("g_shell_parse_argv() test %d - ", i);
173       do_argv_test (test_command_lines[i], &correct_results[i]);
174       printf ("ok (%s)\n", test_command_lines[i]);
175       
176       ++i;
177     }
178 }
179
180 int
181 main (int   argc,
182       char *argv[])
183 {
184   run_tests ();
185   
186   return 0;
187 }
188
189