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