From 8b1046a675217d8721d5370ef1a793f97a11fddb Mon Sep 17 00:00:00 2001 From: Adolfo Fumero Date: Sat, 11 Jan 2025 20:31:51 -0500 Subject: [PATCH] Add files via upload --- .../triangle_challenge/test_triangle_side.py | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 solutions/tests/triangle_challenge/test_triangle_side.py diff --git a/solutions/tests/triangle_challenge/test_triangle_side.py b/solutions/tests/triangle_challenge/test_triangle_side.py new file mode 100644 index 000000000..6a2b57bf4 --- /dev/null +++ b/solutions/tests/triangle_challenge/test_triangle_side.py @@ -0,0 +1,57 @@ +""" +Unit test for the Triangular number sequence. + +This module runs 6 unit tests to verify the code can successfully calculate +The nth triangle side of the triangular number sequence +This test will include +- Basic cases: Test will calculate basic positive correct inputs +- Defensive cases: Test will include: Inputs that may create an error + +Created on 2025-01-06 +Author: Adolfo Fumero +""" + +import unittest + +from solutions.triangle_challenge.main import triangle_side + + +def triangle(n): + """ + Calculate the nth triangular number + + Parameters: + n(int): The position in the triangular number sequence. + + Returns: + int: The nth triangular number + + """ + if n < 1: + return "Input must be a positive integer." + return n * (n + 1) // 2 + +class TestTriangleFunction(unittest.TestCase): + def test1(self): + """Test valid positive integer input for the 1st number""" + self.assertEqual(triangle_side(1), 1) # First triangular number + + def test2(self): + """Test valid positive integer inputs for the 5th number""" + self.assertEqual(triangle_side(5), 15) # 1 + 2 + 3 + 4 + 5 = 15 + + def test3(self): + """Test valid positive integer inputs for the 10th number""" + self.assertEqual(triangle_side(10), 55) # Sum of first 10 integers + + def test4(self): + """Test input of 0.""" + self.assertEqual(triangle_side(0), "Input must be a positive integer.") + + def test5(self): + """Test negative integer input.""" + self.assertEqual(triangle_side(-5), "Input must be a positive integer.") + + def test6(self): + """Test a large input value.""" + self.assertEqual(triangle_side(100), 5050) # Sum of first 100 integers