Skip to content

Commit

Permalink
test(utils): Add comprehensive unit tests for utility functions
Browse files Browse the repository at this point in the history
Implement TestUtils class with test methods for bits_le_with_width,
pow_2, is_power_of_two, next_power_of_two, and log_2 functions.
Include edge cases and error handling tests.
  • Loading branch information
ahy231 committed Sep 30, 2024
1 parent 126f5ae commit 79cda5f
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from unittest import TestCase, main
import sys

sys.path.append("src")
sys.path.append("../src")

from utils import bits_le_with_width, pow_2, is_power_of_two, next_power_of_two, log_2

class TestUtils(TestCase):

def test_bits_le_with_width(self):
self.assertEqual(bits_le_with_width(5, 4), [1, 0, 1, 0])
self.assertEqual(bits_le_with_width(0, 3), [0, 0, 0])
self.assertEqual(bits_le_with_width(7, 3), [1, 1, 1])
self.assertEqual(bits_le_with_width(8, 3), "Failed")
self.assertEqual(bits_le_with_width(15, 4), [1, 1, 1, 1])

def test_pow_2(self):
self.assertEqual(pow_2(0), 1)
self.assertEqual(pow_2(1), 2)
self.assertEqual(pow_2(3), 8)
self.assertEqual(pow_2(10), 1024)

def test_is_power_of_two(self):
self.assertEqual(is_power_of_two(1), True)
self.assertEqual(is_power_of_two(2), True)
self.assertEqual(is_power_of_two(4), True)
self.assertEqual(is_power_of_two(8), True)
self.assertEqual(is_power_of_two(16), True)
self.assertEqual(is_power_of_two(3), False)
self.assertEqual(is_power_of_two(6), False)
self.assertEqual(is_power_of_two(10), False)

def test_next_power_of_two(self):
self.assertEqual(next_power_of_two(0), 1)
self.assertEqual(next_power_of_two(1), 1)
self.assertEqual(next_power_of_two(2), 2)
self.assertEqual(next_power_of_two(3), 4)
self.assertEqual(next_power_of_two(5), 8)
self.assertEqual(next_power_of_two(7), 8)
self.assertEqual(next_power_of_two(9), 16)
self.assertEqual(next_power_of_two(1023), 1024)

with self.assertRaises(AssertionError):
next_power_of_two(-1)

def test_log_2(self):
self.assertEqual(log_2(1), 0)
self.assertEqual(log_2(2), 1)
self.assertEqual(log_2(3), 1)
self.assertEqual(log_2(4), 2)
self.assertEqual(log_2(7), 2)
self.assertEqual(log_2(8), 3)
self.assertEqual(log_2(1024), 10)

with self.assertRaises(ValueError):
log_2(0)

with self.assertRaises(ValueError):
log_2(-1)

with self.assertRaises(ValueError):
log_2(1.5)


if __name__ == "__main__":
main()

0 comments on commit 79cda5f

Please sign in to comment.