test_scope.py 1.7 KB
Newer Older
D
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#  Copyright (c) 2018 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
Qiao Longfei 已提交
14
import paddle.v2.fluid.core
15 16 17 18 19
import unittest


class TestScope(unittest.TestCase):
    def test_create_destroy(self):
Q
Qiao Longfei 已提交
20
        paddle_c = paddle.v2.fluid.core
Y
Yu Yang 已提交
21
        scope = paddle_c.Scope()
22
        self.assertIsNotNone(scope)
Y
Yu Yang 已提交
23
        scope_with_parent = scope.new_scope()
24 25 26
        self.assertIsNotNone(scope_with_parent)

    def test_none_variable(self):
Q
Qiao Longfei 已提交
27
        paddle_c = paddle.v2.fluid.core
Y
Yu Yang 已提交
28 29
        scope = paddle_c.Scope()
        self.assertIsNone(scope.find_var("test"))
30 31

    def test_create_var_get_var(self):
Q
Qiao Longfei 已提交
32
        paddle_c = paddle.v2.fluid.core
Y
Yu Yang 已提交
33
        scope = paddle_c.Scope()
D
dongzhihong 已提交
34
        var_a = scope.var("var_a")
35
        self.assertIsNotNone(var_a)
Y
Yu Yang 已提交
36 37 38
        self.assertIsNotNone(scope.find_var('var_a'))
        scope2 = scope.new_scope()
        self.assertIsNotNone(scope2.find_var('var_a'))
39 40

    def test_var_get_int(self):
Q
Qiao Longfei 已提交
41
        paddle_c = paddle.v2.fluid.core
Y
Yu Yang 已提交
42
        scope = paddle_c.Scope()
D
dongzhihong 已提交
43
        var = scope.var("test_int")
44 45 46 47 48 49 50
        var.set_int(10)
        self.assertTrue(var.is_int())
        self.assertEqual(10, var.get_int())


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