1
1
import operator
2
- from platform import architecture
3
2
4
3
import numpy as np
5
4
import pytest
6
5
7
6
from pandas ._libs import ops
8
7
8
+ from pandas .core .ops import (
9
+ radd ,
10
+ rsub ,
11
+ )
12
+
9
13
10
14
@pytest .fixture (name = "int_max" , scope = "module" )
11
15
def fixture_int_max () -> int :
@@ -17,16 +21,6 @@ def fixture_int_min() -> int:
17
21
return np .iinfo (np .int64 ).min
18
22
19
23
20
- @pytest .fixture (name = "float_max" , scope = "module" )
21
- def fixture_float_max () -> np .float64 :
22
- return np .finfo (np .float64 ).max
23
-
24
-
25
- @pytest .fixture (name = "float_min" , scope = "module" )
26
- def fixture_float_min () -> np .float64 :
27
- return np .finfo (np .float64 ).min
28
-
29
-
30
24
@pytest .fixture (name = "overflow_msg" , scope = "module" )
31
25
def fixture_overflow_msg () -> str :
32
26
return "|" .join (
@@ -37,116 +31,37 @@ def fixture_overflow_msg() -> str:
37
31
)
38
32
39
33
40
- class TestCalcIntInt :
41
- def test_raises_for_too_large_arg (self , int_max : int , overflow_msg : str ):
42
- with pytest .raises (OverflowError , match = overflow_msg ):
43
- ops .calc_int_int (operator .add , int_max + 1 , 1 )
44
-
45
- with pytest .raises (OverflowError , match = overflow_msg ):
46
- ops .calc_int_int (operator .add , 1 , int_max + 1 )
47
-
48
- def test_raises_for_too_small_arg (self , int_min : int , overflow_msg : str ):
49
- with pytest .raises (OverflowError , match = overflow_msg ):
50
- ops .calc_int_int (operator .add , int_min - 1 , 1 )
34
+ @pytest .fixture (name = "add_op" , params = (operator .add , radd ))
35
+ def fixture_add_op (request ):
36
+ return request .param
51
37
52
- with pytest .raises (OverflowError , match = overflow_msg ):
53
- ops .calc_int_int (operator .add , 1 , int_min - 1 )
54
38
55
- def test_raises_for_too_large_result ( self , int_max : int , overflow_msg : str ):
56
- with pytest . raises ( OverflowError , match = overflow_msg ):
57
- ops . calc_int_int ( operator . add , int_max , 1 )
39
+ @ pytest . fixture ( name = "sub_op" , params = ( operator . sub , rsub ))
40
+ def fixture_sub_op ( request ):
41
+ return request . param
58
42
59
- with pytest .raises (OverflowError , match = overflow_msg ):
60
- ops .calc_int_int (operator .add , 1 , int_max )
61
43
62
- def test_raises_for_too_small_result ( self , int_min : int , overflow_msg : str ) :
63
- with pytest . raises ( OverflowError , match = overflow_msg ):
64
- ops . calc_int_int ( operator . sub , int_min , 1 )
44
+ class TestCalcIntInt :
45
+ def test_raises_for_too_large_arg ( self , int_max : int , add_op , overflow_msg : str ):
46
+ add_op ( int_max + 1 , 1 )
65
47
66
48
with pytest .raises (OverflowError , match = overflow_msg ):
67
- ops .calc_int_int (operator . sub , 1 , int_min )
49
+ ops .calc_int_int (add_op , int_max + 1 , 1 )
68
50
51
+ def test_raises_for_too_small_arg (self , int_min : int , sub_op , overflow_msg : str ):
52
+ sub_op (int_min - 1 , 1 )
69
53
70
- class TestCalcIntFloat :
71
- @pytest .mark .parametrize (
72
- "op,lval,rval,expected" ,
73
- (
74
- (operator .add , 1 , 1.0 , 2 ),
75
- (operator .sub , 2 , 1.0 , 1 ),
76
- (operator .mul , 1 , 2.0 , 2 ),
77
- (operator .truediv , 1 , 0.5 , 2 ),
78
- ),
79
- ids = ("+" , "-" , "*" , "/" ),
80
- )
81
- def test_arithmetic_ops (self , op , lval : int , rval : float , expected : int ):
82
- result = ops .calc_int_float (op , lval , rval )
83
-
84
- assert result == expected
85
- assert isinstance (result , int )
86
-
87
- def test_raises_for_too_large_arg (
88
- self ,
89
- int_max : int ,
90
- float_max : float ,
91
- overflow_msg : str ,
92
- ):
93
54
with pytest .raises (OverflowError , match = overflow_msg ):
94
- ops .calc_int_float ( operator . add , int_max + 1 , 1 )
55
+ ops .calc_int_int ( sub_op , int_min - 1 , 1 )
95
56
96
- with pytest .raises (OverflowError , match = overflow_msg ):
97
- ops .calc_int_float (operator .add , 1 , float_max + 1 )
98
-
99
- def test_raises_for_too_small_arg (
100
- self ,
101
- int_min : int ,
102
- float_min : float ,
103
- overflow_msg : str ,
104
- ):
105
- with pytest .raises (OverflowError , match = overflow_msg ):
106
- ops .calc_int_float (operator .add , int_min - 1 , 1 )
57
+ def test_raises_for_too_large_result (self , int_max : int , add_op , overflow_msg : str ):
58
+ assert add_op (int_max , 1 ) == int_max + 1
107
59
108
60
with pytest .raises (OverflowError , match = overflow_msg ):
109
- ops .calc_int_float (operator .add , 1 , float_min - 1 )
110
-
111
- def test_raises_for_too_large_result (
112
- self ,
113
- int_max : int ,
114
- float_max : float ,
115
- overflow_msg : str ,
116
- ):
117
- with pytest .raises (OverflowError , match = overflow_msg ):
118
- ops .calc_int_float (operator .add , int_max , 1 )
61
+ ops .calc_int_int (add_op , int_max , 1 )
119
62
120
- with pytest . raises ( OverflowError , match = overflow_msg ):
121
- ops . calc_int_float ( operator . add , 1 , float_max )
63
+ def test_raises_for_too_small_result ( self , int_min : int , sub_op , overflow_msg : str ):
64
+ assert abs ( sub_op ( int_min , 1 )) == abs ( int_min - 1 )
122
65
123
- @pytest .mark .parametrize (
124
- "value" ,
125
- (
126
- pytest .param (
127
- 1024 ,
128
- marks = pytest .mark .xfail (
129
- reason = "TBD" ,
130
- raises = pytest .fail .Exception ,
131
- strict = True ,
132
- ),
133
- ),
134
- pytest .param (
135
- 1024.1 ,
136
- marks = pytest .mark .xfail (
137
- condition = architecture ()[0 ] == "32bit" ,
138
- reason = "overflows earlier" ,
139
- raises = pytest .fail .Exception ,
140
- strict = True ,
141
- ),
142
- ),
143
- ),
144
- )
145
- def test_raises_for_most_too_small_results (
146
- self ,
147
- value : float ,
148
- int_min : int ,
149
- overflow_msg : str ,
150
- ):
151
66
with pytest .raises (OverflowError , match = overflow_msg ):
152
- ops .calc_int_float ( operator . sub , int_min , value )
67
+ ops .calc_int_int ( sub_op , int_min , 1 )
0 commit comments