Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/org/rascalmpl/interpreter/result/ElementResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,27 @@ public <U extends IValue, V extends IValue> Result<U> slice(Result<?> first, Res

if(first != null){
firstIndex = getInt(first);
if(firstIndex < 0)
firstIndex += len;
if(firstIndex < 0){
firstIndex = Math.max(firstIndex+len, 0);
}
else{
firstIndex = Math.min(firstIndex, len);
}
}
if(end != null){
endIndex = getInt(end);
if(endIndex < 0){
endIndex += len;
endIndex = Math.max(endIndex+len, 0);
}
else{
endIndex = Math.min(endIndex, len);
}
}

if(firstIndex == len && endIndex<firstIndex){
firstIndex = len - 1;
}

if(second == null){
secondIndex = firstIndex + ((firstIndex <= endIndex) ? 1 : -1);
} else {
Expand Down
7 changes: 7 additions & 0 deletions src/org/rascalmpl/library/lang/rascal/tests/basic/Lists.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,13 @@ test bool sliceSecondNegative(list[int] L) {
return S == makeSlice(L, 0, size(L) - incr, size(L));
}

test bool sliceOutOfBoundIndex1() { L = [0,1,2,3,4,5,6,7,8,9]; return L[..] == [0,1,2,3,4,5,6,7,8,9];}
test bool sliceOutOfBoundIndex2() { L = [0,1,2,3,4,5,6,7,8,9]; return L[10..] == [];}
test bool sliceOutOfBoundIndex3() { L = [0,1,2,3,4,5,6,7,8,9]; return L[10..10] == [];}
test bool sliceOutOfBoundIndex4() { L = [0,1,2,3,4,5,6,7,8,9]; return L[10..-11] == [9,8,7,6,5,4,3,2,1];}
test bool sliceOutOfBoundIndex5() { L = [0,1,2,3,4,5,6,7,8,9]; return L[-15..-11] == [];}
test bool sliceOutOfBoundIndex6() { L = [0,1,2,3,4,5,6,7,8,9]; return L[10..-5] == [9,8,7,6];}

test bool assignSlice1() { L = [0,1,2,3,4,5,6,7,8,9]; L[..] = [10,20]; return L == [10,20,10,20,10,20,10,20,10,20];}
test bool assignSlice2() { L = [0,1,2,3,4,5,6,7,8,9]; L[2..] = [10,20]; return L == [0,1,10,20,10,20,10,20,10,20];}
test bool assignSlice3() { L = [0,1,2,3,4,5,6,7,8,9]; L[2..6] = [10,20]; return L == [0,1,10,20,10,20,6,7,8,9];}
Expand Down
Loading