Compare commits
9
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8605a8c596 | ||
|
|
c7f0712d12 | ||
|
|
65ce7f1994 | ||
|
|
f5363514bf | ||
|
|
ff830c6fb8 | ||
|
|
cb4c5516ad | ||
|
|
d5861fb2e3 | ||
|
|
8f7f09f44a | ||
|
|
3f2a5c8a07 |
@@ -0,0 +1,2 @@
|
||||
__pycache__/
|
||||
*.pyc
|
||||
@@ -1,2 +1,2 @@
|
||||
verify:
|
||||
@python3 -m compileall -q src && echo "verification passed"
|
||||
@python3 -m compileall -q src tests && python3 -m unittest discover -s tests -q && echo "verification passed"
|
||||
|
||||
Binary file not shown.
+12
-2
@@ -8,5 +8,15 @@ def subtract(left: int, right: int) -> int:
|
||||
return left - right
|
||||
|
||||
|
||||
def power(base: int, exponent: int) -> int:
|
||||
return base ** exponent
|
||||
def clamp(value: int, low: int, high: int) -> int:
|
||||
if low > high:
|
||||
raise ValueError
|
||||
if value < low:
|
||||
return low
|
||||
if value > high:
|
||||
return high
|
||||
return value
|
||||
|
||||
|
||||
def clamp_to_byte(value: int) -> int:
|
||||
return max(0, min(255, value))
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
@@ -0,0 +1,63 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import math
|
||||
import unittest
|
||||
|
||||
from src.calculator import clamp
|
||||
|
||||
|
||||
class TestClampFunction(unittest.TestCase):
|
||||
|
||||
def test_value_below_low_returns_low(self) -> None:
|
||||
self.assertEqual(clamp(-5, 0, 10), 0)
|
||||
|
||||
def test_value_above_high_returns_high(self) -> None:
|
||||
self.assertEqual(clamp(15, 0, 10), 10)
|
||||
|
||||
def test_value_in_range_returns_value(self) -> None:
|
||||
self.assertEqual(clamp(5, 0, 10), 5)
|
||||
|
||||
def test_value_equals_low_returns_low(self) -> None:
|
||||
self.assertEqual(clamp(0, 0, 10), 0)
|
||||
|
||||
def test_value_equals_high_returns_high(self) -> None:
|
||||
self.assertEqual(clamp(10, 0, 10), 10)
|
||||
|
||||
def test_low_greater_than_high_raises_value_error(self) -> None:
|
||||
with self.assertRaises(ValueError):
|
||||
clamp(5, 10, 0)
|
||||
|
||||
def test_all_negative_values(self) -> None:
|
||||
self.assertEqual(clamp(-10, -5, -1), -5)
|
||||
|
||||
def test_float_value_below_low_returns_low_as_int(self) -> None:
|
||||
self.assertEqual(clamp(-1.0, 0, 10), 0)
|
||||
|
||||
def test_float_value_above_high_returns_high_as_int(self) -> None:
|
||||
self.assertEqual(clamp(15.0, 0, 10), 10)
|
||||
|
||||
def test_float_value_in_range_returns_float(self) -> None:
|
||||
result = clamp(5.0, 0, 10)
|
||||
self.assertIsInstance(result, float)
|
||||
self.assertEqual(result, 5.0)
|
||||
|
||||
def test_float_value_equals_boundary_returns_boundary(self) -> None:
|
||||
self.assertEqual(clamp(0.0, 0, 10), 0)
|
||||
self.assertEqual(clamp(10.0, 0, 10), 10)
|
||||
|
||||
def test_float_low_greater_than_float_high_raises_value_error(self) -> None:
|
||||
with self.assertRaises(ValueError):
|
||||
clamp(5.0, 10.0, 0.0)
|
||||
|
||||
def test_large_values(self) -> None:
|
||||
self.assertEqual(clamp(10**9, 0, 10**6), 10**6)
|
||||
|
||||
def test_negative_infinity_not_clamped_by_default(self) -> None:
|
||||
self.assertEqual(clamp(-math.inf, 0, 10), 0)
|
||||
|
||||
def test_positive_infinity_not_clamped_by_default(self) -> None:
|
||||
self.assertEqual(clamp(math.inf, 0, 10), 10)
|
||||
|
||||
def test_nan_returns_nan(self) -> None:
|
||||
result = clamp(math.nan, 0, 10)
|
||||
self.assertTrue(math.isnan(result))
|
||||
Reference in New Issue
Block a user