scope_test.cc 1.4 KB
Newer Older
Q
qiaolongfei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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. */

Q
qiaolongfei 已提交
15 16 17
#include "paddle/framework/scope.h"
#include "gtest/gtest.h"

Y
Yi Wang 已提交
18 19
using paddle::framework::Scope;
using paddle::framework::Variable;
Q
qiaolongfei 已提交
20

Y
Yi Wang 已提交
21 22 23 24
TEST(Scope, VarsShadowing) {
  Scope s;
  Scope& ss1 = s.NewScope();
  Scope& ss2 = s.NewScope();
Q
qiaolongfei 已提交
25

Y
Yi Wang 已提交
26 27
  Variable* v0 = s.NewVar("a");
  Variable* v1 = ss1.NewVar("a");
Q
qiaolongfei 已提交
28

Y
Yi Wang 已提交
29
  EXPECT_NE(v0, v1);
Q
qiaolongfei 已提交
30

Y
Yi Wang 已提交
31 32 33 34
  EXPECT_EQ(v0, s.FindVar("a"));
  EXPECT_EQ(v1, ss1.FindVar("a"));
  EXPECT_EQ(v0, ss2.FindVar("a"));
}
Q
qiaolongfei 已提交
35

Y
Yi Wang 已提交
36 37 38
TEST(Scope, FindVar) {
  Scope s;
  Scope& ss = s.NewScope();
39

Y
Yi Wang 已提交
40 41
  EXPECT_EQ(nullptr, s.FindVar("a"));
  EXPECT_EQ(nullptr, ss.FindVar("a"));
Q
qiaolongfei 已提交
42

Y
Yi Wang 已提交
43
  ss.NewVar("a");
Q
qiaolongfei 已提交
44

Y
Yi Wang 已提交
45 46 47
  EXPECT_EQ(nullptr, s.FindVar("a"));
  EXPECT_NE(nullptr, ss.FindVar("a"));
}
Q
qiaolongfei 已提交
48

Y
Yi Wang 已提交
49 50 51
TEST(Scope, FindScope) {
  Scope s;
  Scope& ss = s.NewScope();
Y
Yi Wang 已提交
52
  Variable* v = s.NewVar("a");
Q
qiaolongfei 已提交
53

Y
Yi Wang 已提交
54 55
  EXPECT_EQ(&s, s.FindScope(v));
  EXPECT_EQ(&s, ss.FindScope(v));
56
}