tests.pocketutils.tools.test_numeric_tools   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 19
dl 0
loc 29
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A TestNumericTools.test_clamp() 0 3 1
A TestNumericTools.test_slice() 0 9 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