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 <gtest/gtest.h>
18
19 #include "command.h"
20
21 using namespace simpleperf;
22
23 class MockCommand : public Command {
24 public:
MockCommand()25 MockCommand() : Command("mock", "mock_short_help", "mock_long_help") {
26 }
27
Run(const std::vector<std::string> &)28 bool Run(const std::vector<std::string>&) override {
29 return true;
30 }
31 };
32
TEST(command,CreateCommandInstance)33 TEST(command, CreateCommandInstance) {
34 ASSERT_TRUE(CreateCommandInstance("mock1") == nullptr);
35 RegisterCommand("mock1", [] { return std::unique_ptr<Command>(new MockCommand); });
36 ASSERT_TRUE(CreateCommandInstance("mock1") != nullptr);
37 UnRegisterCommand("mock1");
38 ASSERT_TRUE(CreateCommandInstance("mock1") == nullptr);
39 }
40
TEST(command,GetAllCommands)41 TEST(command, GetAllCommands) {
42 size_t command_count = GetAllCommandNames().size();
43 RegisterCommand("mock1", [] { return std::unique_ptr<Command>(new MockCommand); });
44 ASSERT_EQ(command_count + 1, GetAllCommandNames().size());
45 UnRegisterCommand("mock1");
46 ASSERT_EQ(command_count, GetAllCommandNames().size());
47 }
48
TEST(command,GetValueForOption)49 TEST(command, GetValueForOption) {
50 MockCommand command;
51 uint64_t value;
52 size_t i;
53 for (bool allow_suffixes : {true, false}) {
54 i = 0;
55 ASSERT_TRUE(command.GetUintOption({"-s", "156"}, &i, &value, 0,
56 std::numeric_limits<uint64_t>::max(), allow_suffixes));
57 ASSERT_EQ(i, 1u);
58 ASSERT_EQ(value, 156u);
59 }
60 i = 0;
61 ASSERT_TRUE(command.GetUintOption({"-s", "156k"}, &i, &value, 0,
62 std::numeric_limits<uint64_t>::max(), true));
63 ASSERT_EQ(value, 156 * (1ULL << 10));
64 i = 0;
65 ASSERT_FALSE(command.GetUintOption({"-s"}, &i, &value));
66 i = 0;
67 ASSERT_FALSE(command.GetUintOption({"-s", "0"}, &i, &value, 1));
68 i = 0;
69 ASSERT_FALSE(command.GetUintOption({"-s", "156"}, &i, &value, 0, 155));
70 i = 0;
71 double double_value;
72 ASSERT_TRUE(command.GetDoubleOption({"-s", "3.2"}, &i, &double_value, 0, 4));
73 ASSERT_DOUBLE_EQ(double_value, 3.2);
74 }
75
TEST(command,PreprocessOptions)76 TEST(command, PreprocessOptions) {
77 MockCommand cmd;
78 OptionValueMap options;
79 std::vector<std::pair<OptionName, OptionValue>> ordered_options;
80 std::vector<std::string> non_option_args;
81
82 OptionFormatMap option_formats = {
83 {"--bool-option", {OptionValueType::NONE, OptionType::SINGLE, AppRunnerType::ALLOWED}},
84 {"--str-option", {OptionValueType::STRING, OptionType::MULTIPLE, AppRunnerType::ALLOWED}},
85 {"--opt-str-option",
86 {OptionValueType::OPT_STRING, OptionType::MULTIPLE, AppRunnerType::ALLOWED}},
87 {"--uint-option", {OptionValueType::UINT, OptionType::SINGLE, AppRunnerType::ALLOWED}},
88 {"--double-option", {OptionValueType::DOUBLE, OptionType::SINGLE, AppRunnerType::ALLOWED}},
89
90 // ordered options
91 {"--ord-str-option", {OptionValueType::STRING, OptionType::ORDERED, AppRunnerType::ALLOWED}},
92 {"--ord-uint-option", {OptionValueType::UINT, OptionType::ORDERED, AppRunnerType::ALLOWED}},
93 };
94
95 // Check options.
96 std::vector<std::string> args = {"--bool-option",
97 "--str-option",
98 "str1",
99 "--str-option",
100 "str2",
101 "--opt-str-option",
102 "--opt-str-option",
103 "opt_str",
104 "--uint-option",
105 "34",
106 "--double-option",
107 "-32.75"};
108 ASSERT_TRUE(cmd.PreprocessOptions(args, option_formats, &options, &ordered_options, nullptr));
109 ASSERT_TRUE(options.PullBoolValue("--bool-option"));
110 auto values = options.PullValues("--str-option").value();
111 ASSERT_EQ(values.size(), 2);
112 ASSERT_EQ(*values[0].str_value, "str1");
113 ASSERT_EQ(*values[1].str_value, "str2");
114 values = options.PullValues("--opt-str-option").value();
115 ASSERT_EQ(values.size(), 2);
116 ASSERT_TRUE(values[0].str_value == nullptr);
117 ASSERT_EQ(*values[1].str_value, "opt_str");
118 size_t uint_value;
119 ASSERT_TRUE(options.PullUintValue("--uint-option", &uint_value));
120 ASSERT_EQ(uint_value, 34);
121 double double_value;
122 ASSERT_TRUE(options.PullDoubleValue("--double-option", &double_value));
123 ASSERT_DOUBLE_EQ(double_value, -32.75);
124 ASSERT_TRUE(options.values.empty());
125
126 // Check ordered options.
127 args = {"--ord-str-option", "str1", "--ord-uint-option", "32", "--ord-str-option", "str2"};
128 ASSERT_TRUE(cmd.PreprocessOptions(args, option_formats, &options, &ordered_options, nullptr));
129 ASSERT_EQ(ordered_options.size(), 3);
130 ASSERT_EQ(ordered_options[0].first, "--ord-str-option");
131 ASSERT_EQ(*(ordered_options[0].second.str_value), "str1");
132 ASSERT_EQ(ordered_options[1].first, "--ord-uint-option");
133 ASSERT_EQ(ordered_options[1].second.uint_value, 32);
134 ASSERT_EQ(ordered_options[2].first, "--ord-str-option");
135 ASSERT_EQ(*(ordered_options[2].second.str_value), "str2");
136
137 // Check non_option_args.
138 ASSERT_TRUE(cmd.PreprocessOptions({"arg1", "--arg2"}, option_formats, &options, &ordered_options,
139 &non_option_args));
140 ASSERT_EQ(non_option_args, std::vector<std::string>({"arg1", "--arg2"}));
141 // "--" can force following args to be non_option_args.
142 ASSERT_TRUE(cmd.PreprocessOptions({"--", "--bool-option"}, option_formats, &options,
143 &ordered_options, &non_option_args));
144 ASSERT_EQ(non_option_args, std::vector<std::string>({"--bool-option"}));
145
146 // Check different errors.
147 // unknown option
148 ASSERT_FALSE(cmd.PreprocessOptions({"--unknown-option"}, option_formats, &options,
149 &ordered_options, nullptr));
150 // no option value
151 ASSERT_FALSE(
152 cmd.PreprocessOptions({"--str-option"}, option_formats, &options, &ordered_options, nullptr));
153 // wrong option value format
154 ASSERT_FALSE(cmd.PreprocessOptions({"--uint-option", "-2"}, option_formats, &options,
155 &ordered_options, nullptr));
156 ASSERT_FALSE(cmd.PreprocessOptions({"--double-option", "str"}, option_formats, &options,
157 &ordered_options, nullptr));
158 // unexpected non_option_args
159 ASSERT_FALSE(cmd.PreprocessOptions({"non_option_args"}, option_formats, &options,
160 &ordered_options, nullptr));
161 }
162
TEST(command,OptionValueMap)163 TEST(command, OptionValueMap) {
164 OptionValue value;
165 value.uint_value = 10;
166
167 OptionValueMap options;
168 uint64_t uint_value;
169 options.values.emplace("--uint-option", value);
170 ASSERT_FALSE(options.PullUintValue("--uint-option", &uint_value, 11));
171 options.values.emplace("--uint-option", value);
172 ASSERT_FALSE(options.PullUintValue("--uint-option", &uint_value, 0, 9));
173 options.values.emplace("--uint-option", value);
174 ASSERT_TRUE(options.PullUintValue("--uint-option", &uint_value, 10, 10));
175
176 double double_value;
177 value.double_value = 0.0;
178 options.values.emplace("--double-option", value);
179 ASSERT_FALSE(options.PullDoubleValue("--double-option", &double_value, 1.0));
180 options.values.emplace("--double-option", value);
181 ASSERT_FALSE(options.PullDoubleValue("--double-option", &double_value, -2.0, -1.0));
182 options.values.emplace("--double-option", value);
183 ASSERT_TRUE(options.PullDoubleValue("--double-option", &double_value, 0.0, 0.0));
184 }
185