From c8f68f6e576635633cf7a4389efe88a901ac7474 Mon Sep 17 00:00:00 2001 From: MilenaVeneva Date: Wed, 16 Nov 2022 12:56:59 +0200 Subject: [PATCH 1/3] implemented a function that finds the sum of squares of the first n numbers; implemented a suitable unittest to check that the function works correctly --- printNumbers/functions/sum_squares.py | 29 +++++++++++ printNumbers/unittests/test_sum_squares.py | 59 ++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100755 printNumbers/functions/sum_squares.py create mode 100755 printNumbers/unittests/test_sum_squares.py diff --git a/printNumbers/functions/sum_squares.py b/printNumbers/functions/sum_squares.py new file mode 100755 index 0000000..cc0c250 --- /dev/null +++ b/printNumbers/functions/sum_squares.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +# +# factorial.py +# +# This file is part of printNumbers. +# +# Copyright (C) 2017 G. Trensch, Simulation & Datalab Neuroscience, JSC, FZ Juelich +# +# printNumbers is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# +# printNumbers is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with printNumbers. If not, see . + +import math + +def Factorial(n): + ''' + :param n: Operand + :return: n! + ''' + return(n * (n + 1) * (2 * n + 1) / 6) diff --git a/printNumbers/unittests/test_sum_squares.py b/printNumbers/unittests/test_sum_squares.py new file mode 100755 index 0000000..024ee1a --- /dev/null +++ b/printNumbers/unittests/test_sum_squares.py @@ -0,0 +1,59 @@ +# -*- coding: utf-8 -*- +# +# test_sum_squares.py +# +# This file is part of PrintNumbers. +# +# Copyright (C) 2017 G. Trensch, Simulation & Datalab Neuroscience, JSC, FZ Juelich +# +# PrintNumbers is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# +# PrintNumbers is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with PrintNumbers. If not, see . + +# +# Unit tests: 'factorial'. +# + +import sys, os +sys.path.append(os.path.join(os.path.dirname(__file__), '..')) + +import unittest +from functions.factorial import * + +class TestFactorial(unittest.TestCase): + + def test_value_0(self): + self.assertEqual(Factorial(0), 0) + + def test_value_1(self): + self.assertEqual(Factorial(1), 1) + + def test_value_2(self): + self.assertEqual(Factorial(2), 4) + + def test_value_3(self): + self.assertEqual(Factorial(3), 14) + + def test_value_10(self): + self.assertEqual(Factorial(10), 10 * 11 * 21 / 6) + + +def suite(): + suite = unittest.makeSuite(TestFactorial, 'test') + return suite + +def run(): + runner = unittest.TextTestRunner(verbosity = 2) + runner.run(suite()) + +if __name__ == "__main__": + run() From 70a1ffe15d87b61002d8b1730c3c261ab7f4fbea Mon Sep 17 00:00:00 2001 From: MilenaVeneva Date: Thu, 17 Nov 2022 13:23:26 +0200 Subject: [PATCH 2/3] added negative test; added the unittest to the runTestSuite.py --- printNumbers/functions/sum_squares.py | 4 +++- printNumbers/unittests/runTestSuite.py | 2 ++ printNumbers/unittests/test_sum_squares.py | 21 ++++++++++++--------- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/printNumbers/functions/sum_squares.py b/printNumbers/functions/sum_squares.py index cc0c250..8fbc438 100755 --- a/printNumbers/functions/sum_squares.py +++ b/printNumbers/functions/sum_squares.py @@ -21,9 +21,11 @@ import math -def Factorial(n): +def Sum_of_squares(n): ''' :param n: Operand :return: n! ''' + if(n < 0): + return(0) return(n * (n + 1) * (2 * n + 1) / 6) diff --git a/printNumbers/unittests/runTestSuite.py b/printNumbers/unittests/runTestSuite.py index f3dca23..5d9efd8 100755 --- a/printNumbers/unittests/runTestSuite.py +++ b/printNumbers/unittests/runTestSuite.py @@ -27,6 +27,7 @@ import test_factorial import test_fibonacci +import test_sum_squares def suite(): @@ -34,6 +35,7 @@ def suite(): suite.addTest(test_factorial.suite()) suite.addTest(test_fibonacci.suite()) + suite.addTest(test_sum_squares.suite()) return suite diff --git a/printNumbers/unittests/test_sum_squares.py b/printNumbers/unittests/test_sum_squares.py index 024ee1a..3222502 100755 --- a/printNumbers/unittests/test_sum_squares.py +++ b/printNumbers/unittests/test_sum_squares.py @@ -20,35 +20,38 @@ # along with PrintNumbers. If not, see . # -# Unit tests: 'factorial'. +# Unit tests: 'sum_of_squares'. # import sys, os sys.path.append(os.path.join(os.path.dirname(__file__), '..')) import unittest -from functions.factorial import * +from functions.sum_squares import * -class TestFactorial(unittest.TestCase): +class TestSum_of_squares(unittest.TestCase): + + def test_value_neg(self): + self.assertEqual(Sum_of_squares(-2), 0) def test_value_0(self): - self.assertEqual(Factorial(0), 0) + self.assertEqual(Sum_of_squares(0), 0) def test_value_1(self): - self.assertEqual(Factorial(1), 1) + self.assertEqual(Sum_of_squares(1), 1) def test_value_2(self): - self.assertEqual(Factorial(2), 4) + self.assertEqual(Sum_of_squares(2), 5) def test_value_3(self): - self.assertEqual(Factorial(3), 14) + self.assertEqual(Sum_of_squares(3), 14) def test_value_10(self): - self.assertEqual(Factorial(10), 10 * 11 * 21 / 6) + self.assertEqual(Sum_of_squares(10), 10 * 11 * 21 / 6) def suite(): - suite = unittest.makeSuite(TestFactorial, 'test') + suite = unittest.makeSuite(TestSum_of_squares, 'test') return suite def run(): From e6511d44088f562159f3566285ce18d4e5dadb45 Mon Sep 17 00:00:00 2001 From: MilenaVeneva Date: Thu, 17 Nov 2022 13:37:54 +0200 Subject: [PATCH 3/3] made the function names consistent --- printNumbers/functions/sum_squares.py | 2 +- printNumbers/unittests/test_sum_squares.py | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/printNumbers/functions/sum_squares.py b/printNumbers/functions/sum_squares.py index 8fbc438..64ee3fe 100755 --- a/printNumbers/functions/sum_squares.py +++ b/printNumbers/functions/sum_squares.py @@ -21,7 +21,7 @@ import math -def Sum_of_squares(n): +def Sum_squares(n): ''' :param n: Operand :return: n! diff --git a/printNumbers/unittests/test_sum_squares.py b/printNumbers/unittests/test_sum_squares.py index 3222502..30aa7e4 100755 --- a/printNumbers/unittests/test_sum_squares.py +++ b/printNumbers/unittests/test_sum_squares.py @@ -20,7 +20,7 @@ # along with PrintNumbers. If not, see . # -# Unit tests: 'sum_of_squares'. +# Unit tests: 'sum_squares'. # import sys, os @@ -29,29 +29,29 @@ import unittest from functions.sum_squares import * -class TestSum_of_squares(unittest.TestCase): +class TestSum_squares(unittest.TestCase): def test_value_neg(self): - self.assertEqual(Sum_of_squares(-2), 0) + self.assertEqual(Sum_squares(-2), 0) def test_value_0(self): - self.assertEqual(Sum_of_squares(0), 0) + self.assertEqual(Sum_squares(0), 0) def test_value_1(self): - self.assertEqual(Sum_of_squares(1), 1) + self.assertEqual(Sum_squares(1), 1) def test_value_2(self): - self.assertEqual(Sum_of_squares(2), 5) + self.assertEqual(Sum_squares(2), 5) def test_value_3(self): - self.assertEqual(Sum_of_squares(3), 14) + self.assertEqual(Sum_squares(3), 14) def test_value_10(self): - self.assertEqual(Sum_of_squares(10), 10 * 11 * 21 / 6) + self.assertEqual(Sum_squares(10), 10 * 11 * 21 / 6) def suite(): - suite = unittest.makeSuite(TestSum_of_squares, 'test') + suite = unittest.makeSuite(TestSum_squares, 'test') return suite def run():