blob: 5d672061dfbd83cf35fb371c5668cd50a171085a [file] [log] [blame]
Igor Murashkinaaebaa02015-01-26 10:55:53 -08001/*
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"
Igor Murashkinaaebaa02015-01-26 10:55:53 -080018
Igor Murashkinaaebaa02015-01-26 10:55:53 -080019#include <numeric>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070020
Igor Murashkinaaebaa02015-01-26 10:55:53 -080021#include "gtest/gtest.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070022
Alex Light40320712017-12-14 11:52:04 -080023#include "jdwp_provider.h"
Andreas Gampe2c30e4a2017-08-23 11:31:32 -070024#include "experimental_flags.h"
25#include "parsed_options.h"
26#include "runtime.h"
27#include "runtime_options.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070028#include "utils.h"
Igor Murashkinaaebaa02015-01-26 10:55:53 -080029
30#define EXPECT_NULL(expected) EXPECT_EQ(reinterpret_cast<const void*>(expected), \
Mathieu Chartier2cebb242015-04-21 16:50:40 -070031 reinterpret_cast<void*>(nullptr));
Igor Murashkinaaebaa02015-01-26 10:55:53 -080032
33namespace art {
34 bool UsuallyEquals(double expected, double actual);
35
36 // This has a gtest dependency, which is why it's in the gtest only.
Calin Juravle138dbff2016-06-28 19:36:58 +010037 bool operator==(const ProfileSaverOptions& lhs, const ProfileSaverOptions& rhs) {
Igor Murashkinaaebaa02015-01-26 10:55:53 -080038 return lhs.enabled_ == rhs.enabled_ &&
Calin Juravle138dbff2016-06-28 19:36:58 +010039 lhs.min_save_period_ms_ == rhs.min_save_period_ms_ &&
40 lhs.save_resolved_classes_delay_ms_ == rhs.save_resolved_classes_delay_ms_ &&
Mathieu Chartier7b135c82017-06-05 12:54:01 -070041 lhs.hot_startup_method_samples_ == rhs.hot_startup_method_samples_ &&
Calin Juravle138dbff2016-06-28 19:36:58 +010042 lhs.min_methods_to_save_ == rhs.min_methods_to_save_ &&
43 lhs.min_classes_to_save_ == rhs.min_classes_to_save_ &&
44 lhs.min_notification_before_wake_ == rhs.min_notification_before_wake_ &&
45 lhs.max_notification_before_wake_ == rhs.max_notification_before_wake_;
Igor Murashkinaaebaa02015-01-26 10:55:53 -080046 }
47
48 bool UsuallyEquals(double expected, double actual) {
49 using FloatingPoint = ::testing::internal::FloatingPoint<double>;
50
51 FloatingPoint exp(expected);
52 FloatingPoint act(actual);
53
54 // Compare with ULPs instead of comparing with ==
55 return exp.AlmostEquals(act);
56 }
57
58 template <typename T>
59 bool UsuallyEquals(const T& expected, const T& actual,
60 typename std::enable_if<
61 detail::SupportsEqualityOperator<T>::value>::type* = 0) {
62 return expected == actual;
63 }
64
65 // Try to use memcmp to compare simple plain-old-data structs.
66 //
67 // This should *not* generate false positives, but it can generate false negatives.
68 // This will mostly work except for fields like float which can have different bit patterns
69 // that are nevertheless equal.
70 // If a test is failing because the structs aren't "equal" when they really are
71 // then it's recommended to implement operator== for it instead.
72 template <typename T, typename ... Ignore>
73 bool UsuallyEquals(const T& expected, const T& actual,
74 const Ignore& ... more ATTRIBUTE_UNUSED,
75 typename std::enable_if<std::is_pod<T>::value>::type* = 0,
76 typename std::enable_if<!detail::SupportsEqualityOperator<T>::value>::type* = 0
77 ) {
78 return memcmp(std::addressof(expected), std::addressof(actual), sizeof(T)) == 0;
79 }
80
81 bool UsuallyEquals(const XGcOption& expected, const XGcOption& actual) {
82 return memcmp(std::addressof(expected), std::addressof(actual), sizeof(expected)) == 0;
83 }
84
Andreas Gampeca620d72016-11-08 08:09:33 -080085 bool UsuallyEquals(const char* expected, const std::string& actual) {
Igor Murashkinaaebaa02015-01-26 10:55:53 -080086 return std::string(expected) == actual;
87 }
88
89 template <typename TMap, typename TKey, typename T>
90 ::testing::AssertionResult IsExpectedKeyValue(const T& expected,
91 const TMap& map,
92 const TKey& key) {
93 auto* actual = map.Get(key);
94 if (actual != nullptr) {
95 if (!UsuallyEquals(expected, *actual)) {
96 return ::testing::AssertionFailure()
97 << "expected " << detail::ToStringAny(expected) << " but got "
98 << detail::ToStringAny(*actual);
99 }
100 return ::testing::AssertionSuccess();
101 }
102
103 return ::testing::AssertionFailure() << "key was not in the map";
104 }
105
Igor Murashkin158f35c2015-06-10 15:55:30 -0700106 template <typename TMap, typename TKey, typename T>
107 ::testing::AssertionResult IsExpectedDefaultKeyValue(const T& expected,
108 const TMap& map,
109 const TKey& key) {
110 const T& actual = map.GetOrDefault(key);
111 if (!UsuallyEquals(expected, actual)) {
112 return ::testing::AssertionFailure()
113 << "expected " << detail::ToStringAny(expected) << " but got "
114 << detail::ToStringAny(actual);
115 }
116 return ::testing::AssertionSuccess();
117 }
118
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800119class CmdlineParserTest : public ::testing::Test {
120 public:
121 CmdlineParserTest() = default;
122 ~CmdlineParserTest() = default;
123
124 protected:
125 using M = RuntimeArgumentMap;
126 using RuntimeParser = ParsedOptions::RuntimeParser;
127
128 static void SetUpTestCase() {
Andreas Gampe51d80cc2017-06-21 21:05:13 -0700129 art::InitLogging(nullptr, art::Runtime::Abort); // argv = null
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800130 }
131
132 virtual void SetUp() {
133 parser_ = ParsedOptions::MakeParser(false); // do not ignore unrecognized options
134 }
135
Andreas Gampeca620d72016-11-08 08:09:33 -0800136 static ::testing::AssertionResult IsResultSuccessful(const CmdlineResult& result) {
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800137 if (result.IsSuccess()) {
138 return ::testing::AssertionSuccess();
139 } else {
140 return ::testing::AssertionFailure()
141 << result.GetStatus() << " with: " << result.GetMessage();
142 }
143 }
144
Andreas Gampeca620d72016-11-08 08:09:33 -0800145 static ::testing::AssertionResult IsResultFailure(const CmdlineResult& result,
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800146 CmdlineResult::Status failure_status) {
147 if (result.IsSuccess()) {
148 return ::testing::AssertionFailure() << " got success but expected failure: "
149 << failure_status;
150 } else if (result.GetStatus() == failure_status) {
151 return ::testing::AssertionSuccess();
152 }
153
154 return ::testing::AssertionFailure() << " expected failure " << failure_status
155 << " but got " << result.GetStatus();
156 }
157
158 std::unique_ptr<RuntimeParser> parser_;
159};
160
161#define EXPECT_KEY_EXISTS(map, key) EXPECT_TRUE((map).Exists(key))
162#define EXPECT_KEY_VALUE(map, key, expected) EXPECT_TRUE(IsExpectedKeyValue(expected, map, key))
Igor Murashkin158f35c2015-06-10 15:55:30 -0700163#define EXPECT_DEFAULT_KEY_VALUE(map, key, expected) EXPECT_TRUE(IsExpectedDefaultKeyValue(expected, map, key))
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800164
Igor Murashkin158f35c2015-06-10 15:55:30 -0700165#define _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv) \
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800166 do { \
167 EXPECT_TRUE(IsResultSuccessful(parser_->Parse(argv))); \
168 EXPECT_EQ(0u, parser_->GetArgumentsMap().Size()); \
Igor Murashkin158f35c2015-06-10 15:55:30 -0700169
170#define EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv) \
171 _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv); \
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800172 } while (false)
173
Igor Murashkin158f35c2015-06-10 15:55:30 -0700174#define EXPECT_SINGLE_PARSE_DEFAULT_VALUE(expected, argv, key)\
175 _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv); \
176 RuntimeArgumentMap args = parser_->ReleaseArgumentsMap(); \
177 EXPECT_DEFAULT_KEY_VALUE(args, key, expected); \
178 } while (false) // NOLINT [readability/namespace] [5]
179
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800180#define _EXPECT_SINGLE_PARSE_EXISTS(argv, key) \
181 do { \
182 EXPECT_TRUE(IsResultSuccessful(parser_->Parse(argv))); \
183 RuntimeArgumentMap args = parser_->ReleaseArgumentsMap(); \
184 EXPECT_EQ(1u, args.Size()); \
185 EXPECT_KEY_EXISTS(args, key); \
186
187#define EXPECT_SINGLE_PARSE_EXISTS(argv, key) \
188 _EXPECT_SINGLE_PARSE_EXISTS(argv, key); \
189 } while (false)
190
191#define EXPECT_SINGLE_PARSE_VALUE(expected, argv, key) \
192 _EXPECT_SINGLE_PARSE_EXISTS(argv, key); \
193 EXPECT_KEY_VALUE(args, key, expected); \
Igor Murashkin5573c372017-11-16 13:34:30 -0800194 } while (false)
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800195
196#define EXPECT_SINGLE_PARSE_VALUE_STR(expected, argv, key) \
197 EXPECT_SINGLE_PARSE_VALUE(std::string(expected), argv, key)
198
199#define EXPECT_SINGLE_PARSE_FAIL(argv, failure_status) \
200 do { \
201 EXPECT_TRUE(IsResultFailure(parser_->Parse(argv), failure_status));\
202 RuntimeArgumentMap args = parser_->ReleaseArgumentsMap();\
203 EXPECT_EQ(0u, args.Size()); \
204 } while (false)
205
206TEST_F(CmdlineParserTest, TestSimpleSuccesses) {
207 auto& parser = *parser_;
208
209 EXPECT_LT(0u, parser.CountDefinedArguments());
210
211 {
212 // Test case 1: No command line arguments
213 EXPECT_TRUE(IsResultSuccessful(parser.Parse("")));
214 RuntimeArgumentMap args = parser.ReleaseArgumentsMap();
215 EXPECT_EQ(0u, args.Size());
216 }
217
218 EXPECT_SINGLE_PARSE_EXISTS("-Xzygote", M::Zygote);
219 EXPECT_SINGLE_PARSE_VALUE_STR("/hello/world", "-Xbootclasspath:/hello/world", M::BootClassPath);
220 EXPECT_SINGLE_PARSE_VALUE("/hello/world", "-Xbootclasspath:/hello/world", M::BootClassPath);
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800221 EXPECT_SINGLE_PARSE_VALUE(Memory<1>(234), "-Xss234", M::StackSize);
222 EXPECT_SINGLE_PARSE_VALUE(MemoryKiB(1234*MB), "-Xms1234m", M::MemoryInitialSize);
223 EXPECT_SINGLE_PARSE_VALUE(true, "-XX:EnableHSpaceCompactForOOM", M::EnableHSpaceCompactForOOM);
224 EXPECT_SINGLE_PARSE_VALUE(false, "-XX:DisableHSpaceCompactForOOM", M::EnableHSpaceCompactForOOM);
225 EXPECT_SINGLE_PARSE_VALUE(0.5, "-XX:HeapTargetUtilization=0.5", M::HeapTargetUtilization);
226 EXPECT_SINGLE_PARSE_VALUE(5u, "-XX:ParallelGCThreads=5", M::ParallelGCThreads);
Jean Christophe Beyler24e04aa2014-09-12 12:03:25 -0700227 EXPECT_SINGLE_PARSE_EXISTS("-Xno-dex-file-fallback", M::NoDexFileFallback);
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800228} // TEST_F
229
230TEST_F(CmdlineParserTest, TestSimpleFailures) {
231 // Test argument is unknown to the parser
232 EXPECT_SINGLE_PARSE_FAIL("abcdefg^%@#*(@#", CmdlineResult::kUnknown);
233 // Test value map substitution fails
234 EXPECT_SINGLE_PARSE_FAIL("-Xverify:whatever", CmdlineResult::kFailure);
235 // Test value type parsing failures
236 EXPECT_SINGLE_PARSE_FAIL("-Xsswhatever", CmdlineResult::kFailure); // invalid memory value
237 EXPECT_SINGLE_PARSE_FAIL("-Xms123", CmdlineResult::kFailure); // memory value too small
238 EXPECT_SINGLE_PARSE_FAIL("-XX:HeapTargetUtilization=0.0", CmdlineResult::kOutOfRange); // toosmal
239 EXPECT_SINGLE_PARSE_FAIL("-XX:HeapTargetUtilization=2.0", CmdlineResult::kOutOfRange); // toolarg
240 EXPECT_SINGLE_PARSE_FAIL("-XX:ParallelGCThreads=-5", CmdlineResult::kOutOfRange); // too small
241 EXPECT_SINGLE_PARSE_FAIL("-Xgc:blablabla", CmdlineResult::kUsage); // not a valid suboption
242} // TEST_F
243
244TEST_F(CmdlineParserTest, TestLogVerbosity) {
245 {
246 const char* log_args = "-verbose:"
Phil Wang751beff2015-08-28 15:17:15 +0800247 "class,compiler,gc,heap,jdwp,jni,monitor,profiler,signals,simulator,startup,"
Andreas Gampe92d77202017-12-06 20:49:00 -0800248 "third-party-jni,threads,verifier,verifier-debug";
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800249
250 LogVerbosity log_verbosity = LogVerbosity();
251 log_verbosity.class_linker = true;
252 log_verbosity.compiler = true;
253 log_verbosity.gc = true;
254 log_verbosity.heap = true;
255 log_verbosity.jdwp = true;
256 log_verbosity.jni = true;
257 log_verbosity.monitor = true;
258 log_verbosity.profiler = true;
259 log_verbosity.signals = true;
Phil Wang751beff2015-08-28 15:17:15 +0800260 log_verbosity.simulator = true;
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800261 log_verbosity.startup = true;
262 log_verbosity.third_party_jni = true;
263 log_verbosity.threads = true;
264 log_verbosity.verifier = true;
Andreas Gampe92d77202017-12-06 20:49:00 -0800265 log_verbosity.verifier_debug = true;
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800266
267 EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
268 }
269
270 {
271 const char* log_args = "-verbose:"
272 "class,compiler,gc,heap,jdwp,jni,monitor";
273
274 LogVerbosity log_verbosity = LogVerbosity();
275 log_verbosity.class_linker = true;
276 log_verbosity.compiler = true;
277 log_verbosity.gc = true;
278 log_verbosity.heap = true;
279 log_verbosity.jdwp = true;
280 log_verbosity.jni = true;
281 log_verbosity.monitor = true;
282
283 EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
284 }
285
286 EXPECT_SINGLE_PARSE_FAIL("-verbose:blablabla", CmdlineResult::kUsage); // invalid verbose opt
Richard Uhler66d874d2015-01-15 09:37:19 -0800287
288 {
Sebastien Hertzbba348e2015-06-01 08:28:18 +0200289 const char* log_args = "-verbose:deopt";
290 LogVerbosity log_verbosity = LogVerbosity();
291 log_verbosity.deopt = true;
292 EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
293 }
294
295 {
Mathieu Chartier66a55392016-02-19 10:25:39 -0800296 const char* log_args = "-verbose:collector";
297 LogVerbosity log_verbosity = LogVerbosity();
298 log_verbosity.collector = true;
299 EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
300 }
301
302 {
Richard Uhler66d874d2015-01-15 09:37:19 -0800303 const char* log_args = "-verbose:oat";
304 LogVerbosity log_verbosity = LogVerbosity();
305 log_verbosity.oat = true;
306 EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
307 }
Andreas Gampebec07a02017-04-11 13:48:37 -0700308
309 {
310 const char* log_args = "-verbose:dex";
311 LogVerbosity log_verbosity = LogVerbosity();
312 log_verbosity.dex = true;
313 EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
314 }
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800315} // TEST_F
316
Nicolas Geoffray8f4ee5c2015-02-05 10:14:10 +0000317// TODO: Enable this b/19274810
318TEST_F(CmdlineParserTest, DISABLED_TestXGcOption) {
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800319 /*
320 * Test success
321 */
322 {
Igor Murashkin5573c372017-11-16 13:34:30 -0800323 XGcOption option_all_true{};
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800324 option_all_true.collector_type_ = gc::CollectorType::kCollectorTypeCMS;
325 option_all_true.verify_pre_gc_heap_ = true;
326 option_all_true.verify_pre_sweeping_heap_ = true;
327 option_all_true.verify_post_gc_heap_ = true;
328 option_all_true.verify_pre_gc_rosalloc_ = true;
329 option_all_true.verify_pre_sweeping_rosalloc_ = true;
330 option_all_true.verify_post_gc_rosalloc_ = true;
331
332 const char * xgc_args_all_true = "-Xgc:concurrent,"
333 "preverify,presweepingverify,postverify,"
334 "preverify_rosalloc,presweepingverify_rosalloc,"
335 "postverify_rosalloc,precise,"
336 "verifycardtable";
337
338 EXPECT_SINGLE_PARSE_VALUE(option_all_true, xgc_args_all_true, M::GcOption);
339
Igor Murashkin5573c372017-11-16 13:34:30 -0800340 XGcOption option_all_false{};
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800341 option_all_false.collector_type_ = gc::CollectorType::kCollectorTypeMS;
342 option_all_false.verify_pre_gc_heap_ = false;
343 option_all_false.verify_pre_sweeping_heap_ = false;
344 option_all_false.verify_post_gc_heap_ = false;
345 option_all_false.verify_pre_gc_rosalloc_ = false;
346 option_all_false.verify_pre_sweeping_rosalloc_ = false;
347 option_all_false.verify_post_gc_rosalloc_ = false;
348
349 const char* xgc_args_all_false = "-Xgc:nonconcurrent,"
350 "nopreverify,nopresweepingverify,nopostverify,nopreverify_rosalloc,"
351 "nopresweepingverify_rosalloc,nopostverify_rosalloc,noprecise,noverifycardtable";
352
353 EXPECT_SINGLE_PARSE_VALUE(option_all_false, xgc_args_all_false, M::GcOption);
354
Igor Murashkin5573c372017-11-16 13:34:30 -0800355 XGcOption option_all_default{};
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800356
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800357 const char* xgc_args_blank = "-Xgc:";
358 EXPECT_SINGLE_PARSE_VALUE(option_all_default, xgc_args_blank, M::GcOption);
359 }
360
361 /*
362 * Test failures
363 */
364 EXPECT_SINGLE_PARSE_FAIL("-Xgc:blablabla", CmdlineResult::kUsage); // invalid Xgc opt
365} // TEST_F
366
367/*
Alex Light40320712017-12-14 11:52:04 -0800368 * { "-XjdwpProvider:_" }
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800369 */
Alex Light40320712017-12-14 11:52:04 -0800370TEST_F(CmdlineParserTest, TestJdwpProviderEmpty) {
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800371 {
Alex Light40320712017-12-14 11:52:04 -0800372 EXPECT_SINGLE_PARSE_DEFAULT_VALUE(JdwpProvider::kInternal, "", M::JdwpProvider);
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800373 }
Alex Light40320712017-12-14 11:52:04 -0800374} // TEST_F
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800375
Alex Light40320712017-12-14 11:52:04 -0800376TEST_F(CmdlineParserTest, TestJdwpProviderDefault) {
377 const char* opt_args = "-XjdwpProvider:default";
378 EXPECT_SINGLE_PARSE_VALUE(JdwpProvider::kInternal, opt_args, M::JdwpProvider);
379} // TEST_F
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800380
Alex Light40320712017-12-14 11:52:04 -0800381TEST_F(CmdlineParserTest, TestJdwpProviderInternal) {
382 const char* opt_args = "-XjdwpProvider:internal";
383 EXPECT_SINGLE_PARSE_VALUE(JdwpProvider::kInternal, opt_args, M::JdwpProvider);
384} // TEST_F
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800385
Alex Light40320712017-12-14 11:52:04 -0800386TEST_F(CmdlineParserTest, TestJdwpProviderNone) {
387 const char* opt_args = "-XjdwpProvider:none";
388 EXPECT_SINGLE_PARSE_VALUE(JdwpProvider::kNone, opt_args, M::JdwpProvider);
389} // TEST_F
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800390
Alex Lightfbf96702017-12-14 13:27:13 -0800391TEST_F(CmdlineParserTest, TestJdwpProviderAdbconnection) {
392 const char* opt_args = "-XjdwpProvider:adbconnection";
393 EXPECT_SINGLE_PARSE_VALUE(JdwpProvider::kAdbConnection, opt_args, M::JdwpProvider);
394} // TEST_F
395
Alex Light40320712017-12-14 11:52:04 -0800396TEST_F(CmdlineParserTest, TestJdwpProviderHelp) {
397 EXPECT_SINGLE_PARSE_FAIL("-XjdwpProvider:help", CmdlineResult::kUsage);
398} // TEST_F
399
400TEST_F(CmdlineParserTest, TestJdwpProviderFail) {
401 EXPECT_SINGLE_PARSE_FAIL("-XjdwpProvider:blablabla", CmdlineResult::kFailure);
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800402} // TEST_F
403
404/*
405 * -D_ -D_ -D_ ...
406 */
407TEST_F(CmdlineParserTest, TestPropertiesList) {
408 /*
409 * Test successes
410 */
411 {
412 std::vector<std::string> opt = {"hello"};
413
414 EXPECT_SINGLE_PARSE_VALUE(opt, "-Dhello", M::PropertiesList);
415 }
416
417 {
418 std::vector<std::string> opt = {"hello", "world"};
419
420 EXPECT_SINGLE_PARSE_VALUE(opt, "-Dhello -Dworld", M::PropertiesList);
421 }
422
423 {
424 std::vector<std::string> opt = {"one", "two", "three"};
425
426 EXPECT_SINGLE_PARSE_VALUE(opt, "-Done -Dtwo -Dthree", M::PropertiesList);
427 }
428} // TEST_F
429
430/*
431* -Xcompiler-option foo -Xcompiler-option bar ...
432*/
433TEST_F(CmdlineParserTest, TestCompilerOption) {
434 /*
435 * Test successes
436 */
437 {
438 std::vector<std::string> opt = {"hello"};
439 EXPECT_SINGLE_PARSE_VALUE(opt, "-Xcompiler-option hello", M::CompilerOptions);
440 }
441
442 {
443 std::vector<std::string> opt = {"hello", "world"};
444 EXPECT_SINGLE_PARSE_VALUE(opt,
445 "-Xcompiler-option hello -Xcompiler-option world",
446 M::CompilerOptions);
447 }
448
449 {
450 std::vector<std::string> opt = {"one", "two", "three"};
451 EXPECT_SINGLE_PARSE_VALUE(opt,
452 "-Xcompiler-option one -Xcompiler-option two -Xcompiler-option three",
453 M::CompilerOptions);
454 }
455} // TEST_F
456
457/*
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800458* -Xjit, -Xnojit, -Xjitcodecachesize, Xjitcompilethreshold
459*/
460TEST_F(CmdlineParserTest, TestJitOptions) {
461 /*
462 * Test successes
463 */
464 {
Calin Juravleffc87072016-04-20 14:22:09 +0100465 EXPECT_SINGLE_PARSE_VALUE(true, "-Xusejit:true", M::UseJitCompilation);
466 EXPECT_SINGLE_PARSE_VALUE(false, "-Xusejit:false", M::UseJitCompilation);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800467 }
468 {
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000469 EXPECT_SINGLE_PARSE_VALUE(
Nicolas Geoffray295a5962015-11-19 18:17:41 +0000470 MemoryKiB(16 * KB), "-Xjitinitialsize:16K", M::JITCodeCacheInitialCapacity);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000471 EXPECT_SINGLE_PARSE_VALUE(
Nicolas Geoffray295a5962015-11-19 18:17:41 +0000472 MemoryKiB(16 * MB), "-Xjitmaxsize:16M", M::JITCodeCacheMaxCapacity);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800473 }
474 {
475 EXPECT_SINGLE_PARSE_VALUE(12345u, "-Xjitthreshold:12345", M::JITCompileThreshold);
476 }
477} // TEST_F
478
479/*
Calin Juravle138dbff2016-06-28 19:36:58 +0100480* -Xps-*
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800481*/
Calin Juravle138dbff2016-06-28 19:36:58 +0100482TEST_F(CmdlineParserTest, ProfileSaverOptions) {
Mathieu Chartier885a7132017-06-10 14:35:11 -0700483 ProfileSaverOptions opt = ProfileSaverOptions(true, 1, 2, 3, 4, 5, 6, 7, "abc", true);
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800484
Calin Juravle138dbff2016-06-28 19:36:58 +0100485 EXPECT_SINGLE_PARSE_VALUE(opt,
486 "-Xjitsaveprofilinginfo "
487 "-Xps-min-save-period-ms:1 "
488 "-Xps-save-resolved-classes-delay-ms:2 "
Mathieu Chartier7b135c82017-06-05 12:54:01 -0700489 "-Xps-hot-startup-method-samples:3 "
Calin Juravle138dbff2016-06-28 19:36:58 +0100490 "-Xps-min-methods-to-save:4 "
491 "-Xps-min-classes-to-save:5 "
492 "-Xps-min-notification-before-wake:6 "
Calin Juravle9545f6d2017-03-16 19:05:09 -0700493 "-Xps-max-notification-before-wake:7 "
Mathieu Chartier885a7132017-06-10 14:35:11 -0700494 "-Xps-profile-path:abc "
495 "-Xps-profile-boot-class-path",
Calin Juravle138dbff2016-06-28 19:36:58 +0100496 M::ProfileSaverOpts);
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800497} // TEST_F
498
Alex Lighteb7c1442015-08-31 13:17:42 -0700499/* -Xexperimental:_ */
500TEST_F(CmdlineParserTest, TestExperimentalFlags) {
Neil Fuller9724c632016-01-07 15:42:47 +0000501 // Default
Alex Lighteb7c1442015-08-31 13:17:42 -0700502 EXPECT_SINGLE_PARSE_DEFAULT_VALUE(ExperimentalFlags::kNone,
Igor Murashkin158f35c2015-06-10 15:55:30 -0700503 "",
Alex Lighteb7c1442015-08-31 13:17:42 -0700504 M::Experimental);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700505
506 // Disabled explicitly
Alex Lighteb7c1442015-08-31 13:17:42 -0700507 EXPECT_SINGLE_PARSE_VALUE(ExperimentalFlags::kNone,
508 "-Xexperimental:none",
509 M::Experimental);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700510}
511
Igor Murashkin7617abd2015-07-10 18:27:47 -0700512// -Xverify:_
513TEST_F(CmdlineParserTest, TestVerify) {
514 EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kNone, "-Xverify:none", M::Verify);
515 EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kEnable, "-Xverify:remote", M::Verify);
516 EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kEnable, "-Xverify:all", M::Verify);
517 EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kSoftFail, "-Xverify:softfail", M::Verify);
518}
519
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800520TEST_F(CmdlineParserTest, TestIgnoreUnrecognized) {
521 RuntimeParser::Builder parserBuilder;
522
523 parserBuilder
524 .Define("-help")
525 .IntoKey(M::Help)
526 .IgnoreUnrecognized(true);
527
528 parser_.reset(new RuntimeParser(parserBuilder.Build()));
529
530 EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option");
531 EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option1 --non-existent-option-2");
532} // TEST_F
533
534TEST_F(CmdlineParserTest, TestIgnoredArguments) {
535 std::initializer_list<const char*> ignored_args = {
536 "-ea", "-da", "-enableassertions", "-disableassertions", "--runtime-arg", "-esa",
537 "-dsa", "-enablesystemassertions", "-disablesystemassertions", "-Xrs", "-Xint:abdef",
538 "-Xdexopt:foobar", "-Xnoquithandler", "-Xjnigreflimit:ixnay", "-Xgenregmap", "-Xnogenregmap",
539 "-Xverifyopt:never", "-Xcheckdexsum", "-Xincludeselectedop", "-Xjitop:noop",
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800540 "-Xincludeselectedmethod", "-Xjitblocking", "-Xjitmethod:_", "-Xjitclass:nosuchluck",
541 "-Xjitoffset:none", "-Xjitconfig:yes", "-Xjitcheckcg", "-Xjitverbose", "-Xjitprofile",
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800542 "-Xjitdisableopt", "-Xjitsuspendpoll", "-XX:mainThreadStackSize=1337"
543 };
544
545 // Check they are ignored when parsed one at a time
546 for (auto&& arg : ignored_args) {
547 SCOPED_TRACE(arg);
548 EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(arg);
549 }
550
551 // Check they are ignored when we pass it all together at once
552 std::vector<const char*> argv = ignored_args;
553 EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv);
554} // TEST_F
555
556TEST_F(CmdlineParserTest, MultipleArguments) {
557 EXPECT_TRUE(IsResultSuccessful(parser_->Parse(
558 "-help -XX:ForegroundHeapGrowthMultiplier=0.5 "
559 "-Xnodex2oat -Xmethod-trace -XX:LargeObjectSpace=map")));
560
561 auto&& map = parser_->ReleaseArgumentsMap();
562 EXPECT_EQ(5u, map.Size());
Igor Murashkin5573c372017-11-16 13:34:30 -0800563 EXPECT_KEY_VALUE(map, M::Help, Unit{});
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800564 EXPECT_KEY_VALUE(map, M::ForegroundHeapGrowthMultiplier, 0.5);
565 EXPECT_KEY_VALUE(map, M::Dex2Oat, false);
Igor Murashkin5573c372017-11-16 13:34:30 -0800566 EXPECT_KEY_VALUE(map, M::MethodTrace, Unit{});
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800567 EXPECT_KEY_VALUE(map, M::LargeObjectSpace, gc::space::LargeObjectSpaceType::kMap);
568} // TEST_F
569} // namespace art