Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "cmdline_parser.h" |
| 18 | #include "runtime/runtime_options.h" |
| 19 | #include "runtime/parsed_options.h" |
| 20 | |
| 21 | #include "utils.h" |
| 22 | #include <numeric> |
| 23 | #include "gtest/gtest.h" |
| 24 | |
| 25 | #define EXPECT_NULL(expected) EXPECT_EQ(reinterpret_cast<const void*>(expected), \ |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 26 | reinterpret_cast<void*>(nullptr)); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | bool UsuallyEquals(double expected, double actual); |
| 30 | |
| 31 | // This has a gtest dependency, which is why it's in the gtest only. |
| 32 | bool operator==(const TestProfilerOptions& lhs, const TestProfilerOptions& rhs) { |
| 33 | return lhs.enabled_ == rhs.enabled_ && |
| 34 | lhs.output_file_name_ == rhs.output_file_name_ && |
| 35 | lhs.period_s_ == rhs.period_s_ && |
| 36 | lhs.duration_s_ == rhs.duration_s_ && |
| 37 | lhs.interval_us_ == rhs.interval_us_ && |
| 38 | UsuallyEquals(lhs.backoff_coefficient_, rhs.backoff_coefficient_) && |
| 39 | UsuallyEquals(lhs.start_immediately_, rhs.start_immediately_) && |
| 40 | UsuallyEquals(lhs.top_k_threshold_, rhs.top_k_threshold_) && |
| 41 | UsuallyEquals(lhs.top_k_change_threshold_, rhs.top_k_change_threshold_) && |
| 42 | lhs.profile_type_ == rhs.profile_type_ && |
| 43 | lhs.max_stack_depth_ == rhs.max_stack_depth_; |
| 44 | } |
| 45 | |
| 46 | bool UsuallyEquals(double expected, double actual) { |
| 47 | using FloatingPoint = ::testing::internal::FloatingPoint<double>; |
| 48 | |
| 49 | FloatingPoint exp(expected); |
| 50 | FloatingPoint act(actual); |
| 51 | |
| 52 | // Compare with ULPs instead of comparing with == |
| 53 | return exp.AlmostEquals(act); |
| 54 | } |
| 55 | |
| 56 | template <typename T> |
| 57 | bool UsuallyEquals(const T& expected, const T& actual, |
| 58 | typename std::enable_if< |
| 59 | detail::SupportsEqualityOperator<T>::value>::type* = 0) { |
| 60 | return expected == actual; |
| 61 | } |
| 62 | |
| 63 | // Try to use memcmp to compare simple plain-old-data structs. |
| 64 | // |
| 65 | // This should *not* generate false positives, but it can generate false negatives. |
| 66 | // This will mostly work except for fields like float which can have different bit patterns |
| 67 | // that are nevertheless equal. |
| 68 | // If a test is failing because the structs aren't "equal" when they really are |
| 69 | // then it's recommended to implement operator== for it instead. |
| 70 | template <typename T, typename ... Ignore> |
| 71 | bool UsuallyEquals(const T& expected, const T& actual, |
| 72 | const Ignore& ... more ATTRIBUTE_UNUSED, |
| 73 | typename std::enable_if<std::is_pod<T>::value>::type* = 0, |
| 74 | typename std::enable_if<!detail::SupportsEqualityOperator<T>::value>::type* = 0 |
| 75 | ) { |
| 76 | return memcmp(std::addressof(expected), std::addressof(actual), sizeof(T)) == 0; |
| 77 | } |
| 78 | |
| 79 | bool UsuallyEquals(const XGcOption& expected, const XGcOption& actual) { |
| 80 | return memcmp(std::addressof(expected), std::addressof(actual), sizeof(expected)) == 0; |
| 81 | } |
| 82 | |
| 83 | bool UsuallyEquals(const char* expected, std::string actual) { |
| 84 | return std::string(expected) == actual; |
| 85 | } |
| 86 | |
| 87 | template <typename TMap, typename TKey, typename T> |
| 88 | ::testing::AssertionResult IsExpectedKeyValue(const T& expected, |
| 89 | const TMap& map, |
| 90 | const TKey& key) { |
| 91 | auto* actual = map.Get(key); |
| 92 | if (actual != nullptr) { |
| 93 | if (!UsuallyEquals(expected, *actual)) { |
| 94 | return ::testing::AssertionFailure() |
| 95 | << "expected " << detail::ToStringAny(expected) << " but got " |
| 96 | << detail::ToStringAny(*actual); |
| 97 | } |
| 98 | return ::testing::AssertionSuccess(); |
| 99 | } |
| 100 | |
| 101 | return ::testing::AssertionFailure() << "key was not in the map"; |
| 102 | } |
| 103 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 104 | template <typename TMap, typename TKey, typename T> |
| 105 | ::testing::AssertionResult IsExpectedDefaultKeyValue(const T& expected, |
| 106 | const TMap& map, |
| 107 | const TKey& key) { |
| 108 | const T& actual = map.GetOrDefault(key); |
| 109 | if (!UsuallyEquals(expected, actual)) { |
| 110 | return ::testing::AssertionFailure() |
| 111 | << "expected " << detail::ToStringAny(expected) << " but got " |
| 112 | << detail::ToStringAny(actual); |
| 113 | } |
| 114 | return ::testing::AssertionSuccess(); |
| 115 | } |
| 116 | |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 117 | class CmdlineParserTest : public ::testing::Test { |
| 118 | public: |
| 119 | CmdlineParserTest() = default; |
| 120 | ~CmdlineParserTest() = default; |
| 121 | |
| 122 | protected: |
| 123 | using M = RuntimeArgumentMap; |
| 124 | using RuntimeParser = ParsedOptions::RuntimeParser; |
| 125 | |
| 126 | static void SetUpTestCase() { |
| 127 | art::InitLogging(nullptr); // argv = null |
| 128 | } |
| 129 | |
| 130 | virtual void SetUp() { |
| 131 | parser_ = ParsedOptions::MakeParser(false); // do not ignore unrecognized options |
| 132 | } |
| 133 | |
| 134 | static ::testing::AssertionResult IsResultSuccessful(CmdlineResult result) { |
| 135 | if (result.IsSuccess()) { |
| 136 | return ::testing::AssertionSuccess(); |
| 137 | } else { |
| 138 | return ::testing::AssertionFailure() |
| 139 | << result.GetStatus() << " with: " << result.GetMessage(); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | static ::testing::AssertionResult IsResultFailure(CmdlineResult result, |
| 144 | CmdlineResult::Status failure_status) { |
| 145 | if (result.IsSuccess()) { |
| 146 | return ::testing::AssertionFailure() << " got success but expected failure: " |
| 147 | << failure_status; |
| 148 | } else if (result.GetStatus() == failure_status) { |
| 149 | return ::testing::AssertionSuccess(); |
| 150 | } |
| 151 | |
| 152 | return ::testing::AssertionFailure() << " expected failure " << failure_status |
| 153 | << " but got " << result.GetStatus(); |
| 154 | } |
| 155 | |
| 156 | std::unique_ptr<RuntimeParser> parser_; |
| 157 | }; |
| 158 | |
| 159 | #define EXPECT_KEY_EXISTS(map, key) EXPECT_TRUE((map).Exists(key)) |
| 160 | #define EXPECT_KEY_VALUE(map, key, expected) EXPECT_TRUE(IsExpectedKeyValue(expected, map, key)) |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 161 | #define EXPECT_DEFAULT_KEY_VALUE(map, key, expected) EXPECT_TRUE(IsExpectedDefaultKeyValue(expected, map, key)) |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 162 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 163 | #define _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv) \ |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 164 | do { \ |
| 165 | EXPECT_TRUE(IsResultSuccessful(parser_->Parse(argv))); \ |
| 166 | EXPECT_EQ(0u, parser_->GetArgumentsMap().Size()); \ |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 167 | |
| 168 | #define EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv) \ |
| 169 | _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv); \ |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 170 | } while (false) |
| 171 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 172 | #define EXPECT_SINGLE_PARSE_DEFAULT_VALUE(expected, argv, key)\ |
| 173 | _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv); \ |
| 174 | RuntimeArgumentMap args = parser_->ReleaseArgumentsMap(); \ |
| 175 | EXPECT_DEFAULT_KEY_VALUE(args, key, expected); \ |
| 176 | } while (false) // NOLINT [readability/namespace] [5] |
| 177 | |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 178 | #define _EXPECT_SINGLE_PARSE_EXISTS(argv, key) \ |
| 179 | do { \ |
| 180 | EXPECT_TRUE(IsResultSuccessful(parser_->Parse(argv))); \ |
| 181 | RuntimeArgumentMap args = parser_->ReleaseArgumentsMap(); \ |
| 182 | EXPECT_EQ(1u, args.Size()); \ |
| 183 | EXPECT_KEY_EXISTS(args, key); \ |
| 184 | |
| 185 | #define EXPECT_SINGLE_PARSE_EXISTS(argv, key) \ |
| 186 | _EXPECT_SINGLE_PARSE_EXISTS(argv, key); \ |
| 187 | } while (false) |
| 188 | |
| 189 | #define EXPECT_SINGLE_PARSE_VALUE(expected, argv, key) \ |
| 190 | _EXPECT_SINGLE_PARSE_EXISTS(argv, key); \ |
| 191 | EXPECT_KEY_VALUE(args, key, expected); \ |
| 192 | } while (false) // NOLINT [readability/namespace] [5] |
| 193 | |
| 194 | #define EXPECT_SINGLE_PARSE_VALUE_STR(expected, argv, key) \ |
| 195 | EXPECT_SINGLE_PARSE_VALUE(std::string(expected), argv, key) |
| 196 | |
| 197 | #define EXPECT_SINGLE_PARSE_FAIL(argv, failure_status) \ |
| 198 | do { \ |
| 199 | EXPECT_TRUE(IsResultFailure(parser_->Parse(argv), failure_status));\ |
| 200 | RuntimeArgumentMap args = parser_->ReleaseArgumentsMap();\ |
| 201 | EXPECT_EQ(0u, args.Size()); \ |
| 202 | } while (false) |
| 203 | |
| 204 | TEST_F(CmdlineParserTest, TestSimpleSuccesses) { |
| 205 | auto& parser = *parser_; |
| 206 | |
| 207 | EXPECT_LT(0u, parser.CountDefinedArguments()); |
| 208 | |
| 209 | { |
| 210 | // Test case 1: No command line arguments |
| 211 | EXPECT_TRUE(IsResultSuccessful(parser.Parse(""))); |
| 212 | RuntimeArgumentMap args = parser.ReleaseArgumentsMap(); |
| 213 | EXPECT_EQ(0u, args.Size()); |
| 214 | } |
| 215 | |
| 216 | EXPECT_SINGLE_PARSE_EXISTS("-Xzygote", M::Zygote); |
| 217 | EXPECT_SINGLE_PARSE_VALUE_STR("/hello/world", "-Xbootclasspath:/hello/world", M::BootClassPath); |
| 218 | EXPECT_SINGLE_PARSE_VALUE("/hello/world", "-Xbootclasspath:/hello/world", M::BootClassPath); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 219 | EXPECT_SINGLE_PARSE_VALUE(Memory<1>(234), "-Xss234", M::StackSize); |
| 220 | EXPECT_SINGLE_PARSE_VALUE(MemoryKiB(1234*MB), "-Xms1234m", M::MemoryInitialSize); |
| 221 | EXPECT_SINGLE_PARSE_VALUE(true, "-XX:EnableHSpaceCompactForOOM", M::EnableHSpaceCompactForOOM); |
| 222 | EXPECT_SINGLE_PARSE_VALUE(false, "-XX:DisableHSpaceCompactForOOM", M::EnableHSpaceCompactForOOM); |
| 223 | EXPECT_SINGLE_PARSE_VALUE(0.5, "-XX:HeapTargetUtilization=0.5", M::HeapTargetUtilization); |
| 224 | EXPECT_SINGLE_PARSE_VALUE(5u, "-XX:ParallelGCThreads=5", M::ParallelGCThreads); |
Jean Christophe Beyler | 24e04aa | 2014-09-12 12:03:25 -0700 | [diff] [blame] | 225 | EXPECT_SINGLE_PARSE_EXISTS("-Xno-dex-file-fallback", M::NoDexFileFallback); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 226 | } // TEST_F |
| 227 | |
| 228 | TEST_F(CmdlineParserTest, TestSimpleFailures) { |
| 229 | // Test argument is unknown to the parser |
| 230 | EXPECT_SINGLE_PARSE_FAIL("abcdefg^%@#*(@#", CmdlineResult::kUnknown); |
| 231 | // Test value map substitution fails |
| 232 | EXPECT_SINGLE_PARSE_FAIL("-Xverify:whatever", CmdlineResult::kFailure); |
| 233 | // Test value type parsing failures |
| 234 | EXPECT_SINGLE_PARSE_FAIL("-Xsswhatever", CmdlineResult::kFailure); // invalid memory value |
| 235 | EXPECT_SINGLE_PARSE_FAIL("-Xms123", CmdlineResult::kFailure); // memory value too small |
| 236 | EXPECT_SINGLE_PARSE_FAIL("-XX:HeapTargetUtilization=0.0", CmdlineResult::kOutOfRange); // toosmal |
| 237 | EXPECT_SINGLE_PARSE_FAIL("-XX:HeapTargetUtilization=2.0", CmdlineResult::kOutOfRange); // toolarg |
| 238 | EXPECT_SINGLE_PARSE_FAIL("-XX:ParallelGCThreads=-5", CmdlineResult::kOutOfRange); // too small |
| 239 | EXPECT_SINGLE_PARSE_FAIL("-Xgc:blablabla", CmdlineResult::kUsage); // not a valid suboption |
| 240 | } // TEST_F |
| 241 | |
| 242 | TEST_F(CmdlineParserTest, TestLogVerbosity) { |
| 243 | { |
| 244 | const char* log_args = "-verbose:" |
| 245 | "class,compiler,gc,heap,jdwp,jni,monitor,profiler,signals,startup,third-party-jni," |
| 246 | "threads,verifier"; |
| 247 | |
| 248 | LogVerbosity log_verbosity = LogVerbosity(); |
| 249 | log_verbosity.class_linker = true; |
| 250 | log_verbosity.compiler = true; |
| 251 | log_verbosity.gc = true; |
| 252 | log_verbosity.heap = true; |
| 253 | log_verbosity.jdwp = true; |
| 254 | log_verbosity.jni = true; |
| 255 | log_verbosity.monitor = true; |
| 256 | log_verbosity.profiler = true; |
| 257 | log_verbosity.signals = true; |
| 258 | log_verbosity.startup = true; |
| 259 | log_verbosity.third_party_jni = true; |
| 260 | log_verbosity.threads = true; |
| 261 | log_verbosity.verifier = true; |
| 262 | |
| 263 | EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose); |
| 264 | } |
| 265 | |
| 266 | { |
| 267 | const char* log_args = "-verbose:" |
| 268 | "class,compiler,gc,heap,jdwp,jni,monitor"; |
| 269 | |
| 270 | LogVerbosity log_verbosity = LogVerbosity(); |
| 271 | log_verbosity.class_linker = true; |
| 272 | log_verbosity.compiler = true; |
| 273 | log_verbosity.gc = true; |
| 274 | log_verbosity.heap = true; |
| 275 | log_verbosity.jdwp = true; |
| 276 | log_verbosity.jni = true; |
| 277 | log_verbosity.monitor = true; |
| 278 | |
| 279 | EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose); |
| 280 | } |
| 281 | |
| 282 | EXPECT_SINGLE_PARSE_FAIL("-verbose:blablabla", CmdlineResult::kUsage); // invalid verbose opt |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 283 | |
| 284 | { |
Sebastien Hertz | bba348e | 2015-06-01 08:28:18 +0200 | [diff] [blame] | 285 | const char* log_args = "-verbose:deopt"; |
| 286 | LogVerbosity log_verbosity = LogVerbosity(); |
| 287 | log_verbosity.deopt = true; |
| 288 | EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose); |
| 289 | } |
| 290 | |
| 291 | { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 292 | const char* log_args = "-verbose:oat"; |
| 293 | LogVerbosity log_verbosity = LogVerbosity(); |
| 294 | log_verbosity.oat = true; |
| 295 | EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose); |
| 296 | } |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 297 | } // TEST_F |
| 298 | |
Nicolas Geoffray | 8f4ee5c | 2015-02-05 10:14:10 +0000 | [diff] [blame] | 299 | // TODO: Enable this b/19274810 |
| 300 | TEST_F(CmdlineParserTest, DISABLED_TestXGcOption) { |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 301 | /* |
| 302 | * Test success |
| 303 | */ |
| 304 | { |
| 305 | XGcOption option_all_true{}; // NOLINT [readability/braces] [4] |
| 306 | option_all_true.collector_type_ = gc::CollectorType::kCollectorTypeCMS; |
| 307 | option_all_true.verify_pre_gc_heap_ = true; |
| 308 | option_all_true.verify_pre_sweeping_heap_ = true; |
| 309 | option_all_true.verify_post_gc_heap_ = true; |
| 310 | option_all_true.verify_pre_gc_rosalloc_ = true; |
| 311 | option_all_true.verify_pre_sweeping_rosalloc_ = true; |
| 312 | option_all_true.verify_post_gc_rosalloc_ = true; |
| 313 | |
| 314 | const char * xgc_args_all_true = "-Xgc:concurrent," |
| 315 | "preverify,presweepingverify,postverify," |
| 316 | "preverify_rosalloc,presweepingverify_rosalloc," |
| 317 | "postverify_rosalloc,precise," |
| 318 | "verifycardtable"; |
| 319 | |
| 320 | EXPECT_SINGLE_PARSE_VALUE(option_all_true, xgc_args_all_true, M::GcOption); |
| 321 | |
| 322 | XGcOption option_all_false{}; // NOLINT [readability/braces] [4] |
| 323 | option_all_false.collector_type_ = gc::CollectorType::kCollectorTypeMS; |
| 324 | option_all_false.verify_pre_gc_heap_ = false; |
| 325 | option_all_false.verify_pre_sweeping_heap_ = false; |
| 326 | option_all_false.verify_post_gc_heap_ = false; |
| 327 | option_all_false.verify_pre_gc_rosalloc_ = false; |
| 328 | option_all_false.verify_pre_sweeping_rosalloc_ = false; |
| 329 | option_all_false.verify_post_gc_rosalloc_ = false; |
| 330 | |
| 331 | const char* xgc_args_all_false = "-Xgc:nonconcurrent," |
| 332 | "nopreverify,nopresweepingverify,nopostverify,nopreverify_rosalloc," |
| 333 | "nopresweepingverify_rosalloc,nopostverify_rosalloc,noprecise,noverifycardtable"; |
| 334 | |
| 335 | EXPECT_SINGLE_PARSE_VALUE(option_all_false, xgc_args_all_false, M::GcOption); |
| 336 | |
| 337 | XGcOption option_all_default{}; // NOLINT [readability/braces] [4] |
| 338 | |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 339 | const char* xgc_args_blank = "-Xgc:"; |
| 340 | EXPECT_SINGLE_PARSE_VALUE(option_all_default, xgc_args_blank, M::GcOption); |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * Test failures |
| 345 | */ |
| 346 | EXPECT_SINGLE_PARSE_FAIL("-Xgc:blablabla", CmdlineResult::kUsage); // invalid Xgc opt |
| 347 | } // TEST_F |
| 348 | |
| 349 | /* |
| 350 | * {"-Xrunjdwp:_", "-agentlib:jdwp=_"} |
| 351 | */ |
| 352 | TEST_F(CmdlineParserTest, TestJdwpOptions) { |
| 353 | /* |
| 354 | * Test success |
| 355 | */ |
| 356 | { |
| 357 | /* |
| 358 | * "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n" |
| 359 | */ |
| 360 | JDWP::JdwpOptions opt = JDWP::JdwpOptions(); |
| 361 | opt.transport = JDWP::JdwpTransportType::kJdwpTransportSocket; |
| 362 | opt.port = 8000; |
| 363 | opt.server = true; |
| 364 | |
| 365 | const char *opt_args = "-Xrunjdwp:transport=dt_socket,address=8000,server=y"; |
| 366 | |
| 367 | EXPECT_SINGLE_PARSE_VALUE(opt, opt_args, M::JdwpOptions); |
| 368 | } |
| 369 | |
| 370 | { |
| 371 | /* |
| 372 | * "Example: -agentlib:jdwp=transport=dt_socket,address=localhost:6500,server=n\n"); |
| 373 | */ |
| 374 | JDWP::JdwpOptions opt = JDWP::JdwpOptions(); |
| 375 | opt.transport = JDWP::JdwpTransportType::kJdwpTransportSocket; |
| 376 | opt.host = "localhost"; |
| 377 | opt.port = 6500; |
| 378 | opt.server = false; |
| 379 | |
| 380 | const char *opt_args = "-agentlib:jdwp=transport=dt_socket,address=localhost:6500,server=n"; |
| 381 | |
| 382 | EXPECT_SINGLE_PARSE_VALUE(opt, opt_args, M::JdwpOptions); |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | * Test failures |
| 387 | */ |
| 388 | EXPECT_SINGLE_PARSE_FAIL("-Xrunjdwp:help", CmdlineResult::kUsage); // usage for help only |
| 389 | EXPECT_SINGLE_PARSE_FAIL("-Xrunjdwp:blabla", CmdlineResult::kFailure); // invalid subarg |
| 390 | EXPECT_SINGLE_PARSE_FAIL("-agentlib:jdwp=help", CmdlineResult::kUsage); // usage for help only |
| 391 | EXPECT_SINGLE_PARSE_FAIL("-agentlib:jdwp=blabla", CmdlineResult::kFailure); // invalid subarg |
| 392 | } // TEST_F |
| 393 | |
| 394 | /* |
| 395 | * -D_ -D_ -D_ ... |
| 396 | */ |
| 397 | TEST_F(CmdlineParserTest, TestPropertiesList) { |
| 398 | /* |
| 399 | * Test successes |
| 400 | */ |
| 401 | { |
| 402 | std::vector<std::string> opt = {"hello"}; |
| 403 | |
| 404 | EXPECT_SINGLE_PARSE_VALUE(opt, "-Dhello", M::PropertiesList); |
| 405 | } |
| 406 | |
| 407 | { |
| 408 | std::vector<std::string> opt = {"hello", "world"}; |
| 409 | |
| 410 | EXPECT_SINGLE_PARSE_VALUE(opt, "-Dhello -Dworld", M::PropertiesList); |
| 411 | } |
| 412 | |
| 413 | { |
| 414 | std::vector<std::string> opt = {"one", "two", "three"}; |
| 415 | |
| 416 | EXPECT_SINGLE_PARSE_VALUE(opt, "-Done -Dtwo -Dthree", M::PropertiesList); |
| 417 | } |
| 418 | } // TEST_F |
| 419 | |
| 420 | /* |
| 421 | * -Xcompiler-option foo -Xcompiler-option bar ... |
| 422 | */ |
| 423 | TEST_F(CmdlineParserTest, TestCompilerOption) { |
| 424 | /* |
| 425 | * Test successes |
| 426 | */ |
| 427 | { |
| 428 | std::vector<std::string> opt = {"hello"}; |
| 429 | EXPECT_SINGLE_PARSE_VALUE(opt, "-Xcompiler-option hello", M::CompilerOptions); |
| 430 | } |
| 431 | |
| 432 | { |
| 433 | std::vector<std::string> opt = {"hello", "world"}; |
| 434 | EXPECT_SINGLE_PARSE_VALUE(opt, |
| 435 | "-Xcompiler-option hello -Xcompiler-option world", |
| 436 | M::CompilerOptions); |
| 437 | } |
| 438 | |
| 439 | { |
| 440 | std::vector<std::string> opt = {"one", "two", "three"}; |
| 441 | EXPECT_SINGLE_PARSE_VALUE(opt, |
| 442 | "-Xcompiler-option one -Xcompiler-option two -Xcompiler-option three", |
| 443 | M::CompilerOptions); |
| 444 | } |
| 445 | } // TEST_F |
| 446 | |
| 447 | /* |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 448 | * -Xjit, -Xnojit, -Xjitcodecachesize, Xjitcompilethreshold |
| 449 | */ |
| 450 | TEST_F(CmdlineParserTest, TestJitOptions) { |
| 451 | /* |
| 452 | * Test successes |
| 453 | */ |
| 454 | { |
Andreas Gampe | 2682699 | 2015-03-05 18:48:52 -0800 | [diff] [blame] | 455 | EXPECT_SINGLE_PARSE_VALUE(true, "-Xusejit:true", M::UseJIT); |
| 456 | EXPECT_SINGLE_PARSE_VALUE(false, "-Xusejit:false", M::UseJIT); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 457 | } |
| 458 | { |
| 459 | EXPECT_SINGLE_PARSE_VALUE(MemoryKiB(16 * KB), "-Xjitcodecachesize:16K", M::JITCodeCacheCapacity); |
| 460 | EXPECT_SINGLE_PARSE_VALUE(MemoryKiB(16 * MB), "-Xjitcodecachesize:16M", M::JITCodeCacheCapacity); |
| 461 | } |
| 462 | { |
| 463 | EXPECT_SINGLE_PARSE_VALUE(12345u, "-Xjitthreshold:12345", M::JITCompileThreshold); |
| 464 | } |
| 465 | } // TEST_F |
| 466 | |
| 467 | /* |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 468 | * -X-profile-* |
| 469 | */ |
| 470 | TEST_F(CmdlineParserTest, TestProfilerOptions) { |
| 471 | /* |
| 472 | * Test successes |
| 473 | */ |
| 474 | |
| 475 | { |
| 476 | TestProfilerOptions opt; |
| 477 | opt.enabled_ = true; |
| 478 | |
| 479 | EXPECT_SINGLE_PARSE_VALUE(opt, |
| 480 | "-Xenable-profiler", |
| 481 | M::ProfilerOpts); |
| 482 | } |
| 483 | |
| 484 | { |
| 485 | TestProfilerOptions opt; |
| 486 | // also need to test 'enabled' |
| 487 | opt.output_file_name_ = "hello_world.txt"; |
| 488 | |
| 489 | EXPECT_SINGLE_PARSE_VALUE(opt, |
| 490 | "-Xprofile-filename:hello_world.txt ", |
| 491 | M::ProfilerOpts); |
| 492 | } |
| 493 | |
| 494 | { |
| 495 | TestProfilerOptions opt = TestProfilerOptions(); |
| 496 | // also need to test 'enabled' |
| 497 | opt.output_file_name_ = "output.txt"; |
| 498 | opt.period_s_ = 123u; |
| 499 | opt.duration_s_ = 456u; |
| 500 | opt.interval_us_ = 789u; |
| 501 | opt.backoff_coefficient_ = 2.0; |
| 502 | opt.start_immediately_ = true; |
| 503 | opt.top_k_threshold_ = 50.0; |
| 504 | opt.top_k_change_threshold_ = 60.0; |
| 505 | opt.profile_type_ = kProfilerMethod; |
| 506 | opt.max_stack_depth_ = 1337u; |
| 507 | |
| 508 | EXPECT_SINGLE_PARSE_VALUE(opt, |
| 509 | "-Xprofile-filename:output.txt " |
| 510 | "-Xprofile-period:123 " |
| 511 | "-Xprofile-duration:456 " |
| 512 | "-Xprofile-interval:789 " |
| 513 | "-Xprofile-backoff:2.0 " |
| 514 | "-Xprofile-start-immediately " |
| 515 | "-Xprofile-top-k-threshold:50.0 " |
| 516 | "-Xprofile-top-k-change-threshold:60.0 " |
| 517 | "-Xprofile-type:method " |
| 518 | "-Xprofile-max-stack-depth:1337", |
| 519 | M::ProfilerOpts); |
| 520 | } |
| 521 | |
| 522 | { |
| 523 | TestProfilerOptions opt = TestProfilerOptions(); |
| 524 | opt.profile_type_ = kProfilerBoundedStack; |
| 525 | |
| 526 | EXPECT_SINGLE_PARSE_VALUE(opt, |
| 527 | "-Xprofile-type:stack", |
| 528 | M::ProfilerOpts); |
| 529 | } |
| 530 | } // TEST_F |
| 531 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 532 | /* -X[no]experimental-lambdas */ |
| 533 | TEST_F(CmdlineParserTest, TestExperimentalLambdas) { |
| 534 | // Off by default |
| 535 | EXPECT_SINGLE_PARSE_DEFAULT_VALUE(false, |
| 536 | "", |
| 537 | M::ExperimentalLambdas); |
| 538 | |
| 539 | // Disabled explicitly |
| 540 | EXPECT_SINGLE_PARSE_VALUE(false, |
| 541 | "-Xnoexperimental-lambdas", |
| 542 | M::ExperimentalLambdas); |
| 543 | |
| 544 | // Enabled explicitly |
| 545 | EXPECT_SINGLE_PARSE_VALUE(true, |
| 546 | "-Xexperimental-lambdas", |
| 547 | M::ExperimentalLambdas); |
| 548 | } |
| 549 | |
Igor Murashkin | 7617abd | 2015-07-10 18:27:47 -0700 | [diff] [blame] | 550 | // -Xverify:_ |
| 551 | TEST_F(CmdlineParserTest, TestVerify) { |
| 552 | EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kNone, "-Xverify:none", M::Verify); |
| 553 | EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kEnable, "-Xverify:remote", M::Verify); |
| 554 | EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kEnable, "-Xverify:all", M::Verify); |
| 555 | EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kSoftFail, "-Xverify:softfail", M::Verify); |
| 556 | } |
| 557 | |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 558 | TEST_F(CmdlineParserTest, TestIgnoreUnrecognized) { |
| 559 | RuntimeParser::Builder parserBuilder; |
| 560 | |
| 561 | parserBuilder |
| 562 | .Define("-help") |
| 563 | .IntoKey(M::Help) |
| 564 | .IgnoreUnrecognized(true); |
| 565 | |
| 566 | parser_.reset(new RuntimeParser(parserBuilder.Build())); |
| 567 | |
| 568 | EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option"); |
| 569 | EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option1 --non-existent-option-2"); |
| 570 | } // TEST_F |
| 571 | |
| 572 | TEST_F(CmdlineParserTest, TestIgnoredArguments) { |
| 573 | std::initializer_list<const char*> ignored_args = { |
| 574 | "-ea", "-da", "-enableassertions", "-disableassertions", "--runtime-arg", "-esa", |
| 575 | "-dsa", "-enablesystemassertions", "-disablesystemassertions", "-Xrs", "-Xint:abdef", |
| 576 | "-Xdexopt:foobar", "-Xnoquithandler", "-Xjnigreflimit:ixnay", "-Xgenregmap", "-Xnogenregmap", |
| 577 | "-Xverifyopt:never", "-Xcheckdexsum", "-Xincludeselectedop", "-Xjitop:noop", |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 578 | "-Xincludeselectedmethod", "-Xjitblocking", "-Xjitmethod:_", "-Xjitclass:nosuchluck", |
| 579 | "-Xjitoffset:none", "-Xjitconfig:yes", "-Xjitcheckcg", "-Xjitverbose", "-Xjitprofile", |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 580 | "-Xjitdisableopt", "-Xjitsuspendpoll", "-XX:mainThreadStackSize=1337" |
| 581 | }; |
| 582 | |
| 583 | // Check they are ignored when parsed one at a time |
| 584 | for (auto&& arg : ignored_args) { |
| 585 | SCOPED_TRACE(arg); |
| 586 | EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(arg); |
| 587 | } |
| 588 | |
| 589 | // Check they are ignored when we pass it all together at once |
| 590 | std::vector<const char*> argv = ignored_args; |
| 591 | EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv); |
| 592 | } // TEST_F |
| 593 | |
| 594 | TEST_F(CmdlineParserTest, MultipleArguments) { |
| 595 | EXPECT_TRUE(IsResultSuccessful(parser_->Parse( |
| 596 | "-help -XX:ForegroundHeapGrowthMultiplier=0.5 " |
| 597 | "-Xnodex2oat -Xmethod-trace -XX:LargeObjectSpace=map"))); |
| 598 | |
| 599 | auto&& map = parser_->ReleaseArgumentsMap(); |
| 600 | EXPECT_EQ(5u, map.Size()); |
| 601 | EXPECT_KEY_VALUE(map, M::Help, Unit{}); // NOLINT [whitespace/braces] [5] |
| 602 | EXPECT_KEY_VALUE(map, M::ForegroundHeapGrowthMultiplier, 0.5); |
| 603 | EXPECT_KEY_VALUE(map, M::Dex2Oat, false); |
| 604 | EXPECT_KEY_VALUE(map, M::MethodTrace, Unit{}); // NOLINT [whitespace/braces] [5] |
| 605 | EXPECT_KEY_VALUE(map, M::LargeObjectSpace, gc::space::LargeObjectSpaceType::kMap); |
| 606 | } // TEST_F |
| 607 | } // namespace art |