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