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" |
| 18 | |
| 19 | namespace tint { |
| 20 | namespace { |
| 21 | |
| 22 | using ScopeStackTest = testing::Test; |
| 23 | |
| 24 | TEST_F(ScopeStackTest, Global) { |
| 25 | ScopeStack<uint32_t> s; |
| 26 | s.set_global("var", 5); |
| 27 | |
| 28 | uint32_t val = 0; |
| 29 | EXPECT_TRUE(s.get("var", &val)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame^] | 30 | EXPECT_EQ(val, 5u); |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | TEST_F(ScopeStackTest, Global_SetWithPointer) { |
| 34 | ast::Variable v; |
| 35 | v.set_name("my_var"); |
| 36 | |
| 37 | ScopeStack<ast::Variable*> s; |
| 38 | s.set_global("var", &v); |
| 39 | |
Greg Roth | 8229360 | 2020-04-06 17:16:56 +0000 | [diff] [blame] | 40 | ast::Variable* v2 = nullptr; |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 41 | EXPECT_TRUE(s.get("var", &v2)); |
| 42 | EXPECT_EQ(v2->name(), "my_var"); |
| 43 | } |
| 44 | |
| 45 | TEST_F(ScopeStackTest, Global_CanNotPop) { |
| 46 | ScopeStack<uint32_t> s; |
| 47 | s.set_global("var", 5); |
| 48 | s.pop_scope(); |
| 49 | |
| 50 | uint32_t val = 0; |
| 51 | EXPECT_TRUE(s.get("var", &val)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame^] | 52 | EXPECT_EQ(val, 5u); |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | TEST_F(ScopeStackTest, Scope) { |
| 56 | ScopeStack<uint32_t> s; |
| 57 | s.push_scope(); |
| 58 | s.set("var", 5); |
| 59 | |
| 60 | uint32_t val = 0; |
| 61 | EXPECT_TRUE(s.get("var", &val)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame^] | 62 | EXPECT_EQ(val, 5u); |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | TEST_F(ScopeStackTest, Get_MissingName) { |
| 66 | ScopeStack<uint32_t> s; |
| 67 | uint32_t ret = 0; |
| 68 | EXPECT_FALSE(s.get("val", &ret)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame^] | 69 | EXPECT_EQ(ret, 0u); |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | TEST_F(ScopeStackTest, Has) { |
| 73 | ScopeStack<uint32_t> s; |
| 74 | s.set_global("var2", 3); |
| 75 | s.push_scope(); |
| 76 | s.set("var", 5); |
| 77 | |
| 78 | EXPECT_TRUE(s.has("var")); |
| 79 | EXPECT_TRUE(s.has("var2")); |
| 80 | } |
| 81 | |
| 82 | TEST_F(ScopeStackTest, ReturnsScopeBeforeGlobalFirst) { |
| 83 | ScopeStack<uint32_t> s; |
| 84 | s.set_global("var", 3); |
| 85 | s.push_scope(); |
| 86 | s.set("var", 5); |
| 87 | |
| 88 | uint32_t ret; |
| 89 | EXPECT_TRUE(s.get("var", &ret)); |
Ryan Harrison | 0a196c1 | 2020-04-17 13:18:20 +0000 | [diff] [blame^] | 90 | EXPECT_EQ(ret, 5u); |
dan sinclair | c2d97ae | 2020-04-03 02:35:23 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | } // namespace |
| 94 | } // namespace tint |