blob: eb6ea50d2a2c457f63076baa6a744ea79a0b2005 [file] [log] [blame]
Chip Boling67b674a2019-02-08 11:42:18 -06001# 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 Williams84a71e92019-11-15 09:00:19 -070014from __future__ import absolute_import
Chip Boling67b674a2019-02-08 11:42:18 -060015from unittest import TestCase, main
16from pyvoltha.common.utils.indexpool import IndexPool
Zack Williams84a71e92019-11-15 09:00:19 -070017from six.moves import range
Chip Boling67b674a2019-02-08 11:42:18 -060018
19class 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
49if __name__ == '__main__':
50 main()