diff --git a/printNumbers/functions/square.py b/printNumbers/functions/square.py
new file mode 100644
index 0000000..acdcf1e
--- /dev/null
+++ b/printNumbers/functions/square.py
@@ -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 .
+
+def Square(n):
+ '''
+ :param n: Operand
+ :return: n*n
+ '''
+
+ return (n*n)
diff --git a/printNumbers/parameters.py b/printNumbers/parameters.py
index aebe0c3..54a7b8b 100755
--- a/printNumbers/parameters.py
+++ b/printNumbers/parameters.py
@@ -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):
@@ -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):
diff --git a/printNumbers/printNumbers.py b/printNumbers/printNumbers.py
index 9f9ef0e..18cfe1b 100755
--- a/printNumbers/printNumbers.py
+++ b/printNumbers/printNumbers.py
@@ -1,3 +1,4 @@
+#! /bin/env python
# -*- coding: utf-8 -*-
#
# printNumbers.py
@@ -27,24 +28,28 @@
"""
Usage:
printNumbers.py -h --help
- printNumbers.py [--fibonacci|--factorial]
+ printNumbers.py [--fibonacci|--factorial|--square]
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,
}
#
@@ -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))
diff --git a/printNumbers/unittests/runTestSuite.py b/printNumbers/unittests/runTestSuite.py
index f3dca23..c375a8b 100755
--- a/printNumbers/unittests/runTestSuite.py
+++ b/printNumbers/unittests/runTestSuite.py
@@ -27,6 +27,7 @@
import test_factorial
import test_fibonacci
+import test_square
def suite():
@@ -34,6 +35,7 @@ def suite():
suite.addTest(test_factorial.suite())
suite.addTest(test_fibonacci.suite())
+ suite.addTest(test_square.suite())
return suite
diff --git a/printNumbers/unittests/test_square.py b/printNumbers/unittests/test_square.py
new file mode 100644
index 0000000..b1b4c88
--- /dev/null
+++ b/printNumbers/unittests/test_square.py
@@ -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 .
+
+#
+# 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()