Rachit Shrivastava | 8e435b4 | 2017-08-08 23:15:26 -0400 | [diff] [blame] | 1 | from unittest import TestCase, main |
| 2 | from common.utils.indexpool import IndexPool |
| 3 | |
| 4 | class TestIndexPool(TestCase): |
| 5 | pool = IndexPool(8, 0) |
| 6 | def test_01_get_next(self): |
| 7 | self.assertEqual(self.pool.indices.bin, '00000000') |
| 8 | for i in range(8): |
| 9 | self.assertEqual(self.pool.get_next(), i) |
| 10 | #to check if there's any bit left after using all 8 bits |
| 11 | self.assertIsNone(self.pool.get_next()) |
| 12 | |
| 13 | def test_02_pre_allocate(self): |
| 14 | _pool2 = IndexPool(8, 0) |
| 15 | self.assertEqual(_pool2.indices.bin, '00000000') |
| 16 | _pool2.pre_allocate((0,1,2,)) |
| 17 | self.assertEqual(_pool2.indices.bin, '11100000') |
| 18 | |
| 19 | def test_03_release(self): |
| 20 | self.pool.release(5) |
| 21 | self.assertEqual(self.pool.indices.bin, '11111011') |
| 22 | self.pool.release(10) |
| 23 | self.assertEqual(self.pool.indices.bin, '11111011') |
| 24 | self.pool.release(0) |
| 25 | self.assertEqual(self.pool.indices.bin, '01111011') |
| 26 | |
| 27 | def test_04_check_offset(self): |
| 28 | _offset = 5 |
| 29 | self.pool = IndexPool(8, _offset) |
| 30 | for i in range(8): |
| 31 | self.assertEqual(self.pool.get_next(), _offset + i) |
| 32 | |
| 33 | |
| 34 | if __name__ == '__main__': |
| 35 | main() |