11module VarNamedTupleTests
22
3+ using Combinatorics: Combinatorics
34using Test: @inferred , @test , @test_throws , @testset
45using DynamicPPL: DynamicPPL, @varname , VarNamedTuple
56using DynamicPPL. VarNamedTuples: PartialArray
@@ -20,11 +21,13 @@ function test_invariants(vnt::VarNamedTuple)
2021 v = getindex (vnt, k)
2122 vnt2 = setindex!! (copy (vnt), v, k)
2223 @test vnt == vnt2
24+ @test isequal (vnt, vnt2)
2325 @test hash (vnt) == hash (vnt2)
2426 end
2527 # Check that the printed representation can be parsed back to an equal VarNamedTuple.
2628 vnt3 = eval (Meta. parse (repr (vnt)))
2729 @test vnt == vnt3
30+ @test isequal (vnt, vnt3)
2831 @test hash (vnt) == hash (vnt3)
2932 # Check that merge with an empty VarNamedTuple is a no-op.
3033 @test merge (vnt, VarNamedTuple ()) == vnt
@@ -218,40 +221,40 @@ end
218221 test_invariants (vnt)
219222 end
220223
221- @testset " equality" begin
222- vnt1 = VarNamedTuple ()
223- vnt2 = VarNamedTuple ()
224- @test vnt1 == vnt2
225-
226- vnt1 = setindex!! (vnt1, 1.0 , @varname (a))
227- @test vnt1 != vnt2
228-
229- vnt2 = setindex!! (vnt2 , 1.0 , @varname (a) )
230- @test vnt1 == vnt2
231-
232- vnt1 = setindex!! (vnt1, [ 1 , 2 ], @varname (b) )
233- vnt2 = setindex!! (vnt2, [ 1 , 2 ], @varname (b) )
234- @test vnt1 == vnt2
235-
236- vnt2 = setindex!! (vnt2, [ 1 , 3 ], @varname (b) )
237- @test vnt1 != vnt2
238- vnt2 = setindex!! (vnt2, [ 1 , 2 ], @varname (b) )
239-
240- # Try with index lenses too
241- vnt1 = setindex!! (vnt1, 2 , @varname (c[ 2 ]))
242- vnt2 = setindex!! (vnt2, 2 , @varname (c[ 2 ]) )
243- @test vnt1 == vnt2
244-
245- vnt2 = setindex!! (vnt2, 3 , @varname (c[ 2 ]) )
246- @test vnt1 != vnt2
247- vnt2 = setindex!! (vnt2, 2 , @varname (c[ 2 ]))
248-
249- vnt1 = setindex!! (vnt1, [ " a " , " b " ], @varname (d . e[ 1 : 2 ]))
250- vnt2 = setindex!! (vnt2, [ " a " , " b " ], @varname (d . e[ 1 : 2 ]))
251- @test vnt1 == vnt2
252-
253- vnt2 = setindex!! (vnt2, :b , @varname (d . e[ 2 ]))
254- @test vnt1 != vnt2
224+ @testset " equality and hash " begin
225+ # Test all combinations of having or not having the below values set, and having
226+ # them set to any of the possible_values, and check that isequal and == return the
227+ # expected value.
228+ # NOTE: Be very careful adding new values to these sets. The below test has three
229+ # nested loops over Combinatorics.combinations, the run time can explode very, very
230+ # quickly.
231+ varnames = ( @varname (b[ 1 ]), @varname (b[ 3 ]), @varname (c . d[ 2 ] . e))
232+ possible_values = ( missing , 1 , - 0 .0 , 0.0 )
233+ for vn_set in Combinatorics . combinations (varnames)
234+ valuesets1 = Combinatorics . with_replacement_combinations (
235+ possible_values, length (vn_set )
236+ )
237+ valuesets2 = Combinatorics . with_replacement_combinations (
238+ possible_values, length (vn_set)
239+ )
240+ for vset1 in valuesets1, vset2 in valuesets2
241+ vnt1 = VarNamedTuple ( )
242+ vnt2 = VarNamedTuple ()
243+ expected_isequal = true
244+ expected_doubleequal = true
245+ for (vn, v1, v2) in zip (vn_set, vset1, vset2 )
246+ vnt1 = setindex!! (vnt1, v1, vn)
247+ vnt2 = setindex!! (vnt2, v2, vn)
248+ expected_isequal = expected_isequal & isequal (v1, v2 )
249+ expected_doubleequal = expected_doubleequal & (v1 == v2)
250+ end
251+ @test isequal (vnt1, vnt2) == expected_isequal
252+ @test (vnt1 == vnt2) === expected_doubleequal
253+ if expected_isequal
254+ @test hash ( vnt1) == hash ( vnt2)
255+ end
256+ end
257+ end
255258 end
256259
257260 @testset " merge" begin
0 commit comments