diff --git a/printNumbers/functions/sum_squares.py b/printNumbers/functions/sum_squares.py new file mode 100755 index 0000000..64ee3fe --- /dev/null +++ b/printNumbers/functions/sum_squares.py @@ -0,0 +1,31 @@ +# -*- 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 Sum_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 new file mode 100755 index 0000000..30aa7e4 --- /dev/null +++ b/printNumbers/unittests/test_sum_squares.py @@ -0,0 +1,62 @@ +# -*- 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: 'sum_squares'. +# + +import sys, os +sys.path.append(os.path.join(os.path.dirname(__file__), '..')) + +import unittest +from functions.sum_squares import * + +class TestSum_squares(unittest.TestCase): + + def test_value_neg(self): + self.assertEqual(Sum_squares(-2), 0) + + def test_value_0(self): + self.assertEqual(Sum_squares(0), 0) + + def test_value_1(self): + self.assertEqual(Sum_squares(1), 1) + + def test_value_2(self): + self.assertEqual(Sum_squares(2), 5) + + def test_value_3(self): + self.assertEqual(Sum_squares(3), 14) + + def test_value_10(self): + self.assertEqual(Sum_squares(10), 10 * 11 * 21 / 6) + + +def suite(): + suite = unittest.makeSuite(TestSum_squares, 'test') + return suite + +def run(): + runner = unittest.TextTestRunner(verbosity = 2) + runner.run(suite()) + +if __name__ == "__main__": + run()