do in-place swap
[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(3, 10):
32 n = 2**i
33 vector = FastDctTest.nonrandom_vector(n)
34 expect = naivedct.transform(vector)
35 original = fastdctlee.transform(vector)
36 actual = fastdctlee.transform2(vector)
37 self.assertListAlmostEqual(actual, expect)
38 expect = naivedct.inverse_transform(vector)
39 actual = fastdctlee.inverse_transform(vector)
40 self.assertListAlmostEqual(actual, expect)
41
42 def notest_fast_dct_lee_invertibility(self):
43 for i in range(1, 10):
44 n = 2**i
45 vector = FastDctTest.random_vector(n)
46 temp = fastdctlee.transform2(vector)
47 temp = fastdctlee.inverse_transform(temp)
48 temp = [(val * 2.0 / n) for val in temp]
49 self.assertListAlmostEqual(vector, temp)
50
51 def assertListAlmostEqual(self, actual, expect):
52 self.assertEqual(len(actual), len(expect))
53 for (x, y) in zip(actual, expect):
54 self.assertAlmostEqual(x, y, delta=FastDctTest._EPSILON)
55
56 @staticmethod
57 def random_vector(n):
58 return [random.uniform(-1.0, 1.0) for _ in range(n)]
59
60 @staticmethod
61 def nonrandom_vector(n):
62 return [(i-n/2.0) for i in range(n)]
63
64
65 _EPSILON = 1e-9
66
67
68 if __name__ == "__main__":
69 unittest.main()