James Price | 4924186 | 2022-03-31 22:30:10 +0000 | [diff] [blame] | 1 | // Copyright 2022 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/tint/ast/compound_assignment_statement.h" |
| 16 | |
| 17 | #include "gtest/gtest-spi.h" |
| 18 | #include "src/tint/ast/test_helper.h" |
| 19 | |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 20 | using namespace tint::number_suffixes; // NOLINT |
| 21 | |
dan sinclair | 34323ac | 2022-04-07 18:39:35 +0000 | [diff] [blame] | 22 | namespace tint::ast { |
James Price | 4924186 | 2022-03-31 22:30:10 +0000 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | using CompoundAssignmentStatementTest = TestHelper; |
| 26 | |
| 27 | TEST_F(CompoundAssignmentStatementTest, Creation) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 28 | auto* lhs = Expr("lhs"); |
| 29 | auto* rhs = Expr("rhs"); |
| 30 | auto op = BinaryOp::kAdd; |
James Price | 4924186 | 2022-03-31 22:30:10 +0000 | [diff] [blame] | 31 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 32 | auto* stmt = create<CompoundAssignmentStatement>(lhs, rhs, op); |
| 33 | EXPECT_EQ(stmt->lhs, lhs); |
| 34 | EXPECT_EQ(stmt->rhs, rhs); |
| 35 | EXPECT_EQ(stmt->op, op); |
James Price | 4924186 | 2022-03-31 22:30:10 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | TEST_F(CompoundAssignmentStatementTest, CreationWithSource) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 39 | auto* lhs = Expr("lhs"); |
| 40 | auto* rhs = Expr("rhs"); |
| 41 | auto op = BinaryOp::kMultiply; |
James Price | 4924186 | 2022-03-31 22:30:10 +0000 | [diff] [blame] | 42 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 43 | auto* stmt = create<CompoundAssignmentStatement>(Source{Source::Location{20, 2}}, lhs, rhs, op); |
| 44 | auto src = stmt->source; |
| 45 | EXPECT_EQ(src.range.begin.line, 20u); |
| 46 | EXPECT_EQ(src.range.begin.column, 2u); |
James Price | 4924186 | 2022-03-31 22:30:10 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | TEST_F(CompoundAssignmentStatementTest, IsCompoundAssign) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 50 | auto* lhs = Expr("lhs"); |
| 51 | auto* rhs = Expr("rhs"); |
| 52 | auto op = BinaryOp::kSubtract; |
James Price | 4924186 | 2022-03-31 22:30:10 +0000 | [diff] [blame] | 53 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 54 | auto* stmt = create<CompoundAssignmentStatement>(lhs, rhs, op); |
| 55 | EXPECT_TRUE(stmt->Is<CompoundAssignmentStatement>()); |
James Price | 4924186 | 2022-03-31 22:30:10 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | TEST_F(CompoundAssignmentStatementTest, Assert_Null_LHS) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 59 | EXPECT_FATAL_FAILURE( |
| 60 | { |
| 61 | ProgramBuilder b; |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 62 | b.create<CompoundAssignmentStatement>(nullptr, b.Expr(1_i), BinaryOp::kAdd); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 63 | }, |
| 64 | "internal compiler error"); |
James Price | 4924186 | 2022-03-31 22:30:10 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | TEST_F(CompoundAssignmentStatementTest, Assert_Null_RHS) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 68 | EXPECT_FATAL_FAILURE( |
| 69 | { |
| 70 | ProgramBuilder b; |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 71 | b.create<CompoundAssignmentStatement>(b.Expr(1_i), nullptr, BinaryOp::kAdd); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 72 | }, |
| 73 | "internal compiler error"); |
James Price | 4924186 | 2022-03-31 22:30:10 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | TEST_F(CompoundAssignmentStatementTest, Assert_DifferentProgramID_LHS) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 77 | EXPECT_FATAL_FAILURE( |
| 78 | { |
| 79 | ProgramBuilder b1; |
| 80 | ProgramBuilder b2; |
| 81 | b1.create<CompoundAssignmentStatement>(b2.Expr("lhs"), b1.Expr("rhs"), BinaryOp::kAdd); |
| 82 | }, |
| 83 | "internal compiler error"); |
James Price | 4924186 | 2022-03-31 22:30:10 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | TEST_F(CompoundAssignmentStatementTest, Assert_DifferentProgramID_RHS) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 87 | EXPECT_FATAL_FAILURE( |
| 88 | { |
| 89 | ProgramBuilder b1; |
| 90 | ProgramBuilder b2; |
| 91 | b1.create<CompoundAssignmentStatement>(b1.Expr("lhs"), b2.Expr("rhs"), BinaryOp::kAdd); |
| 92 | }, |
| 93 | "internal compiler error"); |
James Price | 4924186 | 2022-03-31 22:30:10 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | } // namespace |
dan sinclair | 34323ac | 2022-04-07 18:39:35 +0000 | [diff] [blame] | 97 | } // namespace tint::ast |