dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 1 | // Copyright 2020 The Tint Authors. // |
| 2 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | // you may not use this file except in compliance with the License. |
| 4 | // You may obtain a copy of the License at |
| 5 | // |
| 6 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | // |
| 8 | // Unless required by applicable law or agreed to in writing, software |
| 9 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | // See the License for the specific language governing permissions and |
| 12 | // limitations under the License. |
| 13 | |
| 14 | #include "src/scope_stack.h" |
| 15 | |
| 16 | #include "gtest/gtest.h" |
| 17 | #include "src/ast/variable.h" |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame^] | 18 | #include "src/program_builder.h" |
dan sinclair | 795b6b5 | 2021-01-11 15:10:19 +0000 | [diff] [blame] | 19 | #include "src/symbol.h" |
Ben Clayton | 207b5e2 | 2021-01-21 15:42:10 +0000 | [diff] [blame] | 20 | #include "src/type/f32_type.h" |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 21 | |
| 22 | namespace tint { |
| 23 | namespace { |
| 24 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame^] | 25 | class ScopeStackTest : public ProgramBuilder, public testing::Test {}; |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 26 | |
| 27 | TEST_F(ScopeStackTest, Global) { |
| 28 | ScopeStack<uint32_t> s; |
dan sinclair | 795b6b5 | 2021-01-11 15:10:19 +0000 | [diff] [blame] | 29 | Symbol sym(1); |
| 30 | s.set_global(sym, 5); |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 31 | |
| 32 | uint32_t val = 0; |
dan sinclair | 795b6b5 | 2021-01-11 15:10:19 +0000 | [diff] [blame] | 33 | EXPECT_TRUE(s.get(sym, &val)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 34 | EXPECT_EQ(val, 5u); |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | TEST_F(ScopeStackTest, Global_SetWithPointer) { |
Ben Clayton | 8d391f7 | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 38 | auto* v = Var("my_var", ast::StorageClass::kNone, ty.f32()); |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 39 | ScopeStack<ast::Variable*> s; |
dan sinclair | 795b6b5 | 2021-01-11 15:10:19 +0000 | [diff] [blame] | 40 | s.set_global(v->symbol(), v); |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 41 | |
Greg Roth | 8229360 | 2020-04-06 17:16:56 +0000 | [diff] [blame] | 42 | ast::Variable* v2 = nullptr; |
dan sinclair | 795b6b5 | 2021-01-11 15:10:19 +0000 | [diff] [blame] | 43 | EXPECT_TRUE(s.get(v->symbol(), &v2)); |
| 44 | EXPECT_EQ(v2->symbol(), v->symbol()); |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | TEST_F(ScopeStackTest, Global_CanNotPop) { |
| 48 | ScopeStack<uint32_t> s; |
dan sinclair | 795b6b5 | 2021-01-11 15:10:19 +0000 | [diff] [blame] | 49 | Symbol sym(1); |
| 50 | s.set_global(sym, 5); |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 51 | s.pop_scope(); |
| 52 | |
| 53 | uint32_t val = 0; |
dan sinclair | 795b6b5 | 2021-01-11 15:10:19 +0000 | [diff] [blame] | 54 | EXPECT_TRUE(s.get(sym, &val)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 55 | EXPECT_EQ(val, 5u); |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | TEST_F(ScopeStackTest, Scope) { |
| 59 | ScopeStack<uint32_t> s; |
dan sinclair | 795b6b5 | 2021-01-11 15:10:19 +0000 | [diff] [blame] | 60 | Symbol sym(1); |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 61 | s.push_scope(); |
dan sinclair | 795b6b5 | 2021-01-11 15:10:19 +0000 | [diff] [blame] | 62 | s.set(sym, 5); |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 63 | |
| 64 | uint32_t val = 0; |
dan sinclair | 795b6b5 | 2021-01-11 15:10:19 +0000 | [diff] [blame] | 65 | EXPECT_TRUE(s.get(sym, &val)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 66 | EXPECT_EQ(val, 5u); |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 67 | } |
| 68 | |
dan sinclair | 795b6b5 | 2021-01-11 15:10:19 +0000 | [diff] [blame] | 69 | TEST_F(ScopeStackTest, Get_MissingSymbol) { |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 70 | ScopeStack<uint32_t> s; |
dan sinclair | 795b6b5 | 2021-01-11 15:10:19 +0000 | [diff] [blame] | 71 | Symbol sym(1); |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 72 | uint32_t ret = 0; |
dan sinclair | 795b6b5 | 2021-01-11 15:10:19 +0000 | [diff] [blame] | 73 | EXPECT_FALSE(s.get(sym, &ret)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 74 | EXPECT_EQ(ret, 0u); |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | TEST_F(ScopeStackTest, Has) { |
| 78 | ScopeStack<uint32_t> s; |
dan sinclair | 795b6b5 | 2021-01-11 15:10:19 +0000 | [diff] [blame] | 79 | Symbol sym(1); |
| 80 | Symbol sym2(2); |
| 81 | s.set_global(sym2, 3); |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 82 | s.push_scope(); |
dan sinclair | 795b6b5 | 2021-01-11 15:10:19 +0000 | [diff] [blame] | 83 | s.set(sym, 5); |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 84 | |
dan sinclair | 795b6b5 | 2021-01-11 15:10:19 +0000 | [diff] [blame] | 85 | EXPECT_TRUE(s.has(sym)); |
| 86 | EXPECT_TRUE(s.has(sym2)); |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | TEST_F(ScopeStackTest, ReturnsScopeBeforeGlobalFirst) { |
| 90 | ScopeStack<uint32_t> s; |
dan sinclair | 795b6b5 | 2021-01-11 15:10:19 +0000 | [diff] [blame] | 91 | Symbol sym(1); |
| 92 | s.set_global(sym, 3); |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 93 | s.push_scope(); |
dan sinclair | 795b6b5 | 2021-01-11 15:10:19 +0000 | [diff] [blame] | 94 | s.set(sym, 5); |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 95 | |
| 96 | uint32_t ret; |
dan sinclair | 795b6b5 | 2021-01-11 15:10:19 +0000 | [diff] [blame] | 97 | EXPECT_TRUE(s.get(sym, &ret)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame] | 98 | EXPECT_EQ(ret, 5u); |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | } // namespace |
| 102 | } // namespace tint |