add prefix_sum and initial tests
[nmutil.git] / src / nmutil / test / test_prefix_sum.py
1 # SPDX-License-Identifier: LGPL-3-or-later
2 # Copyright 2022 Jacob Lifshay programmerjake@gmail.com
3
4 # Funded by NLnet Assure Programme 2021-02-052, https://nlnet.nl/assure part
5 # of Horizon 2020 EU Programme 957073.
6
7 from nmutil.formaltest import FHDLTestCase
8 from itertools import accumulate
9 import operator
10 from nmutil.prefix_sum import prefix_sum
11 import unittest
12
13
14 def reference_prefix_sum(items, fn):
15 return list(accumulate(items, fn))
16
17
18 class TestPrefixSum(FHDLTestCase):
19 def test_prefix_sum_str(self):
20 input_items = ("a", "b", "c", "d", "e", "f", "g", "h", "i")
21 expected = reference_prefix_sum(input_items, operator.add)
22 with self.subTest(expected=repr(expected)):
23 non_work_efficient = prefix_sum(input_items, work_efficient=False)
24 self.assertEqual(expected, non_work_efficient)
25 with self.subTest(expected=repr(expected)):
26 work_efficient = prefix_sum(input_items, work_efficient=True)
27 self.assertEqual(expected, work_efficient)
28
29 # TODO: add more tests
30
31
32 if __name__ == "__main__":
33 unittest.main()