forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |