test-flags.cc 8.93 KB
Newer Older
1
// Copyright 2006-2008 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <stdlib.h>

30 31
#include "src/v8.h"
#include "test/cctest/cctest.h"
32 33 34 35 36

using namespace v8::internal;

// This test must be executed first!
TEST(Default) {
37 38 39 40
  CHECK(FLAG_testing_bool_flag);
  CHECK_EQ(13, FLAG_testing_int_flag);
  CHECK_EQ(2.5, FLAG_testing_float_flag);
  CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "Hello, world!"));
41 42 43 44
}


static void SetFlagsToDefault() {
45
  FlagList::ResetAllFlags();
46 47 48 49 50
  TestDefault();
}


TEST(Flags1) {
51
  FlagList::PrintHelp();
52 53 54 55 56
}


TEST(Flags2) {
  SetFlagsToDefault();
57 58 59
  int argc = 8;
  const char* argv[] = { "Test2", "-notesting-bool-flag",
                         "--notesting-maybe-bool-flag", "notaflag",
60 61
                         "--testing_int_flag=77", "-testing_float_flag=.25",
                         "--testing_string_flag", "no way!" };
62 63 64
  CHECK_EQ(0, FlagList::SetFlagsFromCommandLine(&argc,
                                                const_cast<char **>(argv),
                                                false));
65
  CHECK_EQ(8, argc);
66
  CHECK(!FLAG_testing_bool_flag);
67 68
  CHECK(FLAG_testing_maybe_bool_flag.has_value);
  CHECK(!FLAG_testing_maybe_bool_flag.value);
69 70 71
  CHECK_EQ(77, FLAG_testing_int_flag);
  CHECK_EQ(.25, FLAG_testing_float_flag);
  CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "no way!"));
72 73 74 75 76 77
}


TEST(Flags2b) {
  SetFlagsToDefault();
  const char* str =
78
      " -notesting-bool-flag notaflag   --testing_int_flag=77 "
79
      "-notesting-maybe-bool-flag   "
80 81
      "-testing_float_flag=.25  "
      "--testing_string_flag   no_way!  ";
82
  CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
83
  CHECK(!FLAG_testing_bool_flag);
84 85
  CHECK(FLAG_testing_maybe_bool_flag.has_value);
  CHECK(!FLAG_testing_maybe_bool_flag.value);
86 87 88
  CHECK_EQ(77, FLAG_testing_int_flag);
  CHECK_EQ(.25, FLAG_testing_float_flag);
  CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "no_way!"));
89 90 91 92 93
}


TEST(Flags3) {
  SetFlagsToDefault();
94
  int argc = 9;
95
  const char* argv[] =
96
      { "Test3", "--testing_bool_flag", "--testing-maybe-bool-flag", "notaflag",
97 98
        "--testing_int_flag", "-666",
        "--testing_float_flag", "-12E10", "-testing-string-flag=foo-bar" };
99 100 101 102
  CHECK_EQ(0, FlagList::SetFlagsFromCommandLine(&argc,
                                                const_cast<char **>(argv),
                                                true));
  CHECK_EQ(2, argc);
103
  CHECK(FLAG_testing_bool_flag);
104 105
  CHECK(FLAG_testing_maybe_bool_flag.has_value);
  CHECK(FLAG_testing_maybe_bool_flag.value);
106 107 108
  CHECK_EQ(-666, FLAG_testing_int_flag);
  CHECK_EQ(-12E10, FLAG_testing_float_flag);
  CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "foo-bar"));
109 110 111 112 113 114
}


TEST(Flags3b) {
  SetFlagsToDefault();
  const char* str =
115 116
      "--testing_bool_flag --testing-maybe-bool-flag notaflag "
      "--testing_int_flag -666 "
117 118
      "--testing_float_flag -12E10 "
      "-testing-string-flag=foo-bar";
119
  CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
120
  CHECK(FLAG_testing_bool_flag);
121 122
  CHECK(FLAG_testing_maybe_bool_flag.has_value);
  CHECK(FLAG_testing_maybe_bool_flag.value);
123 124 125
  CHECK_EQ(-666, FLAG_testing_int_flag);
  CHECK_EQ(-12E10, FLAG_testing_float_flag);
  CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "foo-bar"));
126 127 128 129 130 131
}


TEST(Flags4) {
  SetFlagsToDefault();
  int argc = 3;
132
  const char* argv[] = { "Test4", "--testing_bool_flag", "--foo" };
133
  CHECK_EQ(0, FlagList::SetFlagsFromCommandLine(&argc,
134 135
                                                const_cast<char **>(argv),
                                                true));
136
  CHECK_EQ(2, argc);
137
  CHECK(!FLAG_testing_maybe_bool_flag.has_value);
138 139 140 141 142
}


TEST(Flags4b) {
  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 149 150 151
}


TEST(Flags5) {
  SetFlagsToDefault();
  int argc = 2;
152
  const char* argv[] = { "Test5", "--testing_int_flag=\"foobar\"" };
153 154 155 156 157 158 159 160 161
  CHECK_EQ(1, FlagList::SetFlagsFromCommandLine(&argc,
                                                const_cast<char **>(argv),
                                                true));
  CHECK_EQ(2, argc);
}


TEST(Flags5b) {
  SetFlagsToDefault();
162
  const char* str = "                     --testing_int_flag=\"foobar\"";
163
  CHECK_EQ(1, FlagList::SetFlagsFromString(str, StrLength(str)));
164 165 166 167 168 169
}


TEST(Flags6) {
  SetFlagsToDefault();
  int argc = 4;
170 171
  const char* argv[] = { "Test5", "--testing-int-flag", "0",
                         "--testing_float_flag" };
172 173 174
  CHECK_EQ(3, FlagList::SetFlagsFromCommandLine(&argc,
                                                const_cast<char **>(argv),
                                                true));
175
  CHECK_EQ(2, argc);
176 177 178 179 180
}


TEST(Flags6b) {
  SetFlagsToDefault();
181
  const char* str = "       --testing-int-flag 0      --testing_float_flag    ";
182
  CHECK_EQ(3, FlagList::SetFlagsFromString(str, StrLength(str)));
183
}
184 185 186 187 188 189 190 191 192 193 194 195 196


TEST(FlagsJSArguments1) {
  SetFlagsToDefault();
  int argc = 6;
  const char* argv[] = {"TestJSArgs1",
                        "--testing-int-flag", "42",
                        "--", "testing-float-flag", "7"};
  CHECK_EQ(0, FlagList::SetFlagsFromCommandLine(&argc,
                                                const_cast<char **>(argv),
                                                true));
  CHECK_EQ(42, FLAG_testing_int_flag);
  CHECK_EQ(2.5, FLAG_testing_float_flag);
197
  CHECK_EQ(2, FLAG_js_arguments.argc);
198 199 200 201 202 203 204 205 206
  CHECK_EQ(0, strcmp(FLAG_js_arguments[0], "testing-float-flag"));
  CHECK_EQ(0, strcmp(FLAG_js_arguments[1], "7"));
  CHECK_EQ(1, argc);
}


TEST(FlagsJSArguments1b) {
  SetFlagsToDefault();
  const char* str = "--testing-int-flag 42 -- testing-float-flag 7";
207
  CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
208 209
  CHECK_EQ(42, FLAG_testing_int_flag);
  CHECK_EQ(2.5, FLAG_testing_float_flag);
210
  CHECK_EQ(2, FLAG_js_arguments.argc);
211 212 213 214 215 216 217 218
  CHECK_EQ(0, strcmp(FLAG_js_arguments[0], "testing-float-flag"));
  CHECK_EQ(0, strcmp(FLAG_js_arguments[1], "7"));
}


TEST(FlagsJSArguments2) {
  SetFlagsToDefault();
  const char* str = "--testing-int-flag 42 --js-arguments testing-float-flag 7";
219
  CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
220 221
  CHECK_EQ(42, FLAG_testing_int_flag);
  CHECK_EQ(2.5, FLAG_testing_float_flag);
222
  CHECK_EQ(2, FLAG_js_arguments.argc);
223 224 225 226 227 228 229 230
  CHECK_EQ(0, strcmp(FLAG_js_arguments[0], "testing-float-flag"));
  CHECK_EQ(0, strcmp(FLAG_js_arguments[1], "7"));
}


TEST(FlagsJSArguments3) {
  SetFlagsToDefault();
  const char* str = "--testing-int-flag 42 --js-arguments=testing-float-flag 7";
231
  CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
232 233
  CHECK_EQ(42, FLAG_testing_int_flag);
  CHECK_EQ(2.5, FLAG_testing_float_flag);
234
  CHECK_EQ(2, FLAG_js_arguments.argc);
235 236 237 238 239 240 241 242
  CHECK_EQ(0, strcmp(FLAG_js_arguments[0], "testing-float-flag"));
  CHECK_EQ(0, strcmp(FLAG_js_arguments[1], "7"));
}


TEST(FlagsJSArguments4) {
  SetFlagsToDefault();
  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 249 250 251 252 253 254 255 256 257

TEST(FlagsRemoveIncomplete) {
  // Test that processed command line arguments are removed, even
  // if the list of arguments ends unexpectedly.
  SetFlagsToDefault();
  int argc = 3;
  const char* argv[] = { "", "--crankshaft", "--expose-debug-as" };
  CHECK_EQ(2, FlagList::SetFlagsFromCommandLine(&argc,
                                                const_cast<char **>(argv),
                                                true));
258
  CHECK(argv[1]);
259 260
  CHECK_EQ(argc, 2);
}