Skip to content
This repository was archived by the owner on Dec 22, 2021. It is now read-only.

Commit 207572c

Browse files
Add unit tests for zip and zipWith
1 parent f816f63 commit 207572c

File tree

7 files changed

+503
-2
lines changed

7 files changed

+503
-2
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package strawman.collection
2+
3+
import org.junit.Assert._
4+
import org.junit.Test
5+
import org.junit.runner.RunWith
6+
import org.junit.runners.JUnit4
7+
import strawman.collection.immutable.List
8+
9+
@RunWith(classOf[JUnit4])
10+
class ArrayOpsTest {
11+
12+
@Test
13+
def hasCorrectOverloadedZipWhenInputHasSameNumberOfElements = {
14+
val array: ArrayOps[Int] = Array(1, 2, 3)
15+
val result: Array[(Int, String)] = array.zip(List("a", "b", "c"))
16+
17+
assertArrayEquals(Array((1, "a"), (2, "b"), (3, "c")).asInstanceOf[Array[AnyRef]], result.asInstanceOf[Array[AnyRef]])
18+
}
19+
20+
@Test
21+
def hasCorrectOverloadedZipWhenInputHasMoreElements = {
22+
val array: ArrayOps[Int] = Array(1, 2)
23+
val result: Array[(Int, String)] = array.zip(List("a", "b", "c"))
24+
25+
assertArrayEquals(Array((1, "a"), (2, "b")).asInstanceOf[Array[AnyRef]], result.asInstanceOf[Array[AnyRef]])
26+
}
27+
28+
@Test
29+
def hasCorrectOverloadedZipWhenInputHasLessElements = {
30+
val array: ArrayOps[Int] = Array(1, 2, 3)
31+
val result: Array[(Int, String)] = array.zip(List("a", "b"))
32+
33+
assertArrayEquals(Array((1, "a"), (2, "b")).asInstanceOf[Array[AnyRef]], result.asInstanceOf[Array[AnyRef]])
34+
}
35+
36+
@Test
37+
def hasCorrectOverloadedZipWhenInputIsEmpty = {
38+
val array: ArrayOps[Int] = Array(1, 2, 3)
39+
val result: Array[(Int, Int)] = array.zip(List.empty[Int])
40+
41+
assertEquals(0, result.length)
42+
}
43+
44+
@Test
45+
def hasCorrectOverloadedZipWithWhenInputHasSameNumberOfElements = {
46+
val result: Array[Int] = Array(1, 2, 3).zipWith(List(4, 5, 6))(_ + _)
47+
48+
assertArrayEquals(Array(5, 7, 9), result)
49+
}
50+
51+
@Test
52+
def hasCorrectOverloadedZipWithWhenInputHasMoreElements = {
53+
val result: Array[Int] = Array(1, 2).zipWith(List(4, 5, 6))(_ * _)
54+
55+
assertArrayEquals(Array(4, 10), result)
56+
}
57+
58+
@Test
59+
def hasCorrectOverloadedZipWithWhenInputHasLessElements = {
60+
val result: Array[Int] = Array(1, 2, 3).zipWith(List(4, 5))(_ + _)
61+
62+
assertArrayEquals(Array(5, 7), result)
63+
}
64+
65+
@Test
66+
def hasCorrectOverloadedZipWithWhenInputIsEmpty = {
67+
val result: Array[Int] = Array(1, 2, 3).zipWith(List.empty[Int])(_ % _)
68+
69+
assertEquals(0, result.length)
70+
}
71+
}

test/junit/src/test/scala/strawman/collection/IterableViewLikeTest.scala

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import org.junit.Assert._
44
import org.junit.Test
55
import org.junit.runner.RunWith
66
import org.junit.runners.JUnit4
7+
78
import language.postfixOps
89

910
@RunWith(classOf[JUnit4])
@@ -19,4 +20,60 @@ class IterableViewLikeTest {
1920
assertEquals(iter, iter.view drop Int.MinValue to Iterable)
2021
assertEquals(iter, iter.view dropRight Int.MinValue to Iterable)
2122
}
23+
24+
@Test
25+
def hasCorrectZipWhenInputHasSameNumberOfElements = {
26+
val result: Iterable[(Int, String)] = Iterable(1, 2, 3).zip(Iterable("a", "b", "c"))
27+
28+
assertEquals(Iterable((1, "a"), (2, "b"), (3, "c")), result)
29+
}
30+
31+
@Test
32+
def hasCorrectZipWhenInputHasMoreElements = {
33+
val result: Iterable[(Int, String)] = Iterable(1, 2).zip(Iterable("a", "b", "c"))
34+
35+
assertEquals(Iterable((1, "a"), (2, "b")), result)
36+
}
37+
38+
@Test
39+
def hasCorrectZipWhenInputHasLessElements = {
40+
val result: Iterable[(Int, String)] = Iterable(1, 2, 3).zip(Iterable("a", "b"))
41+
42+
assertEquals(Iterable((1, "a"), (2, "b")), result)
43+
}
44+
45+
@Test
46+
def hasCorrectZipWhenInputIsEmpty = {
47+
val result: Iterable[(Int, Int)] = Iterable(1, 2, 3).zip(Iterable.empty[Int])
48+
49+
assertTrue(result.isEmpty)
50+
}
51+
52+
@Test
53+
def hasCorrectZipWithWhenInputHasSameNumberOfElements = {
54+
val result: Iterable[Int] = Iterable(1, 2, 3).zipWith(Iterable(4, 5, 6))(_ + _)
55+
56+
assertEquals(Iterable(5, 7, 9), result)
57+
}
58+
59+
@Test
60+
def hasCorrectZipWithWhenInputHasMoreElements = {
61+
val result: Iterable[Int] = Iterable(1, 2).zipWith(Iterable(4, 5, 6))(_ * _)
62+
63+
assertEquals(Iterable(4, 10), result)
64+
}
65+
66+
@Test
67+
def hasCorrectZipWithWhenInputHasLessElements = {
68+
val result: Iterable[Int] = Iterable(1, 2, 3).zipWith(Iterable(4, 5))(_ + _)
69+
70+
assertEquals(Iterable(5, 7), result)
71+
}
72+
73+
@Test
74+
def hasCorrectZipWithWhenInputIsEmpty = {
75+
val result: Iterable[Int] = Iterable(1, 2, 3).zipWith(Iterable.empty[Int])(_ % _)
76+
77+
assertTrue(result.isEmpty)
78+
}
2279
}

test/junit/src/test/scala/strawman/collection/IteratorTest.scala

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,88 @@ class IteratorTest {
256256
// assertEquals(v2, v4)
257257
// assertEquals(Some(v1), v2)
258258
// }
259+
260+
@Test
261+
def hasCorrectZipWhenInputHasSameNumberOfElements = {
262+
val result: Iterator[(Int, String)] = List(1, 2, 3).iterator().zip(List("a", "b", "c"))
263+
264+
assertTrue(result.hasNext)
265+
assertEquals((1, "a"), result.next())
266+
assertTrue(result.hasNext)
267+
assertEquals((2, "b"), result.next())
268+
assertTrue(result.hasNext)
269+
assertEquals((3, "c"), result.next())
270+
assertFalse(result.hasNext)
271+
}
272+
273+
@Test
274+
def hasCorrectZipWhenInputHasMoreElements = {
275+
val result: Iterator[(Int, String)] = List(1, 2).iterator().zip(List("a", "b", "c"))
276+
277+
assertTrue(result.hasNext)
278+
assertEquals((1, "a"), result.next())
279+
assertTrue(result.hasNext)
280+
assertEquals((2, "b"), result.next())
281+
assertFalse(result.hasNext)
282+
}
283+
284+
@Test
285+
def hasCorrectZipWhenInputHasLessElements = {
286+
val result: Iterator[(Int, String)] = List(1, 2, 3).iterator().zip(List("a", "b"))
287+
288+
assertTrue(result.hasNext)
289+
assertEquals((1, "a"), result.next())
290+
assertTrue(result.hasNext)
291+
assertEquals((2, "b"), result.next())
292+
assertFalse(result.hasNext)
293+
}
294+
295+
@Test
296+
def hasCorrectZipWhenInputIsEmpty = {
297+
val result: Iterator[(Int, Int)] = List(1, 2, 3).iterator().zip(List.empty[Int])
298+
299+
assertFalse(result.hasNext)
300+
}
301+
302+
@Test
303+
def hasCorrectZipWithWhenInputHasSameNumberOfElements = {
304+
val result: Iterator[Int] = List(1, 2, 3).iterator().zipWith(List(4, 5, 6))(_ + _)
305+
306+
assertTrue(result.hasNext)
307+
assertEquals(5, result.next())
308+
assertTrue(result.hasNext)
309+
assertEquals(7, result.next())
310+
assertTrue(result.hasNext)
311+
assertEquals(9, result.next())
312+
assertFalse(result.hasNext)
313+
}
314+
315+
@Test
316+
def hasCorrectZipWithWhenInputHasMoreElements = {
317+
val result: Iterator[Int] = List(1, 2).iterator().zipWith(List(4, 5, 6))(_ * _)
318+
319+
assertTrue(result.hasNext)
320+
assertEquals(4, result.next())
321+
assertTrue(result.hasNext)
322+
assertEquals(10, result.next())
323+
assertFalse(result.hasNext)
324+
}
325+
326+
@Test
327+
def hasCorrectZipWithWhenInputHasLessElements = {
328+
val result: Iterator[Int] = List(1, 2, 3).iterator().zipWith(List(4, 5))(_ + _)
329+
330+
assertTrue(result.hasNext)
331+
assertEquals(5, result.next())
332+
assertTrue(result.hasNext)
333+
assertEquals(7, result.next())
334+
assertFalse(result.hasNext)
335+
}
336+
337+
@Test
338+
def hasCorrectZipWithWhenInputIsEmpty = {
339+
val result: Iterator[Int] = List(1, 2, 3).iterator().zipWith(List.empty[Int])(_ % _)
340+
341+
assertFalse(result.hasNext)
342+
}
259343
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package strawman.collection.immutable
2+
3+
import org.junit.Assert._
4+
import org.junit.Test
5+
import org.junit.runner.RunWith
6+
import org.junit.runners.JUnit4
7+
8+
@RunWith(classOf[JUnit4])
9+
class ImmutableArrayTest {
10+
11+
@Test
12+
def hasCorrectZipWhenInputIsImmutableArrayAndHasSameNumberOfElements = {
13+
val result: ImmutableArray[(Int, String)] = ImmutableArray(1, 2, 3).zip(ImmutableArray("a", "b", "c"))
14+
15+
assertEquals(ImmutableArray((1, "a"), (2, "b"), (3, "c")), result)
16+
}
17+
18+
@Test
19+
def hasCorrectZipWhenInputIsImmutableArrayAndHasMoreElements = {
20+
val result: ImmutableArray[(Int, String)] = ImmutableArray(1, 2).zip(ImmutableArray("a", "b", "c"))
21+
22+
assertEquals(ImmutableArray((1, "a"), (2, "b")), result)
23+
}
24+
25+
@Test
26+
def hasCorrectZipWhenInputIsImmutableArrayAndHasLessElements = {
27+
val result: ImmutableArray[(Int, String)] = ImmutableArray(1, 2, 3).zip(ImmutableArray("a", "b"))
28+
29+
assertEquals(ImmutableArray((1, "a"), (2, "b")), result)
30+
}
31+
32+
@Test
33+
def hasCorrectZipWhenInputIsImmutableArrayAndIsEmpty = {
34+
val result: ImmutableArray[(Int, Int)] = ImmutableArray(1, 2, 3).zip(ImmutableArray.empty[Int])
35+
36+
assertTrue(result.isEmpty)
37+
}
38+
39+
@Test
40+
def hasCorrectZipWhenInputIsOtherCollectionTypeAndHasSameNumberOfElements = {
41+
val result: ImmutableArray[(Int, String)] = ImmutableArray(1, 2, 3).zip(List("a", "b", "c"))
42+
43+
assertEquals(ImmutableArray((1, "a"), (2, "b"), (3, "c")), result)
44+
}
45+
46+
@Test
47+
def hasCorrectZipWhenInputIsOtherCollectionTypeAndHasMoreElements = {
48+
val result: ImmutableArray[(Int, String)] = ImmutableArray(1, 2).zip(List("a", "b", "c"))
49+
50+
assertEquals(ImmutableArray((1, "a"), (2, "b")), result)
51+
}
52+
53+
@Test
54+
def hasCorrectZipWhenInputIsOtherCollectionTypeAndHasLessElements = {
55+
val result: ImmutableArray[(Int, String)] = ImmutableArray(1, 2, 3).zip(List("a", "b"))
56+
57+
assertEquals(ImmutableArray((1, "a"), (2, "b")), result)
58+
}
59+
60+
@Test
61+
def hasCorrectZipWhenInputIsOtherCollectionTypeAndIsEmpty = {
62+
val result: ImmutableArray[(Int, Int)] = ImmutableArray(1, 2, 3).zip(List.empty[Int])
63+
64+
assertTrue(result.isEmpty)
65+
}
66+
67+
@Test
68+
def hasCorrectZipWithWhenInputIsImmutableArrayAndHasSameNumberOfElements = {
69+
val result: ImmutableArray[Int] = ImmutableArray(1, 2, 3).zipWith(ImmutableArray(4, 5, 6))(_ + _)
70+
71+
assertEquals(ImmutableArray(5, 7, 9), result)
72+
}
73+
74+
@Test
75+
def hasCorrectZipWithWhenInputIsImmutableArrayAndHasMoreElements = {
76+
val result: ImmutableArray[Int] = ImmutableArray(1, 2).zipWith(ImmutableArray(4, 5, 6))(_ * _)
77+
78+
assertEquals(ImmutableArray(4, 10), result)
79+
}
80+
81+
@Test
82+
def hasCorrectZipWithWhenInputIsImmutableArrayAndHasLessElements = {
83+
val result: ImmutableArray[Int] = ImmutableArray(1, 2, 3).zipWith(ImmutableArray(4, 5))(_ + _)
84+
85+
assertEquals(ImmutableArray(5, 7), result)
86+
}
87+
88+
@Test
89+
def hasCorrectZipWithWhenInputIsImmutableArrayAndIsEmpty = {
90+
val result: ImmutableArray[Int] = ImmutableArray(1, 2, 3).zipWith(ImmutableArray.empty[Int])(_ % _)
91+
92+
assertTrue(result.isEmpty)
93+
}
94+
95+
@Test
96+
def hasCorrectZipWithWhenInputIsOtherCollectionTypeAndHasSameNumberOfElements = {
97+
val result: ImmutableArray[Int] = ImmutableArray(1, 2, 3).zipWith(List(4, 5, 6))(_ + _)
98+
99+
assertEquals(ImmutableArray(5, 7, 9), result)
100+
}
101+
102+
@Test
103+
def hasCorrectZipWithWhenInputIsOtherCollectionTypeAndHasMoreElements = {
104+
val result: ImmutableArray[Int] = ImmutableArray(1, 2).zipWith(List(4, 5, 6))(_ * _)
105+
106+
assertEquals(ImmutableArray(4, 10), result)
107+
}
108+
109+
@Test
110+
def hasCorrectZipWithWhenInputIsOtherCollectionTypeAndHasLessElements = {
111+
val result: ImmutableArray[Int] = ImmutableArray(1, 2, 3).zipWith(List(4, 5))(_ + _)
112+
113+
assertEquals(ImmutableArray(5, 7), result)
114+
}
115+
116+
@Test
117+
def hasCorrectZipWithWhenInputIsOtherCollectionTypeAndIsEmpty = {
118+
val result: ImmutableArray[Int] = ImmutableArray(1, 2, 3).zipWith(List.empty[Int])(_ % _)
119+
120+
assertTrue(result.isEmpty)
121+
}
122+
}

0 commit comments

Comments
 (0)