@@ -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)
@@ -235,15 +242,27 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
235242 @test ForwardDiff. isconstant (one (NESTED_FDNUM))
236243 @test ForwardDiff. isconstant (NESTED_FDNUM) == (N == 0 )
237244
238- @test isequal (FDNUM, Dual {TestTag()} (PRIMAL, PARTIALS2))
239- @test isequal (PRIMAL, PRIMAL2) == isequal (FDNUM, FDNUM2)
240-
241- @test isequal (NESTED_FDNUM, Dual {TestTag()} (Dual {TestTag()} (PRIMAL, M_PARTIALS2), NESTED_PARTIALS2))
242- @test isequal (PRIMAL, PRIMAL2) == isequal (NESTED_FDNUM, NESTED_FDNUM2)
243-
244- @test FDNUM == Dual {TestTag()} (PRIMAL, PARTIALS2)
245- @test (PRIMAL == PRIMAL2) == (FDNUM == FDNUM2)
246- @test (PRIMAL == PRIMAL2) == (NESTED_FDNUM == NESTED_FDNUM2)
245+ # Recall that FDNUM = Dual{TestTag()}(PRIMAL, PARTIALS) has N partials,
246+ # and FDNUM2 has everything with a 2, and all random numbers nonzero.
247+ # M is the length of M_PARTIALS, which affects:
248+ # NESTED_FDNUM = Dual{TestTag()}(Dual{TestTag()}(PRIMAL, M_PARTIALS), NESTED_PARTIALS)
249+
250+ @test (FDNUM == Dual {TestTag()} (PRIMAL, PARTIALS2)) == (PARTIALS == PARTIALS2)
251+ @test isequal (FDNUM, Dual {TestTag()} (PRIMAL, PARTIALS2)) == (PARTIALS == PARTIALS2)
252+ @test isequal (NESTED_FDNUM, Dual {TestTag()} (Dual {TestTag()} (PRIMAL, M_PARTIALS2), NESTED_PARTIALS2)) == ((M_PARTIALS == M_PARTIALS2) && (NESTED_PARTIALS == NESTED_PARTIALS2))
253+
254+ if PRIMAL == PRIMAL2
255+ @test isequal (FDNUM, Dual {TestTag()} (PRIMAL, PARTIALS2)) == (PARTIALS == PARTIALS2)
256+ @test isequal (FDNUM, FDNUM2) == (PARTIALS == PARTIALS2)
257+
258+ @test (FDNUM == FDNUM2) == (PARTIALS == PARTIALS2)
259+ @test (NESTED_FDNUM == NESTED_FDNUM2) == ((M_PARTIALS == M_PARTIALS2) && (NESTED_PARTIALS == NESTED_PARTIALS2))
260+ else
261+ @test ! isequal (FDNUM, FDNUM2)
262+
263+ @test FDNUM != FDNUM2
264+ @test NESTED_FDNUM != NESTED_FDNUM2
265+ end
247266
248267 @test isless (Dual {TestTag()} (1 , PARTIALS), Dual {TestTag()} (2 , PARTIALS2))
249268 @test ! (isless (Dual {TestTag()} (1 , PARTIALS), Dual {TestTag()} (1 , PARTIALS2)))
@@ -348,7 +367,7 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
348367 @test typeof (WIDE_NESTED_FDNUM) === Dual{TestTag (),Dual{TestTag (),WIDE_T,M},N}
349368
350369 @test value (WIDE_FDNUM) == PRIMAL
351- @test value (WIDE_NESTED_FDNUM) == PRIMAL
370+ @test ( value (WIDE_NESTED_FDNUM) == PRIMAL) == (M == 0 )
352371
353372 @test convert (Dual, FDNUM) === FDNUM
354373 @test convert (Dual, NESTED_FDNUM) === NESTED_FDNUM
@@ -399,6 +418,8 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
399418 # ----------#
400419
401420 if M > 0 && N > 0
421+ # Recall that FDNUM = Dual{TestTag()}(PRIMAL, PARTIALS) has N partials,
422+ # all random numbers nonzero, and FDNUM2 another draw. M only affects NESTED_FDNUM.
402423 @test Dual {1} (FDNUM) / Dual {1} (PRIMAL) === Dual {1} (FDNUM / PRIMAL)
403424 @test Dual {1} (PRIMAL) / Dual {1} (FDNUM) === Dual {1} (PRIMAL / FDNUM)
404425 @test_broken Dual {1} (FDNUM) / FDNUM2 === Dual {1} (FDNUM / FDNUM2)
@@ -417,6 +438,7 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
417438
418439 # Exponentiation #
419440 # ----------------#
441+
420442 # If V == Int, the LHS terms are Int's. Large inputs cause integer overflow
421443 # within the generic fallback of `isapprox`, resulting in a DomainError.
422444 # Promote to Float64 to avoid issues.
@@ -446,7 +468,7 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
446468 @test abs (NESTED_FDNUM) === NESTED_FDNUM
447469
448470 if V != Int
449- for (M, f, arity) in DiffRules. diffrules (filter_modules = nothing )
471+ @testset " $f " for (M, f, arity) in DiffRules. diffrules (filter_modules = nothing )
450472 if f in (:/ , :rem2pi )
451473 continue # Skip these rules
452474 elseif ! (isdefined (@__MODULE__ , M) && isdefined (getfield (@__MODULE__ , M), f))
@@ -506,10 +528,14 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
506528 else
507529 @test dx isa Complex{<: Dual{TestTag()} }
508530 @test dy isa Complex{<: Dual{TestTag()} }
509- @test real (value (dx)) == real (actualval)
510- @test real (value (dy)) == real (actualval)
511- @test imag (value (dx)) == imag (actualval)
512- @test imag (value (dy)) == imag (actualval)
531+ # @test real(value(dx)) == real(actualval)
532+ # @test real(value(dy)) == real(actualval)
533+ # @test imag(value(dx)) == imag(actualval)
534+ # @test imag(value(dy)) == imag(actualval)
535+ @test value (real (dx)) == real (actualval)
536+ @test value (real (dy)) == real (actualval)
537+ @test value (imag (dx)) == imag (actualval)
538+ @test value (imag (dy)) == imag (actualval)
513539 @test partials (real (dx), 1 ) ≈ real (actualdx) nans= true
514540 @test partials (real (dy), 1 ) ≈ real (actualdy) nans= true
515541 @test partials (imag (dx), 1 ) ≈ imag (actualdx) nans= true
@@ -572,6 +598,10 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
572598 end
573599end
574600
601+ # ############
602+ # bug fixes #
603+ # ############
604+
575605@testset " Exponentiation of zero" begin
576606 x0 = 0.0
577607 x1 = Dual {:t1} (x0, 1.0 )
0 commit comments