|
| 1 | +from typing import List, Union |
| 2 | + |
| 3 | +import pytest |
| 4 | +import ulist as ul |
| 5 | +import operator as op |
| 6 | +from ulist.utils import check_test_result |
| 7 | + |
| 8 | +LIST_TYPE = Union[List[float], List[int], List[bool]] |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.parametrize( |
| 12 | + "dtype, nums, kwargs, expected_value", |
| 13 | + [ |
| 14 | + ( |
| 15 | + "float", |
| 16 | + [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], |
| 17 | + { |
| 18 | + "conditions": [(op.lt, 2.0), (op.lt, 4.0)], |
| 19 | + "choices": [False, True], |
| 20 | + "default": False, |
| 21 | + }, |
| 22 | + [False, False, True, True, False, False], |
| 23 | + ), |
| 24 | + ( |
| 25 | + "float", |
| 26 | + [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], |
| 27 | + { |
| 28 | + "conditions": [(op.lt, 2.0), (op.lt, 4.0)], |
| 29 | + "choices": [0.0, 1.0], |
| 30 | + "default": 2.0, |
| 31 | + }, |
| 32 | + [0.0, 0.0, 1.0, 1.0, 2.0, 2.0], |
| 33 | + ), |
| 34 | + ( |
| 35 | + "float", |
| 36 | + [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], |
| 37 | + { |
| 38 | + "conditions": [(op.lt, 2.0), (op.lt, 4.0)], |
| 39 | + "choices": [0, 1], |
| 40 | + "default": 2, |
| 41 | + }, |
| 42 | + [0, 0, 1, 1, 2, 2], |
| 43 | + ), |
| 44 | +
|
| 45 | + ( |
| 46 | + "int", |
| 47 | + [0, 1, 2, 3, 4, 5], |
| 48 | + { |
| 49 | + "conditions": [(op.lt, 2), (op.lt, 4)], |
| 50 | + "choices": [False, True], |
| 51 | + "default": False, |
| 52 | + }, |
| 53 | + [False, False, True, True, False, False], |
| 54 | + ), |
| 55 | + ( |
| 56 | + "int", |
| 57 | + [0, 1, 2, 3, 4, 5], |
| 58 | + { |
| 59 | + "conditions": [(op.lt, 2), (op.lt, 4)], |
| 60 | + "choices": [0.0, 1.0], |
| 61 | + "default": 2.0, |
| 62 | + }, |
| 63 | + [0.0, 0.0, 1.0, 1.0, 2.0, 2.0], |
| 64 | + ), |
| 65 | + ( |
| 66 | + "int", |
| 67 | + [0, 1, 2, 3, 4, 5], |
| 68 | + { |
| 69 | + "conditions": [(op.lt, 2), (op.lt, 4)], |
| 70 | + "choices": [0, 1], |
| 71 | + "default": 2, |
| 72 | + }, |
| 73 | + [0, 0, 1, 1, 2, 2], |
| 74 | + ), |
| 75 | + ], |
| 76 | +) |
| 77 | +def test_select( |
| 78 | + dtype: str, |
| 79 | + nums: LIST_TYPE, |
| 80 | + kwargs: dict, |
| 81 | + expected_value: List[bool], |
| 82 | +) -> None: |
| 83 | + arr = ul.from_seq(nums, dtype) |
| 84 | + choices = kwargs["choices"] |
| 85 | + default = kwargs["default"] |
| 86 | + conditions = [f(arr, v) for f, v in kwargs["conditions"]] |
| 87 | + result = ul.select(conditions, choices=choices, default=default) |
| 88 | + if type(expected_value[0]) == int: |
| 89 | + dtype = "int" |
| 90 | + elif type(expected_value[0]) == float: |
| 91 | + dtype = "float" |
| 92 | + elif type(expected_value[0]) == bool: |
| 93 | + dtype = "bool" |
| 94 | + else: |
| 95 | + raise TypeError(f"Unexpected type {type(expected_value[0])}!") |
| 96 | + check_test_result(dtype, "ul.select", result, expected_value) |
0 commit comments