blob: 85bd86ba6d3eba2d64c0a6b4376464fbb9cf3428 [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"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000017#include "src/program_builder.h"
dan sinclairc2d97ae2020-04-03 02:35:23 +000018
19namespace tint {
20namespace {
21
Ben Claytona6b9a8e2021-01-26 16:57:10 +000022class ScopeStackTest : public ProgramBuilder, public testing::Test {};
dan sinclairc2d97ae2020-04-03 02:35:23 +000023
24TEST_F(ScopeStackTest, Global) {
25 ScopeStack<uint32_t> s;
Ben Clayton13ef87c2021-04-15 18:20:03 +000026 Symbol sym(1, ID());
dan sinclair795b6b52021-01-11 15:10:19 +000027 s.set_global(sym, 5);
dan sinclairc2d97ae2020-04-03 02:35:23 +000028
29 uint32_t val = 0;
dan sinclair795b6b52021-01-11 15:10:19 +000030 EXPECT_TRUE(s.get(sym, &val));
Ryan Harrison0a196c12020-04-17 13:18:20 +000031 EXPECT_EQ(val, 5u);
dan sinclairc2d97ae2020-04-03 02:35:23 +000032}
33
34TEST_F(ScopeStackTest, Global_SetWithPointer) {
Ben Clayton37571bc2021-02-16 23:57:01 +000035 auto* v = Var("my_var", ty.f32(), ast::StorageClass::kNone);
dan sinclairc2d97ae2020-04-03 02:35:23 +000036 ScopeStack<ast::Variable*> s;
dan sinclair795b6b52021-01-11 15:10:19 +000037 s.set_global(v->symbol(), v);
dan sinclairc2d97ae2020-04-03 02:35:23 +000038
Greg Roth82293602020-04-06 17:16:56 +000039 ast::Variable* v2 = nullptr;
dan sinclair795b6b52021-01-11 15:10:19 +000040 EXPECT_TRUE(s.get(v->symbol(), &v2));
41 EXPECT_EQ(v2->symbol(), v->symbol());
dan sinclairc2d97ae2020-04-03 02:35:23 +000042}
43
44TEST_F(ScopeStackTest, Global_CanNotPop) {
45 ScopeStack<uint32_t> s;
Ben Clayton13ef87c2021-04-15 18:20:03 +000046 Symbol sym(1, ID());
dan sinclair795b6b52021-01-11 15:10:19 +000047 s.set_global(sym, 5);
dan sinclairc2d97ae2020-04-03 02:35:23 +000048 s.pop_scope();
49
50 uint32_t val = 0;
dan sinclair795b6b52021-01-11 15:10:19 +000051 EXPECT_TRUE(s.get(sym, &val));
Ryan Harrison0a196c12020-04-17 13:18:20 +000052 EXPECT_EQ(val, 5u);
dan sinclairc2d97ae2020-04-03 02:35:23 +000053}
54
55TEST_F(ScopeStackTest, Scope) {
56 ScopeStack<uint32_t> s;
Ben Clayton13ef87c2021-04-15 18:20:03 +000057 Symbol sym(1, ID());
dan sinclairc2d97ae2020-04-03 02:35:23 +000058 s.push_scope();
dan sinclair795b6b52021-01-11 15:10:19 +000059 s.set(sym, 5);
dan sinclairc2d97ae2020-04-03 02:35:23 +000060
61 uint32_t val = 0;
dan sinclair795b6b52021-01-11 15:10:19 +000062 EXPECT_TRUE(s.get(sym, &val));
Ryan Harrison0a196c12020-04-17 13:18:20 +000063 EXPECT_EQ(val, 5u);
dan sinclairc2d97ae2020-04-03 02:35:23 +000064}
65
dan sinclair795b6b52021-01-11 15:10:19 +000066TEST_F(ScopeStackTest, Get_MissingSymbol) {
dan sinclairc2d97ae2020-04-03 02:35:23 +000067 ScopeStack<uint32_t> s;
Ben Clayton13ef87c2021-04-15 18:20:03 +000068 Symbol sym(1, ID());
dan sinclairc2d97ae2020-04-03 02:35:23 +000069 uint32_t ret = 0;
dan sinclair795b6b52021-01-11 15:10:19 +000070 EXPECT_FALSE(s.get(sym, &ret));
Ryan Harrison0a196c12020-04-17 13:18:20 +000071 EXPECT_EQ(ret, 0u);
dan sinclairc2d97ae2020-04-03 02:35:23 +000072}
73
74TEST_F(ScopeStackTest, Has) {
75 ScopeStack<uint32_t> s;
Ben Clayton13ef87c2021-04-15 18:20:03 +000076 Symbol sym(1, ID());
77 Symbol sym2(2, ID());
dan sinclair795b6b52021-01-11 15:10:19 +000078 s.set_global(sym2, 3);
dan sinclairc2d97ae2020-04-03 02:35:23 +000079 s.push_scope();
dan sinclair795b6b52021-01-11 15:10:19 +000080 s.set(sym, 5);
dan sinclairc2d97ae2020-04-03 02:35:23 +000081
dan sinclair795b6b52021-01-11 15:10:19 +000082 EXPECT_TRUE(s.has(sym));
83 EXPECT_TRUE(s.has(sym2));
dan sinclairc2d97ae2020-04-03 02:35:23 +000084}
85
86TEST_F(ScopeStackTest, ReturnsScopeBeforeGlobalFirst) {
87 ScopeStack<uint32_t> s;
Ben Clayton13ef87c2021-04-15 18:20:03 +000088 Symbol sym(1, ID());
dan sinclair795b6b52021-01-11 15:10:19 +000089 s.set_global(sym, 3);
dan sinclairc2d97ae2020-04-03 02:35:23 +000090 s.push_scope();
dan sinclair795b6b52021-01-11 15:10:19 +000091 s.set(sym, 5);
dan sinclairc2d97ae2020-04-03 02:35:23 +000092
93 uint32_t ret;
dan sinclair795b6b52021-01-11 15:10:19 +000094 EXPECT_TRUE(s.get(sym, &ret));
Ryan Harrison0a196c12020-04-17 13:18:20 +000095 EXPECT_EQ(ret, 5u);
dan sinclairc2d97ae2020-04-03 02:35:23 +000096}
97
98} // namespace
99} // namespace tint