blob: f856504786089312aeaac61fad599e04ede5777b [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001// 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/tint/ast/return_statement.h"
17#include "src/tint/ast/test_helper.h"
18
19namespace tint {
20namespace {
21
22using ProgramTest = ast::TestHelper;
23
24TEST_F(ProgramTest, Unbuilt) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000025 Program program;
26 EXPECT_FALSE(program.IsValid());
Ryan Harrisondbc13af2022-02-21 15:19:07 +000027}
28
29TEST_F(ProgramTest, Creation) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000030 Program program(std::move(*this));
Ben Clayton783b1692022-08-02 17:03:35 +000031 EXPECT_EQ(program.AST().Functions().Length(), 0u);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000032}
33
34TEST_F(ProgramTest, EmptyIsValid) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000035 Program program(std::move(*this));
36 EXPECT_TRUE(program.IsValid());
Ryan Harrisondbc13af2022-02-21 15:19:07 +000037}
38
39TEST_F(ProgramTest, IDsAreUnique) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000040 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());
Ryan Harrisondbc13af2022-02-21 15:19:07 +000046}
47
48TEST_F(ProgramTest, Assert_GlobalVariable) {
dan sinclair18b21582023-01-21 19:56:49 +000049 GlobalVar("var", ty.f32(), type::AddressSpace::kPrivate);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000050
dan sinclair41e4d9a2022-05-01 14:40:55 +000051 Program program(std::move(*this));
52 EXPECT_TRUE(program.IsValid());
Ryan Harrisondbc13af2022-02-21 15:19:07 +000053}
54
55TEST_F(ProgramTest, Assert_NullGlobalVariable) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000056 EXPECT_FATAL_FAILURE(
57 {
58 ProgramBuilder b;
59 b.AST().AddGlobalVariable(nullptr);
60 },
61 "internal compiler error");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000062}
63
64TEST_F(ProgramTest, Assert_NullTypeDecl) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000065 EXPECT_FATAL_FAILURE(
66 {
67 ProgramBuilder b;
68 b.AST().AddTypeDecl(nullptr);
69 },
70 "internal compiler error");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000071}
72
73TEST_F(ProgramTest, Assert_Null_Function) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000074 EXPECT_FATAL_FAILURE(
75 {
76 ProgramBuilder b;
77 b.AST().AddFunction(nullptr);
78 },
79 "internal compiler error");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000080}
81
82TEST_F(ProgramTest, DiagnosticsMove) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000083 Diagnostics().add_error(diag::System::Program, "an error message");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000084
dan sinclair41e4d9a2022-05-01 14:40:55 +000085 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");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000090
dan sinclair41e4d9a2022-05-01 14:40:55 +000091 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");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000096}
97
98TEST_F(ProgramTest, ReuseMovedFromVariable) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000099 Program a(std::move(*this));
100 EXPECT_TRUE(a.IsValid());
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000101
dan sinclair41e4d9a2022-05-01 14:40:55 +0000102 Program b = std::move(a);
103 EXPECT_TRUE(b.IsValid());
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000104
dan sinclair41e4d9a2022-05-01 14:40:55 +0000105 a = std::move(b);
106 EXPECT_TRUE(a.IsValid());
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000107}
108
109} // namespace
110} // namespace tint