dan sinclair | 4510159 | 2020-07-08 20:52:46 +0000 | [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 | |
Ben Clayton | b8ea591 | 2021-04-19 16:52:42 +0000 | [diff] [blame] | 15 | #include "gmock/gmock.h" |
dan sinclair | 4510159 | 2020-07-08 20:52:46 +0000 | [diff] [blame] | 16 | #include "src/ast/variable_decl_statement.h" |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 17 | #include "src/writer/msl/test_helper.h" |
dan sinclair | 4510159 | 2020-07-08 20:52:46 +0000 | [diff] [blame] | 18 | |
| 19 | namespace tint { |
| 20 | namespace writer { |
| 21 | namespace msl { |
| 22 | namespace { |
| 23 | |
Ben Clayton | b8ea591 | 2021-04-19 16:52:42 +0000 | [diff] [blame] | 24 | using ::testing::HasSubstr; |
| 25 | |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 26 | using MslGeneratorImplTest = TestHelper; |
dan sinclair | 4510159 | 2020-07-08 20:52:46 +0000 | [diff] [blame] | 27 | |
| 28 | TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement) { |
Ben Clayton | 37571bc | 2021-02-16 23:57:01 +0000 | [diff] [blame] | 29 | auto* var = Var("a", ty.f32(), ast::StorageClass::kNone); |
Ben Clayton | 43073d8 | 2021-04-22 13:50:53 +0000 | [diff] [blame] | 30 | auto* stmt = Decl(var); |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 31 | WrapInFunction(stmt); |
dan sinclair | 4510159 | 2020-07-08 20:52:46 +0000 | [diff] [blame] | 32 | |
Ben Clayton | f12054e | 2021-01-21 16:15:00 +0000 | [diff] [blame] | 33 | GeneratorImpl& gen = Build(); |
| 34 | |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 35 | gen.increment_indent(); |
dan sinclair | 4510159 | 2020-07-08 20:52:46 +0000 | [diff] [blame] | 36 | |
Ben Clayton | e6e7041 | 2021-01-05 15:29:29 +0000 | [diff] [blame] | 37 | ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error(); |
dan sinclair | 987376c | 2021-01-12 04:34:53 +0000 | [diff] [blame] | 38 | EXPECT_EQ(gen.result(), " float a = 0.0f;\n"); |
dan sinclair | 4510159 | 2020-07-08 20:52:46 +0000 | [diff] [blame] | 39 | } |
| 40 | |
dan sinclair | cbe0668 | 2020-07-30 22:26:46 +0000 | [diff] [blame] | 41 | TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Const) { |
James Price | 08df57a | 2021-05-06 13:37:13 +0000 | [diff] [blame] | 42 | auto* var = Const("a", ty.f32(), Construct(ty.f32())); |
Ben Clayton | 43073d8 | 2021-04-22 13:50:53 +0000 | [diff] [blame] | 43 | auto* stmt = Decl(var); |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 44 | WrapInFunction(stmt); |
dan sinclair | cbe0668 | 2020-07-30 22:26:46 +0000 | [diff] [blame] | 45 | |
Ben Clayton | f12054e | 2021-01-21 16:15:00 +0000 | [diff] [blame] | 46 | GeneratorImpl& gen = Build(); |
| 47 | |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 48 | gen.increment_indent(); |
dan sinclair | cbe0668 | 2020-07-30 22:26:46 +0000 | [diff] [blame] | 49 | |
Ben Clayton | e6e7041 | 2021-01-05 15:29:29 +0000 | [diff] [blame] | 50 | ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error(); |
James Price | 6c58277 | 2021-05-20 16:45:47 +0000 | [diff] [blame] | 51 | EXPECT_EQ(gen.result(), " float const a = float();\n"); |
dan sinclair | cbe0668 | 2020-07-30 22:26:46 +0000 | [diff] [blame] | 52 | } |
| 53 | |
dan sinclair | 4510159 | 2020-07-08 20:52:46 +0000 | [diff] [blame] | 54 | TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Array) { |
Ben Clayton | c6ed25c | 2021-05-04 19:02:01 +0000 | [diff] [blame] | 55 | auto* var = Var("a", ty.array<f32, 5>(), ast::StorageClass::kNone); |
Ben Clayton | 43073d8 | 2021-04-22 13:50:53 +0000 | [diff] [blame] | 56 | auto* stmt = Decl(var); |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 57 | WrapInFunction(stmt); |
dan sinclair | 4510159 | 2020-07-08 20:52:46 +0000 | [diff] [blame] | 58 | |
Ben Clayton | f12054e | 2021-01-21 16:15:00 +0000 | [diff] [blame] | 59 | GeneratorImpl& gen = Build(); |
| 60 | |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 61 | gen.increment_indent(); |
dan sinclair | 4510159 | 2020-07-08 20:52:46 +0000 | [diff] [blame] | 62 | |
Ben Clayton | e6e7041 | 2021-01-05 15:29:29 +0000 | [diff] [blame] | 63 | ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error(); |
dan sinclair | 987376c | 2021-01-12 04:34:53 +0000 | [diff] [blame] | 64 | EXPECT_EQ(gen.result(), " float a[5] = {0.0f};\n"); |
dan sinclair | 4510159 | 2020-07-08 20:52:46 +0000 | [diff] [blame] | 65 | } |
| 66 | |
dan sinclair | f367ead | 2020-09-24 14:33:24 +0000 | [diff] [blame] | 67 | TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Struct) { |
Ben Clayton | ba6ab5e | 2021-05-07 14:49:34 +0000 | [diff] [blame] | 68 | auto* s = Structure("S", { |
| 69 | Member("a", ty.f32()), |
| 70 | Member("b", ty.f32()), |
| 71 | }); |
dan sinclair | f367ead | 2020-09-24 14:33:24 +0000 | [diff] [blame] | 72 | |
Ben Clayton | 8758f10 | 2021-06-09 14:32:14 +0000 | [diff] [blame] | 73 | auto* var = Var("a", ty.Of(s), ast::StorageClass::kNone); |
Ben Clayton | 43073d8 | 2021-04-22 13:50:53 +0000 | [diff] [blame] | 74 | auto* stmt = Decl(var); |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 75 | WrapInFunction(stmt); |
dan sinclair | 4510159 | 2020-07-08 20:52:46 +0000 | [diff] [blame] | 76 | |
Ben Clayton | f12054e | 2021-01-21 16:15:00 +0000 | [diff] [blame] | 77 | GeneratorImpl& gen = Build(); |
| 78 | |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 79 | gen.increment_indent(); |
dan sinclair | 4510159 | 2020-07-08 20:52:46 +0000 | [diff] [blame] | 80 | |
Ben Clayton | e6e7041 | 2021-01-05 15:29:29 +0000 | [diff] [blame] | 81 | ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error(); |
dan sinclair | 987376c | 2021-01-12 04:34:53 +0000 | [diff] [blame] | 82 | EXPECT_EQ(gen.result(), R"( S a = {}; |
dan sinclair | f367ead | 2020-09-24 14:33:24 +0000 | [diff] [blame] | 83 | )"); |
| 84 | } |
| 85 | |
| 86 | TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Vector) { |
Ben Clayton | f6c84e4 | 2021-05-12 21:08:22 +0000 | [diff] [blame] | 87 | auto* var = Var("a", ty.vec2<f32>()); |
Ben Clayton | 43073d8 | 2021-04-22 13:50:53 +0000 | [diff] [blame] | 88 | auto* stmt = Decl(var); |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 89 | WrapInFunction(stmt); |
dan sinclair | f367ead | 2020-09-24 14:33:24 +0000 | [diff] [blame] | 90 | |
Ben Clayton | f12054e | 2021-01-21 16:15:00 +0000 | [diff] [blame] | 91 | GeneratorImpl& gen = Build(); |
| 92 | |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 93 | gen.increment_indent(); |
dan sinclair | f367ead | 2020-09-24 14:33:24 +0000 | [diff] [blame] | 94 | |
Ben Clayton | e6e7041 | 2021-01-05 15:29:29 +0000 | [diff] [blame] | 95 | ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error(); |
dan sinclair | 987376c | 2021-01-12 04:34:53 +0000 | [diff] [blame] | 96 | EXPECT_EQ(gen.result(), " float2 a = 0.0f;\n"); |
dan sinclair | f367ead | 2020-09-24 14:33:24 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Matrix) { |
Ben Clayton | f6c84e4 | 2021-05-12 21:08:22 +0000 | [diff] [blame] | 100 | auto* var = Var("a", ty.mat3x2<f32>()); |
dan sinclair | f367ead | 2020-09-24 14:33:24 +0000 | [diff] [blame] | 101 | |
Ben Clayton | 43073d8 | 2021-04-22 13:50:53 +0000 | [diff] [blame] | 102 | auto* stmt = Decl(var); |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 103 | WrapInFunction(stmt); |
dan sinclair | f367ead | 2020-09-24 14:33:24 +0000 | [diff] [blame] | 104 | |
Ben Clayton | f12054e | 2021-01-21 16:15:00 +0000 | [diff] [blame] | 105 | GeneratorImpl& gen = Build(); |
| 106 | |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 107 | gen.increment_indent(); |
dan sinclair | f367ead | 2020-09-24 14:33:24 +0000 | [diff] [blame] | 108 | |
Ben Clayton | e6e7041 | 2021-01-05 15:29:29 +0000 | [diff] [blame] | 109 | ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error(); |
James Price | 9486759 | 2021-05-19 11:15:58 +0000 | [diff] [blame] | 110 | EXPECT_EQ(gen.result(), " float3x2 a = float3x2(0.0f);\n"); |
dan sinclair | 4510159 | 2020-07-08 20:52:46 +0000 | [diff] [blame] | 111 | } |
| 112 | |
James Price | 7a47fa8 | 2021-05-26 15:41:02 +0000 | [diff] [blame] | 113 | TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Private) { |
Ben Clayton | b8ea591 | 2021-04-19 16:52:42 +0000 | [diff] [blame] | 114 | Global("a", ty.f32(), ast::StorageClass::kPrivate); |
| 115 | |
| 116 | WrapInFunction(Expr("a")); |
dan sinclair | 4510159 | 2020-07-08 20:52:46 +0000 | [diff] [blame] | 117 | |
James Price | 7a47fa8 | 2021-05-26 15:41:02 +0000 | [diff] [blame] | 118 | GeneratorImpl& gen = SanitizeAndBuild(); |
Ben Clayton | f12054e | 2021-01-21 16:15:00 +0000 | [diff] [blame] | 119 | |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 120 | gen.increment_indent(); |
dan sinclair | 4510159 | 2020-07-08 20:52:46 +0000 | [diff] [blame] | 121 | |
Ben Clayton | b8ea591 | 2021-04-19 16:52:42 +0000 | [diff] [blame] | 122 | ASSERT_TRUE(gen.Generate()) << gen.error(); |
James Price | 2940c70 | 2021-06-11 12:29:56 +0000 | [diff] [blame] | 123 | EXPECT_THAT(gen.result(), HasSubstr("thread float tint_symbol_1 = 0.0f;\n")); |
dan sinclair | 4510159 | 2020-07-08 20:52:46 +0000 | [diff] [blame] | 124 | } |
| 125 | |
James Price | 7a47fa8 | 2021-05-26 15:41:02 +0000 | [diff] [blame] | 126 | TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_Private) { |
| 127 | GlobalConst("initializer", ty.f32(), Expr(0.f)); |
Ben Clayton | b8ea591 | 2021-04-19 16:52:42 +0000 | [diff] [blame] | 128 | Global("a", ty.f32(), ast::StorageClass::kPrivate, Expr("initializer")); |
| 129 | |
| 130 | WrapInFunction(Expr("a")); |
dan sinclair | 4510159 | 2020-07-08 20:52:46 +0000 | [diff] [blame] | 131 | |
James Price | 7a47fa8 | 2021-05-26 15:41:02 +0000 | [diff] [blame] | 132 | GeneratorImpl& gen = SanitizeAndBuild(); |
Ben Clayton | f12054e | 2021-01-21 16:15:00 +0000 | [diff] [blame] | 133 | |
Ben Clayton | b8ea591 | 2021-04-19 16:52:42 +0000 | [diff] [blame] | 134 | ASSERT_TRUE(gen.Generate()) << gen.error(); |
James Price | 7a47fa8 | 2021-05-26 15:41:02 +0000 | [diff] [blame] | 135 | EXPECT_THAT(gen.result(), |
James Price | 2940c70 | 2021-06-11 12:29:56 +0000 | [diff] [blame] | 136 | HasSubstr("thread float tint_symbol_1 = initializer;\n")); |
James Price | 7a47fa8 | 2021-05-26 15:41:02 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Workgroup) { |
| 140 | Global("a", ty.f32(), ast::StorageClass::kWorkgroup); |
| 141 | |
| 142 | WrapInFunction(Expr("a")); |
| 143 | |
| 144 | GeneratorImpl& gen = SanitizeAndBuild(); |
| 145 | |
| 146 | gen.increment_indent(); |
| 147 | |
| 148 | ASSERT_TRUE(gen.Generate()) << gen.error(); |
Ben Clayton | 75db82c | 2021-06-18 22:44:31 +0000 | [diff] [blame^] | 149 | EXPECT_THAT(gen.result(), HasSubstr("threadgroup float tint_symbol_2;\n")); |
dan sinclair | 4510159 | 2020-07-08 20:52:46 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_ZeroVec) { |
Ben Clayton | 1637cbb | 2021-01-05 15:44:39 +0000 | [diff] [blame] | 153 | auto* zero_vec = vec3<f32>(); |
dan sinclair | 4510159 | 2020-07-08 20:52:46 +0000 | [diff] [blame] | 154 | |
Ben Clayton | f6c84e4 | 2021-05-12 21:08:22 +0000 | [diff] [blame] | 155 | auto* var = Var("a", ty.vec3<f32>(), ast::StorageClass::kNone, zero_vec); |
Ben Clayton | 43073d8 | 2021-04-22 13:50:53 +0000 | [diff] [blame] | 156 | auto* stmt = Decl(var); |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 157 | WrapInFunction(stmt); |
dan sinclair | 4510159 | 2020-07-08 20:52:46 +0000 | [diff] [blame] | 158 | |
Ben Clayton | f12054e | 2021-01-21 16:15:00 +0000 | [diff] [blame] | 159 | GeneratorImpl& gen = Build(); |
| 160 | |
Ben Clayton | e6e7041 | 2021-01-05 15:29:29 +0000 | [diff] [blame] | 161 | ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error(); |
James Price | 6c58277 | 2021-05-20 16:45:47 +0000 | [diff] [blame] | 162 | EXPECT_EQ(gen.result(), R"(float3 a = float3(); |
dan sinclair | 4510159 | 2020-07-08 20:52:46 +0000 | [diff] [blame] | 163 | )"); |
| 164 | } |
| 165 | |
| 166 | } // namespace |
| 167 | } // namespace msl |
| 168 | } // namespace writer |
| 169 | } // namespace tint |