Ben Clayton | e43c830 | 2021-01-29 11:59:32 +0000 | [diff] [blame] | 1 | // 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 | |
| 19 | namespace tint { |
| 20 | namespace { |
| 21 | |
| 22 | using ProgramBuilderTest = testing::Test; |
| 23 | |
Ben Clayton | e6995de | 2021-04-13 23:27:27 +0000 | [diff] [blame] | 24 | TEST_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 Clayton | e43c830 | 2021-01-29 11:59:32 +0000 | [diff] [blame] | 33 | TEST_F(ProgramBuilderTest, WrapDoesntAffectInner) { |
| 34 | Program inner([] { |
| 35 | ProgramBuilder builder; |
Ben Clayton | 19d3205 | 2021-05-20 15:10:48 +0000 | [diff] [blame] | 36 | auto* ty = builder.ty.f32(); |
Ben Clayton | 42d1e09 | 2021-02-02 14:29:15 +0000 | [diff] [blame] | 37 | builder.Func("a", {}, ty, {}, {}); |
Ben Clayton | e43c830 | 2021-01-29 11:59:32 +0000 | [diff] [blame] | 38 | 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 Clayton | 19d3205 | 2021-05-20 15:10:48 +0000 | [diff] [blame] | 57 | auto* ty = outer.ty.f32(); |
Ben Clayton | 42d1e09 | 2021-02-02 14:29:15 +0000 | [diff] [blame] | 58 | outer.Func("b", {}, ty, {}, {}); |
Ben Clayton | e43c830 | 2021-01-29 11:59:32 +0000 | [diff] [blame] | 59 | |
| 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 |