Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 1 | // 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 "src/ast/module.h" |
| 16 | |
David Neto | 0afb943 | 2020-03-18 20:09:44 +0000 | [diff] [blame] | 17 | #include <sstream> |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 18 | #include <utility> |
| 19 | |
David Neto | 0afb943 | 2020-03-18 20:09:44 +0000 | [diff] [blame] | 20 | #include "gmock/gmock.h" |
Dan Sinclair | 3eaf827 | 2020-03-16 14:17:52 +0000 | [diff] [blame] | 21 | #include "src/ast/entry_point.h" |
| 22 | #include "src/ast/function.h" |
| 23 | #include "src/ast/import.h" |
| 24 | #include "src/ast/type/f32_type.h" |
| 25 | #include "src/ast/variable.h" |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 26 | |
| 27 | namespace tint { |
| 28 | namespace ast { |
dan sinclair | 989cee6 | 2020-03-26 15:31:43 +0000 | [diff] [blame^] | 29 | namespace { |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 30 | |
| 31 | using ModuleTest = testing::Test; |
| 32 | |
| 33 | TEST_F(ModuleTest, Creation) { |
| 34 | Module m; |
| 35 | |
| 36 | EXPECT_EQ(m.imports().size(), 0); |
| 37 | } |
| 38 | |
David Neto | 0afb943 | 2020-03-18 20:09:44 +0000 | [diff] [blame] | 39 | TEST_F(ModuleTest, ToStrEmitsPreambleAndPostamble) { |
| 40 | Module m; |
| 41 | const auto str = m.to_str(); |
| 42 | const auto expected = "Module{\n}\n"; |
| 43 | EXPECT_EQ(str, expected); |
| 44 | } |
| 45 | |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 46 | TEST_F(ModuleTest, Imports) { |
| 47 | Module m; |
| 48 | |
| 49 | m.AddImport(std::make_unique<Import>("GLSL.std.430", "std::glsl")); |
| 50 | m.AddImport(std::make_unique<Import>("OpenCL.debug.100", "std::debug")); |
| 51 | |
| 52 | EXPECT_EQ(2, m.imports().size()); |
| 53 | EXPECT_EQ("std::glsl", m.imports()[0]->name()); |
| 54 | } |
| 55 | |
David Neto | 0afb943 | 2020-03-18 20:09:44 +0000 | [diff] [blame] | 56 | TEST_F(ModuleTest, ToStrWithImport) { |
| 57 | Module m; |
| 58 | m.AddImport(std::make_unique<Import>("GLSL.std.430", "std::glsl")); |
| 59 | const auto str = m.to_str(); |
| 60 | EXPECT_EQ(str, R"(Module{ |
| 61 | Import{"GLSL.std.430" as std::glsl} |
| 62 | } |
| 63 | )"); |
| 64 | } |
| 65 | |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 66 | TEST_F(ModuleTest, LookupImport) { |
| 67 | Module m; |
| 68 | |
| 69 | auto i = std::make_unique<Import>("GLSL.std.430", "std::glsl"); |
| 70 | m.AddImport(std::move(i)); |
| 71 | m.AddImport(std::make_unique<Import>("OpenCL.debug.100", "std::debug")); |
| 72 | |
| 73 | auto import = m.FindImportByName("std::glsl"); |
| 74 | ASSERT_NE(nullptr, import); |
| 75 | EXPECT_EQ(import->path(), "GLSL.std.430"); |
| 76 | EXPECT_EQ(import->name(), "std::glsl"); |
| 77 | } |
| 78 | |
| 79 | TEST_F(ModuleTest, LookupImportMissing) { |
| 80 | Module m; |
| 81 | EXPECT_EQ(nullptr, m.FindImportByName("Missing")); |
| 82 | } |
| 83 | |
| 84 | TEST_F(ModuleTest, IsValid_Empty) { |
| 85 | Module m; |
| 86 | EXPECT_TRUE(m.IsValid()); |
| 87 | } |
| 88 | |
Dan Sinclair | 3eaf827 | 2020-03-16 14:17:52 +0000 | [diff] [blame] | 89 | TEST_F(ModuleTest, IsValid_Import) { |
| 90 | Module m; |
| 91 | m.AddImport(std::make_unique<Import>("GLSL.std.430", "std::glsl")); |
| 92 | EXPECT_TRUE(m.IsValid()); |
| 93 | } |
| 94 | |
| 95 | TEST_F(ModuleTest, IsValid_Null_Import) { |
| 96 | Module m; |
| 97 | m.AddImport(nullptr); |
| 98 | EXPECT_FALSE(m.IsValid()); |
| 99 | } |
| 100 | |
| 101 | TEST_F(ModuleTest, IsValid_Invalid_Import) { |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 102 | Module m; |
| 103 | m.AddImport(std::make_unique<Import>()); |
| 104 | EXPECT_FALSE(m.IsValid()); |
| 105 | } |
| 106 | |
Dan Sinclair | 3eaf827 | 2020-03-16 14:17:52 +0000 | [diff] [blame] | 107 | TEST_F(ModuleTest, IsValid_GlobalVariable) { |
| 108 | type::F32Type f32; |
| 109 | auto var = std::make_unique<Variable>("var", StorageClass::kInput, &f32); |
| 110 | |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 111 | Module m; |
Dan Sinclair | 3eaf827 | 2020-03-16 14:17:52 +0000 | [diff] [blame] | 112 | m.AddGlobalVariable(std::move(var)); |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 113 | EXPECT_TRUE(m.IsValid()); |
| 114 | } |
| 115 | |
Dan Sinclair | 3eaf827 | 2020-03-16 14:17:52 +0000 | [diff] [blame] | 116 | TEST_F(ModuleTest, IsValid_Null_GlobalVariable) { |
| 117 | Module m; |
| 118 | m.AddGlobalVariable(nullptr); |
| 119 | EXPECT_FALSE(m.IsValid()); |
| 120 | } |
| 121 | |
| 122 | TEST_F(ModuleTest, IsValid_Invalid_GlobalVariable) { |
| 123 | auto var = std::make_unique<Variable>("var", StorageClass::kInput, nullptr); |
| 124 | |
| 125 | Module m; |
| 126 | m.AddGlobalVariable(std::move(var)); |
| 127 | EXPECT_FALSE(m.IsValid()); |
| 128 | } |
| 129 | |
| 130 | TEST_F(ModuleTest, IsValid_EntryPoint) { |
| 131 | Module m; |
| 132 | m.AddEntryPoint( |
| 133 | std::make_unique<EntryPoint>(PipelineStage::kVertex, "main", "vtx_main")); |
| 134 | EXPECT_TRUE(m.IsValid()); |
| 135 | } |
| 136 | |
| 137 | TEST_F(ModuleTest, IsValid_Null_EntryPoint) { |
| 138 | Module m; |
| 139 | m.AddEntryPoint(nullptr); |
| 140 | EXPECT_FALSE(m.IsValid()); |
| 141 | } |
| 142 | |
| 143 | TEST_F(ModuleTest, IsValid_Invalid_EntryPoint) { |
| 144 | Module m; |
| 145 | m.AddEntryPoint(std::make_unique<EntryPoint>()); |
| 146 | EXPECT_FALSE(m.IsValid()); |
| 147 | } |
| 148 | |
| 149 | TEST_F(ModuleTest, IsValid_Alias) { |
| 150 | type::F32Type f32; |
| 151 | type::AliasType alias("alias", &f32); |
| 152 | |
| 153 | Module m; |
| 154 | m.AddAliasType(&alias); |
| 155 | EXPECT_TRUE(m.IsValid()); |
| 156 | } |
| 157 | |
| 158 | TEST_F(ModuleTest, IsValid_Null_Alias) { |
| 159 | Module m; |
| 160 | m.AddAliasType(nullptr); |
| 161 | EXPECT_FALSE(m.IsValid()); |
| 162 | } |
| 163 | |
| 164 | TEST_F(ModuleTest, IsValid_Function) { |
| 165 | type::F32Type f32; |
| 166 | auto func = std::make_unique<Function>( |
| 167 | "main", std::vector<std::unique_ptr<Variable>>(), &f32); |
| 168 | |
| 169 | Module m; |
| 170 | m.AddFunction(std::move(func)); |
| 171 | EXPECT_TRUE(m.IsValid()); |
| 172 | } |
| 173 | |
| 174 | TEST_F(ModuleTest, IsValid_Null_Function) { |
| 175 | Module m; |
| 176 | m.AddFunction(nullptr); |
| 177 | EXPECT_FALSE(m.IsValid()); |
| 178 | } |
| 179 | |
| 180 | TEST_F(ModuleTest, IsValid_Invalid_Function) { |
| 181 | auto func = std::make_unique<Function>(); |
| 182 | |
| 183 | Module m; |
| 184 | m.AddFunction(std::move(func)); |
| 185 | EXPECT_FALSE(m.IsValid()); |
| 186 | } |
| 187 | |
dan sinclair | 989cee6 | 2020-03-26 15:31:43 +0000 | [diff] [blame^] | 188 | } // namespace |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 189 | } // namespace ast |
| 190 | } // namespace tint |