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>
32 #include "dispatch_table.hh"
34 uint64_t test_key_str1
;
35 uint64_t test_key_str2
;
39 test_m5_init_param(uint64_t key_str1
, uint64_t key_str2
)
41 test_key_str1
= key_str1
;
42 test_key_str2
= key_str2
;
47 DispatchTable dt
= { .m5_init_param
= &test_m5_init_param
};
49 std::string cout_output
;
52 run(std::initializer_list
<std::string
> arg_args
)
56 // Redirect cout into a stringstream.
57 std::stringstream buffer
;
58 std::streambuf
*orig
= std::cout
.rdbuf(buffer
.rdbuf());
60 bool res
= Command::run(dt
, args
);
62 // Capture the contents of the stringstream and restore cout.
63 cout_output
= buffer
.str();
64 std::cout
.rdbuf(orig
);
71 // Called with no arguments.
74 EXPECT_FALSE(run({"initparam"}));
75 EXPECT_EQ(cout_output
, "");
77 // Called with an empty argument.
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");
86 // Called with a short argument.
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");
96 // Call with a longer argument.
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");
108 // Call with an almost too long argument.
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");
123 // Call with an argument that is too long.
124 EXPECT_FALSE(run({"initparam", "12345678876543210"}));
125 EXPECT_EQ(cout_output
, "");
127 // Call with a valid argument and then one extra.
128 EXPECT_FALSE(run({"valid", "extra"}));
129 EXPECT_EQ(cout_output
, "");