File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 504504 [] +> throws-on-bytes-of-wrong-size-as-i64
505505 01-01-01-01.as-i64 > @
506506
507+ # Tests that slicing out of bounds throws an error.
508+ [] +> throws-on-out-of-bounds-slice
509+ 20-1F-EE-B5-90.slice 3 10 > @
510+
507511 # Tests that bytes can be correctly converted to i64 integer.
508512 [] +> tests-bytes-converts-to-i64
509513 eq. > @
Original file line number Diff line number Diff line change @@ -30,8 +30,8 @@ public final class EObytes$EOslice extends PhDefault implements Atom {
3030 }
3131
3232 @ Override
33- @ SuppressWarnings ("PMD.UnnecessaryLocalRule" )
3433 public Phi lambda () {
34+ final byte [] bytes = new Dataized (this .take (Phi .RHO )).take ();
3535 final int start = Expect .at (this , "start" )
3636 .that (phi -> new Dataized (phi ).asNumber ())
3737 .otherwise ("must be a number" )
@@ -49,12 +49,11 @@ public Phi lambda() {
4949 .otherwise ("must be an integer" )
5050 .must (integer -> integer >= 0 )
5151 .otherwise ("must be a positive integer" )
52- .it ();
53- return new Data .ToPhi (
54- Arrays .copyOfRange (
55- new Dataized (this .take (Phi .RHO )).take (),
56- start , start + length
52+ .must (integer -> start + integer <= bytes .length )
53+ .otherwise (
54+ String .format ("is out of bounds for bytes of size %d" , bytes .length )
5755 )
58- );
56+ .it ();
57+ return new Data .ToPhi (Arrays .copyOfRange (bytes , start , start + length ));
5958 }
6059}
Original file line number Diff line number Diff line change @@ -46,6 +46,37 @@ void takesLegalSlice() {
4646 );
4747 }
4848
49+ @ Test
50+ @ SuppressWarnings ("PMD.UnitTestContainsTooManyAsserts" )
51+ void throwsOnOutOfBoundsSlice () {
52+ MatcherAssert .assertThat (
53+ "error message is correct for out of bounds slice" ,
54+ new UncheckedText (
55+ new TextOf (
56+ Assertions .assertThrows (
57+ EOerror .ExError .class ,
58+ () -> new Dataized (
59+ new PhWith (
60+ new PhWith (
61+ new Data .ToPhi ("hello" )
62+ .take ("as-bytes" )
63+ .take ("slice" )
64+ .copy (),
65+ "start" ,
66+ new Data .ToPhi (3 )
67+ ),
68+ "len" ,
69+ new Data .ToPhi (10 )
70+ )
71+ ).asString (),
72+ "doesnt throw on out of bounds slice"
73+ )
74+ )
75+ ).asString (),
76+ Matchers .containsString ("is out of bounds for bytes of size 5" )
77+ );
78+ }
79+
4980 @ Test
5081 @ SuppressWarnings ("PMD.UnitTestContainsTooManyAsserts" )
5182 void takesWrongSlice () {
You can’t perform that action at this time.
0 commit comments