util: Add stub unit tests for the call types in the m5 utility.
[gem5.git] / util / m5 / src / command / initparam.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 "args.hh"
31 #include "command.hh"
32 #include "dispatch_table.hh"
33
34 uint64_t test_key_str1;
35 uint64_t test_key_str2;
36 uint64_t test_result;
37
38 uint64_t
39 test_m5_init_param(uint64_t key_str1, uint64_t key_str2)
40 {
41 test_key_str1 = key_str1;
42 test_key_str2 = key_str2;
43
44 return test_result;
45 }
46
47 DispatchTable dt = { .m5_init_param = &test_m5_init_param };
48
49 std::string cout_output;
50
51 bool
52 run(std::initializer_list<std::string> arg_args)
53 {
54 Args args(arg_args);
55
56 // Redirect cout into a stringstream.
57 std::stringstream buffer;
58 std::streambuf *orig = std::cout.rdbuf(buffer.rdbuf());
59
60 bool res = Command::run(dt, args);
61
62 // Capture the contents of the stringstream and restore cout.
63 cout_output = buffer.str();
64 std::cout.rdbuf(orig);
65
66 return res;
67 }
68
69 TEST(Fail, Arguments)
70 {
71 // Called with no arguments.
72 test_key_str1 = 0;
73 test_key_str2 = 0;
74 EXPECT_FALSE(run({"initparam"}));
75 EXPECT_EQ(cout_output, "");
76
77 // Called with an empty argument.
78 test_key_str1 = 1;
79 test_key_str2 = 1;
80 test_result = 5;
81 EXPECT_TRUE(run({"initparam", ""}));
82 EXPECT_EQ(test_key_str1, 0);
83 EXPECT_EQ(test_key_str2, 0);
84 EXPECT_EQ(cout_output, "5");
85
86 // Called with a short argument.
87 test_key_str1 = 1;
88 test_key_str2 = 1;
89 test_result = 4;
90 EXPECT_TRUE(run({"initparam", "shrt"}));
91 EXPECT_EQ(test_key_str1, ((uint64_t)'s' << 0) | ((uint64_t)'h' << 8) |
92 ((uint64_t)'r' << 16) | ((uint64_t)'t' << 24));
93 EXPECT_EQ(test_key_str2, 0);
94 EXPECT_EQ(cout_output, "4");
95
96 // Call with a longer argument.
97 test_key_str1 = 1;
98 test_key_str2 = 1;
99 test_result = 3;
100 EXPECT_TRUE(run({"initparam", "longer arg"}));
101 EXPECT_EQ(test_key_str1, ((uint64_t)'l' << 0) | ((uint64_t)'o' << 8) |
102 ((uint64_t)'n' << 16) | ((uint64_t)'g' << 24) |
103 ((uint64_t)'e' << 32) | ((uint64_t)'r' << 40) |
104 ((uint64_t)' ' << 48) | ((uint64_t)'a' << 56));
105 EXPECT_EQ(test_key_str2, ((uint64_t)'r' << 0) | ((uint64_t)'g' << 8));
106 EXPECT_EQ(cout_output, "3");
107
108 // Call with an almost too long argument.
109 test_key_str1 = 1;
110 test_key_str2 = 1;
111 test_result = 2;
112 EXPECT_TRUE(run({"initparam", "1234567887654321"}));
113 EXPECT_EQ(test_key_str1, ((uint64_t)'1' << 0) | ((uint64_t)'2' << 8) |
114 ((uint64_t)'3' << 16) | ((uint64_t)'4' << 24) |
115 ((uint64_t)'5' << 32) | ((uint64_t)'6' << 40) |
116 ((uint64_t)'7' << 48) | ((uint64_t)'8' << 56));
117 EXPECT_EQ(test_key_str2, ((uint64_t)'8' << 0) | ((uint64_t)'7' << 8) |
118 ((uint64_t)'6' << 16) | ((uint64_t)'5' << 24) |
119 ((uint64_t)'4' << 32) | ((uint64_t)'3' << 40) |
120 ((uint64_t)'2' << 48) | ((uint64_t)'1' << 56));
121 EXPECT_EQ(cout_output, "2");
122
123 // Call with an argument that is too long.
124 EXPECT_FALSE(run({"initparam", "12345678876543210"}));
125 EXPECT_EQ(cout_output, "");
126
127 // Call with a valid argument and then one extra.
128 EXPECT_FALSE(run({"valid", "extra"}));
129 EXPECT_EQ(cout_output, "");
130 }