@@ -489,6 +489,123 @@ function test_DenseAxisArray_vector_keys()
489
489
return
490
490
end
491
491
492
+ function test_containers_denseaxisarray_setindex_vector ()
493
+ A = Containers. DenseAxisArray (zeros (3 ), 1 : 3 )
494
+ A[2 : 3 ] .= 1.0
495
+ @test A. data == [0.0 , 1.0 , 1.0 ]
496
+ A = Containers. DenseAxisArray (zeros (3 ), 1 : 3 )
497
+ A[[2 , 3 ]] .= 1.0
498
+ @test A. data == [0.0 , 1.0 , 1.0 ]
499
+ A = Containers. DenseAxisArray (zeros (3 ), 1 : 3 )
500
+ A[[1 , 3 ]] .= 1.0
501
+ @test A. data == [1.0 , 0.0 , 1.0 ]
502
+ A = Containers. DenseAxisArray (zeros (3 ), 1 : 3 )
503
+ A[[2 ]] .= 1.0
504
+ @test A. data == [0.0 , 1.0 , 0.0 ]
505
+ A[2 : 3 ] = Containers. DenseAxisArray ([2.0 , 3.0 ], 2 : 3 )
506
+ @test A. data == [0.0 , 2.0 , 3.0 ]
507
+ A = Containers. DenseAxisArray (zeros (3 ), 1 : 3 )
508
+ A[:] .= 1.0
509
+ @test A. data == [1.0 , 1.0 , 1.0 ]
510
+ return
511
+ end
512
+
513
+ function test_containers_denseaxisarray_setindex_matrix ()
514
+ A = Containers. DenseAxisArray (zeros (3 , 3 ), 1 : 3 , [:a , :b , :c ])
515
+ A[:, [:a , :b ]] .= 1.0
516
+ @test A. data == [1.0 1.0 0.0 ; 1.0 1.0 0.0 ; 1.0 1.0 0.0 ]
517
+ A = Containers. DenseAxisArray (zeros (3 , 3 ), 1 : 3 , [:a , :b , :c ])
518
+ A[2 : 3 , [:a , :b ]] .= 1.0
519
+ @test A. data == [0.0 0.0 0.0 ; 1.0 1.0 0.0 ; 1.0 1.0 0.0 ]
520
+ A = Containers. DenseAxisArray (zeros (3 , 3 ), 1 : 3 , [:a , :b , :c ])
521
+ A[3 : 3 , [:a , :b ]] .= 1.0
522
+ @test A. data == [0.0 0.0 0.0 ; 0.0 0.0 0.0 ; 1.0 1.0 0.0 ]
523
+ A = Containers. DenseAxisArray (zeros (3 , 3 ), 1 : 3 , [:a , :b , :c ])
524
+ A[[1 , 3 ], [:a , :b ]] .= 1.0
525
+ @test A. data == [1.0 1.0 0.0 ; 0.0 0.0 0.0 ; 1.0 1.0 0.0 ]
526
+ A = Containers. DenseAxisArray (zeros (3 , 3 ), 1 : 3 , [:a , :b , :c ])
527
+ A[[1 , 3 ], [:a , :c ]] .= 1.0
528
+ @test A. data == [1.0 0.0 1.0 ; 0.0 0.0 0.0 ; 1.0 0.0 1.0 ]
529
+ return
530
+ end
531
+
532
+ function test_containers_denseaxisarray_view ()
533
+ A = Containers. DenseAxisArray (zeros (3 , 3 ), 1 : 3 , [:a , :b , :c ])
534
+ B = view (A, :, [:a , :b ])
535
+ @test_throws KeyError view (A, :, [:d ])
536
+ @test size (B) == (3 , 2 )
537
+ @test B[1 , :a ] == A[1 , :a ]
538
+ @test B[3 , :a ] == A[3 , :a ]
539
+ # Views are weird, because we can still access the underlying array?
540
+ @test B[3 , :c ] == A[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_setindex_invalid ()
584
+ c = Containers. @container ([i = 1 : 4 , j = 2 : 3 ], 0 )
585
+ d = Containers. @container ([i = 1 : 4 , j = 2 : 3 ], i + 2 * j)
586
+ setindex! (c, d, 1 : 4 , 2 : 3 )
587
+ @test c == d
588
+ c .= 0
589
+ setindex! (c, d, 1 : 4 , 2 : 2 )
590
+ @test c == Containers. @container ([i = 1 : 4 , j = 2 : 3 ], (4 + i) * (j == 2 ))
591
+ d = Containers. @container ([i = 5 : 6 , j = 2 : 3 ], i + 2 * j)
592
+ @test_throws KeyError setindex! (c, d, 1 : 4 , 2 : 3 )
593
+ return
594
+ end
595
+
596
+ function test_containers_denseaxisarray_setindex_keys ()
597
+ c = Containers. @container ([i = 1 : 4 , j = 2 : 3 ], 0 )
598
+ for (i, k) in enumerate (keys (c))
599
+ c[k] = c[k] + i
600
+ end
601
+ @test c == Containers. @container ([i = 1 : 4 , j = 2 : 3 ], 4 * (j - 2 ) + i)
602
+ for (i, k) in enumerate (keys (c))
603
+ c[k] = c[k] + i
604
+ end
605
+ @test c == Containers. @container ([i = 1 : 4 , j = 2 : 3 ], 2 * (4 * (j - 2 ) + i))
606
+ return
607
+ end
608
+
492
609
end # module
493
610
494
611
TestContainersDenseAxisArray. runtests ()
0 commit comments