⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
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
28 changes: 28 additions & 0 deletions printNumbers/functions/square.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
#
# square.py
#
# This file is part of printNumbers.
#
# Copyright (C) 2022 P. Carlsson, IEK-8, 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/>.

def Square(n):
'''
:param n: Operand
:return: n*n
'''

return (n*n)
3 changes: 3 additions & 0 deletions printNumbers/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
CONST_MAX_OPERAND_VAL = 20
CONST_FUNC_CODE_FIBONACCI = 0
CONST_FUNC_CODE_FACTORIAL = 1
CONST_FUNC_CODE_SQUARE = 2

class Parameters(object):

Expand All @@ -43,6 +44,8 @@ def __setParameters(self, cmdLineArgs):
self.functionIndex = CONST_FUNC_CODE_FIBONACCI
elif cmdLineArgs['--factorial']:
self.functionIndex = CONST_FUNC_CODE_FACTORIAL
elif cmdLineArgs['--square']:
self.functionIndex = CONST_FUNC_CODE_SQUARE

@property
def operand(self):
Expand Down
9 changes: 8 additions & 1 deletion printNumbers/printNumbers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#! /bin/env python
# -*- coding: utf-8 -*-
#
# printNumbers.py
Expand Down Expand Up @@ -27,24 +28,28 @@
"""
Usage:
printNumbers.py -h --help
printNumbers.py [--fibonacci|--factorial] <operand>
printNumbers.py [--fibonacci|--factorial|--square] <operand>

Options:
-h --help Print usage.
--fibonacci Print the fibonacci sequence.
--factorial Print the factorial.
--square Print the square of the operand.
"""

from docopt import docopt
from parameters import *
from functions.fibonacci import *
from functions.factorial import *
from functions.square import *


#
# FUNCTION TABLE
#
functionTable = { CONST_FUNC_CODE_FIBONACCI : FibonacciSequence,
CONST_FUNC_CODE_FACTORIAL : Factorial,
CONST_FUNC_CODE_SQUARE : Square,
}

#
Expand All @@ -67,3 +72,5 @@
print('fib(' + str(params.operand) + ') =', result)
elif params.functionIndex == CONST_FUNC_CODE_FACTORIAL:
print(str(params.operand) + '! =', str(result))
elif params.functionIndex == CONST_FUNC_CODE_SQUARE:
print(str(params.operand) + ' squared is ', str(result))
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_square


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

suite.addTest(test_factorial.suite())
suite.addTest(test_fibonacci.suite())
suite.addTest(test_square.suite())

return suite

Expand Down
56 changes: 56 additions & 0 deletions printNumbers/unittests/test_square.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
#
# test_square.py
#
# This file is part of PrintNumbers.
#
# Copyright (C) 2022 P. Carlsson, IEK8, 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: 'square'.
#

import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import unittest
from functions.square import *

class TestSquare(unittest.TestCase):

def test_value_0(self):
self.assertEqual(Square(0), 0)

def test_value_1(self):
self.assertEqual(Square(1), 1)

def test_value_2(self):
self.assertEqual(Square(2), 4)

def test_value_20(self):
self.assertEqual(Square(20), 400)


def suite():
suite = unittest.makeSuite(TestSquare, 'test')
return suite

def run():
runner = unittest.TextTestRunner(verbosity = 2)
runner.run(suite())

if __name__ == "__main__":
run()