1# Copyright (C) 2022 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.
14import datetime
15import unittest
16from pathlib import Path
17
18from util import groupby
19from util import hhmmss
20from util import next_path
21from util import period_to_seconds
22
23
24class UtilTest(unittest.TestCase):
25    def test_groupby(self):
26        x1 = {"g": "b", "id": 1}
27        x2 = {"g": "a", "id": 2}
28        x3 = {"g": "b", "id": 3}
29        grouped = groupby([x1, x2, x3], lambda x: x["g"])
30        self.assertEqual(grouped, {"b": [x1, x3], "a": [x2]})
31        self.assertEqual(list(grouped.keys()), ["b", "a"], "insertion order maintained")
32
33    def test_next_path(self):
34        examples = [
35            ("output", "output-001"),
36            ("output-1", "output-002"),
37            ("output-09", "output-010"),
38            ("output-010", "output-011"),
39            ("output-999", "output-1000"),
40        ]
41        for pattern, expected in examples:
42            with self.subTest(msg=pattern, pattern=pattern, expected=expected):
43                generator = next_path(Path(pattern))
44                n = next(generator)
45                self.assertEqual(n, Path(expected))
46
47    def test_hhmmss(self):
48        decimal_precision_examples = [
49            (datetime.timedelta(seconds=(2 * 60 + 5)), "02:05.000"),
50            (datetime.timedelta(seconds=(3600 + 23 * 60 + 45.897898)), "1:23:45.898"),
51        ]
52        non_decimal_precision_examples = [
53            (datetime.timedelta(seconds=(2 * 60 + 5)), "02:05"),
54            (datetime.timedelta(seconds=(3600 + 23 * 60 + 45.897898)), "1:23:46"),
55        ]
56        for ts, expected in decimal_precision_examples:
57            with self.subTest(ts=ts, expected=expected):
58                self.assertEqual(hhmmss(ts, decimal_precision=True), expected)
59        for ts, expected in non_decimal_precision_examples:
60            with self.subTest(ts=ts, expected=expected):
61                self.assertEqual(hhmmss(ts, decimal_precision=False), expected)
62
63    def test_period_to_seconds(self):
64        examples = [
65            ("02:05.000", 2 * 60 + 5),
66            ("1:23:45.898", 3600 + 23 * 60 + 45.898),
67            ("1.898", 1.898),
68            ("0.3", 0.3),
69            ("0", 0),
70            ("0:00", 0),
71            ("0:00:00", 0),
72            ("", 0),
73        ]
74        for ts, expected in examples:
75            with self.subTest(ts=ts, expected=expected):
76                self.assertEqual(period_to_seconds(ts), expected)
77
78
79if __name__ == "__main__":
80    unittest.main()
81