@@ -620,4 +620,63 @@ contract LibPRNGTest is SoladyTest {
620620 function lazyShuffler1Get (uint256 i ) public view returns (uint256 ) {
621621 return _lazyShuffler1.get (i);
622622 }
623+
624+ function testLazyShufflerRevertsOnGrowAcrossWidthBoundary () public {
625+ _lazyShuffler0.initialize (2 );
626+ _lazyShuffler0.next (0 );
627+ vm.expectRevert (LibPRNG.InvalidNewLazyShufflerLength.selector );
628+ this .lazyShufflerGrow (65535 );
629+ }
630+
631+ function testLazyShufflerRevertsOnGrowAcrossWidthBoundaryUndrawn () public {
632+ _lazyShuffler0.initialize (2 );
633+ vm.expectRevert (LibPRNG.InvalidNewLazyShufflerLength.selector );
634+ this .lazyShufflerGrow (65535 );
635+ }
636+
637+ // `grow` had no upper bound check, so the length silently truncated to zero.
638+ function testLazyShufflerRevertsOnGrowOutOfRange (uint256 n ) public {
639+ _lazyShuffler0.initialize (10 );
640+ n = _bound (n, 2 ** 32 - 1 , type (uint256 ).max);
641+ vm.expectRevert (LibPRNG.InvalidNewLazyShufflerLength.selector );
642+ this .lazyShufflerGrow (n);
643+ assertEq (_lazyShuffler0.length (), 10 );
644+ }
645+
646+ function testLazyShufflerGrowWithinSameWidth () public {
647+ _lazyShuffler0.initialize (2 );
648+ uint256 first = _lazyShuffler0.next (0 );
649+ _lazyShuffler0.grow (1000 );
650+ assertEq (_lazyShuffler0.get (0 ), first);
651+ assertLt (_lazyShuffler0.get (0 ), 1000 );
652+ }
653+
654+ function testLazyShufflerGrowWithinWideWidth () public {
655+ _lazyShuffler0.initialize (70000 );
656+ uint256 first = _lazyShuffler0.next (0 );
657+ _lazyShuffler0.grow (200000 );
658+ assertEq (_lazyShuffler0.get (0 ), first);
659+ assertLt (_lazyShuffler0.get (0 ), 200000 );
660+ }
661+
662+ // A 32-bit shuffler still draws each value at most once across a grow.
663+ function testLazyShufflerWideProducesNoDuplicatesAcrossGrow () public {
664+ _lazyShuffler0.initialize (65535 );
665+ uint256 [] memory seen = new uint256 [](8 );
666+ unchecked {
667+ for (uint256 i; i != 4 ; ++ i) {
668+ seen[i] = _lazyShuffler0.next (_random ());
669+ }
670+ _lazyShuffler0.grow (65600 );
671+ for (uint256 i = 4 ; i != 8 ; ++ i) {
672+ seen[i] = _lazyShuffler0.next (_random ());
673+ }
674+ LibSort.sort (seen);
675+ LibSort.uniquifySorted (seen);
676+ assertEq (seen.length , 8 );
677+ for (uint256 i; i != 8 ; ++ i) {
678+ assertLt (seen[i], 65600 );
679+ }
680+ }
681+ }
623682}
0 commit comments