Make it an error to skip or take a negative count.

This commit is contained in:
Bob Nystrom
2017-03-15 07:22:44 -07:00
parent 9f93119377
commit 8d313be3ce
12 changed files with 52 additions and 28 deletions
+3 -6
View File
@@ -15,14 +15,11 @@ var test = TestSequence.new().skip(0)
System.print(test is Sequence) // expect: true
System.print(test) // expect: instance of SkipSequence
// Skipping 0 changes nothing
// Skipping 0 changes nothing.
System.print(test.toList) // expect: [1, 2, 3]
// Skipping 1 works
// Skipping 1 works.
System.print(test.skip(1).toList) // expect: [2, 3]
// Skipping more than length of sequence produces empty list
// Skipping more than length of sequence produces empty list.
System.print(test.skip(4).isEmpty) // expect: true
// Skipping less than 0 changes nothing
System.print(test.skip(-10).toList) // expect: [1, 2, 3]
+1
View File
@@ -0,0 +1 @@
[1, 2, 3].skip(-1) // expect runtime error: Count must be a non-negative integer.
+1
View File
@@ -0,0 +1 @@
[1, 2, 3].skip(1.2) // expect runtime error: Count must be a non-negative integer.
+1
View File
@@ -0,0 +1 @@
[1, 2, 3].skip("s") // expect runtime error: Count must be a non-negative integer.
+3 -6
View File
@@ -15,14 +15,11 @@ var test = TestSequence.new().take(3)
System.print(test is Sequence) // expect: true
System.print(test) // expect: instance of TakeSequence
// Taking 0 produces empty list
// Taking 0 produces empty list.
System.print(test.take(0).isEmpty) // expect: true
// Taking 1 works
// Taking 1 works.
System.print(test.take(1).toList) // expect: [1]
// Taking more than length of sequence produces whole sequence
// Taking more than length of sequence produces whole sequence.
System.print(test.take(4).toList) // expect: [1, 2, 3]
// Taking less than 0 produces empty list
System.print(test.take(-10).isEmpty) // expect: true
+1
View File
@@ -0,0 +1 @@
[1, 2, 3].take(-1) // expect runtime error: Count must be a non-negative integer.
+1
View File
@@ -0,0 +1 @@
[1, 2, 3].take(1.2) // expect runtime error: Count must be a non-negative integer.
+1
View File
@@ -0,0 +1 @@
[1, 2, 3].take("s") // expect runtime error: Count must be a non-negative integer.