test_scope.py 1.7 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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.

15 16
from __future__ import print_function

17
import paddle.fluid.core
18 19 20 21 22
import unittest


class TestScope(unittest.TestCase):
    def test_create_destroy(self):
23
        paddle_c = paddle.fluid.core
Y
Yu Yang 已提交
24
        scope = paddle_c.Scope()
25
        self.assertIsNotNone(scope)
Y
Yu Yang 已提交
26
        scope_with_parent = scope.new_scope()
27 28 29
        self.assertIsNotNone(scope_with_parent)

    def test_none_variable(self):
30
        paddle_c = paddle.fluid.core
Y
Yu Yang 已提交
31 32
        scope = paddle_c.Scope()
        self.assertIsNone(scope.find_var("test"))
33 34

    def test_create_var_get_var(self):
35
        paddle_c = paddle.fluid.core
Y
Yu Yang 已提交
36
        scope = paddle_c.Scope()
D
dongzhihong 已提交
37
        var_a = scope.var("var_a")
38
        self.assertIsNotNone(var_a)
Y
Yu Yang 已提交
39 40 41
        self.assertIsNotNone(scope.find_var('var_a'))
        scope2 = scope.new_scope()
        self.assertIsNotNone(scope2.find_var('var_a'))
42 43

    def test_var_get_int(self):
44
        paddle_c = paddle.fluid.core
Y
Yu Yang 已提交
45
        scope = paddle_c.Scope()
D
dongzhihong 已提交
46
        var = scope.var("test_int")
47 48 49 50 51 52 53
        var.set_int(10)
        self.assertTrue(var.is_int())
        self.assertEqual(10, var.get_int())


if __name__ == '__main__':
    unittest.main()