@@ -84,6 +84,139 @@ using Test
84
84
@test isnan (NaNMath. max (NaN ))
85
85
@test NaNMath. max (NaN , NaN , 0.0 , 1.0 ) == 1.0
86
86
87
+ # # Based on https://github.com/sethaxen/NaNMath.jl/blob/41b3e7edd9dd4cb6c2873abf6e0d90acf43138ec/test/runtests.jl
88
+ @testset " findmin/findmax" begin
89
+ if VERSION ≥ v " 1.7"
90
+ xvals = [
91
+ [1. , 2. , 3. , 3. , 1. ],
92
+ (1. , 2. , 3. , 3. , .1 ),
93
+ (1f0 , 2f0 , 3f0 , - 1f0 ),
94
+ (x= 1.0 , y= 3f0 , z= - 4.0 , w= - 2f0 ),
95
+ Dict (:a => 1.0 , :b => 1.0 , :d => 3.0 , :c => 2.0 ),
96
+ ]
97
+ @testset for x in xvals
98
+ @test NaNMath. findmin (x) === findmin (x)
99
+ @test NaNMath. findmax (x) === findmax (x)
100
+ @test NaNMath. findmin (identity, x) === findmin (identity, x)
101
+ @test NaNMath. findmax (identity, x) === findmax (identity, x)
102
+ @test NaNMath. findmin (sin, x) === findmin (sin, x)
103
+ @test NaNMath. findmax (sin, x) === findmax (sin, x)
104
+ end
105
+ end
106
+
107
+ x = [7 , 7 , NaN , 1 , 1 , NaN ]
108
+ @test NaNMath. findmin (x) === (1.0 , 4 )
109
+ @test NaNMath. findmax (x) === (7.0 , 1 )
110
+ @test NaNMath. findmin (identity, x) === (1.0 , 4 )
111
+ @test NaNMath. findmax (identity, x) === (7.0 , 1 )
112
+ @test NaNMath. findmin (- , x) === (- 7.0 , 1 )
113
+ @test NaNMath. findmax (- , x) === (- 1.0 , 4 )
114
+
115
+ x = [NaN , NaN ]
116
+ @test NaNMath. findmin (x) === (NaN , 1 )
117
+ @test NaNMath. findmax (x) === (NaN , 1 )
118
+ @test NaNMath. findmin (identity, x) === (NaN , 1 )
119
+ @test NaNMath. findmax (identity, x) === (NaN , 1 )
120
+ @test NaNMath. findmin (sin, x) === (NaN , 1 )
121
+ @test NaNMath. findmax (sin, x) === (NaN , 1 )
122
+
123
+ x = Dict (:a => 1.0 , :b => 1 + 2im , :d => 3.0 , :c => 2.0 )
124
+ @test_throws ErrorException NaNMath. findmin (x)
125
+ @test_throws ErrorException NaNMath. findmax (x)
126
+
127
+ x = [3 , missing , NaN , - 1 ]
128
+ @test_throws ErrorException NaNMath. findmin (x)
129
+
130
+ x = Dict (' a' => 1.0 , missing => NaN , ' c' => 2.0 )
131
+ @test NaNMath. findmin (x) === (1.0 , ' a' )
132
+ @test NaNMath. findmax (x) === (2.0 , ' c' )
133
+
134
+ x = Dict (:x => 3.0 , :w => 2f0 , :y => - 1.0 , :z => NaN )
135
+ @test NaNMath. findmin (x) === (- 1.0 , :y )
136
+ @test NaNMath. findmax (x) === (3.0 , :x )
137
+ @test NaNMath. findmin (identity, x) === (- 1.0 , :y )
138
+ @test NaNMath. findmax (identity, x) === (3.0 , :x )
139
+ @test NaNMath. findmin (- , x) === (- 3.0 , :x )
140
+ @test NaNMath. findmax (- , x) === (1.0 , :y )
141
+ @test NaNMath. findmin (exp, x) === (exp (- 1.0 ), :y )
142
+ @test NaNMath. findmax (exp, x) === (exp (3.0 ), :x )
143
+
144
+ x = (x= 1.0 , y= NaN , z= NaN , w= - 2.0 )
145
+ @test NaNMath. findmin (x) === (- 2.0 , :w )
146
+ @test NaNMath. findmax (x) === (1.0 , :x )
147
+ @test NaNMath. findmin (- ,x) === (- 1.0 , :x )
148
+ @test NaNMath. findmax (- ,x) === (2.0 , :w )
149
+
150
+ x = [2.0 3.0 ; 2.0 - 1.0 ]
151
+ @test NaNMath. findmin (x) === (- 1.0 , CartesianIndex (2 , 2 ))
152
+ @test NaNMath. findmax (x) === (3.0 , CartesianIndex (1 , 2 ))
153
+ @test NaNMath. findmin (exp,x) === (exp (- 1 ), CartesianIndex (2 , 2 ))
154
+ @test NaNMath. findmax (exp,x) === (exp (3.0 ), CartesianIndex (1 , 2 ))
155
+ end
156
+
157
+ @testset " argmin/argmax" begin
158
+ if VERSION ≥ v " 1.7"
159
+ xvals = [
160
+ [1. , 2. , 4. , 3. , 1. ],
161
+ (1. , 2. , 4. , 3. , .1 ),
162
+ (1f0 , 2f0 , 3f0 , - 1f0 ),
163
+ (x= 1.0 , y= 3f0 , z= - 4.0 , w= - 2f0 ),
164
+ Dict (:a => 1.0 , :b => 1.0 , :d => 3.0 , :c => 2.0 ),
165
+ ]
166
+ @testset for x in xvals
167
+ @test NaNMath. argmin (x) === argmin (x)
168
+ @test NaNMath. argmax (x) === argmax (x)
169
+ x isa Dict || @test NaNMath. argmin (identity, x) === argmin (identity, x)
170
+ x isa Dict || @test NaNMath. argmax (identity, x) === argmax (identity, x)
171
+ x isa Dict || @test NaNMath. argmin (sin, x) === argmin (sin, x)
172
+ x isa Dict || @test NaNMath. argmax (sin, x) === argmax (sin, x)
173
+ end
174
+ end
175
+ x = [7 , 7 , NaN , 1 , 1 , NaN ]
176
+ @test NaNMath. argmin (x) === 4
177
+ @test NaNMath. argmax (x) === 1
178
+ @test NaNMath. argmin (identity, x) === 1.0
179
+ @test NaNMath. argmax (identity, x) === 7.0
180
+ @test NaNMath. argmin (- , x) === 7.0
181
+ @test NaNMath. argmax (- , x) === 1.0
182
+
183
+ x = [NaN , NaN ]
184
+ @test NaNMath. argmin (x) === 1
185
+ @test NaNMath. argmax (x) === 1
186
+ @test NaNMath. argmin (identity, x) === NaN
187
+ @test NaNMath. argmax (identity, x) === NaN
188
+ @test NaNMath. argmin (- , x) === NaN
189
+ @test NaNMath. argmax (- , x) === NaN
190
+
191
+ x = [3 , missing , NaN , - 1 ]
192
+ @test_throws ErrorException NaNMath. argmin (x)
193
+ @test_throws ErrorException NaNMath. argmax (x)
194
+
195
+ x = Dict (' a' => 1.0 , missing => NaN , ' c' => 2.0 )
196
+ @test NaNMath. argmin (x) === ' a'
197
+ @test NaNMath. argmax (x) === ' c'
198
+
199
+ x = Dict (:v => NaN , :w => 2.1f0 , :x => 3.1 , :z => - 1.0 , :y => NaN )
200
+ @test NaNMath. argmin (x) === :z
201
+ @test NaNMath. argmax (x) === :x
202
+ @test NaNMath. argmin (- , x) === 3.1
203
+ @test NaNMath. argmax (- , x) === - 1.0
204
+ @test NaNMath. argmin (exp, x) === - 1.0
205
+ @test NaNMath. argmax (exp, x) === 3.1
206
+
207
+ x = (x= 1.1 , y= NaN , z= NaN , w= - 2.3 )
208
+ @test NaNMath. argmin (x) === :w
209
+ @test NaNMath. argmax (x) === :x
210
+ @test NaNMath. argmin (exp, x) === - 2.3
211
+ @test NaNMath. argmax (exp, x) === 1.1
212
+
213
+ x = [2.0 3.0 ; 2.0 - 1.0 ]
214
+ @test NaNMath. argmin (x) === CartesianIndex (2 , 2 )
215
+ @test NaNMath. argmax (x) === CartesianIndex (1 , 2 )
216
+ @test NaNMath. argmin (exp,x) === - 1.0
217
+ @test NaNMath. argmax (exp,x) === 3.0
218
+ end
219
+
87
220
# Test forwarding
88
221
x = 1 + 2im
89
222
for f in (:sin , :cos , :tan , :asin , :acos , :acosh , :atanh , :log , :log2 , :log10 ,
0 commit comments