blob: aeb7e73214f14c8308736a46f48ebcadacba962a [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +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/tint/scope_stack.h"
15
16#include "gtest/gtest.h"
17#include "src/tint/program_builder.h"
18
19namespace tint {
20namespace {
21
22class ScopeStackTest : public ProgramBuilder, public testing::Test {};
23
24TEST_F(ScopeStackTest, Get) {
James Price2cf32b12022-05-11 13:50:33 +000025 ScopeStack<Symbol, uint32_t> s;
dan sinclair41e4d9a2022-05-01 14:40:55 +000026 Symbol a(1, ID());
27 Symbol b(3, ID());
28 s.Push();
29 s.Set(a, 5u);
30 s.Set(b, 10u);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000031
dan sinclair41e4d9a2022-05-01 14:40:55 +000032 EXPECT_EQ(s.Get(a), 5u);
33 EXPECT_EQ(s.Get(b), 10u);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000034
dan sinclair41e4d9a2022-05-01 14:40:55 +000035 s.Push();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000036
dan sinclair41e4d9a2022-05-01 14:40:55 +000037 s.Set(a, 15u);
38 EXPECT_EQ(s.Get(a), 15u);
39 EXPECT_EQ(s.Get(b), 10u);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000040
dan sinclair41e4d9a2022-05-01 14:40:55 +000041 s.Pop();
42 EXPECT_EQ(s.Get(a), 5u);
43 EXPECT_EQ(s.Get(b), 10u);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000044}
45
46TEST_F(ScopeStackTest, Get_MissingSymbol) {
James Price2cf32b12022-05-11 13:50:33 +000047 ScopeStack<Symbol, uint32_t> s;
dan sinclair41e4d9a2022-05-01 14:40:55 +000048 Symbol sym(1, ID());
49 EXPECT_EQ(s.Get(sym), 0u);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000050}
51
52TEST_F(ScopeStackTest, Set) {
James Price2cf32b12022-05-11 13:50:33 +000053 ScopeStack<Symbol, uint32_t> s;
dan sinclair41e4d9a2022-05-01 14:40:55 +000054 Symbol a(1, ID());
55 Symbol b(2, ID());
Ryan Harrisondbc13af2022-02-21 15:19:07 +000056
dan sinclair41e4d9a2022-05-01 14:40:55 +000057 EXPECT_EQ(s.Set(a, 5u), 0u);
58 EXPECT_EQ(s.Get(a), 5u);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000059
dan sinclair41e4d9a2022-05-01 14:40:55 +000060 EXPECT_EQ(s.Set(b, 10u), 0u);
61 EXPECT_EQ(s.Get(b), 10u);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000062
dan sinclair41e4d9a2022-05-01 14:40:55 +000063 EXPECT_EQ(s.Set(a, 20u), 5u);
64 EXPECT_EQ(s.Get(a), 20u);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000065
dan sinclair41e4d9a2022-05-01 14:40:55 +000066 EXPECT_EQ(s.Set(b, 25u), 10u);
67 EXPECT_EQ(s.Get(b), 25u);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000068}
69
James Pricebe656f72022-05-11 22:05:15 +000070TEST_F(ScopeStackTest, Clear) {
71 ScopeStack<Symbol, uint32_t> s;
72 Symbol a(1, ID());
73 Symbol b(2, ID());
74
75 EXPECT_EQ(s.Set(a, 5u), 0u);
76 EXPECT_EQ(s.Get(a), 5u);
77
78 s.Push();
79
80 EXPECT_EQ(s.Set(b, 10u), 0u);
81 EXPECT_EQ(s.Get(b), 10u);
82
83 s.Push();
84
85 s.Clear();
86 EXPECT_EQ(s.Get(a), 0u);
87 EXPECT_EQ(s.Get(b), 0u);
88}
89
Ryan Harrisondbc13af2022-02-21 15:19:07 +000090} // namespace
91} // namespace tint