1+ import unittest
2+ import numpy as np
3+ from tabox .ta_func .ta_utils import (
4+ check_array , check_timeperiod , check_begidx1 , check_begidx2 ,
5+ check_begidx3 , check_begidx4 , check_length2 , check_length3 ,
6+ check_length4 , make_double_array
7+ )
8+
9+ class TestTAUtils (unittest .TestCase ):
10+
11+ def test_check_array (self ):
12+ """Test array validation function"""
13+ # Test numpy array input
14+ arr = np .array ([1.0 , 2.0 , 3.0 ], dtype = np .float64 )
15+ result = check_array (arr )
16+ np .testing .assert_array_equal (result , arr )
17+
18+ # Test list input
19+ lst = [1.0 , 2.0 , 3.0 ]
20+ result = check_array (lst )
21+ expected = np .array (lst , dtype = np .float64 )
22+ np .testing .assert_array_equal (result , expected )
23+
24+ # Test wrong dtype
25+ arr_int = np .array ([1 , 2 , 3 ], dtype = np .int32 )
26+ with self .assertRaises (Exception ):
27+ check_array (arr_int )
28+
29+ # Test wrong dimensions
30+ arr_2d = np .array ([[1.0 , 2.0 ], [3.0 , 4.0 ]], dtype = np .float64 )
31+ with self .assertRaises (Exception ):
32+ check_array (arr_2d )
33+
34+ def test_check_timeperiod (self ):
35+ """Test timeperiod validation"""
36+ # Valid timeperiod
37+ try :
38+ check_timeperiod (5 )
39+ except Exception :
40+ self .fail ("check_timeperiod raised exception for valid timeperiod" )
41+
42+ # Invalid timeperiods
43+ with self .assertRaises (Exception ):
44+ check_timeperiod (1 )
45+ with self .assertRaises (Exception ):
46+ check_timeperiod (0 )
47+ with self .assertRaises (Exception ):
48+ check_timeperiod (- 1 )
49+
50+ def test_check_begidx1 (self ):
51+ """Test single array begin index check"""
52+ # Normal array
53+ arr = np .array ([1.0 , 2.0 , 3.0 ], dtype = np .float64 )
54+ result = check_begidx1 (arr )
55+ self .assertEqual (result , 0 )
56+
57+ # Array with leading NaN
58+ arr_nan = np .array ([np .nan , np .nan , 1.0 , 2.0 ], dtype = np .float64 )
59+ result = check_begidx1 (arr_nan )
60+ self .assertEqual (result , 2 )
61+
62+ # All NaN array
63+ arr_all_nan = np .array ([np .nan , np .nan , np .nan ], dtype = np .float64 )
64+ with self .assertRaises (Exception ):
65+ check_begidx1 (arr_all_nan )
66+
67+ def test_check_begidx2 (self ):
68+ """Test dual array begin index check"""
69+ arr1 = np .array ([1.0 , 2.0 , 3.0 ], dtype = np .float64 )
70+ arr2 = np .array ([4.0 , 5.0 , 6.0 ], dtype = np .float64 )
71+ result = check_begidx2 (arr1 , arr2 )
72+ self .assertEqual (result , 0 )
73+
74+ # With NaN in first array
75+ arr1_nan = np .array ([np .nan , 2.0 , 3.0 ], dtype = np .float64 )
76+ arr2 = np .array ([4.0 , 5.0 , 6.0 ], dtype = np .float64 )
77+ result = check_begidx2 (arr1_nan , arr2 )
78+ self .assertEqual (result , 1 )
79+
80+ def test_check_length2 (self ):
81+ """Test dual array length validation"""
82+ arr1 = np .array ([1.0 , 2.0 , 3.0 ], dtype = np .float64 )
83+ arr2 = np .array ([4.0 , 5.0 , 6.0 ], dtype = np .float64 )
84+ length = check_length2 (arr1 , arr2 )
85+ self .assertEqual (length , 3 )
86+
87+ # Different lengths
88+ arr1 = np .array ([1.0 , 2.0 ], dtype = np .float64 )
89+ arr2 = np .array ([4.0 , 5.0 , 6.0 ], dtype = np .float64 )
90+ with self .assertRaises (Exception ):
91+ check_length2 (arr1 , arr2 )
92+
93+ def test_make_double_array (self ):
94+ """Test double array creation"""
95+ length = 5
96+ lookback = 2
97+ result = make_double_array (length , lookback )
98+
99+ self .assertEqual (len (result ), length )
100+ self .assertEqual (result .dtype , np .float64 )
101+
102+ # Check that first 'lookback' elements are NaN
103+ for i in range (lookback ):
104+ self .assertTrue (np .isnan (result [i ]))
105+
106+ # Check that remaining elements are not NaN (they should be uninitialized but not NaN)
107+ # Actually, np.empty might contain arbitrary values, so we just check length and dtype
108+
109+ if __name__ == '__main__' :
110+ unittest .main ()
0 commit comments