test_protobuf_descs.py 2.1 KB
Newer Older
Y
Update  
Yu Yang 已提交
1 2 3 4
import unittest
import paddle.v2.framework.core as core


Y
Yu Yang 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
class TestOpDesc(unittest.TestCase):
    def test_op_desc(self):
        prog = core.ProgramDesc.__create_program_desc__()
        self.assertIsNotNone(prog)
        block = prog.block(0)
        self.assertIsNotNone(block)
        op = block.append_op()
        self.assertIsNotNone(op)
        op.set_type("test")
        self.assertEqual("test", op.type())
        op.set_input("X", ["a", "b", "c"])
        self.assertEqual(["a", "b", "c"], op.input("X"))
        self.assertEqual(["X"], op.input_names())

        op.set_output("Out", ["z"])
        self.assertEqual(['z'], op.output("Out"))
        self.assertEqual(["Out"], op.output_names())


Y
Update  
Yu Yang 已提交
24 25 26 27 28 29 30
class TestProgramDesc(unittest.TestCase):
    def test_instance(self):
        program_desc = core.ProgramDesc.instance()
        self.assertIsNotNone(program_desc)
        del program_desc
        program_desc = core.ProgramDesc.instance()
        self.assertIsNotNone(program_desc)
Y
Yu Yang 已提交
31
        self.assertIsNotNone(program_desc.block(0))
Y
Update  
Yu Yang 已提交
32 33
        del program_desc

Y
Yu Yang 已提交
34 35 36
    def test_append_block(self):
        prog_desc = core.ProgramDesc.__create_program_desc__()
        self.assertIsNotNone(prog_desc)
Y
Yu Yang 已提交
37 38 39
        block_root = prog_desc.block(0)
        self.assertIsNotNone(block_root)
        self.assertEqual(block_root.id, 0)
F
fengjiayi 已提交
40
        block1 = prog_desc.append_block(block_root)
Y
Yu Yang 已提交
41
        block2 = prog_desc.append_block(block1)
Y
Yu Yang 已提交
42 43 44
        self.assertIsNotNone(block1)
        self.assertEqual(block1.id, block2.parent)
        self.assertEqual(block_root.id, block1.parent)
F
fengjiayi 已提交
45
        block3 = prog_desc.append_block(block_root)
Y
Yu Yang 已提交
46 47 48
        self.assertEqual(block3.parent, block_root.id)
        self.assertEqual(prog_desc.block(1).id, 1)
        self.assertEqual(4, prog_desc.num_blocks())
F
fengjiayi 已提交
49 50 51 52 53 54


class TestVarDesc(unittest.TestCase):
    def test_shape(self):
        program_desc = core.ProgramDesc.instance()
        block = program_desc.root_block()
F
fengjiayi 已提交
55
        var = block.new_var('my_var')
F
fengjiayi 已提交
56 57 58 59
        src_shape = [3, 2, 10, 8]
        var.set_shape(src_shape)
        res_shape = var.shape()
        self.assertEqual(src_shape, res_shape)
Y
Yu Yang 已提交
60

Y
Update  
Yu Yang 已提交
61 62 63

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