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/return_statement.h"
17 #include "src/ast/test_helper.h"
18
19 namespace tint {
20 namespace {
21
22 using ProgramTest = ast::TestHelper;
23
TEST_F(ProgramTest,Unbuilt)24 TEST_F(ProgramTest, Unbuilt) {
25 Program program;
26 EXPECT_FALSE(program.IsValid());
27 }
28
TEST_F(ProgramTest,Creation)29 TEST_F(ProgramTest, Creation) {
30 Program program(std::move(*this));
31 EXPECT_EQ(program.AST().Functions().size(), 0u);
32 }
33
TEST_F(ProgramTest,EmptyIsValid)34 TEST_F(ProgramTest, EmptyIsValid) {
35 Program program(std::move(*this));
36 EXPECT_TRUE(program.IsValid());
37 }
38
TEST_F(ProgramTest,IDsAreUnique)39 TEST_F(ProgramTest, IDsAreUnique) {
40 Program program_a(ProgramBuilder{});
41 Program program_b(ProgramBuilder{});
42 Program program_c(ProgramBuilder{});
43 EXPECT_NE(program_a.ID(), program_b.ID());
44 EXPECT_NE(program_b.ID(), program_c.ID());
45 EXPECT_NE(program_c.ID(), program_a.ID());
46 }
47
TEST_F(ProgramTest,Assert_GlobalVariable)48 TEST_F(ProgramTest, Assert_GlobalVariable) {
49 Global("var", ty.f32(), ast::StorageClass::kPrivate);
50
51 Program program(std::move(*this));
52 EXPECT_TRUE(program.IsValid());
53 }
54
TEST_F(ProgramTest,Assert_NullGlobalVariable)55 TEST_F(ProgramTest, Assert_NullGlobalVariable) {
56 EXPECT_FATAL_FAILURE(
57 {
58 ProgramBuilder b;
59 b.AST().AddGlobalVariable(nullptr);
60 },
61 "internal compiler error");
62 }
63
TEST_F(ProgramTest,Assert_NullTypeDecl)64 TEST_F(ProgramTest, Assert_NullTypeDecl) {
65 EXPECT_FATAL_FAILURE(
66 {
67 ProgramBuilder b;
68 b.AST().AddTypeDecl(nullptr);
69 },
70 "internal compiler error");
71 }
72
TEST_F(ProgramTest,Assert_Null_Function)73 TEST_F(ProgramTest, Assert_Null_Function) {
74 EXPECT_FATAL_FAILURE(
75 {
76 ProgramBuilder b;
77 b.AST().AddFunction(nullptr);
78 },
79 "internal compiler error");
80 }
81
TEST_F(ProgramTest,DiagnosticsMove)82 TEST_F(ProgramTest, DiagnosticsMove) {
83 Diagnostics().add_error(diag::System::Program, "an error message");
84
85 Program program_a(std::move(*this));
86 EXPECT_FALSE(program_a.IsValid());
87 EXPECT_EQ(program_a.Diagnostics().count(), 1u);
88 EXPECT_EQ(program_a.Diagnostics().error_count(), 1u);
89 EXPECT_EQ(program_a.Diagnostics().begin()->message, "an error message");
90
91 Program program_b(std::move(program_a));
92 EXPECT_FALSE(program_b.IsValid());
93 EXPECT_EQ(program_b.Diagnostics().count(), 1u);
94 EXPECT_EQ(program_b.Diagnostics().error_count(), 1u);
95 EXPECT_EQ(program_b.Diagnostics().begin()->message, "an error message");
96 }
97
TEST_F(ProgramTest,ReuseMovedFromVariable)98 TEST_F(ProgramTest, ReuseMovedFromVariable) {
99 Program a(std::move(*this));
100 EXPECT_TRUE(a.IsValid());
101
102 Program b = std::move(a);
103 EXPECT_TRUE(b.IsValid());
104
105 a = std::move(b);
106 EXPECT_TRUE(a.IsValid());
107 }
108
109 } // namespace
110 } // namespace tint
111