@@ -490,4 +490,127 @@ function test_ambiguity_isassigned()
490
490
return
491
491
end
492
492
493
+ function test_containers_denseaxisarray_setindex_vector ()
494
+ A = Containers. DenseAxisArray (zeros (3 ), 1 : 3 )
495
+ A[2 : 3 ] .= 1.0
496
+ @test A. data == [0.0 , 1.0 , 1.0 ]
497
+ A = Containers. DenseAxisArray (zeros (3 ), 1 : 3 )
498
+ A[[2 , 3 ]] .= 1.0
499
+ @test A. data == [0.0 , 1.0 , 1.0 ]
500
+ A = Containers. DenseAxisArray (zeros (3 ), 1 : 3 )
501
+ A[[1 , 3 ]] .= 1.0
502
+ @test A. data == [1.0 , 0.0 , 1.0 ]
503
+ A = Containers. DenseAxisArray (zeros (3 ), 1 : 3 )
504
+ A[[2 ]] .= 1.0
505
+ @test A. data == [0.0 , 1.0 , 0.0 ]
506
+ A[2 : 3 ] = Containers. DenseAxisArray ([2.0 , 3.0 ], 2 : 3 )
507
+ @test A. data == [0.0 , 2.0 , 3.0 ]
508
+ A = Containers. DenseAxisArray (zeros (3 ), 1 : 3 )
509
+ A[:] .= 1.0
510
+ @test A. data == [1.0 , 1.0 , 1.0 ]
511
+ return
512
+ end
513
+
514
+ function test_containers_denseaxisarray_setindex_matrix ()
515
+ A = Containers. DenseAxisArray (zeros (3 , 3 ), 1 : 3 , [:a , :b , :c ])
516
+ A[:, [:a , :b ]] .= 1.0
517
+ @test A. data == [1.0 1.0 0.0 ; 1.0 1.0 0.0 ; 1.0 1.0 0.0 ]
518
+ A = Containers. DenseAxisArray (zeros (3 , 3 ), 1 : 3 , [:a , :b , :c ])
519
+ A[2 : 3 , [:a , :b ]] .= 1.0
520
+ @test A. data == [0.0 0.0 0.0 ; 1.0 1.0 0.0 ; 1.0 1.0 0.0 ]
521
+ A = Containers. DenseAxisArray (zeros (3 , 3 ), 1 : 3 , [:a , :b , :c ])
522
+ A[3 : 3 , [:a , :b ]] .= 1.0
523
+ @test A. data == [0.0 0.0 0.0 ; 0.0 0.0 0.0 ; 1.0 1.0 0.0 ]
524
+ A = Containers. DenseAxisArray (zeros (3 , 3 ), 1 : 3 , [:a , :b , :c ])
525
+ A[[1 , 3 ], [:a , :b ]] .= 1.0
526
+ @test A. data == [1.0 1.0 0.0 ; 0.0 0.0 0.0 ; 1.0 1.0 0.0 ]
527
+ A = Containers. DenseAxisArray (zeros (3 , 3 ), 1 : 3 , [:a , :b , :c ])
528
+ A[[1 , 3 ], [:a , :c ]] .= 1.0
529
+ @test A. data == [1.0 0.0 1.0 ; 0.0 0.0 0.0 ; 1.0 0.0 1.0 ]
530
+ return
531
+ end
532
+
533
+ function test_containers_denseaxisarray_view ()
534
+ A = Containers. DenseAxisArray (zeros (3 , 3 ), 1 : 3 , [:a , :b , :c ])
535
+ B = view (A, :, [:a , :b ])
536
+ @test_throws KeyError view (A, :, [:d ])
537
+ @test size (B) == (3 , 2 )
538
+ @test B[1 , :a ] == A[1 , :a ]
539
+ @test B[3 , :a ] == A[3 , :a ]
540
+ @test_throws KeyError B[3 , :c ]
541
+ @test sprint (show, B) == sprint (show, B. data)
542
+ @test sprint (Base. print_array, B) == sprint (show, B. data)
543
+ @test sprint (Base. summary, B) ==
544
+ " view(::DenseAxisArray, 1:3, [:a, :b]), over"
545
+ return
546
+ end
547
+
548
+ function test_containers_denseaxisarray_jump_3151 ()
549
+ D = Containers. DenseAxisArray (zeros (3 ), [:a , :b , :c ])
550
+ E = Containers. DenseAxisArray (ones (3 ), [:a , :b , :c ])
551
+ I = [:a , :b ]
552
+ D[I] = E[I]
553
+ @test D. data == [1.0 , 1.0 , 0.0 ]
554
+ D = Containers. DenseAxisArray (zeros (3 ), [:a , :b , :c ])
555
+ I = [:b , :c ]
556
+ D[I] = E[I]
557
+ @test D. data == [0.0 , 1.0 , 1.0 ]
558
+ D = Containers. DenseAxisArray (zeros (3 ), [:a , :b , :c ])
559
+ I = [:a , :c ]
560
+ D[I] = E[I]
561
+ @test D. data == [1.0 , 0.0 , 1.0 ]
562
+ return
563
+ end
564
+
565
+ function test_containers_denseaxisarray_view_operations ()
566
+ c = Containers. @container ([i = 1 : 4 , j = 2 : 3 ], i + 2 * j)
567
+ d = view (c, 2 : 3 , :)
568
+ @test sum (c) == 60
569
+ @test sum (d) == 30
570
+ d .= 1
571
+ @test sum (d) == 4
572
+ @test sum (c) == 34
573
+ return
574
+ end
575
+
576
+ function test_containers_denseaxisarray_view_addition ()
577
+ c = Containers. @container ([i = 1 : 4 , j = 2 : 3 ], i + 2 * j)
578
+ d = view (c, 2 : 3 , :)
579
+ @test_throws MethodError d + d
580
+ return
581
+ end
582
+
583
+ function test_containers_denseaxisarray_view_colon ()
584
+ c = Containers. @container ([i = 1 : 4 , j = 2 : 3 ], i + 2 * j)
585
+ d = view (c, 2 : 3 , :)
586
+ @test d[:, 2 ] == Containers. @container ([i = 2 : 3 ], i + 2 * 2 )
587
+ return
588
+ end
589
+
590
+ function test_containers_denseaxisarray_setindex_invalid ()
591
+ c = Containers. @container ([i = 1 : 4 , j = 2 : 3 ], 0 )
592
+ d = Containers. @container ([i = 1 : 4 , j = 2 : 3 ], i + 2 * j)
593
+ setindex! (c, d, 1 : 4 , 2 : 3 )
594
+ @test c == d
595
+ c .= 0
596
+ setindex! (c, d, 1 : 4 , 2 : 2 )
597
+ @test c == Containers. @container ([i = 1 : 4 , j = 2 : 3 ], (4 + i) * (j == 2 ))
598
+ d = Containers. @container ([i = 5 : 6 , j = 2 : 3 ], i + 2 * j)
599
+ @test_throws KeyError setindex! (c, d, 1 : 4 , 2 : 3 )
600
+ return
601
+ end
602
+
603
+ function test_containers_denseaxisarray_setindex_keys ()
604
+ c = Containers. @container ([i = 1 : 4 , j = 2 : 3 ], 0 )
605
+ for (i, k) in enumerate (keys (c))
606
+ c[k] = c[k] + i
607
+ end
608
+ @test c == Containers. @container ([i = 1 : 4 , j = 2 : 3 ], 4 * (j - 2 ) + i)
609
+ for (i, k) in enumerate (keys (c))
610
+ c[k] = c[k] + i
611
+ end
612
+ @test c == Containers. @container ([i = 1 : 4 , j = 2 : 3 ], 2 * (4 * (j - 2 ) + i))
613
+ return
614
+ end
615
+
493
616
end # module
0 commit comments