scope_test.cc 1.1 KB
Newer Older
Q
qiaolongfei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include "paddle/framework/scope.h"
#include "gtest/gtest.h"

TEST(Scope, Create) {
  using paddle::framework::Scope;
  using paddle::Error;
  using paddle::framework::Variable;
  using paddle::framework::AlreadyCreated;

  Scope* scope = new Scope();

  Error err = scope->CreateVariable("");
  EXPECT_FALSE(err.isOK());

  Variable* var1 = scope->GetVariable("a");
  EXPECT_EQ(var1, nullptr);

  Error err1 = scope->CreateVariable("a");
  EXPECT_TRUE(err1.isOK());

  Error err2 = scope->CreateVariable("a");
  EXPECT_EQ(err2, AlreadyCreated);

  Variable* var2 = scope->GetVariable("a");
  EXPECT_NE(var2, nullptr);

  Variable* var3 = scope->GetOrCreateVariable("b");
  EXPECT_NE(var3, nullptr);
}

TEST(Scope, Parent) {
  using paddle::framework::Scope;
  using paddle::framework::Variable;
  using paddle::Error;

  const auto parent_scope_ptr = std::shared_ptr<Scope>(new Scope());
  Scope* scope = new Scope(parent_scope_ptr);

  Error err = parent_scope_ptr->CreateVariable("a");
  EXPECT_TRUE(err.isOK());

  Variable* var1 = scope->GetVarLocally("a");
  EXPECT_EQ(var1, nullptr);

  Variable* var2 = scope->GetVariable("a");
  EXPECT_NE(var2, nullptr);
}