1 // Copyright 2020 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/writer/wgsl/test_helper.h"
16
17 namespace tint {
18 namespace writer {
19 namespace wgsl {
20 namespace {
21
22 struct BinaryData {
23 const char* result;
24 ast::BinaryOp op;
25 };
operator <<(std::ostream & out,BinaryData data)26 inline std::ostream& operator<<(std::ostream& out, BinaryData data) {
27 out << data.op;
28 return out;
29 }
30 using WgslBinaryTest = TestParamHelper<BinaryData>;
TEST_P(WgslBinaryTest,Emit)31 TEST_P(WgslBinaryTest, Emit) {
32 auto params = GetParam();
33
34 auto op_ty = [&]() -> const ast::Type* {
35 if (params.op == ast::BinaryOp::kLogicalAnd ||
36 params.op == ast::BinaryOp::kLogicalOr) {
37 return ty.bool_();
38 } else {
39 return ty.u32();
40 }
41 };
42
43 Global("left", op_ty(), ast::StorageClass::kPrivate);
44 Global("right", op_ty(), ast::StorageClass::kPrivate);
45 auto* left = Expr("left");
46 auto* right = Expr("right");
47
48 auto* expr = create<ast::BinaryExpression>(params.op, left, right);
49 WrapInFunction(expr);
50
51 GeneratorImpl& gen = Build();
52
53 std::stringstream out;
54 ASSERT_TRUE(gen.EmitExpression(out, expr)) << gen.error();
55 EXPECT_EQ(out.str(), params.result);
56 }
57 INSTANTIATE_TEST_SUITE_P(
58 WgslGeneratorImplTest,
59 WgslBinaryTest,
60 testing::Values(
61 BinaryData{"(left & right)", ast::BinaryOp::kAnd},
62 BinaryData{"(left | right)", ast::BinaryOp::kOr},
63 BinaryData{"(left ^ right)", ast::BinaryOp::kXor},
64 BinaryData{"(left && right)", ast::BinaryOp::kLogicalAnd},
65 BinaryData{"(left || right)", ast::BinaryOp::kLogicalOr},
66 BinaryData{"(left == right)", ast::BinaryOp::kEqual},
67 BinaryData{"(left != right)", ast::BinaryOp::kNotEqual},
68 BinaryData{"(left < right)", ast::BinaryOp::kLessThan},
69 BinaryData{"(left > right)", ast::BinaryOp::kGreaterThan},
70 BinaryData{"(left <= right)", ast::BinaryOp::kLessThanEqual},
71 BinaryData{"(left >= right)", ast::BinaryOp::kGreaterThanEqual},
72 BinaryData{"(left << right)", ast::BinaryOp::kShiftLeft},
73 BinaryData{"(left >> right)", ast::BinaryOp::kShiftRight},
74 BinaryData{"(left + right)", ast::BinaryOp::kAdd},
75 BinaryData{"(left - right)", ast::BinaryOp::kSubtract},
76 BinaryData{"(left * right)", ast::BinaryOp::kMultiply},
77 BinaryData{"(left / right)", ast::BinaryOp::kDivide},
78 BinaryData{"(left % right)", ast::BinaryOp::kModulo}));
79
80 } // namespace
81 } // namespace wgsl
82 } // namespace writer
83 } // namespace tint
84