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 "v8.h"
31#include "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 = 7;
58  const char* argv[] = { "Test2", "-notesting-bool-flag", "notaflag",
59                         "--testing_int_flag=77", "-testing_float_flag=.25",
60                         "--testing_string_flag", "no way!" };
61  CHECK_EQ(0, FlagList::SetFlagsFromCommandLine(&argc,
62                                                const_cast<char **>(argv),
63                                                false));
64  CHECK_EQ(7, argc);
65  CHECK(!FLAG_testing_bool_flag);
66  CHECK_EQ(77, FLAG_testing_int_flag);
67  CHECK_EQ(.25, FLAG_testing_float_flag);
68  CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "no way!"));
69}
70
71
72TEST(Flags2b) {
73  SetFlagsToDefault();
74  const char* str =
75      " -notesting-bool-flag notaflag   --testing_int_flag=77 "
76      "-testing_float_flag=.25  "
77      "--testing_string_flag   no_way!  ";
78  CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
79  CHECK(!FLAG_testing_bool_flag);
80  CHECK_EQ(77, FLAG_testing_int_flag);
81  CHECK_EQ(.25, FLAG_testing_float_flag);
82  CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "no_way!"));
83}
84
85
86TEST(Flags3) {
87  SetFlagsToDefault();
88  int argc = 8;
89  const char* argv[] =
90      { "Test3", "--testing_bool_flag", "notaflag",
91        "--testing_int_flag", "-666",
92        "--testing_float_flag", "-12E10", "-testing-string-flag=foo-bar" };
93  CHECK_EQ(0, FlagList::SetFlagsFromCommandLine(&argc,
94                                                const_cast<char **>(argv),
95                                                true));
96  CHECK_EQ(2, argc);
97  CHECK(FLAG_testing_bool_flag);
98  CHECK_EQ(-666, FLAG_testing_int_flag);
99  CHECK_EQ(-12E10, FLAG_testing_float_flag);
100  CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "foo-bar"));
101}
102
103
104TEST(Flags3b) {
105  SetFlagsToDefault();
106  const char* str =
107      "--testing_bool_flag notaflag --testing_int_flag -666 "
108      "--testing_float_flag -12E10 "
109      "-testing-string-flag=foo-bar";
110  CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
111  CHECK(FLAG_testing_bool_flag);
112  CHECK_EQ(-666, FLAG_testing_int_flag);
113  CHECK_EQ(-12E10, FLAG_testing_float_flag);
114  CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "foo-bar"));
115}
116
117
118TEST(Flags4) {
119  SetFlagsToDefault();
120  int argc = 3;
121  const char* argv[] = { "Test4", "--testing_bool_flag", "--foo" };
122  CHECK_EQ(0, FlagList::SetFlagsFromCommandLine(&argc,
123                                                const_cast<char **>(argv),
124                                                true));
125  CHECK_EQ(2, argc);
126}
127
128
129TEST(Flags4b) {
130  SetFlagsToDefault();
131  const char* str = "--testing_bool_flag --foo";
132  CHECK_EQ(2, FlagList::SetFlagsFromString(str, StrLength(str)));
133}
134
135
136TEST(Flags5) {
137  SetFlagsToDefault();
138  int argc = 2;
139  const char* argv[] = { "Test5", "--testing_int_flag=\"foobar\"" };
140  CHECK_EQ(1, FlagList::SetFlagsFromCommandLine(&argc,
141                                                const_cast<char **>(argv),
142                                                true));
143  CHECK_EQ(2, argc);
144}
145
146
147TEST(Flags5b) {
148  SetFlagsToDefault();
149  const char* str = "                     --testing_int_flag=\"foobar\"";
150  CHECK_EQ(1, FlagList::SetFlagsFromString(str, StrLength(str)));
151}
152
153
154TEST(Flags6) {
155  SetFlagsToDefault();
156  int argc = 4;
157  const char* argv[] = { "Test5", "--testing-int-flag", "0",
158                         "--testing_float_flag" };
159  CHECK_EQ(3, FlagList::SetFlagsFromCommandLine(&argc,
160                                                const_cast<char **>(argv),
161                                                true));
162  CHECK_EQ(4, argc);
163}
164
165
166TEST(Flags6b) {
167  SetFlagsToDefault();
168  const char* str = "       --testing-int-flag 0      --testing_float_flag    ";
169  CHECK_EQ(3, FlagList::SetFlagsFromString(str, StrLength(str)));
170}
171
172
173TEST(FlagsJSArguments1) {
174  SetFlagsToDefault();
175  int argc = 6;
176  const char* argv[] = {"TestJSArgs1",
177                        "--testing-int-flag", "42",
178                        "--", "testing-float-flag", "7"};
179  CHECK_EQ(0, FlagList::SetFlagsFromCommandLine(&argc,
180                                                const_cast<char **>(argv),
181                                                true));
182  CHECK_EQ(42, FLAG_testing_int_flag);
183  CHECK_EQ(2.5, FLAG_testing_float_flag);
184  CHECK_EQ(2, FLAG_js_arguments.argc());
185  CHECK_EQ(0, strcmp(FLAG_js_arguments[0], "testing-float-flag"));
186  CHECK_EQ(0, strcmp(FLAG_js_arguments[1], "7"));
187  CHECK_EQ(1, argc);
188}
189
190
191TEST(FlagsJSArguments1b) {
192  SetFlagsToDefault();
193  const char* str = "--testing-int-flag 42 -- testing-float-flag 7";
194  CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
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}
201
202
203TEST(FlagsJSArguments2) {
204  SetFlagsToDefault();
205  const char* str = "--testing-int-flag 42 --js-arguments testing-float-flag 7";
206  CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
207  CHECK_EQ(42, FLAG_testing_int_flag);
208  CHECK_EQ(2.5, FLAG_testing_float_flag);
209  CHECK_EQ(2, FLAG_js_arguments.argc());
210  CHECK_EQ(0, strcmp(FLAG_js_arguments[0], "testing-float-flag"));
211  CHECK_EQ(0, strcmp(FLAG_js_arguments[1], "7"));
212}
213
214
215TEST(FlagsJSArguments3) {
216  SetFlagsToDefault();
217  const char* str = "--testing-int-flag 42 --js-arguments=testing-float-flag 7";
218  CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
219  CHECK_EQ(42, FLAG_testing_int_flag);
220  CHECK_EQ(2.5, FLAG_testing_float_flag);
221  CHECK_EQ(2, FLAG_js_arguments.argc());
222  CHECK_EQ(0, strcmp(FLAG_js_arguments[0], "testing-float-flag"));
223  CHECK_EQ(0, strcmp(FLAG_js_arguments[1], "7"));
224}
225
226
227TEST(FlagsJSArguments4) {
228  SetFlagsToDefault();
229  const char* str = "--testing-int-flag 42 --";
230  CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
231  CHECK_EQ(42, FLAG_testing_int_flag);
232  CHECK_EQ(0, FLAG_js_arguments.argc());
233}
234
235