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