OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 # Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
3 # | 3 # |
4 # Use of this source code is governed by a BSD-style license | 4 # Use of this source code is governed by a BSD-style license |
5 # that can be found in the LICENSE file in the root of the source | 5 # that can be found in the LICENSE file in the root of the source |
6 # tree. An additional intellectual property rights grant can be found | 6 # tree. An additional intellectual property rights grant can be found |
7 # in the file PATENTS. All contributing project authors may | 7 # in the file PATENTS. All contributing project authors may |
8 # be found in the AUTHORS file in the root of the source tree. | 8 # be found in the AUTHORS file in the root of the source tree. |
9 | 9 |
10 """Run the tests with | 10 """Run the tests with |
11 | 11 |
12 python misc_test.py | 12 python misc_test.py |
13 or | 13 or |
14 python3 misc_test.py | 14 python3 misc_test.py |
15 """ | 15 """ |
16 | 16 |
17 from __future__ import division | 17 from __future__ import division |
18 import random | 18 import random |
19 import unittest | 19 import unittest |
20 | 20 |
21 import misc | 21 import misc |
22 | 22 |
23 | 23 |
24 class TestMisc(unittest.TestCase): | 24 class TestMisc(unittest.TestCase): |
25 def testUnwrapMod3(self): | 25 def testUnwrapMod3(self): |
26 data = [0, 1, 2, 0, -1, -2, -3, -4] | 26 data = [0, 1, 2, 0, -1, -2, -3, -4] |
27 unwrapped_3 = misc.unwrap(data, 3) | 27 unwrapped_3 = misc.Unwrap(data, 3) |
28 self.assertEqual([0, 1, 2, 3, 2, 1, 0, -1], unwrapped_3) | 28 self.assertEqual([0, 1, 2, 3, 2, 1, 0, -1], unwrapped_3) |
29 | 29 |
30 def testUnwrapMod4(self): | 30 def testUnwrapMod4(self): |
31 data = [0, 1, 2, 0, -1, -2, -3, -4] | 31 data = [0, 1, 2, 0, -1, -2, -3, -4] |
32 unwrapped_4 = misc.unwrap(data, 4) | 32 unwrapped_4 = misc.Unwrap(data, 4) |
33 self.assertEqual([0, 1, 2, 0, -1, -2, -3, -4], unwrapped_4) | 33 self.assertEqual([0, 1, 2, 0, -1, -2, -3, -4], unwrapped_4) |
34 | 34 |
35 def testDataShouldNotChangeAfterUnwrap(self): | 35 def testDataShouldNotChangeAfterUnwrap(self): |
36 data = [0, 1, 2, 0, -1, -2, -3, -4] | 36 data = [0, 1, 2, 0, -1, -2, -3, -4] |
37 _ = misc.unwrap(data, 4) | 37 _ = misc.Unwrap(data, 4) |
38 | 38 |
39 self.assertEqual([0, 1, 2, 0, -1, -2, -3, -4], data) | 39 self.assertEqual([0, 1, 2, 0, -1, -2, -3, -4], data) |
40 | 40 |
41 def testRandomlyMultiplesOfModAdded(self): | 41 def testRandomlyMultiplesOfModAdded(self): |
42 # `unwrap` definition says only multiples of mod are added. | 42 # `unwrap` definition says only multiples of mod are added. |
43 random_data = [random.randint(0, 9) for _ in range(100)] | 43 random_data = [random.randint(0, 9) for _ in range(100)] |
44 | 44 |
45 for mod in range(1, 100): | 45 for mod in range(1, 100): |
46 random_data_unwrapped_mod = misc.unwrap(random_data, mod) | 46 random_data_unwrapped_mod = misc.Unwrap(random_data, mod) |
47 | 47 |
48 for (old_a, a) in zip(random_data, random_data_unwrapped_mod): | 48 for (old_a, a) in zip(random_data, random_data_unwrapped_mod): |
49 self.assertEqual((old_a - a) % mod, 0) | 49 self.assertEqual((old_a - a) % mod, 0) |
50 | 50 |
51 def testRandomlyAgainstInequalityDefinition(self): | 51 def testRandomlyAgainstInequalityDefinition(self): |
52 # Data has to satisfy -mod/2 <= difference < mod/2 for every | 52 # Data has to satisfy -mod/2 <= difference < mod/2 for every |
53 # difference between consecutive values after unwrap. | 53 # difference between consecutive values after unwrap. |
54 random_data = [random.randint(0, 9) for _ in range(100)] | 54 random_data = [random.randint(0, 9) for _ in range(100)] |
55 | 55 |
56 for mod in range(1, 100): | 56 for mod in range(1, 100): |
57 random_data_unwrapped_mod = misc.unwrap(random_data, mod) | 57 random_data_unwrapped_mod = misc.Unwrap(random_data, mod) |
58 | 58 |
59 for (a, b) in zip(random_data_unwrapped_mod, | 59 for (a, b) in zip(random_data_unwrapped_mod, |
60 random_data_unwrapped_mod[1:]): | 60 random_data_unwrapped_mod[1:]): |
61 self.assertTrue(-mod / 2 <= b - a < mod / 2) | 61 self.assertTrue(-mod / 2 <= b - a < mod / 2) |
62 | 62 |
63 def testRandomlyDataShouldNotChangeAfterUnwrap(self): | 63 def testRandomlyDataShouldNotChangeAfterUnwrap(self): |
64 random_data = [random.randint(0, 9) for _ in range(100)] | 64 random_data = [random.randint(0, 9) for _ in range(100)] |
65 random_data_copy = random_data[:] | 65 random_data_copy = random_data[:] |
66 for mod in range(1, 100): | 66 for mod in range(1, 100): |
67 _ = misc.unwrap(random_data, mod) | 67 _ = misc.Unwrap(random_data, mod) |
68 | 68 |
69 self.assertEqual(random_data, random_data_copy) | 69 self.assertEqual(random_data, random_data_copy) |
70 | 70 |
71 if __name__ == "__main__": | 71 if __name__ == "__main__": |
72 unittest.main() | 72 unittest.main() |
OLD | NEW |