1#!/usr/bin/env python
2from __future__ import division, print_function
3
4import sys
5import unittest
6
7try:
8    import efuse_table_gen
9except ImportError:
10    sys.path.append('..')
11    import efuse_table_gen
12
13
14'''
15To run the test on local PC:
16cd ~/esp/esp-idf/components/efuse/test_efuse_host/
17 ./efuse_tests.py
18'''
19
20
21class Py23TestCase(unittest.TestCase):
22
23    def __init__(self, *args, **kwargs):
24        super(Py23TestCase, self).__init__(*args, **kwargs)
25        try:
26            self.assertRaisesRegex
27        except AttributeError:
28            # assertRaisesRegexp is deprecated in Python3 but assertRaisesRegex doesn't exist in Python2
29            # This fix is used in order to avoid using the alias from the six library
30            self.assertRaisesRegex = self.assertRaisesRegexp
31
32
33class CSVParserTests(Py23TestCase):
34
35    def test_general(self):
36        csv = """
37# field_name,  efuse_block(EFUSE_BLK0..EFUSE_BLK3),  bit_start(0..255),    bit_count,        comment
38name1,                   EFUSE_BLK3,                       0,                     5,              Use for test name 1
39name2,                   EFUSE_BLK3,                       5,                     4,              Use for test name 2
40"""
41        t = efuse_table_gen.FuseTable.from_csv(csv)
42        t.verify()
43
44        self.assertEqual(t[0].field_name, 'name1')
45        self.assertEqual(t[0].efuse_block, 'EFUSE_BLK3')
46        self.assertEqual(t[0].bit_start, 0)
47        self.assertEqual(t[0].bit_count, 5)
48        self.assertEqual(t[0].comment, 'Use for test name 1')
49
50        self.assertEqual(t[1].field_name, 'name2')
51        self.assertEqual(t[1].efuse_block, 'EFUSE_BLK3')
52        self.assertEqual(t[1].bit_start, 5)
53        self.assertEqual(t[1].bit_count, 4)
54        self.assertEqual(t[1].comment, 'Use for test name 2')
55
56    def test_seq_bit_start1_fill(self):
57        csv = """
58# field_name,  efuse_block(EFUSE_BLK0..EFUSE_BLK3),  bit_start(0..255),    bit_count,        comment
59name1,                   EFUSE_BLK3,                       ,                     5,
60name2,                   EFUSE_BLK3,                       ,                     4,
61"""
62        t = efuse_table_gen.FuseTable.from_csv(csv)
63        t.verify()
64
65        self.assertEqual(t[0].field_name, 'name1')
66        self.assertEqual(t[0].bit_start, 0)
67        self.assertEqual(t[0].bit_count, 5)
68
69        self.assertEqual(t[1].field_name, 'name2')
70        self.assertEqual(t[1].bit_start, 5)
71        self.assertEqual(t[1].bit_count, 4)
72
73    def test_seq_bit_start2_fill(self):
74        csv = """
75# field_name,  efuse_block(EFUSE_BLK0..EFUSE_BLK3),  bit_start(0..255),    bit_count,        comment
76name1,                   EFUSE_BLK3,                       ,                     5,
77name2,                   EFUSE_BLK2,                       ,                     4,
78"""
79        t = efuse_table_gen.FuseTable.from_csv(csv)
80        t.verify()
81
82        self.assertEqual(t[0].field_name, 'name1')
83        self.assertEqual(t[0].bit_start, 0)
84        self.assertEqual(t[0].bit_count, 5)
85
86        self.assertEqual(t[1].field_name, 'name2')
87        self.assertEqual(t[1].bit_start, 0)
88        self.assertEqual(t[1].bit_count, 4)
89
90    def test_seq_bit_start3_fill(self):
91        csv = """
92# field_name,  efuse_block(EFUSE_BLK0..EFUSE_BLK3),  bit_start(0..255),    bit_count,        comment
93name1,                   EFUSE_BLK3,                       ,                     5,
94name2,                   EFUSE_BLK2,                       ,                     4,
95
96name3,                   EFUSE_BLK2,                       5,                    4,
97"""
98        t = efuse_table_gen.FuseTable.from_csv(csv)
99        t.verify()
100
101        self.assertEqual(t[0].field_name, 'name1')
102        self.assertEqual(t[0].bit_start, 0)
103        self.assertEqual(t[0].bit_count, 5)
104
105        self.assertEqual(t[1].field_name, 'name2')
106        self.assertEqual(t[1].bit_start, 0)
107        self.assertEqual(t[1].bit_count, 4)
108
109        self.assertEqual(t[2].field_name, 'name3')
110        self.assertEqual(t[2].bit_start, 5)
111        self.assertEqual(t[2].bit_count, 4)
112
113    def test_seq_bit_start4_fill(self):
114        csv = """
115# field_name,  efuse_block(EFUSE_BLK0..EFUSE_BLK3),  bit_start(0..255),    bit_count,        comment
116name1,                   EFUSE_BLK3,                       ,                     5,
117name2,                   EFUSE_BLK2,                       ,                     4,
118,                        EFUSE_BLK2,                       ,                     4,
119name1,                   EFUSE_BLK3,                       ,                     5,
120"""
121        with self.assertRaisesRegex(efuse_table_gen.InputError, 'Field names must be unique'):
122            efuse_table_gen.FuseTable.from_csv(csv)
123
124    def test_seq_bit_start5_fill(self):
125        csv = """
126# field_name,  efuse_block(EFUSE_BLK0..EFUSE_BLK3),  bit_start(0..255),    bit_count,        comment
127name1,                   EFUSE_BLK3,                       ,                     5,
128name2,                   EFUSE_BLK2,                       ,                     4,
129,                        EFUSE_BLK2,                       ,                     4,
130name3,                   EFUSE_BLK3,                       5,                    5,
131"""
132        t = efuse_table_gen.FuseTable.from_csv(csv)
133        t.verify()
134
135        self.assertEqual(t[0].field_name, 'name1')
136        self.assertEqual(t[0].bit_start, 0)
137        self.assertEqual(t[0].bit_count, 5)
138
139        self.assertEqual(t[1].field_name, 'name2')
140        self.assertEqual(t[1].bit_start, 0)
141        self.assertEqual(t[1].bit_count, 4)
142
143        self.assertEqual(t[2].field_name, 'name2')
144        self.assertEqual(t[2].bit_start, 4)
145        self.assertEqual(t[2].bit_count, 4)
146
147        self.assertEqual(t[3].field_name, 'name3')
148        self.assertEqual(t[3].bit_start, 5)
149        self.assertEqual(t[3].bit_count, 5)
150
151    def test_overlapping_bit_start_fail(self):
152        csv = """
153# field_name,  efuse_block(EFUSE_BLK0..EFUSE_BLK3),  bit_start(0..255),    bit_count,        comment
154name1,                   EFUSE_BLK3,                     1,                     5,              Use for test name 1
155name2,                   EFUSE_BLK3,                     5,                     4,              Use for test name 2
156            """
157        t = efuse_table_gen.FuseTable.from_csv(csv)
158        with self.assertRaisesRegex(efuse_table_gen.InputError, 'overlap'):
159            t.verify()
160
161    def test_empty_field_name_fail(self):
162        csv = """
163# field_name,  efuse_block(EFUSE_BLK0..EFUSE_BLK3),  bit_start(0..255),    bit_count,        comment
164,                        EFUSE_BLK3,                       ,                     5,
165name2,                   EFUSE_BLK2,                       ,                     4,
166"""
167        with self.assertRaisesRegex(efuse_table_gen.InputError, 'missing field name'):
168            efuse_table_gen.FuseTable.from_csv(csv)
169
170    def test_unique_field_name_fail(self):
171        csv = """
172# field_name,  efuse_block(EFUSE_BLK0..EFUSE_BLK3),  bit_start(0..255),    bit_count,        comment
173name1,                   EFUSE_BLK3,                     0,                     5,              Use for test name 1
174name1,                   EFUSE_BLK3,                     5,                     4,              Use for test name 2
175            """
176        with self.assertRaisesRegex(efuse_table_gen.InputError, 'Field names must be unique'):
177            efuse_table_gen.FuseTable.from_csv(csv)
178
179    def test_bit_count_empty_fail(self):
180        csv = """
181# field_name,  efuse_block(EFUSE_BLK0..EFUSE_BLK3),  bit_start(0..255),    bit_count,        comment
182name1,                   EFUSE_BLK3,                     0,                     ,              Use for test name 1
183name2,                   EFUSE_BLK3,                     5,                     4,              Use for test name 2
184            """
185        with self.assertRaisesRegex(efuse_table_gen.InputError, 'empty'):
186            efuse_table_gen.FuseTable.from_csv(csv)
187
188    def test_bit_start_num_fail(self):
189        csv = """
190# field_name,  efuse_block(EFUSE_BLK0..EFUSE_BLK3),  bit_start(0..255),    bit_count,        comment
191name1,                   EFUSE_BLK3,                     k,                     5,              Use for test name 1
192name2,                   EFUSE_BLK3,                     5,                     4,              Use for test name 2
193            """
194        with self.assertRaisesRegex(efuse_table_gen.InputError, 'Invalid field value'):
195            efuse_table_gen.FuseTable.from_csv(csv)
196
197    def test_join_entry(self):
198        csv = """
199# field_name,  efuse_block(EFUSE_BLK0..EFUSE_BLK3),  bit_start(0..255),    bit_count,        comment
200name1,                   EFUSE_BLK2,                     0,                     6,              Use for test name 1
201name2,                   EFUSE_BLK2,                     6,                     5,              Use for test name 2
202name3,                   EFUSE_BLK3,                     20,                    5,              Use for test name 3
203,                        EFUSE_BLK3,                     30,                    5,              Use for test name 3
204name4,                   EFUSE_BLK2,                     30,                    5,              Use for test name 4
205            """
206        t = efuse_table_gen.FuseTable.from_csv(csv)
207        t.verify()
208
209        self.assertEqual(t[0].field_name, 'name1')
210        self.assertEqual(t[0].efuse_block, 'EFUSE_BLK2')
211        self.assertEqual(t[0].bit_start, 0)
212        self.assertEqual(t[0].bit_count, 6)
213
214        self.assertEqual(t[1].field_name, 'name2')
215        self.assertEqual(t[1].efuse_block, 'EFUSE_BLK2')
216        self.assertEqual(t[1].bit_start, 6)
217        self.assertEqual(t[1].bit_count, 5)
218
219        self.assertEqual(t[2].field_name, 'name3')
220        self.assertEqual(t[2].efuse_block, 'EFUSE_BLK3')
221        self.assertEqual(t[2].bit_start, 20)
222        self.assertEqual(t[2].bit_count, 5)
223
224        self.assertEqual(t[3].field_name, 'name3')
225        self.assertEqual(t[3].efuse_block, 'EFUSE_BLK3')
226        self.assertEqual(t[3].bit_start, 30)
227        self.assertEqual(t[3].bit_count, 5)
228
229        self.assertEqual(t[4].field_name, 'name4')
230        self.assertEqual(t[4].efuse_block, 'EFUSE_BLK2')
231        self.assertEqual(t[4].bit_start, 30)
232        self.assertEqual(t[4].bit_count, 5)
233
234    def test_block_fail(self):
235        csv = """
236# field_name,  efuse_block(EFUSE_BLK0..EFUSE_BLK3),  bit_start(0..255),    bit_count,        comment
237name1,                   EFUSE_BLK5,                     0,                     5,              Use for test name 1
238name2,                   EFUSE_BLK3,                     5,                     4,              Use for test name 2
239            """
240        with self.assertRaisesRegex(efuse_table_gen.InputError, "'efuse_block' should be one of EFUSE_BLK0..EFUSE_BLK3"):
241            efuse_table_gen.FuseTable.from_csv(csv)
242
243    def test_field_size_is_ok(self):
244        csv = """
245# field_name,  efuse_block(EFUSE_BLK0..EFUSE_BLK3),  bit_start(0..255),    bit_count,        comment
246name1,                   EFUSE_BLK0,                     0,                     224,            Use for test name 1
247name2,                   EFUSE_BLK1,                     0,                     256,            Use for test name 2
248            """
249        efuse_table_gen.max_blk_len = 256
250        t = efuse_table_gen.FuseTable.from_csv(csv)
251        t.verify()
252
253    def test_field_blk3_size_is_more(self):
254        csv = """
255# field_name,  efuse_block(EFUSE_BLK0..EFUSE_BLK3),  bit_start(0..255),    bit_count,        comment
256name1,                   EFUSE_BLK3,                     190,                   1,            Use for test name 1
257name2,                   EFUSE_BLK3,                     191,                   5,            Use for test name 2
258            """
259        efuse_table_gen.max_blk_len = 192
260        t = efuse_table_gen.FuseTable.from_csv(csv)
261        with self.assertRaisesRegex(efuse_table_gen.InputError, 'The field is outside the boundaries'):
262            t.verify()
263
264    def test_field_blk1_size_is_more(self):
265        csv = """
266# field_name,  efuse_block(EFUSE_BLK0..EFUSE_BLK3),  bit_start(0..255),    bit_count,        comment
267name1,                   EFUSE_BLK0,                     0,                     224,            Use for test name 1
268name2,                   EFUSE_BLK1,                     1,                     256,            Use for test name 2
269            """
270        t = efuse_table_gen.FuseTable.from_csv(csv)
271        with self.assertRaisesRegex(efuse_table_gen.InputError, 'The field is outside the boundaries'):
272            t.verify()
273
274
275class VerificationTests(Py23TestCase):
276
277    def test_general(self):
278        csv = """
279# field_name,  efuse_block(EFUSE_BLK0..EFUSE_BLK3),  bit_start(0..255),    bit_count,        comment
280name1,                   EFUSE_BLK3,                     0,                     5,              Use for test name 1
281name2,                   EFUSE_BLK3,                     5,                     4,              Use for test name 2
282name1_1,                 EFUSE_BLK2,                     0,                     5,              Use for test name 1_1
283name2_1,                 EFUSE_BLK2,                     5,                     4,              Use for test name 2_1
284            """
285        t = efuse_table_gen.FuseTable.from_csv(csv)
286        t.verify()
287
288        self.assertEqual(t[0].field_name, 'name1')
289        self.assertEqual(t[0].efuse_block, 'EFUSE_BLK3')
290        self.assertEqual(t[0].bit_start, 0)
291        self.assertEqual(t[0].bit_count, 5)
292
293        self.assertEqual(t[1].field_name, 'name2')
294        self.assertEqual(t[1].efuse_block, 'EFUSE_BLK3')
295        self.assertEqual(t[1].bit_start, 5)
296        self.assertEqual(t[1].bit_count, 4)
297
298        self.assertEqual(t[2].field_name, 'name1_1')
299        self.assertEqual(t[2].efuse_block, 'EFUSE_BLK2')
300        self.assertEqual(t[2].bit_start, 0)
301        self.assertEqual(t[2].bit_count, 5)
302
303        self.assertEqual(t[3].field_name, 'name2_1')
304        self.assertEqual(t[3].efuse_block, 'EFUSE_BLK2')
305        self.assertEqual(t[3].bit_start, 5)
306        self.assertEqual(t[3].bit_count, 4)
307
308    def test_custom_use_only_BLK3(self):
309        csv = """
310# field_name,  efuse_block(EFUSE_BLK0..EFUSE_BLK3),  bit_start(0..255),    bit_count,        comment
311name1,                   EFUSE_BLK3,                     0,                     5,              Use for test name 1
312name2,                   EFUSE_BLK2,                     5,                     4,              Use for test name 2
313            """
314        t = efuse_table_gen.FuseTable.from_csv(csv)
315        with self.assertRaisesRegex(efuse_table_gen.ValidationError, 'custom_table should use only EFUSE_BLK3'):
316            t.verify('custom_table')
317
318    def test_common_and_custom_table_use_the_same_bits(self):
319        csv_common = """
320# field_name,  efuse_block(EFUSE_BLK0..EFUSE_BLK3),  bit_start(0..255),    bit_count,        comment
321name1,                   EFUSE_BLK3,                     0,                     5,              Use for test name 1
322name2,                   EFUSE_BLK2,                     5,                     4,              Use for test name 2
323                     """
324        common_table = efuse_table_gen.FuseTable.from_csv(csv_common)
325        common_table.verify('common_table')
326        two_tables = common_table
327
328        csv_custom = """
329# field_name,  efuse_block(EFUSE_BLK0..EFUSE_BLK3),  bit_start(0..255),    bit_count,        comment
330name3,                   EFUSE_BLK3,                     20,                    5,              Use for test name 1
331name4,                   EFUSE_BLK3,                      4,                    1,              Use for test name 2
332            """
333        custom_table = efuse_table_gen.FuseTable.from_csv(csv_custom)
334        custom_table.verify('custom_table')
335
336        two_tables += custom_table
337        with self.assertRaisesRegex(efuse_table_gen.InputError, 'overlaps'):
338            two_tables.verify()
339
340
341if __name__ == '__main__':
342    unittest.main()
343