1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <sstream>
6
7 #include "src/compiler/operator.h"
8 #include "test/cctest/cctest.h"
9
10 namespace v8 {
11 namespace internal {
12 namespace compiler {
13
14 #define NONE Operator::kNoProperties
15 #define FOLD Operator::kFoldable
16
17
TEST(TestOperator_Mnemonic)18 TEST(TestOperator_Mnemonic) {
19 Operator op1(10, NONE, "ThisOne", 0, 0, 0, 0, 0, 0);
20 CHECK_EQ(0, strcmp(op1.mnemonic(), "ThisOne"));
21
22 Operator op2(11, NONE, "ThatOne", 0, 0, 0, 0, 0, 0);
23 CHECK_EQ(0, strcmp(op2.mnemonic(), "ThatOne"));
24
25 Operator1<int> op3(12, NONE, "Mnemonic1", 0, 0, 0, 1, 0, 0, 12333);
26 CHECK_EQ(0, strcmp(op3.mnemonic(), "Mnemonic1"));
27
28 Operator1<double> op4(13, NONE, "TheOther", 0, 0, 0, 1, 0, 0, 99.9);
29 CHECK_EQ(0, strcmp(op4.mnemonic(), "TheOther"));
30 }
31
32
TEST(TestOperator_Hash)33 TEST(TestOperator_Hash) {
34 Operator op1(17, NONE, "Another", 0, 0, 0, 0, 0, 0);
35 CHECK_EQ(17, static_cast<int>(op1.HashCode()));
36
37 Operator op2(18, NONE, "Falsch", 0, 0, 0, 0, 0, 0);
38 CHECK_EQ(18, static_cast<int>(op2.HashCode()));
39 }
40
41
TEST(TestOperator_Equals)42 TEST(TestOperator_Equals) {
43 Operator op1a(19, NONE, "Another1", 0, 0, 0, 0, 0, 0);
44 Operator op1b(19, FOLD, "Another2", 2, 0, 0, 2, 0, 0);
45
46 CHECK(op1a.Equals(&op1a));
47 CHECK(op1a.Equals(&op1b));
48 CHECK(op1b.Equals(&op1a));
49 CHECK(op1b.Equals(&op1b));
50
51 Operator op2a(20, NONE, "Falsch1", 0, 0, 0, 0, 0, 0);
52 Operator op2b(20, FOLD, "Falsch2", 1, 0, 0, 1, 0, 0);
53
54 CHECK(op2a.Equals(&op2a));
55 CHECK(op2a.Equals(&op2b));
56 CHECK(op2b.Equals(&op2a));
57 CHECK(op2b.Equals(&op2b));
58
59 CHECK(!op1a.Equals(&op2a));
60 CHECK(!op1a.Equals(&op2b));
61 CHECK(!op1b.Equals(&op2a));
62 CHECK(!op1b.Equals(&op2b));
63
64 CHECK(!op2a.Equals(&op1a));
65 CHECK(!op2a.Equals(&op1b));
66 CHECK(!op2b.Equals(&op1a));
67 CHECK(!op2b.Equals(&op1b));
68 }
69
70
OperatorToString(Operator * op)71 static v8::base::SmartArrayPointer<const char> OperatorToString(Operator* op) {
72 std::ostringstream os;
73 os << *op;
74 return v8::base::SmartArrayPointer<const char>(StrDup(os.str().c_str()));
75 }
76
77
TEST(TestOperator_Print)78 TEST(TestOperator_Print) {
79 Operator op1a(19, NONE, "Another1", 0, 0, 0, 0, 0, 0);
80 Operator op1b(19, FOLD, "Another2", 2, 0, 0, 2, 0, 0);
81
82 CHECK_EQ(0, strcmp("Another1", OperatorToString(&op1a).get()));
83 CHECK_EQ(0, strcmp("Another2", OperatorToString(&op1b).get()));
84
85 Operator op2a(20, NONE, "Flog1", 0, 0, 0, 0, 0, 0);
86 Operator op2b(20, FOLD, "Flog2", 1, 0, 0, 1, 0, 0);
87
88 CHECK_EQ(0, strcmp("Flog1", OperatorToString(&op2a).get()));
89 CHECK_EQ(0, strcmp("Flog2", OperatorToString(&op2b).get()));
90 }
91
92
TEST(TestOperator1int_Hash)93 TEST(TestOperator1int_Hash) {
94 Operator1<int> op1a(23, NONE, "Wolfie", 0, 0, 0, 0, 0, 0, 11);
95 Operator1<int> op1b(23, FOLD, "Doggie", 2, 0, 0, 2, 0, 0, 11);
96
97 CHECK(op1a.HashCode() == op1b.HashCode());
98
99 Operator1<int> op2a(24, NONE, "Arfie", 0, 0, 0, 0, 0, 0, 3);
100 Operator1<int> op2b(24, NONE, "Arfie", 0, 0, 0, 0, 0, 0, 4);
101
102 CHECK(op1a.HashCode() != op2a.HashCode());
103 CHECK(op2a.HashCode() != op2b.HashCode());
104 }
105
106
TEST(TestOperator1int_Equals)107 TEST(TestOperator1int_Equals) {
108 Operator1<int> op1a(23, NONE, "Scratchy", 0, 0, 0, 0, 0, 0, 11);
109 Operator1<int> op1b(23, FOLD, "Scratchy", 2, 0, 0, 2, 0, 0, 11);
110
111 CHECK(op1a.Equals(&op1a));
112 CHECK(op1a.Equals(&op1b));
113 CHECK(op1b.Equals(&op1a));
114 CHECK(op1b.Equals(&op1b));
115
116 Operator1<int> op2a(24, NONE, "Im", 0, 0, 0, 0, 0, 0, 3);
117 Operator1<int> op2b(24, NONE, "Im", 0, 0, 0, 0, 0, 0, 4);
118
119 CHECK(op2a.Equals(&op2a));
120 CHECK(!op2a.Equals(&op2b));
121 CHECK(!op2b.Equals(&op2a));
122 CHECK(op2b.Equals(&op2b));
123
124 CHECK(!op1a.Equals(&op2a));
125 CHECK(!op1a.Equals(&op2b));
126 CHECK(!op1b.Equals(&op2a));
127 CHECK(!op1b.Equals(&op2b));
128
129 CHECK(!op2a.Equals(&op1a));
130 CHECK(!op2a.Equals(&op1b));
131 CHECK(!op2b.Equals(&op1a));
132 CHECK(!op2b.Equals(&op1b));
133
134 Operator op3(25, NONE, "Weepy", 0, 0, 0, 0, 0, 0);
135
136 CHECK(!op1a.Equals(&op3));
137 CHECK(!op1b.Equals(&op3));
138 CHECK(!op2a.Equals(&op3));
139 CHECK(!op2b.Equals(&op3));
140
141 CHECK(!op3.Equals(&op1a));
142 CHECK(!op3.Equals(&op1b));
143 CHECK(!op3.Equals(&op2a));
144 CHECK(!op3.Equals(&op2b));
145 }
146
147
TEST(TestOperator1int_Print)148 TEST(TestOperator1int_Print) {
149 Operator1<int> op1(12, NONE, "Op1Test", 0, 0, 0, 1, 0, 0, 0);
150 CHECK_EQ(0, strcmp("Op1Test[0]", OperatorToString(&op1).get()));
151
152 Operator1<int> op2(12, NONE, "Op1Test", 0, 0, 0, 1, 0, 0, 66666666);
153 CHECK_EQ(0, strcmp("Op1Test[66666666]", OperatorToString(&op2).get()));
154
155 Operator1<int> op3(12, NONE, "FooBar", 0, 0, 0, 1, 0, 0, 2347);
156 CHECK_EQ(0, strcmp("FooBar[2347]", OperatorToString(&op3).get()));
157
158 Operator1<int> op4(12, NONE, "BarFoo", 0, 0, 0, 1, 0, 0, -879);
159 CHECK_EQ(0, strcmp("BarFoo[-879]", OperatorToString(&op4).get()));
160 }
161
162
TEST(TestOperator1double_Hash)163 TEST(TestOperator1double_Hash) {
164 Operator1<double> op1a(23, NONE, "Wolfie", 0, 0, 0, 0, 0, 0, 11.77);
165 Operator1<double> op1b(23, FOLD, "Doggie", 2, 0, 0, 2, 0, 0, 11.77);
166
167 CHECK(op1a.HashCode() == op1b.HashCode());
168
169 Operator1<double> op2a(24, NONE, "Arfie", 0, 0, 0, 0, 0, 0, -6.7);
170 Operator1<double> op2b(24, NONE, "Arfie", 0, 0, 0, 0, 0, 0, -6.8);
171
172 CHECK(op1a.HashCode() != op2a.HashCode());
173 CHECK(op2a.HashCode() != op2b.HashCode());
174 }
175
176
TEST(TestOperator1doublePrint)177 TEST(TestOperator1doublePrint) {
178 Operator1<double> op1a(23, NONE, "Canary", 0, 0, 0, 0, 0, 0, 0.5);
179 Operator1<double> op1b(23, FOLD, "Finch", 2, 0, 0, 2, 0, 0, -1.5);
180
181 CHECK_EQ(0, strcmp("Canary[0.5]", OperatorToString(&op1a).get()));
182 CHECK_EQ(0, strcmp("Finch[-1.5]", OperatorToString(&op1b).get()));
183 }
184
185
TEST(TestOperator1double_Equals)186 TEST(TestOperator1double_Equals) {
187 Operator1<double> op1a(23, NONE, "Scratchy", 0, 0, 0, 0, 0, 0, 11.77);
188 Operator1<double> op1b(23, FOLD, "Scratchy", 2, 0, 0, 2, 0, 0, 11.77);
189
190 CHECK(op1a.Equals(&op1a));
191 CHECK(op1a.Equals(&op1b));
192 CHECK(op1b.Equals(&op1a));
193 CHECK(op1b.Equals(&op1b));
194
195 Operator1<double> op2a(24, NONE, "Im", 0, 0, 0, 0, 0, 0, 3.1);
196 Operator1<double> op2b(24, NONE, "Im", 0, 0, 0, 0, 0, 0, 3.2);
197
198 CHECK(op2a.Equals(&op2a));
199 CHECK(!op2a.Equals(&op2b));
200 CHECK(!op2b.Equals(&op2a));
201 CHECK(op2b.Equals(&op2b));
202
203 CHECK(!op1a.Equals(&op2a));
204 CHECK(!op1a.Equals(&op2b));
205 CHECK(!op1b.Equals(&op2a));
206 CHECK(!op1b.Equals(&op2b));
207
208 CHECK(!op2a.Equals(&op1a));
209 CHECK(!op2a.Equals(&op1b));
210 CHECK(!op2b.Equals(&op1a));
211 CHECK(!op2b.Equals(&op1b));
212
213 Operator op3(25, NONE, "Weepy", 0, 0, 0, 0, 0, 0);
214
215 CHECK(!op1a.Equals(&op3));
216 CHECK(!op1b.Equals(&op3));
217 CHECK(!op2a.Equals(&op3));
218 CHECK(!op2b.Equals(&op3));
219
220 CHECK(!op3.Equals(&op1a));
221 CHECK(!op3.Equals(&op1b));
222 CHECK(!op3.Equals(&op2a));
223 CHECK(!op3.Equals(&op2b));
224
225 Operator1<double> op4a(24, NONE, "Bashful", 0, 0, 0, 0, 0, 0, 1.0);
226 Operator1<double> op4b(24, NONE, "Bashful", 0, 0, 0, 0, 0, 0, 1.0);
227
228 CHECK(op4a.Equals(&op4a));
229 CHECK(op4a.Equals(&op4b));
230 CHECK(op4b.Equals(&op4a));
231 CHECK(op4b.Equals(&op4b));
232
233 CHECK(!op3.Equals(&op4a));
234 CHECK(!op3.Equals(&op4b));
235 CHECK(!op3.Equals(&op4a));
236 CHECK(!op3.Equals(&op4b));
237 }
238
239
TEST(TestOpParameter_Operator1double)240 TEST(TestOpParameter_Operator1double) {
241 double values[] = {7777.5, -66, 0, 11, 0.1};
242
243 for (size_t i = 0; i < arraysize(values); i++) {
244 Operator1<double> op(33, NONE, "Scurvy", 0, 0, 0, 0, 0, 0, values[i]);
245 CHECK_EQ(values[i], OpParameter<double>(&op));
246 }
247 }
248
249
TEST(TestOpParameter_Operator1float)250 TEST(TestOpParameter_Operator1float) {
251 float values[] = {// thanks C++.
252 static_cast<float>(7777.5), static_cast<float>(-66),
253 static_cast<float>(0), static_cast<float>(11),
254 static_cast<float>(0.1)};
255
256 for (size_t i = 0; i < arraysize(values); i++) {
257 Operator1<float> op(33, NONE, "Scurvy", 0, 0, 0, 0, 0, 0, values[i]);
258 CHECK_EQ(values[i], OpParameter<float>(&op));
259 }
260 }
261
262
TEST(TestOpParameter_Operator1int)263 TEST(TestOpParameter_Operator1int) {
264 int values[] = {7777, -66, 0, 11, 1, 0x666aff};
265
266 for (size_t i = 0; i < arraysize(values); i++) {
267 Operator1<int> op(33, NONE, "Scurvy", 0, 0, 0, 0, 0, 0, values[i]);
268 CHECK_EQ(values[i], OpParameter<int>(&op));
269 }
270 }
271
272
TEST(Operator_CountsOrder)273 TEST(Operator_CountsOrder) {
274 Operator op(29, NONE, "Flashy", 11, 22, 33, 44, 55, 66);
275 CHECK_EQ(11, op.ValueInputCount());
276 CHECK_EQ(22, op.EffectInputCount());
277 CHECK_EQ(33, op.ControlInputCount());
278
279 CHECK_EQ(44, op.ValueOutputCount());
280 CHECK_EQ(55, op.EffectOutputCount());
281 CHECK_EQ(66, op.ControlOutputCount());
282 }
283
284 } // namespace compiler
285 } // namespace internal
286 } // namespace v8
287