• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/program_builder.h"
18 
19 namespace tint {
20 namespace {
21 
22 class ScopeStackTest : public ProgramBuilder, public testing::Test {};
23 
TEST_F(ScopeStackTest,Get)24 TEST_F(ScopeStackTest, Get) {
25   ScopeStack<uint32_t> s;
26   Symbol a(1, ID());
27   Symbol b(3, ID());
28   s.Push();
29   s.Set(a, 5u);
30   s.Set(b, 10u);
31 
32   EXPECT_EQ(s.Get(a), 5u);
33   EXPECT_EQ(s.Get(b), 10u);
34 
35   s.Push();
36 
37   s.Set(a, 15u);
38   EXPECT_EQ(s.Get(a), 15u);
39   EXPECT_EQ(s.Get(b), 10u);
40 
41   s.Pop();
42   EXPECT_EQ(s.Get(a), 5u);
43   EXPECT_EQ(s.Get(b), 10u);
44 }
45 
TEST_F(ScopeStackTest,Get_MissingSymbol)46 TEST_F(ScopeStackTest, Get_MissingSymbol) {
47   ScopeStack<uint32_t> s;
48   Symbol sym(1, ID());
49   EXPECT_EQ(s.Get(sym), 0u);
50 }
51 
TEST_F(ScopeStackTest,Set)52 TEST_F(ScopeStackTest, Set) {
53   ScopeStack<uint32_t> s;
54   Symbol a(1, ID());
55   Symbol b(2, ID());
56 
57   EXPECT_EQ(s.Set(a, 5u), 0u);
58   EXPECT_EQ(s.Get(a), 5u);
59 
60   EXPECT_EQ(s.Set(b, 10u), 0u);
61   EXPECT_EQ(s.Get(b), 10u);
62 
63   EXPECT_EQ(s.Set(a, 20u), 5u);
64   EXPECT_EQ(s.Get(a), 20u);
65 
66   EXPECT_EQ(s.Set(b, 25u), 10u);
67   EXPECT_EQ(s.Get(b), 25u);
68 }
69 
70 }  // namespace
71 }  // namespace tint
72