Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2f86b1c6cc | ||
|
|
3c5079a9d8 | ||
|
|
078766216d | ||
|
|
8605a8c596 | ||
|
|
c7f0712d12 | ||
|
|
65ce7f1994 | ||
|
|
f5363514bf | ||
|
|
ff830c6fb8 | ||
|
|
cb4c5516ad | ||
|
|
d5861fb2e3 |
@@ -0,0 +1,2 @@
|
||||
__pycache__/
|
||||
*.pyc
|
||||
Binary file not shown.
+22
-6
@@ -1,5 +1,8 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from typing import Sequence
|
||||
|
||||
|
||||
def add(left: int, right: int) -> int:
|
||||
return left + right
|
||||
|
||||
@@ -8,11 +11,24 @@ def subtract(left: int, right: int) -> int:
|
||||
return left - right
|
||||
|
||||
|
||||
def multiply(left: int, right: int) -> int:
|
||||
return left * right
|
||||
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))
|
||||
|
||||
|
||||
def variance(values: Sequence[float]) -> float:
|
||||
if not values:
|
||||
raise ValueError
|
||||
mean = sum(values) / len(values)
|
||||
return sum((x - mean) ** 2 for x in values) / len(values)
|
||||
|
||||
|
||||
def divide(left: int, right: int) -> int:
|
||||
if right == 0:
|
||||
raise ValueError('division by zero')
|
||||
return left // right
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
Binary file not shown.
+74
-36
@@ -1,58 +1,96 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import math
|
||||
import unittest
|
||||
|
||||
from src.calculator import add, subtract, multiply, divide
|
||||
from src.calculator import clamp, variance
|
||||
|
||||
|
||||
class TestCalculator(unittest.TestCase):
|
||||
class TestClampFunction(unittest.TestCase):
|
||||
|
||||
def test_add(self) -> None:
|
||||
self.assertEqual(add(2, 3), 5)
|
||||
def test_value_below_low_returns_low(self) -> None:
|
||||
self.assertEqual(clamp(-5, 0, 10), 0)
|
||||
|
||||
def test_subtract(self) -> None:
|
||||
self.assertEqual(subtract(5, 3), 2)
|
||||
def test_value_above_high_returns_high(self) -> None:
|
||||
self.assertEqual(clamp(15, 0, 10), 10)
|
||||
|
||||
def test_multiply_positive(self) -> None:
|
||||
self.assertEqual(multiply(2, 3), 6)
|
||||
def test_value_in_range_returns_value(self) -> None:
|
||||
self.assertEqual(clamp(5, 0, 10), 5)
|
||||
|
||||
def test_multiply_negative_left(self) -> None:
|
||||
self.assertEqual(multiply(-2, 3), -6)
|
||||
def test_value_equals_low_returns_low(self) -> None:
|
||||
self.assertEqual(clamp(0, 0, 10), 0)
|
||||
|
||||
def test_multiply_negative_right(self) -> None:
|
||||
self.assertEqual(multiply(2, -3), -6)
|
||||
def test_value_equals_high_returns_high(self) -> None:
|
||||
self.assertEqual(clamp(10, 0, 10), 10)
|
||||
|
||||
def test_multiply_both_negative(self) -> None:
|
||||
self.assertEqual(multiply(-2, -3), 6)
|
||||
def test_low_greater_than_high_raises_value_error(self) -> None:
|
||||
with self.assertRaises(ValueError):
|
||||
clamp(5, 10, 0)
|
||||
|
||||
def test_multiply_zero_left(self) -> None:
|
||||
self.assertEqual(multiply(0, 5), 0)
|
||||
def test_all_negative_values(self) -> None:
|
||||
self.assertEqual(clamp(-10, -5, -1), -5)
|
||||
|
||||
def test_multiply_zero_right(self) -> None:
|
||||
self.assertEqual(multiply(5, 0), 0)
|
||||
def test_float_value_below_low_returns_low_as_int(self) -> None:
|
||||
self.assertEqual(clamp(-1.0, 0, 10), 0)
|
||||
|
||||
def test_multiply_both_zero(self) -> None:
|
||||
self.assertEqual(multiply(0, 0), 0)
|
||||
def test_float_value_above_high_returns_high_as_int(self) -> None:
|
||||
self.assertEqual(clamp(15.0, 0, 10), 10)
|
||||
|
||||
def test_divide_positive(self) -> None:
|
||||
self.assertEqual(divide(6, 3), 2)
|
||||
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_divide_negative_left(self) -> None:
|
||||
self.assertEqual(divide(-6, 3), -2)
|
||||
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_divide_negative_right(self) -> None:
|
||||
self.assertEqual(divide(6, -3), -2)
|
||||
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_divide_both_negative(self) -> None:
|
||||
self.assertEqual(divide(-6, -3), 2)
|
||||
def test_large_values(self) -> None:
|
||||
self.assertEqual(clamp(10**9, 0, 10**6), 10**6)
|
||||
|
||||
def test_divide_floor(self) -> None:
|
||||
self.assertEqual(divide(7, 3), 2)
|
||||
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))
|
||||
|
||||
|
||||
class TestVarianceFunction(unittest.TestCase):
|
||||
|
||||
def test_empty_list_raises_value_error(self) -> None:
|
||||
with self.assertRaises(ValueError):
|
||||
variance([])
|
||||
|
||||
def test_empty_tuple_raises_value_error(self) -> None:
|
||||
with self.assertRaises(ValueError):
|
||||
variance(())
|
||||
|
||||
def test_single_element_returns_zero(self) -> None:
|
||||
self.assertEqual(variance([42.0]), 0.0)
|
||||
|
||||
def test_constant_values_return_zero_variance(self) -> None:
|
||||
self.assertEqual(variance([1.0, 1.0, 1.0]), 0.0)
|
||||
|
||||
def test_population_variance_of_known_set(self) -> None:
|
||||
self.assertEqual(variance([1, 2, 3, 4, 5]), 2.0)
|
||||
|
||||
def test_two_element_variance(self) -> None:
|
||||
self.assertEqual(variance([0, 2]), 1.0)
|
||||
|
||||
def test_integer_inputs_return_float(self) -> None:
|
||||
result = variance([10, 20, 30])
|
||||
self.assertIsInstance(result, float)
|
||||
self.assertEqual(result, 200.0 / 3.0)
|
||||
|
||||
def test_tuple_input_returns_variance(self) -> None:
|
||||
self.assertEqual(variance((1, 2, 3, 4, 5)), 2.0)
|
||||
|
||||
def test_divide_zero_numerator(self) -> None:
|
||||
self.assertEqual(divide(0, 5), 0)
|
||||
|
||||
def test_divide_by_zero(self) -> None:
|
||||
with self.assertRaises(ValueError) as cm:
|
||||
divide(5, 0)
|
||||
self.assertEqual(str(cm.exception), 'division by zero')
|
||||
|
||||
Reference in New Issue
Block a user