|
| 1 | +"""Unit tests for loop patterns using Python's range function. |
| 2 | +Each test case validates the expected output against the actual output |
| 3 | +from the corresponding function in the loop module. |
| 4 | +""" |
| 5 | + |
| 6 | +import unittest |
| 7 | + |
| 8 | +from src.basic.loop import ( |
| 9 | + basic_forward_loop, |
| 10 | + loop_with_index, |
| 11 | + range_with_start_end, |
| 12 | + reverse_loop, |
| 13 | + step_loop, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class TestLoop(unittest.TestCase): |
| 18 | + """Test cases for various loop patterns using range function.""" |
| 19 | + |
| 20 | + def test_basic_forward_loop(self): |
| 21 | + """Test basic forward loop from 0 to 4""" |
| 22 | + expected = [0, 1, 2, 3, 4] |
| 23 | + self.assertEqual(basic_forward_loop(), expected) |
| 24 | + |
| 25 | + def test_reverse_loop(self): |
| 26 | + """Test reverse loop from 4 to 0""" |
| 27 | + expected = [4, 3, 2, 1, 0] |
| 28 | + self.assertEqual(reverse_loop(), expected) |
| 29 | + |
| 30 | + def test_step_loop(self): |
| 31 | + """Test loop with step interval of 2""" |
| 32 | + expected = [0, 2, 4, 6, 8] |
| 33 | + self.assertEqual(step_loop(), expected) |
| 34 | + |
| 35 | + def test_loop_with_index(self): |
| 36 | + """Test loop with index using enumerate""" |
| 37 | + test_input = ["a", "b", "c"] |
| 38 | + expected = [(0, "a"), (1, "b"), (2, "c")] |
| 39 | + self.assertEqual(loop_with_index(test_input), expected) |
| 40 | + |
| 41 | + def test_range_with_start_end(self): |
| 42 | + """Test loop with specified start and end values""" |
| 43 | + expected = [2, 3, 4, 5, 6] |
| 44 | + self.assertEqual(range_with_start_end(), expected) |
0 commit comments