blob: 7560889b6760f04945e182856ef4b87fc204c363 [file] [log] [blame]
James Price49241862022-03-31 22:30:10 +00001// 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 Clayton0ce9ab02022-05-05 20:23:40 +000020using namespace tint::number_suffixes; // NOLINT
21
dan sinclair34323ac2022-04-07 18:39:35 +000022namespace tint::ast {
James Price49241862022-03-31 22:30:10 +000023namespace {
24
25using CompoundAssignmentStatementTest = TestHelper;
26
27TEST_F(CompoundAssignmentStatementTest, Creation) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000028 auto* lhs = Expr("lhs");
29 auto* rhs = Expr("rhs");
30 auto op = BinaryOp::kAdd;
James Price49241862022-03-31 22:30:10 +000031
dan sinclair41e4d9a2022-05-01 14:40:55 +000032 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 Price49241862022-03-31 22:30:10 +000036}
37
38TEST_F(CompoundAssignmentStatementTest, CreationWithSource) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000039 auto* lhs = Expr("lhs");
40 auto* rhs = Expr("rhs");
41 auto op = BinaryOp::kMultiply;
James Price49241862022-03-31 22:30:10 +000042
dan sinclair41e4d9a2022-05-01 14:40:55 +000043 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 Price49241862022-03-31 22:30:10 +000047}
48
49TEST_F(CompoundAssignmentStatementTest, IsCompoundAssign) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000050 auto* lhs = Expr("lhs");
51 auto* rhs = Expr("rhs");
52 auto op = BinaryOp::kSubtract;
James Price49241862022-03-31 22:30:10 +000053
dan sinclair41e4d9a2022-05-01 14:40:55 +000054 auto* stmt = create<CompoundAssignmentStatement>(lhs, rhs, op);
55 EXPECT_TRUE(stmt->Is<CompoundAssignmentStatement>());
James Price49241862022-03-31 22:30:10 +000056}
57
58TEST_F(CompoundAssignmentStatementTest, Assert_Null_LHS) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000059 EXPECT_FATAL_FAILURE(
60 {
61 ProgramBuilder b;
Ben Clayton0ce9ab02022-05-05 20:23:40 +000062 b.create<CompoundAssignmentStatement>(nullptr, b.Expr(1_i), BinaryOp::kAdd);
dan sinclair41e4d9a2022-05-01 14:40:55 +000063 },
64 "internal compiler error");
James Price49241862022-03-31 22:30:10 +000065}
66
67TEST_F(CompoundAssignmentStatementTest, Assert_Null_RHS) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000068 EXPECT_FATAL_FAILURE(
69 {
70 ProgramBuilder b;
Ben Clayton0ce9ab02022-05-05 20:23:40 +000071 b.create<CompoundAssignmentStatement>(b.Expr(1_i), nullptr, BinaryOp::kAdd);
dan sinclair41e4d9a2022-05-01 14:40:55 +000072 },
73 "internal compiler error");
James Price49241862022-03-31 22:30:10 +000074}
75
76TEST_F(CompoundAssignmentStatementTest, Assert_DifferentProgramID_LHS) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000077 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 Price49241862022-03-31 22:30:10 +000084}
85
86TEST_F(CompoundAssignmentStatementTest, Assert_DifferentProgramID_RHS) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000087 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 Price49241862022-03-31 22:30:10 +000094}
95
96} // namespace
dan sinclair34323ac2022-04-07 18:39:35 +000097} // namespace tint::ast