blob: 24e7344a98e6bf0a41368323635ab3ecda51d20b [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +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/tint/program_builder.h"
16
17#include "gtest/gtest.h"
18
19namespace tint {
20namespace {
21
22using ProgramBuilderTest = testing::Test;
23
24TEST_F(ProgramBuilderTest, IDsAreUnique) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000025 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());
Ryan Harrisondbc13af2022-02-21 15:19:07 +000031}
32
33TEST_F(ProgramBuilderTest, WrapDoesntAffectInner) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000034 Program inner([] {
35 ProgramBuilder builder;
36 auto* ty = builder.ty.f32();
37 builder.Func("a", {}, ty, {}, {});
38 return builder;
39 }());
Ryan Harrisondbc13af2022-02-21 15:19:07 +000040
Ben Clayton783b1692022-08-02 17:03:35 +000041 ASSERT_EQ(inner.AST().Functions().Length(), 1u);
dan sinclair41e4d9a2022-05-01 14:40:55 +000042 ASSERT_TRUE(inner.Symbols().Get("a").IsValid());
43 ASSERT_FALSE(inner.Symbols().Get("b").IsValid());
Ryan Harrisondbc13af2022-02-21 15:19:07 +000044
dan sinclair41e4d9a2022-05-01 14:40:55 +000045 ProgramBuilder outer = ProgramBuilder::Wrap(&inner);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000046
Ben Clayton783b1692022-08-02 17:03:35 +000047 ASSERT_EQ(inner.AST().Functions().Length(), 1u);
48 ASSERT_EQ(outer.AST().Functions().Length(), 1u);
dan sinclair41e4d9a2022-05-01 14:40:55 +000049 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());
Ryan Harrisondbc13af2022-02-21 15:19:07 +000056
dan sinclair41e4d9a2022-05-01 14:40:55 +000057 auto* ty = outer.ty.f32();
58 outer.Func("b", {}, ty, {}, {});
Ryan Harrisondbc13af2022-02-21 15:19:07 +000059
Ben Clayton783b1692022-08-02 17:03:35 +000060 ASSERT_EQ(inner.AST().Functions().Length(), 1u);
61 ASSERT_EQ(outer.AST().Functions().Length(), 2u);
dan sinclair41e4d9a2022-05-01 14:40:55 +000062 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());
Ryan Harrisondbc13af2022-02-21 15:19:07 +000069}
70
71} // namespace
72} // namespace tint