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/ast/struct_block_decoration.h"
16 #include "src/resolver/resolver.h"
17 #include "src/resolver/resolver_test_helper.h"
18 #include "src/sem/atomic_type.h"
19 #include "src/sem/reference_type.h"
20
21 #include "gmock/gmock.h"
22
23 namespace tint {
24 namespace resolver {
25 namespace {
26
27 struct ResolverAtomicTest : public resolver::TestHelper,
28 public testing::Test {};
29
TEST_F(ResolverAtomicTest,GlobalWorkgroupI32)30 TEST_F(ResolverAtomicTest, GlobalWorkgroupI32) {
31 auto* g = Global("a", ty.atomic(Source{{12, 34}}, ty.i32()),
32 ast::StorageClass::kWorkgroup);
33
34 EXPECT_TRUE(r()->Resolve()) << r()->error();
35 ASSERT_TRUE(TypeOf(g)->Is<sem::Reference>());
36 auto* atomic = TypeOf(g)->UnwrapRef()->As<sem::Atomic>();
37 ASSERT_NE(atomic, nullptr);
38 EXPECT_TRUE(atomic->Type()->Is<sem::I32>());
39 }
40
TEST_F(ResolverAtomicTest,GlobalWorkgroupU32)41 TEST_F(ResolverAtomicTest, GlobalWorkgroupU32) {
42 auto* g = Global("a", ty.atomic(Source{{12, 34}}, ty.u32()),
43 ast::StorageClass::kWorkgroup);
44
45 EXPECT_TRUE(r()->Resolve()) << r()->error();
46 ASSERT_TRUE(TypeOf(g)->Is<sem::Reference>());
47 auto* atomic = TypeOf(g)->UnwrapRef()->As<sem::Atomic>();
48 ASSERT_NE(atomic, nullptr);
49 EXPECT_TRUE(atomic->Type()->Is<sem::U32>());
50 }
51
TEST_F(ResolverAtomicTest,GlobalStorageStruct)52 TEST_F(ResolverAtomicTest, GlobalStorageStruct) {
53 auto* s = Structure("s", {Member("a", ty.atomic(Source{{12, 34}}, ty.i32()))},
54 {create<ast::StructBlockDecoration>()});
55 auto* g = Global("g", ty.Of(s), ast::StorageClass::kStorage,
56 ast::Access::kReadWrite,
57 ast::DecorationList{
58 create<ast::BindingDecoration>(0),
59 create<ast::GroupDecoration>(0),
60 });
61
62 EXPECT_TRUE(r()->Resolve()) << r()->error();
63 ASSERT_TRUE(TypeOf(g)->Is<sem::Reference>());
64 auto* str = TypeOf(g)->UnwrapRef()->As<sem::Struct>();
65 ASSERT_NE(str, nullptr);
66 ASSERT_EQ(str->Members().size(), 1u);
67 auto* atomic = str->Members()[0]->Type()->As<sem::Atomic>();
68 ASSERT_NE(atomic, nullptr);
69 ASSERT_TRUE(atomic->Type()->Is<sem::I32>());
70 }
71
72 } // namespace
73 } // namespace resolver
74 } // namespace tint
75