Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [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. |
Zack Williams | 84a71e9 | 2019-11-15 09:00:19 -0700 | [diff] [blame] | 14 | from __future__ import absolute_import |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 15 | from unittest import TestCase, main |
| 16 | from pyvoltha.common.utils.indexpool import IndexPool |
Zack Williams | 84a71e9 | 2019-11-15 09:00:19 -0700 | [diff] [blame] | 17 | from six.moves import range |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 18 | |
| 19 | class TestIndexPool(TestCase): |
| 20 | pool = IndexPool(8, 0) |
| 21 | def test_01_get_next(self): |
| 22 | self.assertEqual(self.pool.indices.bin, '00000000') |
| 23 | for i in range(8): |
| 24 | self.assertEqual(self.pool.get_next(), i) |
| 25 | #to check if there's any bit left after using all 8 bits |
| 26 | self.assertIsNone(self.pool.get_next()) |
| 27 | |
| 28 | def test_02_pre_allocate(self): |
| 29 | _pool2 = IndexPool(8, 0) |
| 30 | self.assertEqual(_pool2.indices.bin, '00000000') |
| 31 | _pool2.pre_allocate((0,1,2,)) |
| 32 | self.assertEqual(_pool2.indices.bin, '11100000') |
| 33 | |
| 34 | def test_03_release(self): |
| 35 | self.pool.release(5) |
| 36 | self.assertEqual(self.pool.indices.bin, '11111011') |
| 37 | self.pool.release(10) |
| 38 | self.assertEqual(self.pool.indices.bin, '11111011') |
| 39 | self.pool.release(0) |
| 40 | self.assertEqual(self.pool.indices.bin, '01111011') |
| 41 | |
| 42 | def test_04_check_offset(self): |
| 43 | _offset = 5 |
| 44 | self.pool = IndexPool(8, _offset) |
| 45 | for i in range(8): |
| 46 | self.assertEqual(self.pool.get_next(), _offset + i) |
| 47 | |
| 48 | |
| 49 | if __name__ == '__main__': |
| 50 | main() |