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 "gtest/gtest-spi.h"
16 #include "src/ast/test_helper.h"
17
18 namespace tint {
19 namespace ast {
20 namespace {
21
22 using BinaryExpressionTest = TestHelper;
23
TEST_F(BinaryExpressionTest,Creation)24 TEST_F(BinaryExpressionTest, Creation) {
25 auto* lhs = Expr("lhs");
26 auto* rhs = Expr("rhs");
27
28 auto* r = create<BinaryExpression>(BinaryOp::kEqual, lhs, rhs);
29 EXPECT_EQ(r->lhs, lhs);
30 EXPECT_EQ(r->rhs, rhs);
31 EXPECT_EQ(r->op, BinaryOp::kEqual);
32 }
33
TEST_F(BinaryExpressionTest,Creation_WithSource)34 TEST_F(BinaryExpressionTest, Creation_WithSource) {
35 auto* lhs = Expr("lhs");
36 auto* rhs = Expr("rhs");
37
38 auto* r = create<BinaryExpression>(Source{Source::Location{20, 2}},
39 BinaryOp::kEqual, lhs, rhs);
40 auto src = r->source;
41 EXPECT_EQ(src.range.begin.line, 20u);
42 EXPECT_EQ(src.range.begin.column, 2u);
43 }
44
TEST_F(BinaryExpressionTest,IsBinary)45 TEST_F(BinaryExpressionTest, IsBinary) {
46 auto* lhs = Expr("lhs");
47 auto* rhs = Expr("rhs");
48
49 auto* r = create<BinaryExpression>(BinaryOp::kEqual, lhs, rhs);
50 EXPECT_TRUE(r->Is<BinaryExpression>());
51 }
52
TEST_F(BinaryExpressionTest,Assert_Null_LHS)53 TEST_F(BinaryExpressionTest, Assert_Null_LHS) {
54 EXPECT_FATAL_FAILURE(
55 {
56 ProgramBuilder b;
57 b.create<BinaryExpression>(BinaryOp::kEqual, nullptr, b.Expr("rhs"));
58 },
59 "internal compiler error");
60 }
61
TEST_F(BinaryExpressionTest,Assert_Null_RHS)62 TEST_F(BinaryExpressionTest, Assert_Null_RHS) {
63 EXPECT_FATAL_FAILURE(
64 {
65 ProgramBuilder b;
66 b.create<BinaryExpression>(BinaryOp::kEqual, b.Expr("lhs"), nullptr);
67 },
68 "internal compiler error");
69 }
70
TEST_F(BinaryExpressionTest,Assert_DifferentProgramID_LHS)71 TEST_F(BinaryExpressionTest, Assert_DifferentProgramID_LHS) {
72 EXPECT_FATAL_FAILURE(
73 {
74 ProgramBuilder b1;
75 ProgramBuilder b2;
76 b1.create<BinaryExpression>(BinaryOp::kEqual, b2.Expr("lhs"),
77 b1.Expr("rhs"));
78 },
79 "internal compiler error");
80 }
81
TEST_F(BinaryExpressionTest,Assert_DifferentProgramID_RHS)82 TEST_F(BinaryExpressionTest, Assert_DifferentProgramID_RHS) {
83 EXPECT_FATAL_FAILURE(
84 {
85 ProgramBuilder b1;
86 ProgramBuilder b2;
87 b1.create<BinaryExpression>(BinaryOp::kEqual, b1.Expr("lhs"),
88 b2.Expr("rhs"));
89 },
90 "internal compiler error");
91 }
92
93 } // namespace
94 } // namespace ast
95 } // namespace tint
96