From 091f33c5213125431b06997e9c67d25beb2b6a55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Bonhomme?= Date: Wed, 1 Mar 2017 07:53:10 +0100 Subject: [PATCH] Test and improved the generators. --- stegano/lsbset/generators.py | 12 +++--- tests/test_generators.py | 84 ++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 5 deletions(-) create mode 100644 tests/test_generators.py diff --git a/stegano/lsbset/generators.py b/stegano/lsbset/generators.py index 53bf645..8c2d217 100644 --- a/stegano/lsbset/generators.py +++ b/stegano/lsbset/generators.py @@ -44,7 +44,7 @@ def Dead_Man_Walking(): yield n + 7 n += 2 -def OEIS_A000217(): +def triangular_numbers(): """http://oeis.org/A000217 Triangular numbers: a(n) = C(n+1,2) = n(n+1)/2 = 0+1+2+...+n. """ @@ -54,17 +54,19 @@ def OEIS_A000217(): n += 1 def fermat(): - """Generate the n-th Fermat Number. + """https://oeis.org/A000215 + Generate the n-th Fermat Number. """ - y = 5 + y = 3 while True: yield y y = pow(y-1,2)+1 def mersenne(): - """Generate 2^n-1. + """https://oeis.org/A001348 + Generate 2^n - 1. """ - y = 1 + y = 3 while True: yield y y = 2*y + 1 diff --git a/tests/test_generators.py b/tests/test_generators.py new file mode 100644 index 0000000..e91f127 --- /dev/null +++ b/tests/test_generators.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python +#-*- coding: utf-8 -*- + +# Stéganô - Stéganô is a basic Python Steganography module. +# Copyright (C) 2010-2017 Cédric Bonhomme - https://www.cedricbonhomme.org +# +# For more information : https://github.com/cedricbonhomme/Stegano +# +# This program 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 3 of the License, or +# (at your option) any later version. +# +# This program 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 this program. If not, see + +__author__ = "Cedric Bonhomme" +__version__ = "$Revision: 0.1 $" +__date__ = "$Date: 2017/03/01 $" +__revision__ = "$Date: 2017/03/01 $" +__license__ = "GPLv3" + +import unittest +import itertools + +from stegano.lsbset import generators + +class TestGenerators(unittest.TestCase): + + def test_identity(self): + """ + Test the identity generator. + """ + self.assertEqual(tuple(itertools.islice(generators.identity(), 15)), + (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)) + + def test_fibonacci(self): + """ + Test the Fibonacci generator. + """ + self.assertEqual(tuple(itertools.islice(generators.fibonacci(), 20)), + (1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, + 987, 1597, 2584, 4181, 6765, 10946)) + + def test_fermat(self): + """ + Test the Fermat generator. + """ + self.assertEqual(tuple(itertools.islice(generators.fermat(), 9)), + (3, 5, 17, 257, 65537, 4294967297, 18446744073709551617, + 340282366920938463463374607431768211457, + 115792089237316195423570985008687907853269984665640564039457584007913129639937)) + + def test_triangular_numbers(self): + """ + Test the Triangular numbers generator. + """ + self.assertEqual(tuple(itertools.islice(generators.triangular_numbers(), 54)), + (0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, + 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, + 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, + 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, + 1128, 1176, 1225, 1275, 1326, 1378, 1431)) + + # def test_mersenne(self): + # """ + # Test the Mersenne generator. + # """ + # self.assertEqual(tuple(itertools.islice(generators.mersenne(), 20)), + # (3, 7, 31, 127, 2047, 8191, 131071, 524287, 8388607, + # 536870911, 2147483647, 137438953471, 2199023255551, + # 8796093022207, 140737488355327, 9007199254740991, + # 576460752303423487, 2305843009213693951, + # 147573952589676412927, 2361183241434822606847)) + + + +if __name__ == '__main__': + unittest.main()