Zack Williams | 41513bf | 2018-07-07 20:08:35 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
Rachit Shrivastava | 8e435b4 | 2017-08-08 23:15:26 -0400 | [diff] [blame] | 14 | from unittest import TestCase, main |
| 15 | from common.utils.indexpool import IndexPool |
| 16 | |
| 17 | class TestIndexPool(TestCase): |
| 18 | pool = IndexPool(8, 0) |
| 19 | def test_01_get_next(self): |
| 20 | self.assertEqual(self.pool.indices.bin, '00000000') |
| 21 | for i in range(8): |
| 22 | self.assertEqual(self.pool.get_next(), i) |
| 23 | #to check if there's any bit left after using all 8 bits |
| 24 | self.assertIsNone(self.pool.get_next()) |
| 25 | |
| 26 | def test_02_pre_allocate(self): |
| 27 | _pool2 = IndexPool(8, 0) |
| 28 | self.assertEqual(_pool2.indices.bin, '00000000') |
| 29 | _pool2.pre_allocate((0,1,2,)) |
| 30 | self.assertEqual(_pool2.indices.bin, '11100000') |
| 31 | |
| 32 | def test_03_release(self): |
| 33 | self.pool.release(5) |
| 34 | self.assertEqual(self.pool.indices.bin, '11111011') |
| 35 | self.pool.release(10) |
| 36 | self.assertEqual(self.pool.indices.bin, '11111011') |
| 37 | self.pool.release(0) |
| 38 | self.assertEqual(self.pool.indices.bin, '01111011') |
| 39 | |
| 40 | def test_04_check_offset(self): |
| 41 | _offset = 5 |
| 42 | self.pool = IndexPool(8, _offset) |
| 43 | for i in range(8): |
| 44 | self.assertEqual(self.pool.get_next(), _offset + i) |
| 45 | |
| 46 | |
| 47 | if __name__ == '__main__': |
| 48 | main() |