Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions printNumbers/functions/sum_squares.py
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

import math

def Sum_squares(n):
'''
:param n: Operand
:return: n!
'''
if(n < 0):
return(0)
return(n * (n + 1) * (2 * n + 1) / 6)
2 changes: 2 additions & 0 deletions printNumbers/unittests/runTestSuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@

import test_factorial
import test_fibonacci
import test_sum_squares


def suite():
suite = unittest.TestSuite()

suite.addTest(test_factorial.suite())
suite.addTest(test_fibonacci.suite())
suite.addTest(test_sum_squares.suite())

return suite

Expand Down
62 changes: 62 additions & 0 deletions printNumbers/unittests/test_sum_squares.py
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

#
# 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()