TestNumericTools.test_clamp()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
# SPDX-FileCopyrightText: Copyright 2020-2023, Contributors to pocketutils
2
# SPDX-PackageHomePage: https://github.com/dmyersturnbull/pocketutils
3
# SPDX-License-Identifier: Apache-2.0
4
from typing import Self
5
6
import numpy as np
7
import pytest
8
from pocketutils.tools.numeric_tools import NumericTools
9
10
11
class TestNumericTools:
12
    def test_slice(self: Self) -> None:
13
        arr = np.arange(5)
14
        assert list(NumericTools.slice(arr, 0, 1)) == [0]
15
        assert list(NumericTools.slice(arr, 2, 4)) == [2, 3]
16
        assert list(NumericTools.slice(arr, 0, 8)) == list(arr)
17
        assert list(NumericTools.slice(arr, None, None)) == list(arr)
18
        # from reverse direction
19
        assert list(NumericTools.slice(arr, -1, 3)) == []
20
        assert list(NumericTools.slice(arr, 1, -2)) == [1, 2, 3, 4]
21
22
    def test_clamp(self: Self) -> None:
23
        arr = [-11, -10, 0, 10, 11]
24
        assert list(NumericTools.clamp(arr, -10, 10)) == [-10, -10, 0, 10, 10]
25
26
27
if __name__ == "__main__":
28
    pytest.main()
29