whitespace
[openpower-isa.git] / src / openpower / decoder / isa / fastdct-test.py
1 #
2 # Fast discrete cosine transform algorithms (Python)
3 #
4 # Copyright (c) 2020 Project Nayuki. (MIT License)
5 # https://www.nayuki.io/page/fast-discrete-cosine-transform-algorithms
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy of
8 # this software and associated documentation files (the "Software"), to deal in
9 # the Software without restriction, including without limitation the rights to
10 # use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 # - The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 # - The Software is provided "as is", without warranty of any kind, express or
16 # implied, including but not limited to the warranties of merchantability,
17 # fitness for a particular purpose and noninfringement. In no event shall the
18 # authors or copyright holders be liable for any claim, damages or other
19 # liability, whether in an action of contract, tort or otherwise,
20 # arising from, out of or in connection with the Software or the use
21 # or other dealings in the Software.
22 #
23
24 import math, random, unittest
25 import fastdctlee, naivedct
26
27
28 class FastDctTest(unittest.TestCase):
29
30 def test_fast_dct_lee_vs_naive(self):
31 for i in range(1, 12):
32 n = 2**i
33 vector = FastDctTest.random_vector(n)
34 expect = naivedct.transform(vector)
35 actual = fastdctlee.transform(vector)
36 self.assertListAlmostEqual(actual, expect)
37 expect = naivedct.inverse_transform(vector)
38 actual = fastdctlee.inverse_transform(vector)
39 self.assertListAlmostEqual(actual, expect)
40
41 def test_fast_dct_lee_invertibility(self):
42 for i in range(1, 18):
43 n = 2**i
44 vector = FastDctTest.random_vector(n)
45 temp = fastdctlee.transform(vector)
46 temp = fastdctlee.inverse_transform(temp)
47 temp = [(val * 2.0 / n) for val in temp]
48 self.assertListAlmostEqual(vector, temp)
49
50 def assertListAlmostEqual(self, actual, expect):
51 self.assertEqual(len(actual), len(expect))
52 for (x, y) in zip(actual, expect):
53 self.assertAlmostEqual(x, y, delta=FastDctTest._EPSILON)
54
55 @staticmethod
56 def random_vector(n):
57 return [random.uniform(-1.0, 1.0) for _ in range(n)]
58
59
60 _EPSILON = 1e-9
61
62
63 if __name__ == "__main__":
64 unittest.main()