blob: bfa9159f26d7bcadba1fa7d025e4849f05fe68b2 [file] [log] [blame]
Ben Claytone43c8302021-01-29 11:59:32 +00001// Copyright 2021 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/program_builder.h"
16
17#include "gtest/gtest.h"
18
19namespace tint {
20namespace {
21
22using ProgramBuilderTest = testing::Test;
23
Ben Claytone6995de2021-04-13 23:27:27 +000024TEST_F(ProgramBuilderTest, IDsAreUnique) {
25 Program program_a(ProgramBuilder{});
26 Program program_b(ProgramBuilder{});
27 Program program_c(ProgramBuilder{});
28 EXPECT_NE(program_a.ID(), program_b.ID());
29 EXPECT_NE(program_b.ID(), program_c.ID());
30 EXPECT_NE(program_c.ID(), program_a.ID());
31}
32
Ben Claytone43c8302021-01-29 11:59:32 +000033TEST_F(ProgramBuilderTest, WrapDoesntAffectInner) {
34 Program inner([] {
35 ProgramBuilder builder;
Ben Clayton19d32052021-05-20 15:10:48 +000036 auto* ty = builder.ty.f32();
Ben Clayton42d1e092021-02-02 14:29:15 +000037 builder.Func("a", {}, ty, {}, {});
Ben Claytone43c8302021-01-29 11:59:32 +000038 return builder;
39 }());
40
41 ASSERT_EQ(inner.AST().Functions().size(), 1u);
42 ASSERT_TRUE(inner.Symbols().Get("a").IsValid());
43 ASSERT_FALSE(inner.Symbols().Get("b").IsValid());
44
45 ProgramBuilder outer = ProgramBuilder::Wrap(&inner);
46
47 ASSERT_EQ(inner.AST().Functions().size(), 1u);
48 ASSERT_EQ(outer.AST().Functions().size(), 1u);
49 EXPECT_EQ(inner.AST().Functions()[0], outer.AST().Functions()[0]);
50 EXPECT_TRUE(inner.Symbols().Get("a").IsValid());
51 EXPECT_EQ(inner.Symbols().Get("a"), outer.Symbols().Get("a"));
52 EXPECT_TRUE(inner.Symbols().Get("a").IsValid());
53 EXPECT_TRUE(outer.Symbols().Get("a").IsValid());
54 EXPECT_FALSE(inner.Symbols().Get("b").IsValid());
55 EXPECT_FALSE(outer.Symbols().Get("b").IsValid());
56
Ben Clayton19d32052021-05-20 15:10:48 +000057 auto* ty = outer.ty.f32();
Ben Clayton42d1e092021-02-02 14:29:15 +000058 outer.Func("b", {}, ty, {}, {});
Ben Claytone43c8302021-01-29 11:59:32 +000059
60 ASSERT_EQ(inner.AST().Functions().size(), 1u);
61 ASSERT_EQ(outer.AST().Functions().size(), 2u);
62 EXPECT_EQ(inner.AST().Functions()[0], outer.AST().Functions()[0]);
63 EXPECT_EQ(outer.AST().Functions()[1]->symbol(), outer.Symbols().Get("b"));
64 EXPECT_EQ(inner.Symbols().Get("a"), outer.Symbols().Get("a"));
65 EXPECT_TRUE(inner.Symbols().Get("a").IsValid());
66 EXPECT_TRUE(outer.Symbols().Get("a").IsValid());
67 EXPECT_FALSE(inner.Symbols().Get("b").IsValid());
68 EXPECT_TRUE(outer.Symbols().Get("b").IsValid());
69}
70
71} // namespace
72} // namespace tint