2020-04-03 02:35:23 +00:00
|
|
|
// Copyright 2020 The Tint Authors. //
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "src/scope_stack.h"
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
2021-01-26 16:57:10 +00:00
|
|
|
#include "src/program_builder.h"
|
2020-04-03 02:35:23 +00:00
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
namespace {
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
class ScopeStackTest : public ProgramBuilder, public testing::Test {};
|
2020-04-03 02:35:23 +00:00
|
|
|
|
|
|
|
TEST_F(ScopeStackTest, Global) {
|
|
|
|
ScopeStack<uint32_t> s;
|
2021-04-15 18:20:03 +00:00
|
|
|
Symbol sym(1, ID());
|
2021-01-11 15:10:19 +00:00
|
|
|
s.set_global(sym, 5);
|
2020-04-03 02:35:23 +00:00
|
|
|
|
|
|
|
uint32_t val = 0;
|
2021-01-11 15:10:19 +00:00
|
|
|
EXPECT_TRUE(s.get(sym, &val));
|
2020-04-17 13:18:20 +00:00
|
|
|
EXPECT_EQ(val, 5u);
|
2020-04-03 02:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ScopeStackTest, Global_SetWithPointer) {
|
2021-02-16 23:57:01 +00:00
|
|
|
auto* v = Var("my_var", ty.f32(), ast::StorageClass::kNone);
|
2020-04-03 02:35:23 +00:00
|
|
|
ScopeStack<ast::Variable*> s;
|
2021-01-11 15:10:19 +00:00
|
|
|
s.set_global(v->symbol(), v);
|
2020-04-03 02:35:23 +00:00
|
|
|
|
2020-04-06 17:16:56 +00:00
|
|
|
ast::Variable* v2 = nullptr;
|
2021-01-11 15:10:19 +00:00
|
|
|
EXPECT_TRUE(s.get(v->symbol(), &v2));
|
|
|
|
EXPECT_EQ(v2->symbol(), v->symbol());
|
2020-04-03 02:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ScopeStackTest, Global_CanNotPop) {
|
|
|
|
ScopeStack<uint32_t> s;
|
2021-04-15 18:20:03 +00:00
|
|
|
Symbol sym(1, ID());
|
2021-01-11 15:10:19 +00:00
|
|
|
s.set_global(sym, 5);
|
2020-04-03 02:35:23 +00:00
|
|
|
s.pop_scope();
|
|
|
|
|
|
|
|
uint32_t val = 0;
|
2021-01-11 15:10:19 +00:00
|
|
|
EXPECT_TRUE(s.get(sym, &val));
|
2020-04-17 13:18:20 +00:00
|
|
|
EXPECT_EQ(val, 5u);
|
2020-04-03 02:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ScopeStackTest, Scope) {
|
|
|
|
ScopeStack<uint32_t> s;
|
2021-04-15 18:20:03 +00:00
|
|
|
Symbol sym(1, ID());
|
2020-04-03 02:35:23 +00:00
|
|
|
s.push_scope();
|
2021-01-11 15:10:19 +00:00
|
|
|
s.set(sym, 5);
|
2020-04-03 02:35:23 +00:00
|
|
|
|
|
|
|
uint32_t val = 0;
|
2021-01-11 15:10:19 +00:00
|
|
|
EXPECT_TRUE(s.get(sym, &val));
|
2020-04-17 13:18:20 +00:00
|
|
|
EXPECT_EQ(val, 5u);
|
2020-04-03 02:35:23 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 15:10:19 +00:00
|
|
|
TEST_F(ScopeStackTest, Get_MissingSymbol) {
|
2020-04-03 02:35:23 +00:00
|
|
|
ScopeStack<uint32_t> s;
|
2021-04-15 18:20:03 +00:00
|
|
|
Symbol sym(1, ID());
|
2020-04-03 02:35:23 +00:00
|
|
|
uint32_t ret = 0;
|
2021-01-11 15:10:19 +00:00
|
|
|
EXPECT_FALSE(s.get(sym, &ret));
|
2020-04-17 13:18:20 +00:00
|
|
|
EXPECT_EQ(ret, 0u);
|
2020-04-03 02:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ScopeStackTest, Has) {
|
|
|
|
ScopeStack<uint32_t> s;
|
2021-04-15 18:20:03 +00:00
|
|
|
Symbol sym(1, ID());
|
|
|
|
Symbol sym2(2, ID());
|
2021-01-11 15:10:19 +00:00
|
|
|
s.set_global(sym2, 3);
|
2020-04-03 02:35:23 +00:00
|
|
|
s.push_scope();
|
2021-01-11 15:10:19 +00:00
|
|
|
s.set(sym, 5);
|
2020-04-03 02:35:23 +00:00
|
|
|
|
2021-01-11 15:10:19 +00:00
|
|
|
EXPECT_TRUE(s.has(sym));
|
|
|
|
EXPECT_TRUE(s.has(sym2));
|
2020-04-03 02:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ScopeStackTest, ReturnsScopeBeforeGlobalFirst) {
|
|
|
|
ScopeStack<uint32_t> s;
|
2021-04-15 18:20:03 +00:00
|
|
|
Symbol sym(1, ID());
|
2021-01-11 15:10:19 +00:00
|
|
|
s.set_global(sym, 3);
|
2020-04-03 02:35:23 +00:00
|
|
|
s.push_scope();
|
2021-01-11 15:10:19 +00:00
|
|
|
s.set(sym, 5);
|
2020-04-03 02:35:23 +00:00
|
|
|
|
|
|
|
uint32_t ret;
|
2021-01-11 15:10:19 +00:00
|
|
|
EXPECT_TRUE(s.get(sym, &ret));
|
2020-04-17 13:18:20 +00:00
|
|
|
EXPECT_EQ(ret, 5u);
|
2020-04-03 02:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace tint
|