2 * Copyright 2020 Google Inc.
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.
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.
28 #include <gtest/gtest.h>
36 // Successful conversion of a decimal number.
38 EXPECT_TRUE(Args::stoi("7", val
));
41 // Successful conversion of a hex number.
43 EXPECT_TRUE(Args::stoi("0xf", val
));
46 // Successful conversion with a default.
48 EXPECT_TRUE(Args::stoi("8", val
, 9));
53 EXPECT_FALSE(Args::stoi("not a number", val
));
55 // Failed conversion with a default.
57 EXPECT_FALSE(Args::stoi("not a number", val
, 4));
64 std::memset(regs
, 0, sizeof(regs
));
67 EXPECT_FALSE(Args::pack("Test", regs
, 0));
68 // Verify that regs was not written to.
69 EXPECT_EQ(regs
[0], 0);
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));
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));
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));
93 // Failure with exactly one too many characters.
94 EXPECT_FALSE(Args::pack("123456789", regs
, 1));
95 EXPECT_EQ(regs
[0], 0);
100 const char *test_argv
[] = { "arg0", "0x1", "2" };
101 const int test_argc
= sizeof(test_argv
) / sizeof(test_argv
[0]);
105 Args
args(test_argc
, test_argv
);
106 const Args reset_args
= args
;
107 EXPECT_EQ(args
.size(), test_argc
);
109 for (int i
= 0; i
< test_argc
; i
++)
110 EXPECT_EQ(args
[i
], test_argv
[i
]);
112 // Initializer list constructor with no elements.
114 EXPECT_EQ(args
.size(), 0);
116 // Initializer list with a few elements.
117 args
= Args({ "arg0", "0x1", "arg2" });
118 EXPECT_EQ(args
.size(), 3);
120 // Pop as an integer and fail with no default.
123 EXPECT_FALSE(args
.pop(val
));
124 EXPECT_EQ(args
.size(), test_argc
);
126 // Pop as an integer and fail with a default.
129 EXPECT_FALSE(args
.pop(val
, 5));
132 // Pop as a string successfully.
133 EXPECT_EQ(args
.pop(), test_argv
[0]);
134 EXPECT_EQ(args
.size(), test_argc
- 1);
136 // Pop as a string successfully with a default.
138 EXPECT_EQ(args
.pop("something else"), "arg0");
139 EXPECT_EQ(args
.size(), test_argc
- 1);
141 // Pop as an integer and succeed.
143 EXPECT_TRUE(args
.pop(val
));
145 EXPECT_EQ(args
.size(), test_argc
- 2);
147 // Pop as an integer and succeed with a default.
149 EXPECT_TRUE(args
.pop(val
, 5));
151 EXPECT_EQ(args
.size(), test_argc
- 3);
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);
160 // Pop an integer from an empty list.
162 EXPECT_FALSE(empty
.pop(val
));
163 EXPECT_EQ(empty
.size(), 0);
165 // Pop an integer from an empty list with a default.
167 EXPECT_TRUE(empty
.pop(val
, 5));
169 EXPECT_EQ(empty
.size(), 0);
171 Args
short_args({ "short" });
172 Args
long_args({ "A really long argument that won't fit." });
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));
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);