1// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include <stdlib.h>
29
30#include "src/v8.h"
31#include "test/cctest/cctest.h"
32
33using namespace v8::internal;
34
35// This test must be executed first!
36TEST(Default) {
37  CHECK(FLAG_testing_bool_flag);
38  CHECK_EQ(13, FLAG_testing_int_flag);
39  CHECK_EQ(2.5, FLAG_testing_float_flag);
40  CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "Hello, world!"));
41}
42
43
44static void SetFlagsToDefault() {
45  FlagList::ResetAllFlags();
46  TestDefault();
47}
48
49
50TEST(Flags1) {
51  FlagList::PrintHelp();
52}
53
54
55TEST(Flags2) {
56  SetFlagsToDefault();
57  int argc = 8;
58  const char* argv[] = { "Test2", "-notesting-bool-flag",
59                         "--notesting-maybe-bool-flag", "notaflag",
60                         "--testing_int_flag=77", "-testing_float_flag=.25",
61                         "--testing_string_flag", "no way!" };
62  CHECK_EQ(0, FlagList::SetFlagsFromCommandLine(&argc,
63                                                const_cast<char **>(argv),
64                                                false));
65  CHECK_EQ(8, argc);
66  CHECK(!FLAG_testing_bool_flag);
67  CHECK(FLAG_testing_maybe_bool_flag.has_value);
68  CHECK(!FLAG_testing_maybe_bool_flag.value);
69  CHECK_EQ(77, FLAG_testing_int_flag);
70  CHECK_EQ(.25, FLAG_testing_float_flag);
71  CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "no way!"));
72}
73
74
75TEST(Flags2b) {
76  SetFlagsToDefault();
77  const char* str =
78      " -notesting-bool-flag notaflag   --testing_int_flag=77 "
79      "-notesting-maybe-bool-flag   "
80      "-testing_float_flag=.25  "
81      "--testing_string_flag   no_way!  ";
82  CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
83  CHECK(!FLAG_testing_bool_flag);
84  CHECK(FLAG_testing_maybe_bool_flag.has_value);
85  CHECK(!FLAG_testing_maybe_bool_flag.value);
86  CHECK_EQ(77, FLAG_testing_int_flag);
87  CHECK_EQ(.25, FLAG_testing_float_flag);
88  CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "no_way!"));
89}
90
91
92TEST(Flags3) {
93  SetFlagsToDefault();
94  int argc = 9;
95  const char* argv[] =
96      { "Test3", "--testing_bool_flag", "--testing-maybe-bool-flag", "notaflag",
97        "--testing_int_flag", "-666",
98        "--testing_float_flag", "-12E10", "-testing-string-flag=foo-bar" };
99  CHECK_EQ(0, FlagList::SetFlagsFromCommandLine(&argc,
100                                                const_cast<char **>(argv),
101                                                true));
102  CHECK_EQ(2, argc);
103  CHECK(FLAG_testing_bool_flag);
104  CHECK(FLAG_testing_maybe_bool_flag.has_value);
105  CHECK(FLAG_testing_maybe_bool_flag.value);
106  CHECK_EQ(-666, FLAG_testing_int_flag);
107  CHECK_EQ(-12E10, FLAG_testing_float_flag);
108  CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "foo-bar"));
109}
110
111
112TEST(Flags3b) {
113  SetFlagsToDefault();
114  const char* str =
115      "--testing_bool_flag --testing-maybe-bool-flag notaflag "
116      "--testing_int_flag -666 "
117      "--testing_float_flag -12E10 "
118      "-testing-string-flag=foo-bar";
119  CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
120  CHECK(FLAG_testing_bool_flag);
121  CHECK(FLAG_testing_maybe_bool_flag.has_value);
122  CHECK(FLAG_testing_maybe_bool_flag.value);
123  CHECK_EQ(-666, FLAG_testing_int_flag);
124  CHECK_EQ(-12E10, FLAG_testing_float_flag);
125  CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "foo-bar"));
126}
127
128
129TEST(Flags4) {
130  SetFlagsToDefault();
131  int argc = 3;
132  const char* argv[] = { "Test4", "--testing_bool_flag", "--foo" };
133  CHECK_EQ(0, FlagList::SetFlagsFromCommandLine(&argc,
134                                                const_cast<char **>(argv),
135                                                true));
136  CHECK_EQ(2, argc);
137  CHECK(!FLAG_testing_maybe_bool_flag.has_value);
138}
139
140
141TEST(Flags4b) {
142  SetFlagsToDefault();
143  const char* str = "--testing_bool_flag --foo";
144  CHECK_EQ(2, FlagList::SetFlagsFromString(str, StrLength(str)));
145  CHECK(!FLAG_testing_maybe_bool_flag.has_value);
146}
147
148
149TEST(Flags5) {
150  SetFlagsToDefault();
151  int argc = 2;
152  const char* argv[] = { "Test5", "--testing_int_flag=\"foobar\"" };
153  CHECK_EQ(1, FlagList::SetFlagsFromCommandLine(&argc,
154                                                const_cast<char **>(argv),
155                                                true));
156  CHECK_EQ(2, argc);
157}
158
159
160TEST(Flags5b) {
161  SetFlagsToDefault();
162  const char* str = "                     --testing_int_flag=\"foobar\"";
163  CHECK_EQ(1, FlagList::SetFlagsFromString(str, StrLength(str)));
164}
165
166
167TEST(Flags6) {
168  SetFlagsToDefault();
169  int argc = 4;
170  const char* argv[] = { "Test5", "--testing-int-flag", "0",
171                         "--testing_float_flag" };
172  CHECK_EQ(3, FlagList::SetFlagsFromCommandLine(&argc,
173                                                const_cast<char **>(argv),
174                                                true));
175  CHECK_EQ(2, argc);
176}
177
178
179TEST(Flags6b) {
180  SetFlagsToDefault();
181  const char* str = "       --testing-int-flag 0      --testing_float_flag    ";
182  CHECK_EQ(3, FlagList::SetFlagsFromString(str, StrLength(str)));
183}
184
185
186TEST(FlagsJSArguments1) {
187  SetFlagsToDefault();
188  int argc = 6;
189  const char* argv[] = {"TestJSArgs1",
190                        "--testing-int-flag", "42",
191                        "--", "testing-float-flag", "7"};
192  CHECK_EQ(0, FlagList::SetFlagsFromCommandLine(&argc,
193                                                const_cast<char **>(argv),
194                                                true));
195  CHECK_EQ(42, FLAG_testing_int_flag);
196  CHECK_EQ(2.5, FLAG_testing_float_flag);
197  CHECK_EQ(2, FLAG_js_arguments.argc);
198  CHECK_EQ(0, strcmp(FLAG_js_arguments[0], "testing-float-flag"));
199  CHECK_EQ(0, strcmp(FLAG_js_arguments[1], "7"));
200  CHECK_EQ(1, argc);
201}
202
203
204TEST(FlagsJSArguments1b) {
205  SetFlagsToDefault();
206  const char* str = "--testing-int-flag 42 -- testing-float-flag 7";
207  CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
208  CHECK_EQ(42, FLAG_testing_int_flag);
209  CHECK_EQ(2.5, FLAG_testing_float_flag);
210  CHECK_EQ(2, FLAG_js_arguments.argc);
211  CHECK_EQ(0, strcmp(FLAG_js_arguments[0], "testing-float-flag"));
212  CHECK_EQ(0, strcmp(FLAG_js_arguments[1], "7"));
213}
214
215
216TEST(FlagsJSArguments2) {
217  SetFlagsToDefault();
218  const char* str = "--testing-int-flag 42 --js-arguments testing-float-flag 7";
219  CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
220  CHECK_EQ(42, FLAG_testing_int_flag);
221  CHECK_EQ(2.5, FLAG_testing_float_flag);
222  CHECK_EQ(2, FLAG_js_arguments.argc);
223  CHECK_EQ(0, strcmp(FLAG_js_arguments[0], "testing-float-flag"));
224  CHECK_EQ(0, strcmp(FLAG_js_arguments[1], "7"));
225}
226
227
228TEST(FlagsJSArguments3) {
229  SetFlagsToDefault();
230  const char* str = "--testing-int-flag 42 --js-arguments=testing-float-flag 7";
231  CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
232  CHECK_EQ(42, FLAG_testing_int_flag);
233  CHECK_EQ(2.5, FLAG_testing_float_flag);
234  CHECK_EQ(2, FLAG_js_arguments.argc);
235  CHECK_EQ(0, strcmp(FLAG_js_arguments[0], "testing-float-flag"));
236  CHECK_EQ(0, strcmp(FLAG_js_arguments[1], "7"));
237}
238
239
240TEST(FlagsJSArguments4) {
241  SetFlagsToDefault();
242  const char* str = "--testing-int-flag 42 --";
243  CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
244  CHECK_EQ(42, FLAG_testing_int_flag);
245  CHECK_EQ(0, FLAG_js_arguments.argc);
246}
247
248
249TEST(FlagsRemoveIncomplete) {
250  // Test that processed command line arguments are removed, even
251  // if the list of arguments ends unexpectedly.
252  SetFlagsToDefault();
253  int argc = 3;
254  const char* argv[] = { "", "--crankshaft", "--expose-debug-as" };
255  CHECK_EQ(2, FlagList::SetFlagsFromCommandLine(&argc,
256                                                const_cast<char **>(argv),
257                                                true));
258  CHECK_NE(NULL, argv[1]);
259  CHECK_EQ(argc, 2);
260}
261