blob: 5e9febf62c49fbe3b258420ac0ea3826e1f43232 [file] [log] [blame]
dan sinclairc2d97ae2020-04-03 02:35:23 +00001// 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 Claytona6b9a8e2021-01-26 16:57:10 +000018#include "src/program_builder.h"
dan sinclair795b6b52021-01-11 15:10:19 +000019#include "src/symbol.h"
Ben Clayton207b5e22021-01-21 15:42:10 +000020#include "src/type/f32_type.h"
dan sinclairc2d97ae2020-04-03 02:35:23 +000021
22namespace tint {
23namespace {
24
Ben Claytona6b9a8e2021-01-26 16:57:10 +000025class ScopeStackTest : public ProgramBuilder, public testing::Test {};
dan sinclairc2d97ae2020-04-03 02:35:23 +000026
27TEST_F(ScopeStackTest, Global) {
28 ScopeStack<uint32_t> s;
dan sinclair795b6b52021-01-11 15:10:19 +000029 Symbol sym(1);
30 s.set_global(sym, 5);
dan sinclairc2d97ae2020-04-03 02:35:23 +000031
32 uint32_t val = 0;
dan sinclair795b6b52021-01-11 15:10:19 +000033 EXPECT_TRUE(s.get(sym, &val));
Ryan Harrison0a196c12020-04-17 13:18:20 +000034 EXPECT_EQ(val, 5u);
dan sinclairc2d97ae2020-04-03 02:35:23 +000035}
36
37TEST_F(ScopeStackTest, Global_SetWithPointer) {
Ben Clayton8d391f72021-01-26 16:57:10 +000038 auto* v = Var("my_var", ast::StorageClass::kNone, ty.f32());
dan sinclairc2d97ae2020-04-03 02:35:23 +000039 ScopeStack<ast::Variable*> s;
dan sinclair795b6b52021-01-11 15:10:19 +000040 s.set_global(v->symbol(), v);
dan sinclairc2d97ae2020-04-03 02:35:23 +000041
Greg Roth82293602020-04-06 17:16:56 +000042 ast::Variable* v2 = nullptr;
dan sinclair795b6b52021-01-11 15:10:19 +000043 EXPECT_TRUE(s.get(v->symbol(), &v2));
44 EXPECT_EQ(v2->symbol(), v->symbol());
dan sinclairc2d97ae2020-04-03 02:35:23 +000045}
46
47TEST_F(ScopeStackTest, Global_CanNotPop) {
48 ScopeStack<uint32_t> s;
dan sinclair795b6b52021-01-11 15:10:19 +000049 Symbol sym(1);
50 s.set_global(sym, 5);
dan sinclairc2d97ae2020-04-03 02:35:23 +000051 s.pop_scope();
52
53 uint32_t val = 0;
dan sinclair795b6b52021-01-11 15:10:19 +000054 EXPECT_TRUE(s.get(sym, &val));
Ryan Harrison0a196c12020-04-17 13:18:20 +000055 EXPECT_EQ(val, 5u);
dan sinclairc2d97ae2020-04-03 02:35:23 +000056}
57
58TEST_F(ScopeStackTest, Scope) {
59 ScopeStack<uint32_t> s;
dan sinclair795b6b52021-01-11 15:10:19 +000060 Symbol sym(1);
dan sinclairc2d97ae2020-04-03 02:35:23 +000061 s.push_scope();
dan sinclair795b6b52021-01-11 15:10:19 +000062 s.set(sym, 5);
dan sinclairc2d97ae2020-04-03 02:35:23 +000063
64 uint32_t val = 0;
dan sinclair795b6b52021-01-11 15:10:19 +000065 EXPECT_TRUE(s.get(sym, &val));
Ryan Harrison0a196c12020-04-17 13:18:20 +000066 EXPECT_EQ(val, 5u);
dan sinclairc2d97ae2020-04-03 02:35:23 +000067}
68
dan sinclair795b6b52021-01-11 15:10:19 +000069TEST_F(ScopeStackTest, Get_MissingSymbol) {
dan sinclairc2d97ae2020-04-03 02:35:23 +000070 ScopeStack<uint32_t> s;
dan sinclair795b6b52021-01-11 15:10:19 +000071 Symbol sym(1);
dan sinclairc2d97ae2020-04-03 02:35:23 +000072 uint32_t ret = 0;
dan sinclair795b6b52021-01-11 15:10:19 +000073 EXPECT_FALSE(s.get(sym, &ret));
Ryan Harrison0a196c12020-04-17 13:18:20 +000074 EXPECT_EQ(ret, 0u);
dan sinclairc2d97ae2020-04-03 02:35:23 +000075}
76
77TEST_F(ScopeStackTest, Has) {
78 ScopeStack<uint32_t> s;
dan sinclair795b6b52021-01-11 15:10:19 +000079 Symbol sym(1);
80 Symbol sym2(2);
81 s.set_global(sym2, 3);
dan sinclairc2d97ae2020-04-03 02:35:23 +000082 s.push_scope();
dan sinclair795b6b52021-01-11 15:10:19 +000083 s.set(sym, 5);
dan sinclairc2d97ae2020-04-03 02:35:23 +000084
dan sinclair795b6b52021-01-11 15:10:19 +000085 EXPECT_TRUE(s.has(sym));
86 EXPECT_TRUE(s.has(sym2));
dan sinclairc2d97ae2020-04-03 02:35:23 +000087}
88
89TEST_F(ScopeStackTest, ReturnsScopeBeforeGlobalFirst) {
90 ScopeStack<uint32_t> s;
dan sinclair795b6b52021-01-11 15:10:19 +000091 Symbol sym(1);
92 s.set_global(sym, 3);
dan sinclairc2d97ae2020-04-03 02:35:23 +000093 s.push_scope();
dan sinclair795b6b52021-01-11 15:10:19 +000094 s.set(sym, 5);
dan sinclairc2d97ae2020-04-03 02:35:23 +000095
96 uint32_t ret;
dan sinclair795b6b52021-01-11 15:10:19 +000097 EXPECT_TRUE(s.get(sym, &ret));
Ryan Harrison0a196c12020-04-17 13:18:20 +000098 EXPECT_EQ(ret, 5u);
dan sinclairc2d97ae2020-04-03 02:35:23 +000099}
100
101} // namespace
102} // namespace tint