@@ -552,6 +552,11 @@ using Infinities
552552 @test maybe_cacheddata (B) === cacheddata (B)
553553 C = [1 , 2 , 3 ]
554554 @test maybe_cacheddata (C) === C
555+
556+ v = cache (1 : 10 )'
557+ @test maybe_cacheddata (v) === cacheddata (parent (v))'
558+ v = transpose (cache (1 : 10 ))
559+ @test maybe_cacheddata (v) === transpose (cacheddata (parent (v)))
555560 end
556561
557562 @testset " Missing BroadcastStyles/MemoryLayouts/cacheddata with CachedArrayStyles" begin
@@ -587,6 +592,149 @@ using Infinities
587592 @test BroadcastStyle (typeof (Hcat (d, (1 : 10 )))) == CachedArrayStyle {2} ()
588593 @test BroadcastStyle (typeof (Hcat ((1 : 10 ), d))) == CachedArrayStyle {2} ()
589594 end
590- end
595+
596+ @testset " Enforce same-size arguments for cacheddata" begin
597+ @testset " max_datasize" begin
598+ @test LazyArrays. _datasize (1 : 10 ) == (10 ,)
599+ x = cache (1 : 10 )
600+ @test LazyArrays. _datasize (x) == (0 ,)
601+ resizedata! (x, 3 )
602+ @test LazyArrays. _datasize (x) == (3 ,)
603+ @test LazyArrays. _datasize (view (x, 3 : 5 )) == (3 ,)
604+ @test LazyArrays. _datasize (transpose (x)) == (1 , 3 )
605+
606+ arr = (1 : 10 , cache (1 : 10 ))
607+ @test LazyArrays. max_datasize (LazyArrays. _datasizes (arr)) == (10 ,)
608+
609+ arr = (cache (1 : 10 ), cache (1 : 10 ))
610+ resizedata! (arr[1 ], 3 )
611+ @test LazyArrays. max_datasize (LazyArrays. _datasizes (arr)) == (3 ,)
612+ resizedata! (arr[2 ], 7 )
613+ @test LazyArrays. max_datasize (LazyArrays. _datasizes (arr)) == (7 ,)
614+
615+ arr = (rand (10 , 5 ), LazyArrays. CachedArray (rand (10 , 5 )))
616+ @test LazyArrays. max_datasize (LazyArrays. _datasizes (arr)) == (10 , 5 )
617+
618+ arr = (LazyArrays. CachedArray (rand (10 , 5 )), LazyArrays. CachedArray (rand (10 , 5 )))
619+ @test LazyArrays. max_datasize (LazyArrays. _datasizes (arr)) == (0 , 0 )
620+ resizedata! (arr[1 ], 3 , 4 )
621+ @test LazyArrays. max_datasize (LazyArrays. _datasizes (arr)) == (3 , 4 )
622+ resizedata! (arr[2 ], 2 , 5 );
623+ @test LazyArrays. max_datasize (LazyArrays. _datasizes (arr)) == (3 , 5 )
624+
625+ arr = (1 , [1 , 2 ])
626+ @test LazyArrays. _datasize (1 ) == (1 , )
627+ @test LazyArrays. max_datasize (LazyArrays. _datasizes (arr)) == (2 ,)
628+ end
629+
630+ @testset " conforming_resize!" begin
631+ args = (cache (1 : 10 ), cache (1 : 10 ));
632+ LazyArrays. conforming_resize! (args);
633+ @test LazyArrays. _datasizes (args) == ((0 ,), (0 ,));
634+ LazyArrays. resizedata! (args[2 ], 4 );
635+ LazyArrays. conforming_resize! (args);
636+ @test LazyArrays. _datasizes (args) == ((4 ,), (4 ,));
637+
638+ args = (1 : 10 , cache (1 : 10 ));
639+ LazyArrays. conforming_resize! (args);
640+ @test LazyArrays. _datasizes (args) == ((10 ,), (10 ,));
641+
642+ args = (cache (1 : 10 )' , cache (1 : 10 )' );
643+ LazyArrays. conforming_resize! (args);
644+ @test LazyArrays. _datasizes (args) == ((0 , 0 ), (0 , 0 ));
645+ LazyArrays. resizedata! (args[1 ], 1 , 3 );
646+ LazyArrays. conforming_resize! (args);
647+ @test LazyArrays. _datasizes (args) == ((1 , 3 ), (1 , 3 ));
648+
649+ args = (cache (1 : 10 )' , LazyArrays. CachedArray (rand (1 , 10 )));
650+ @test LazyArrays. _datasizes (args) == ((0 , 0 ), (0 , 0 ));
651+ LazyArrays. conforming_resize! (args);
652+ @test LazyArrays. _datasizes (args) == ((0 , 0 ), (0 , 0 ));
653+ LazyArrays. resizedata! (args[1 ], 1 , 4 );
654+ LazyArrays. conforming_resize! (args);
655+ @test LazyArrays. _datasizes (args) == ((1 , 4 ), (1 , 4 ));
656+ LazyArrays. resizedata! (args[2 ], 1 , 6 )
657+ LazyArrays. conforming_resize! (args);
658+ @test LazyArrays. _datasizes (args) == ((1 , 6 ), (1 , 6 ));
659+
660+ args = (cache (1 : 10 ), LazyArrays. CachedArray (rand (1 , 10 ))' );
661+ LazyArrays. resizedata! (args[1 ], 4 );
662+ LazyArrays. conforming_resize! (args);
663+ @test LazyArrays. _datasizes (args) == ((4 ,), (4 , 1 ));
664+
665+ args = (cache (1 : 10 ), LazyArrays. CachedArray (rand (1 , 10 ))' , 1 : 10 );
666+ LazyArrays. conforming_resize! (args);
667+ @test LazyArrays. _datasizes (args) == ((10 ,), (10 , 1 ), (10 ,));
668+
669+ args = (1 , cache (1 : 2 ));
670+ LazyArrays. conforming_resize! (args);
671+ @test LazyArrays. _datasizes (args) == ((1 ,), (1 ,)) # because scalars are treated as size (1,)
672+
673+ args = (rand (10 , 10 ), LazyArrays. CachedArray (rand (10 , 10 )));
674+ LazyArrays. conforming_resize! (args);
675+ @test LazyArrays. _datasizes (args) == ((10 , 10 ), (10 , 10 ));
676+
677+ args = (LazyArrays. CachedArray (rand (10 , 10 )), LazyArrays. CachedArray (rand (10 , 10 )));
678+ LazyArrays. conforming_resize! (args);
679+ @test LazyArrays. _datasizes (args) == ((0 , 0 ), (0 , 0 ));
680+ LazyArrays. resizedata! (args[1 ], 3 , 4 );
681+ LazyArrays. conforming_resize! (args);
682+ @test LazyArrays. _datasizes (args) == ((3 , 4 ), (3 , 4 ));
683+ LazyArrays. resizedata! (args[2 ], 5 , 6 );
684+ LazyArrays. conforming_resize! (args);
685+ @test LazyArrays. _datasizes (args) == ((5 , 6 ), (5 , 6 ));
686+
687+ @testset " conforming_resize! dimension mismatch" begin
688+ args = (cache (1 : 10 ), cache (1 : 10 ))
689+ @test LazyArrays. conforming_resize! (args) === args
690+
691+ args = (cache (1 : 10 ), LazyArrays. CachedArray (rand (10 , 5 )))
692+ @test_throws ArgumentError LazyArrays. conforming_resize! (args)
693+
694+ args = (LazyArrays. CachedArray (rand (3 , 4 )), LazyArrays. CachedArray (rand (5 , 2 )))
695+ @test LazyArrays. conforming_resize! (args) === args
696+
697+ args = (1 , cache (1 : 10 ))
698+ @test LazyArrays. conforming_resize! (args) === args
699+
700+ args = (cache (1 : 10 ), LazyArrays. CachedArray (rand (2 , 3 )), reshape (cache (1 : 8 ), 2 , 2 , 2 ))
701+ @test_throws ArgumentError LazyArrays. conforming_resize! (args)
702+
703+ args = (reshape (cache (1 : 8 ), 2 , 2 , 2 ), reshape (cache (1 : 27 ), 3 , 3 , 3 ))
704+ @test LazyArrays. conforming_resize! (args) === args
705+
706+ args = ()
707+ @test LazyArrays. conforming_resize! (args) === args
708+
709+ args = (cache (1 : 10 ),)
710+ @test LazyArrays. conforming_resize! (args) === args
711+ end
712+ end
713+ end
714+
715+ @testset " cacheddata for ApplyArray and BroadcastArray" begin
716+ x = ApplyArray (+ , 1 : 10 , cache (11 : 20 ));
717+ @test cacheddata (x) == ApplyArray (+ , 1 : 10 , 11 : 20 )
718+ @test Base. Broadcast. BroadcastStyle (typeof (cacheddata (x))) == LazyArrays. LazyArrayStyle {1} ()
719+
720+ x = BroadcastVector (* , 1 : 10 , cache (1 : 10 ));
721+ @test cacheddata (x) == BroadcastVector (* , 1 : 10 , 1 : 10 )
722+ @test Base. Broadcast. BroadcastStyle (typeof (cacheddata (x))) == LazyArrays. LazyArrayStyle {1} ()
723+
724+ x = ApplyArray (+ , cache (1 : 10 ), cache (11 : 20 ));
725+ @test cacheddata (x) == ApplyArray (+ , 1 : 0 , 1 : 0 )
726+ @test Base. Broadcast. BroadcastStyle (typeof (cacheddata (x))) == LazyArrays. LazyArrayStyle {1} ()
727+ LazyArrays. resizedata! (x. args[1 ], 3 )
728+ @test cacheddata (x) == ApplyArray (+ , 1 : 3 , 11 : 13 )
729+ @test Base. Broadcast. BroadcastStyle (typeof (cacheddata (x))) == LazyArrays. LazyArrayStyle {1} ()
730+
731+ x = BroadcastVector (* , cache (1 : 10 ), cache (11 : 20 ));
732+ @test cacheddata (x) == BroadcastVector (* , 1 : 0 , 1 : 0 )
733+ @test Base. Broadcast. BroadcastStyle (typeof (cacheddata (x))) == LazyArrays. LazyArrayStyle {1} ()
734+ LazyArrays. resizedata! (x. args[1 ], 4 )
735+ @test cacheddata (x) == BroadcastVector (* , 1 : 4 , 11 : 14 )
736+ @test Base. Broadcast. BroadcastStyle (typeof (cacheddata (x))) == LazyArrays. LazyArrayStyle {1} ()
737+ end
738+ end
591739
592740end # module
0 commit comments