@@ -30,7 +30,7 @@ ForwardDiff.:≺(::Int, ::Type{TestTag()}) = false
3030ForwardDiff.:≺ (:: Type{TestTag} , :: Type{OuterTestTag} ) = true
3131ForwardDiff.:≺ (:: Type{OuterTestTag} , :: Type{TestTag} ) = false
3232
33- for N in (0 ,3 ), M in (0 ,4 ), V in (Int, Float32)
33+ @testset " Dual{Z, $V , $N } and Dual{Z,Dual{Z, $V , $M }, $N } " for N in (0 ,3 ), M in (0 ,4 ), V in (Int, Float32)
3434 println (" ...testing Dual{TestTag(),$V ,$N } and Dual{TestTag(),Dual{TestTag(),$V ,$M },$N }" )
3535
3636 PARTIALS = Partials {N,V} (ntuple (n -> intrand (V), N))
@@ -44,6 +44,13 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
4444 PARTIALS3 = Partials {N,V} (ntuple (n -> intrand (V), N))
4545 PRIMAL3 = intrand (V)
4646 FDNUM3 = Dual {TestTag()} (PRIMAL3, PARTIALS3)
47+
48+ if ! allunique ([PRIMAL, PRIMAL2, PRIMAL3])
49+ @info " testing with non-unique primals" PRIMAL PRIMAL2 PRIMAL3
50+ end
51+ if N > 0 && ! allunique ([PARTIALS, PARTIALS2, PARTIALS3])
52+ @info " testing with non-unique partials" PARTIALS PARTIALS2 PARTIALS3
53+ end
4754
4855 M_PARTIALS = Partials {M,V} (ntuple (m -> intrand (V), M))
4956 NESTED_PARTIALS = convert (Partials{N,Dual{TestTag (),V,M}}, PARTIALS)
@@ -231,15 +238,27 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
231238 @test ForwardDiff. isconstant (one (NESTED_FDNUM))
232239 @test ForwardDiff. isconstant (NESTED_FDNUM) == (N == 0 )
233240
234- @test isequal (FDNUM, Dual {TestTag()} (PRIMAL, PARTIALS2))
235- @test isequal (PRIMAL, PRIMAL2) == isequal (FDNUM, FDNUM2)
236-
237- @test isequal (NESTED_FDNUM, Dual {TestTag()} (Dual {TestTag()} (PRIMAL, M_PARTIALS2), NESTED_PARTIALS2))
238- @test isequal (PRIMAL, PRIMAL2) == isequal (NESTED_FDNUM, NESTED_FDNUM2)
239-
240- @test FDNUM == Dual {TestTag()} (PRIMAL, PARTIALS2)
241- @test (PRIMAL == PRIMAL2) == (FDNUM == FDNUM2)
242- @test (PRIMAL == PRIMAL2) == (NESTED_FDNUM == NESTED_FDNUM2)
241+ # Recall that FDNUM = Dual{TestTag()}(PRIMAL, PARTIALS) has N partials,
242+ # and FDNUM2 has everything with a 2, and all random numbers nonzero.
243+ # M is the length of M_PARTIALS, which affects:
244+ # NESTED_FDNUM = Dual{TestTag()}(Dual{TestTag()}(PRIMAL, M_PARTIALS), NESTED_PARTIALS)
245+
246+ @test (FDNUM == Dual {TestTag()} (PRIMAL, PARTIALS2)) == (PARTIALS == PARTIALS2)
247+ @test isequal (FDNUM, Dual {TestTag()} (PRIMAL, PARTIALS2)) == (PARTIALS == PARTIALS2)
248+ @test isequal (NESTED_FDNUM, Dual {TestTag()} (Dual {TestTag()} (PRIMAL, M_PARTIALS2), NESTED_PARTIALS2)) == ((M_PARTIALS == M_PARTIALS2) && (NESTED_PARTIALS == NESTED_PARTIALS2))
249+
250+ if PRIMAL == PRIMAL2
251+ @test isequal (FDNUM, Dual {TestTag()} (PRIMAL, PARTIALS2)) == (PARTIALS == PARTIALS2)
252+ @test isequal (FDNUM, FDNUM2) == (PARTIALS == PARTIALS2)
253+
254+ @test (FDNUM == FDNUM2) == (PARTIALS == PARTIALS2)
255+ @test (NESTED_FDNUM == NESTED_FDNUM2) == ((M_PARTIALS == M_PARTIALS2) && (NESTED_PARTIALS == NESTED_PARTIALS2))
256+ else
257+ @test ! isequal (FDNUM, FDNUM2)
258+
259+ @test FDNUM != FDNUM2
260+ @test NESTED_FDNUM != NESTED_FDNUM2
261+ end
243262
244263 @test isless (Dual {TestTag()} (1 , PARTIALS), Dual {TestTag()} (2 , PARTIALS2))
245264 @test ! (isless (Dual {TestTag()} (1 , PARTIALS), Dual {TestTag()} (1 , PARTIALS2)))
@@ -344,7 +363,7 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
344363 @test typeof (WIDE_NESTED_FDNUM) === Dual{TestTag (),Dual{TestTag (),WIDE_T,M},N}
345364
346365 @test value (WIDE_FDNUM) == PRIMAL
347- @test value (WIDE_NESTED_FDNUM) == PRIMAL
366+ @test ( value (WIDE_NESTED_FDNUM) == PRIMAL) == (M == 0 )
348367
349368 @test convert (Dual, FDNUM) === FDNUM
350369 @test convert (Dual, NESTED_FDNUM) === NESTED_FDNUM
@@ -395,6 +414,8 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
395414 # ----------#
396415
397416 if M > 0 && N > 0
417+ # Recall that FDNUM = Dual{TestTag()}(PRIMAL, PARTIALS) has N partials,
418+ # all random numbers nonzero, and FDNUM2 another draw. M only affects NESTED_FDNUM.
398419 @test Dual {1} (FDNUM) / Dual {1} (PRIMAL) === Dual {1} (FDNUM / PRIMAL)
399420 @test Dual {1} (PRIMAL) / Dual {1} (FDNUM) === Dual {1} (PRIMAL / FDNUM)
400421 @test_broken Dual {1} (FDNUM) / FDNUM2 === Dual {1} (FDNUM / FDNUM2)
@@ -413,6 +434,7 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
413434
414435 # Exponentiation #
415436 # ----------------#
437+
416438 # If V == Int, the LHS terms are Int's. Large inputs cause integer overflow
417439 # within the generic fallback of `isapprox`, resulting in a DomainError.
418440 # Promote to Float64 to avoid issues.
@@ -442,7 +464,7 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
442464 @test abs (NESTED_FDNUM) === NESTED_FDNUM
443465
444466 if V != Int
445- for (M, f, arity) in DiffRules. diffrules (filter_modules = nothing )
467+ @testset " $f " for (M, f, arity) in DiffRules. diffrules (filter_modules = nothing )
446468 if f in (:/ , :rem2pi )
447469 continue # Skip these rules
448470 elseif ! (isdefined (@__MODULE__ , M) && isdefined (getfield (@__MODULE__ , M), f))
@@ -502,10 +524,14 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
502524 else
503525 @test dx isa Complex{<: Dual{TestTag()} }
504526 @test dy isa Complex{<: Dual{TestTag()} }
505- @test real (value (dx)) == real (actualval)
506- @test real (value (dy)) == real (actualval)
507- @test imag (value (dx)) == imag (actualval)
508- @test imag (value (dy)) == imag (actualval)
527+ # @test real(value(dx)) == real(actualval)
528+ # @test real(value(dy)) == real(actualval)
529+ # @test imag(value(dx)) == imag(actualval)
530+ # @test imag(value(dy)) == imag(actualval)
531+ @test value (real (dx)) == real (actualval)
532+ @test value (real (dy)) == real (actualval)
533+ @test value (imag (dx)) == imag (actualval)
534+ @test value (imag (dy)) == imag (actualval)
509535 @test partials (real (dx), 1 ) ≈ real (actualdx) nans= true
510536 @test partials (real (dy), 1 ) ≈ real (actualdy) nans= true
511537 @test partials (imag (dx), 1 ) ≈ imag (actualdx) nans= true
@@ -568,6 +594,10 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
568594 end
569595end
570596
597+ # ############
598+ # bug fixes #
599+ # ############
600+
571601@testset " Exponentiation of zero" begin
572602 x0 = 0.0
573603 x1 = Dual {:t1} (x0, 1.0 )
0 commit comments