Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 1 | // Copyright 2021 The Dawn & Tint Authors |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 3 | // Redistribution and use in source and binary forms, with or without |
| 4 | // modification, are permitted provided that the following conditions are met: |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 5 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 6 | // 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | // list of conditions and the following disclaimer. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 8 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 9 | // 2. Redistributions in binary form must reproduce the above copyright notice, |
| 10 | // this list of conditions and the following disclaimer in the documentation |
| 11 | // and/or other materials provided with the distribution. |
| 12 | // |
| 13 | // 3. Neither the name of the copyright holder nor the names of its |
| 14 | // contributors may be used to endorse or promote products derived from |
| 15 | // this software without specific prior written permission. |
| 16 | // |
| 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 21 | // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 23 | // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 24 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 25 | // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 27 | |
dan sinclair | 96db554 | 2023-07-20 09:21:10 +0000 | [diff] [blame] | 28 | #include "src/tint/lang/wgsl/program/program_builder.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 29 | |
| 30 | #include "gtest/gtest.h" |
| 31 | |
| 32 | namespace tint { |
| 33 | namespace { |
| 34 | |
| 35 | using ProgramBuilderTest = testing::Test; |
| 36 | |
| 37 | TEST_F(ProgramBuilderTest, IDsAreUnique) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 38 | Program program_a(ProgramBuilder{}); |
| 39 | Program program_b(ProgramBuilder{}); |
| 40 | Program program_c(ProgramBuilder{}); |
| 41 | EXPECT_NE(program_a.ID(), program_b.ID()); |
| 42 | EXPECT_NE(program_b.ID(), program_c.ID()); |
| 43 | EXPECT_NE(program_c.ID(), program_a.ID()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | TEST_F(ProgramBuilderTest, WrapDoesntAffectInner) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 47 | Program inner([] { |
| 48 | ProgramBuilder builder; |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 49 | auto ty = builder.ty.f32(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 50 | builder.Func("a", {}, ty, {}, {}); |
| 51 | return builder; |
| 52 | }()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 53 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 54 | ASSERT_EQ(inner.AST().Functions().Length(), 1u); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 55 | ASSERT_TRUE(inner.Symbols().Get("a").IsValid()); |
| 56 | ASSERT_FALSE(inner.Symbols().Get("b").IsValid()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 57 | |
Ben Clayton | 5ed5cc4 | 2023-09-22 10:31:04 +0000 | [diff] [blame] | 58 | ProgramBuilder outer = ProgramBuilder::Wrap(inner); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 59 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 60 | ASSERT_EQ(inner.AST().Functions().Length(), 1u); |
| 61 | ASSERT_EQ(outer.AST().Functions().Length(), 1u); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 62 | EXPECT_EQ(inner.AST().Functions()[0], outer.AST().Functions()[0]); |
| 63 | EXPECT_TRUE(inner.Symbols().Get("a").IsValid()); |
| 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_FALSE(outer.Symbols().Get("b").IsValid()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 69 | |
Ben Clayton | 971318f | 2023-02-14 13:52:43 +0000 | [diff] [blame] | 70 | auto ty = outer.ty.f32(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 71 | outer.Func("b", {}, ty, {}, {}); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 72 | |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 73 | ASSERT_EQ(inner.AST().Functions().Length(), 1u); |
| 74 | ASSERT_EQ(outer.AST().Functions().Length(), 2u); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 75 | EXPECT_EQ(inner.AST().Functions()[0], outer.AST().Functions()[0]); |
Ben Clayton | ce31d18 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 76 | EXPECT_EQ(outer.AST().Functions()[1]->name->symbol, outer.Symbols().Get("b")); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 77 | EXPECT_EQ(inner.Symbols().Get("a"), outer.Symbols().Get("a")); |
| 78 | EXPECT_TRUE(inner.Symbols().Get("a").IsValid()); |
| 79 | EXPECT_TRUE(outer.Symbols().Get("a").IsValid()); |
| 80 | EXPECT_FALSE(inner.Symbols().Get("b").IsValid()); |
| 81 | EXPECT_TRUE(outer.Symbols().Get("b").IsValid()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | } // namespace |
| 85 | } // namespace tint |