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
import paddle.fluid.core
16 17 18 19 20
import unittest


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

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

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

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


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