util: Add stub unit tests for the call types in the m5 utility.
[gem5.git] / util / m5 / src / args.test.cc
1 /*
2 * Copyright 2020 Google Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <gtest/gtest.h>
29
30 #include <cstring>
31
32 #include "args.hh"
33
34 TEST(ArgsTest, Stoi)
35 {
36 // Successful conversion of a decimal number.
37 uint64_t val = 0;
38 EXPECT_TRUE(Args::stoi("7", val));
39 EXPECT_EQ(val, 7);
40
41 // Successful conversion of a hex number.
42 val = 0;
43 EXPECT_TRUE(Args::stoi("0xf", val));
44 EXPECT_EQ(val, 15);
45
46 // Successful conversion with a default.
47 val = 0;
48 EXPECT_TRUE(Args::stoi("8", val, 9));
49 EXPECT_EQ(val, 8);
50
51 // Failed conversion.
52 val = 0;
53 EXPECT_FALSE(Args::stoi("not a number", val));
54
55 // Failed conversion with a default.
56 val = 0;
57 EXPECT_FALSE(Args::stoi("not a number", val, 4));
58 EXPECT_EQ(val, 4);
59 }
60
61 TEST(ArgsTest, Pack)
62 {
63 uint64_t regs[32];
64 std::memset(regs, 0, sizeof(regs));
65
66 // Too long a string.
67 EXPECT_FALSE(Args::pack("Test", regs, 0));
68 // Verify that regs was not written to.
69 EXPECT_EQ(regs[0], 0);
70
71 // Success with one register.
72 EXPECT_TRUE(Args::pack("Test", regs, 1));
73 EXPECT_EQ(regs[0], ('T' << 0) | ('e' << 8) | ('s' << 16) | ('t' << 24));
74
75 // Success with two registers.
76 EXPECT_TRUE(Args::pack("A longer string", regs, 2));
77 EXPECT_EQ(regs[0], ((uint64_t)'A' << 0) | ((uint64_t)' ' << 8) |
78 ((uint64_t)'l' << 16) | ((uint64_t)'o' << 24) |
79 ((uint64_t)'n' << 32) | ((uint64_t)'g' << 40) |
80 ((uint64_t)'e' << 48) | ((uint64_t)'r' << 56));
81 EXPECT_EQ(regs[1], ((uint64_t)' ' << 0) | ((uint64_t)'s' << 8) |
82 ((uint64_t)'t' << 16) | ((uint64_t)'r' << 24) |
83 ((uint64_t)'i' << 32) | ((uint64_t)'n' << 40) |
84 ((uint64_t)'g' << 48));
85
86 // Success with exactly the right number of characters.
87 EXPECT_TRUE(Args::pack("12345678", regs, 1));
88 EXPECT_EQ(regs[0], ((uint64_t)'1' << 0) | ((uint64_t)'2' << 8) |
89 ((uint64_t)'3' << 16) | ((uint64_t)'4' << 24) |
90 ((uint64_t)'5' << 32) | ((uint64_t)'6' << 40) |
91 ((uint64_t)'7' << 48) | ((uint64_t)'8' << 56));
92
93 // Failure with exactly one too many characters.
94 EXPECT_FALSE(Args::pack("123456789", regs, 1));
95 EXPECT_EQ(regs[0], 0);
96 }
97
98 TEST(ArgsTest, Pop)
99 {
100 const char *test_argv[] = { "arg0", "0x1", "2" };
101 const int test_argc = sizeof(test_argv) / sizeof(test_argv[0]);
102
103 uint64_t val = 0;
104
105 Args args(test_argc, test_argv);
106 const Args reset_args = args;
107 EXPECT_EQ(args.size(), test_argc);
108
109 for (int i = 0; i < test_argc; i++)
110 EXPECT_EQ(args[i], test_argv[i]);
111
112 // Initializer list constructor with no elements.
113 args = Args({});
114 EXPECT_EQ(args.size(), 0);
115
116 // Initializer list with a few elements.
117 args = Args({ "arg0", "0x1", "arg2" });
118 EXPECT_EQ(args.size(), 3);
119
120 // Pop as an integer and fail with no default.
121 args = reset_args;
122 val = 0;
123 EXPECT_FALSE(args.pop(val));
124 EXPECT_EQ(args.size(), test_argc);
125
126 // Pop as an integer and fail with a default.
127 args = reset_args;
128 val = 0;
129 EXPECT_FALSE(args.pop(val, 5));
130 EXPECT_EQ(val, 5);
131
132 // Pop as a string successfully.
133 EXPECT_EQ(args.pop(), test_argv[0]);
134 EXPECT_EQ(args.size(), test_argc - 1);
135
136 // Pop as a string successfully with a default.
137 args = reset_args;
138 EXPECT_EQ(args.pop("something else"), "arg0");
139 EXPECT_EQ(args.size(), test_argc - 1);
140
141 // Pop as an integer and succeed.
142 val = 0;
143 EXPECT_TRUE(args.pop(val));
144 EXPECT_EQ(val, 1);
145 EXPECT_EQ(args.size(), test_argc - 2);
146
147 // Pop as an integer and succeed with a default.
148 val = 0;
149 EXPECT_TRUE(args.pop(val, 5));
150 EXPECT_EQ(val, 2);
151 EXPECT_EQ(args.size(), test_argc - 3);
152
153 Args empty({});
154
155 // Pop from an empty list.
156 EXPECT_EQ(empty.size(), 0);
157 EXPECT_EQ(empty.pop("the default"), "the default");
158 EXPECT_EQ(empty.size(), 0);
159
160 // Pop an integer from an empty list.
161 val = 0;
162 EXPECT_FALSE(empty.pop(val));
163 EXPECT_EQ(empty.size(), 0);
164
165 // Pop an integer from an empty list with a default.
166 val = 0;
167 EXPECT_TRUE(empty.pop(val, 5));
168 EXPECT_EQ(val, 5);
169 EXPECT_EQ(empty.size(), 0);
170
171 Args short_args({ "short" });
172 Args long_args({ "A really long argument that won't fit." });
173 uint64_t regs[1];
174
175 // Pop into a list of registers and succeed.
176 EXPECT_EQ(short_args.size(), 1);
177 EXPECT_TRUE(short_args.pop(regs, 1));
178 EXPECT_EQ(short_args.size(), 0);
179 EXPECT_EQ(regs[0], ((uint64_t)'s' << 0) | ((uint64_t)'h' << 8) |
180 ((uint64_t)'o' << 16) | ((uint64_t)'r' << 24) |
181 ((uint64_t)'t' << 32));
182
183 // Pop into a list of register and fail.
184 EXPECT_EQ(long_args.size(), 1);
185 EXPECT_FALSE(long_args.pop(regs, 1));
186 EXPECT_EQ(long_args.size(), 1);
187 }