1# Copyright 2023 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Tests for capture_request_utils."""
15
16
17import math
18import unittest
19
20import capture_request_utils
21
22
23class CaptureRequestUtilsTest(unittest.TestCase):
24  """Unit tests for this module.
25
26  Ensures rational number conversion dicts are created properly.
27  """
28  _FLOAT_HALF = 0.5
29  # No immutable container: frozendict requires package install on partner host
30  _RATIONAL_HALF = {'numerator': 32, 'denominator': 64}
31
32  def test_float_to_rational(self):
33    """Unit test for float_to_rational."""
34    self.assertEqual(
35        capture_request_utils.float_to_rational(self._FLOAT_HALF, 64),
36        self._RATIONAL_HALF)
37
38  def test_rational_to_float(self):
39    """Unit test for rational_to_float."""
40    self.assertTrue(
41        math.isclose(capture_request_utils.rational_to_float(
42            self._RATIONAL_HALF), self._FLOAT_HALF, abs_tol=0.0001))
43
44  def test_int_to_rational(self):
45    """Unit test for int_to_rational."""
46    rational_10 = {'numerator': 10, 'denominator': 1}
47    rational_1 = {'numerator': 1, 'denominator': 1}
48    rational_2 = {'numerator': 2, 'denominator': 1}
49    # Simple test
50    self.assertEqual(capture_request_utils.int_to_rational(10), rational_10)
51    # Handle list entries
52    self.assertEqual(
53        capture_request_utils.int_to_rational([1, 2]),
54        [rational_1, rational_2])
55
56if __name__ == '__main__':
57  unittest.main()
58