main
py 71 lines 1.73 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 import pytest
4
5 from vllm.utils.collection_utils import (
6 common_prefix,
7 is_list_of_numbers,
8 swap_dict_values,
9 )
10
11
12 @pytest.mark.parametrize(
13 "value,expected",
14 [
15 ([], True),
16 ([1, -2, 2**1024], True),
17 ([1, 0.5], True),
18 ([1, True], False),
19 ([1, float("nan")], False),
20 ([1, float("inf")], False),
21 ([1, -float("inf")], False),
22 ([1, "2"], False),
23 ([[1]], False),
24 ((1, 2), False),
25 (None, False),
26 ],
27 )
28 def test_is_list_of_numbers_checks_all_finite_non_boolean_items(value, expected):
29 assert is_list_of_numbers(value) is expected
30
31
32 @pytest.mark.parametrize(
33 ("inputs", "expected_output"),
34 [
35 ([""], ""),
36 (["a"], "a"),
37 (["a", "b"], ""),
38 (["a", "ab"], "a"),
39 (["a", "ab", "b"], ""),
40 (["abc", "a", "ab"], "a"),
41 (["aba", "abc", "ab"], "ab"),
42 ],
43 )
44 def test_common_prefix(inputs, expected_output):
45 assert common_prefix(inputs) == expected_output
46
47
48 @pytest.mark.parametrize(
49 ("obj", "key1", "key2"),
50 [
51 # Tests for both keys exist
52 ({1: "a", 2: "b"}, 1, 2),
53 # Tests for one key does not exist
54 ({1: "a", 2: "b"}, 1, 3),
55 # Tests for both keys do not exist
56 ({1: "a", 2: "b"}, 3, 4),
57 ],
58 )
59 def test_swap_dict_values(obj, key1, key2):
60 original_obj = obj.copy()
61
62 swap_dict_values(obj, key1, key2)
63
64 if key1 in original_obj:
65 assert obj[key2] == original_obj[key1]
66 else:
67 assert key2 not in obj
68 if key2 in original_obj:
69 assert obj[key1] == original_obj[key2]
70 else:
71 assert key1 not in obj