Mapytex/pymath/calculus/test/test_generic.py

75 lines
1.7 KiB
Python
Raw Normal View History

2014-02-21 08:45:45 +00:00
#!/usr/bin/env python
# encoding: utf-8
import unittest
2016-01-17 15:07:05 +00:00
from pymath.calculus import generic
2014-02-21 08:45:45 +00:00
2016-02-13 03:49:37 +00:00
2014-02-21 08:45:45 +00:00
class TestGeneric(unittest.TestCase):
2016-01-07 16:56:30 +00:00
"""Testing functions from pymath.calculus.generic"""
2014-02-21 08:45:45 +00:00
def test_flatten_list1(self):
2016-02-13 03:49:37 +00:00
l = [1, [2, 3], [[4, 5], 6], 7]
2014-02-21 08:45:45 +00:00
flat_l = generic.flatten_list(l)
2016-02-13 03:49:37 +00:00
true_flat = list(range(1, 8))
2014-02-21 08:45:45 +00:00
self.assertEqual(flat_l, true_flat)
def test_flatten_list2(self):
l = list(range(10))
flat_l = generic.flatten_list(l)
true_flat = list(range(10))
self.assertEqual(flat_l, true_flat)
def test_first_elem_simple_iter(self):
""" For simple iterable """
l = range(10)
first = generic.first_elem(l)
2016-02-13 03:49:37 +00:00
self.assertAlmostEqual(0, first)
2014-02-21 08:45:45 +00:00
s = "plopplop"
first = generic.first_elem(s)
self.assertAlmostEqual("p", first)
def test_first_elem_iter_in_iter(self):
""" Interable in iterable """
2016-02-13 03:49:37 +00:00
l = [[1, 2], [4, 5, [6, 7, 8]], 9]
2014-02-21 08:45:45 +00:00
first = generic.first_elem(l)
self.assertAlmostEqual(first, 1)
l = [[[1]]]
first = generic.first_elem(l)
self.assertAlmostEqual(first, 1)
l = ["abc"]
first = generic.first_elem(l)
self.assertAlmostEqual(first, "a")
2016-02-13 03:49:37 +00:00
l = ["abc", [4, 5, [6, 7, 8]], 9]
2014-02-21 08:45:45 +00:00
first = generic.first_elem(l)
self.assertAlmostEqual(first, "a")
2016-02-13 03:49:37 +00:00
l = [["abc", 1], [4, 5, [6, 7, 8]], 9]
2014-02-21 08:45:45 +00:00
first = generic.first_elem(l)
self.assertAlmostEqual(first, "a")
if __name__ == '__main__':
unittest.main()
2016-02-13 03:29:26 +00:00
2014-02-21 08:45:45 +00:00
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
2016-02-13 03:29:26 +00:00
# cursor: 16 del