Skip to content

Commit

Permalink
Add new code
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeinab15 committed Dec 29, 2024
1 parent ebb90f0 commit df31035
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
11 changes: 11 additions & 0 deletions solutions/tests/sum_range.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def sum_range(start, end):
"""Calculates the sum of all integers from start to end, inclusive."""
# Ensure start is less than or equal to end
assert isinstance (start, int), "start must be an integer"
assert isinstance (end, int), "end must be an integer"
if start > end:
start, end = end, start
total = 0
for number in range(start, end + 1):
total += number
return total
34 changes: 34 additions & 0 deletions solutions/tests/test_sumrange.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import unittest
from .sum_range import sum_range

class TestSumRange(unittest.TestCase):
"""Unit tests for the Sum_range function."""

def test_positive_range(self):
"""Test with a normal positive range."""
self.assertEqual(sum_range(1, 5), 15)

def test_reversed_range(self):
"""Test when start > end (reversed input)."""
self.assertEqual(sum_range(5, 1), 15)

def test_single_number(self):
"""Test a range with a single number."""
self.assertEqual(sum_range(10, 10), 10)

def test_negative_range(self):
"""Test a range that includes negative numbers."""
self.assertEqual(sum_range(-3, 3), 0)

def test_large_range(self):
"""Test with a large range."""
self.assertEqual(sum_range(1, 100), 5050)

def test_large_range_reversed(self):
"""Test with a large range."""
self.assertEqual(sum_range(100, 1), 5050)

def test_float_range(self):
"""Test with a range that includes floats."""
with self.assertRaises(AssertionError):
sum_range(0.5, 3.5)

0 comments on commit df31035

Please sign in to comment.