test-flags.cc 7.27 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
#include "src/flags.h"
31 32
#include "src/v8.h"
#include "test/cctest/cctest.h"
33

34 35
namespace v8 {
namespace internal {
36 37 38

// This test must be executed first!
TEST(Default) {
39 40 41 42
  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!"));
43 44 45
}

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


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


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


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


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


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


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


TEST(Flags4b) {
  SetFlagsToDefault();
144
  const char* str = "--testing_bool_flag --foo";
145
  CHECK_EQ(2, FlagList::SetFlagsFromString(str, StrLength(str)));
146
  CHECK(!FLAG_testing_maybe_bool_flag.has_value);
147 148 149 150 151 152
}


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


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


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


TEST(Flags6b) {
  SetFlagsToDefault();
182
  const char* str = "       --testing-int-flag 0      --testing_float_flag    ";
183
  CHECK_EQ(3, FlagList::SetFlagsFromString(str, StrLength(str)));
184
}
185

186 187 188 189 190
TEST(FlagsRemoveIncomplete) {
  // Test that processed command line arguments are removed, even
  // if the list of arguments ends unexpectedly.
  SetFlagsToDefault();
  int argc = 3;
191
  const char* argv[] = {"", "--testing-bool-flag", "--expose-gc-as"};
192 193 194
  CHECK_EQ(2, FlagList::SetFlagsFromCommandLine(&argc,
                                                const_cast<char **>(argv),
                                                true));
195
  CHECK(argv[1]);
196
  CHECK_EQ(2, argc);
197
}
198

199 200 201 202 203 204 205 206 207 208 209 210 211
TEST(FlagsJitlessImplications) {
  if (FLAG_jitless) {
    // Double-check implications work as expected. Our implication system is
    // fairly primitive and can break easily depending on the implication
    // definition order in flag-definitions.h.
    CHECK(!FLAG_opt);
    CHECK(!FLAG_validate_asm);
    CHECK(FLAG_wasm_interpret_all);
    CHECK(!FLAG_asm_wasm_lazy_compilation);
    CHECK(!FLAG_wasm_lazy_compilation);
  }
}

212 213
}  // namespace internal
}  // namespace v8