Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / front / kaldi / loader / utils_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 io
18 import struct
19 import unittest
20
21 from mo.front.kaldi.loader.utils import end_of_nnet_tag, end_of_component_tag, get_bool, get_uint16, get_uint32, \
22     get_uint64, read_binary_bool_token, read_binary_integer32_token, read_binary_integer64_token, find_next_tag, \
23     find_next_component, find_end_of_component, get_parameters
24 from mo.utils.error import Error
25
26
27 class TestKaldiUtilsLoading(unittest.TestCase):
28     bool_fmt = '?'
29     uint16_fmt = 'H'
30     uint32_fmt = 'I'
31     uint64_fmt = 'q'
32     float32_fmt = 'f'
33
34     @staticmethod
35     def bytesio_from(buffer):
36         return io.BytesIO(buffer)
37
38     @staticmethod
39     def pack_value(value, fmt):
40         return struct.pack(fmt, value)
41
42     def test_check_common_tags(self):
43         self.assertEqual(end_of_nnet_tag, '</Nnet>')
44         self.assertEqual(end_of_component_tag, '<!EndOfComponent>')
45
46     def test_check_results_getting_function(self):
47         self.assertTrue(get_bool(self.pack_value(True, self.bool_fmt)))
48         self.assertFalse(get_bool(self.pack_value(False, self.bool_fmt)))
49         self.assertEqual(get_uint16(self.pack_value(16, self.uint16_fmt)), 16)
50         self.assertEqual(get_uint32(self.pack_value(32, self.uint32_fmt)), 32)
51         self.assertEqual(get_uint64(self.pack_value(64, self.uint64_fmt)), 64)
52
53     def test_read_binary_bool_token(self):
54         true_value = self.bytesio_from(self.pack_value(True, self.bool_fmt))
55         false_value = self.bytesio_from(self.pack_value(False, self.bool_fmt))
56         self.assertTrue(read_binary_bool_token(true_value))
57         self.assertFalse(read_binary_bool_token(false_value))
58
59     def test_read_binary_integer32_token(self):
60         stream = self.bytesio_from(self.pack_value(4, 'B') + self.pack_value(32, self.uint32_fmt))
61         self.assertEqual(read_binary_integer32_token(stream), 32)
62
63     def test_read_binary_integer64_token(self):
64         stream = self.bytesio_from(self.pack_value(8, 'B') + self.pack_value(64, self.uint64_fmt))
65         self.assertEqual(read_binary_integer64_token(stream), 64)
66
67     def test_find_next_tag(self):
68         test_token = b'<TestToken>'
69         self.assertEqual(find_next_tag(self.bytesio_from(test_token)), test_token.decode('ascii'))
70         fake_token = b'<FakeBegin' + test_token
71         self.assertEqual(find_next_tag(self.bytesio_from(fake_token)), test_token.decode('ascii'))
72
73     def test_find_next_tag_raise_error(self):
74         test_token = b'some bytes'
75         self.assertRaises(Error, find_next_tag, self.bytesio_from(test_token))
76
77     def test_find_next_component(self):
78         component = b'<LstmProjectedStreams>'
79         test_file = b'<Nnet>somefakeinfo<another>info' + component + b'<tag><!EndOfComponent></Nnet>'
80         self.assertEqual(find_next_component(self.bytesio_from(test_file)), component.decode('ascii').lower()[1:-1])
81
82     def test_find_next_component_end_of_nnet(self):
83         test_file = b'<Nnet>somefakeinfo<another>info<tag><!EndOfComponent></Nnet>'
84         self.assertEqual(find_next_component(self.bytesio_from(test_file)), end_of_nnet_tag.lower()[1:-1])
85
86     def test_find_end_of_component(self):
87         component = '<AffineComponent>'
88         test_file = b'somefakeinfo<another>info<tag>' + bytes(end_of_component_tag, 'ascii') + b'</Nnet>'
89         end_tag, position = find_end_of_component(self.bytesio_from(test_file), component.lower()[1:-1])
90         self.assertEqual(end_tag, end_of_component_tag)
91         self.assertEqual(position, test_file.decode('ascii').index(end_of_component_tag) + len(end_of_component_tag))
92
93     def test_get_pb(self):
94         component = '<AffineComponent>'
95         test_file = b'somefakeinfo<another>info<tag>' + bytes(end_of_component_tag, 'ascii') + b'</Nnet>'
96         end_tag, end_position = find_end_of_component(self.bytesio_from(test_file), component[1:-1].lower())
97         pb = get_parameters(self.bytesio_from(test_file), 0, end_position)