Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / back / ie_ir_ver_2 / emitter_test.py
1 """
2  Copyright (c) 2018-2019 Intel Corporation
3
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 """
16
17 import unittest
18 from unittest.mock import MagicMock
19 from xml.etree.ElementTree import Element, tostring
20
21 import numpy as np
22
23 from mo.back.ie_ir_ver_2.emitter import soft_get, xml_shape
24 from mo.utils.error import Error
25
26 expected_result = b'<net><dim>2</dim><dim>10</dim><dim>50</dim><dim>50</dim></net>'
27
28
29 class TestEmitter(unittest.TestCase):
30     def test_xml_shape(self):
31         net = Element('net')
32         xml_shape(np.array([2, 10, 50, 50], dtype=np.int64), net)
33         self.assertEqual(tostring(net), expected_result)
34
35     def test_xml_shape_float_values(self):
36         net = Element('net')
37         xml_shape(np.array([2.0, 10.0, 50.0, 50.0], dtype=np.float32), net)
38         self.assertEqual(tostring(net), expected_result)
39
40     def test_xml_shape_non_integer_values(self):
41         net = Element('net')
42         with self.assertRaises(Error):
43             xml_shape(np.array([2.0, 10.0, 50.0, 50.5], dtype=np.float32), net)
44
45     def test_xml_shape_negative_values(self):
46         net = Element('net')
47         with self.assertRaises(Error):
48             xml_shape(np.array([2, 10, 50, -50], dtype=np.int64), net)
49
50     def test_xml_shape_zero_values(self):
51         net = Element('net')
52         with self.assertRaises(Error):
53             xml_shape(np.array([2, 0, 50, 50], dtype=np.int64), net)
54
55
56 class TestSoftGet(unittest.TestCase):
57
58     def test_node(self):
59         node = MagicMock()
60         node.soft_get = lambda attr: attr
61         self.assertEqual(soft_get(node, 'string'), 'string')
62
63     def test_not_callable(self):
64         node = MagicMock()
65         node.soft_get = 'foo'
66         self.assertEqual(soft_get(node, 'string'), '<SUB-ELEMENT>')
67
68     def test_not_node_1(self):
69         node = {'soft_get': lambda attr: attr}
70         self.assertEqual(soft_get(node, 'string'), '<SUB-ELEMENT>')
71
72     def test_not_node_2(self):
73         node = 'something-else'
74         self.assertEqual(soft_get(node, 'string'), '<SUB-ELEMENT>')