xref: /aosp_15_r20/system/extras/simpleperf/command_test.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
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 
Run(const std::vector<std::string> &)27   bool Run(const std::vector<std::string>&) override { return true; }
28 };
29 
30 // @CddTest = 6.1/C-0-2
TEST(command,CreateCommandInstance)31 TEST(command, CreateCommandInstance) {
32   ASSERT_TRUE(CreateCommandInstance("mock1") == nullptr);
33   RegisterCommand("mock1", [] { return std::unique_ptr<Command>(new MockCommand); });
34   ASSERT_TRUE(CreateCommandInstance("mock1") != nullptr);
35   UnRegisterCommand("mock1");
36   ASSERT_TRUE(CreateCommandInstance("mock1") == nullptr);
37 }
38 
39 // @CddTest = 6.1/C-0-2
TEST(command,GetAllCommands)40 TEST(command, GetAllCommands) {
41   size_t command_count = GetAllCommandNames().size();
42   RegisterCommand("mock1", [] { return std::unique_ptr<Command>(new MockCommand); });
43   ASSERT_EQ(command_count + 1, GetAllCommandNames().size());
44   UnRegisterCommand("mock1");
45   ASSERT_EQ(command_count, GetAllCommandNames().size());
46 }
47 
48 // @CddTest = 6.1/C-0-2
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 
76 // @CddTest = 6.1/C-0-2
TEST(command,PreprocessOptions)77 TEST(command, PreprocessOptions) {
78   MockCommand cmd;
79   OptionValueMap options;
80   std::vector<std::pair<OptionName, OptionValue>> ordered_options;
81   std::vector<std::string> non_option_args;
82 
83   OptionFormatMap option_formats = {
84       {"--bool-option", {OptionValueType::NONE, OptionType::SINGLE}},
85       {"--str-option", {OptionValueType::STRING, OptionType::MULTIPLE}},
86       {"--str2-option", {OptionValueType::STRING, OptionType::SINGLE}},
87       {"--opt-str-option", {OptionValueType::OPT_STRING, OptionType::MULTIPLE}},
88       {"--opt-str-after-equal-option",
89        {OptionValueType::OPT_STRING_AFTER_EQUAL, OptionType::MULTIPLE}},
90       {"--uint-option", {OptionValueType::UINT, OptionType::SINGLE}},
91       {"--double-option", {OptionValueType::DOUBLE, OptionType::SINGLE}},
92 
93       // ordered options
94       {"--ord-str-option", {OptionValueType::STRING, OptionType::ORDERED}},
95       {"--ord-uint-option", {OptionValueType::UINT, OptionType::ORDERED}},
96   };
97 
98   // Check options.
99   std::vector<std::string> args = {"--bool-option",
100                                    "--str-option",
101                                    "str1",
102                                    "--str-option",
103                                    "str1_2",
104                                    "--str2-option",
105                                    "str2_value",
106                                    "--opt-str-option",
107                                    "--opt-str-option",
108                                    "opt_str",
109                                    "--opt-str-after-equal-option",
110                                    "--opt-str-after-equal-option=str3",
111                                    "--uint-option",
112                                    "34",
113                                    "--double-option",
114                                    "-32.75"};
115   ASSERT_TRUE(cmd.PreprocessOptions(args, option_formats, &options, &ordered_options, nullptr));
116   ASSERT_TRUE(options.PullBoolValue("--bool-option"));
117   auto values = options.PullValues("--str-option");
118   ASSERT_EQ(values.size(), 2);
119   ASSERT_EQ(values[0].str_value, "str1");
120   ASSERT_EQ(values[1].str_value, "str1_2");
121   std::string str2_value;
122   options.PullStringValue("--str2-option", &str2_value);
123   ASSERT_EQ(str2_value, "str2_value");
124   values = options.PullValues("--opt-str-option");
125   ASSERT_EQ(values.size(), 2);
126   ASSERT_TRUE(values[0].str_value.empty());
127   ASSERT_EQ(values[1].str_value, "opt_str");
128   values = options.PullValues("--opt-str-after-equal-option");
129   ASSERT_EQ(values.size(), 2);
130   ASSERT_TRUE(values[0].str_value.empty());
131   ASSERT_EQ(values[1].str_value, "str3");
132   size_t uint_value;
133   ASSERT_TRUE(options.PullUintValue("--uint-option", &uint_value));
134   ASSERT_EQ(uint_value, 34);
135   double double_value;
136   ASSERT_TRUE(options.PullDoubleValue("--double-option", &double_value));
137   ASSERT_DOUBLE_EQ(double_value, -32.75);
138   ASSERT_TRUE(options.values.empty());
139 
140   // Check ordered options.
141   args = {"--ord-str-option", "str1", "--ord-uint-option", "32", "--ord-str-option", "str2"};
142   ASSERT_TRUE(cmd.PreprocessOptions(args, option_formats, &options, &ordered_options, nullptr));
143   ASSERT_EQ(ordered_options.size(), 3);
144   ASSERT_EQ(ordered_options[0].first, "--ord-str-option");
145   ASSERT_EQ(ordered_options[0].second.str_value, "str1");
146   ASSERT_EQ(ordered_options[1].first, "--ord-uint-option");
147   ASSERT_EQ(ordered_options[1].second.uint_value, 32);
148   ASSERT_EQ(ordered_options[2].first, "--ord-str-option");
149   ASSERT_EQ(ordered_options[2].second.str_value, "str2");
150 
151   // Check non_option_args.
152   ASSERT_TRUE(cmd.PreprocessOptions({"arg1", "--arg2"}, option_formats, &options, &ordered_options,
153                                     &non_option_args));
154   ASSERT_EQ(non_option_args, std::vector<std::string>({"arg1", "--arg2"}));
155   // "--" can force following args to be non_option_args.
156   ASSERT_TRUE(cmd.PreprocessOptions({"--", "--bool-option"}, option_formats, &options,
157                                     &ordered_options, &non_option_args));
158   ASSERT_EQ(non_option_args, std::vector<std::string>({"--bool-option"}));
159   // Pass nullptr to not accept non option args.
160   ASSERT_FALSE(cmd.PreprocessOptions({"non_option_arg"}, option_formats, &options, &ordered_options,
161                                      nullptr));
162 
163   // Check different errors.
164   // unknown option
165   ASSERT_FALSE(cmd.PreprocessOptions({"--unknown-option"}, option_formats, &options,
166                                      &ordered_options, nullptr));
167   // no option value
168   ASSERT_FALSE(
169       cmd.PreprocessOptions({"--str-option"}, option_formats, &options, &ordered_options, nullptr));
170   // wrong option value format
171   ASSERT_FALSE(cmd.PreprocessOptions({"--uint-option", "-2"}, option_formats, &options,
172                                      &ordered_options, nullptr));
173   ASSERT_FALSE(cmd.PreprocessOptions({"--double-option", "str"}, option_formats, &options,
174                                      &ordered_options, nullptr));
175   // unexpected non_option_args
176   ASSERT_FALSE(cmd.PreprocessOptions({"non_option_args"}, option_formats, &options,
177                                      &ordered_options, nullptr));
178 
179   auto parse_args = [&](const std::vector<std::string>& args) {
180     return cmd.PreprocessOptions(args, option_formats, &options, &ordered_options, nullptr);
181   };
182 
183   ASSERT_TRUE(parse_args({"--opt-str-after-equal-option"}));
184   std::string s;
185   options.PullStringValue("--opt-str-after-equal-option", &s);
186   ASSERT_EQ(s, "");
187   ASSERT_TRUE(parse_args({"--opt-str-after-equal-option=str_value"}));
188   options.PullStringValue("--opt-str-after-equal-option", &s);
189   ASSERT_EQ(s, "str_value");
190 }
191 
192 // @CddTest = 6.1/C-0-2
TEST(command,OptionValueMap)193 TEST(command, OptionValueMap) {
194   OptionValue value;
195   value.uint_value = 10;
196 
197   OptionValueMap options;
198   uint64_t uint_value;
199   options.values.emplace("--uint-option", value);
200   ASSERT_FALSE(options.PullUintValue("--uint-option", &uint_value, 11));
201   options.values.emplace("--uint-option", value);
202   ASSERT_FALSE(options.PullUintValue("--uint-option", &uint_value, 0, 9));
203   options.values.emplace("--uint-option", value);
204   ASSERT_TRUE(options.PullUintValue("--uint-option", &uint_value, 10, 10));
205 
206   double double_value;
207   value.double_value = 0.0;
208   options.values.emplace("--double-option", value);
209   ASSERT_FALSE(options.PullDoubleValue("--double-option", &double_value, 1.0));
210   options.values.emplace("--double-option", value);
211   ASSERT_FALSE(options.PullDoubleValue("--double-option", &double_value, -2.0, -1.0));
212   options.values.emplace("--double-option", value);
213   ASSERT_TRUE(options.PullDoubleValue("--double-option", &double_value, 0.0, 0.0));
214 }
215