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

Commit

Permalink
Add unit tests for zip and zipWith
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelocenerine committed Sep 9, 2017
1 parent f816f63 commit 207572c
Show file tree
Hide file tree
Showing 7 changed files with 503 additions and 2 deletions.
71 changes: 71 additions & 0 deletions test/junit/src/test/scala/strawman/collection/ArrayOpsTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package strawman.collection

import org.junit.Assert._
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import strawman.collection.immutable.List

@RunWith(classOf[JUnit4])
class ArrayOpsTest {

@Test
def hasCorrectOverloadedZipWhenInputHasSameNumberOfElements = {
val array: ArrayOps[Int] = Array(1, 2, 3)
val result: Array[(Int, String)] = array.zip(List("a", "b", "c"))

assertArrayEquals(Array((1, "a"), (2, "b"), (3, "c")).asInstanceOf[Array[AnyRef]], result.asInstanceOf[Array[AnyRef]])
}

@Test
def hasCorrectOverloadedZipWhenInputHasMoreElements = {
val array: ArrayOps[Int] = Array(1, 2)
val result: Array[(Int, String)] = array.zip(List("a", "b", "c"))

assertArrayEquals(Array((1, "a"), (2, "b")).asInstanceOf[Array[AnyRef]], result.asInstanceOf[Array[AnyRef]])
}

@Test
def hasCorrectOverloadedZipWhenInputHasLessElements = {
val array: ArrayOps[Int] = Array(1, 2, 3)
val result: Array[(Int, String)] = array.zip(List("a", "b"))

assertArrayEquals(Array((1, "a"), (2, "b")).asInstanceOf[Array[AnyRef]], result.asInstanceOf[Array[AnyRef]])
}

@Test
def hasCorrectOverloadedZipWhenInputIsEmpty = {
val array: ArrayOps[Int] = Array(1, 2, 3)
val result: Array[(Int, Int)] = array.zip(List.empty[Int])

assertEquals(0, result.length)
}

@Test
def hasCorrectOverloadedZipWithWhenInputHasSameNumberOfElements = {
val result: Array[Int] = Array(1, 2, 3).zipWith(List(4, 5, 6))(_ + _)

assertArrayEquals(Array(5, 7, 9), result)
}

@Test
def hasCorrectOverloadedZipWithWhenInputHasMoreElements = {
val result: Array[Int] = Array(1, 2).zipWith(List(4, 5, 6))(_ * _)

assertArrayEquals(Array(4, 10), result)
}

@Test
def hasCorrectOverloadedZipWithWhenInputHasLessElements = {
val result: Array[Int] = Array(1, 2, 3).zipWith(List(4, 5))(_ + _)

assertArrayEquals(Array(5, 7), result)
}

@Test
def hasCorrectOverloadedZipWithWhenInputIsEmpty = {
val result: Array[Int] = Array(1, 2, 3).zipWith(List.empty[Int])(_ % _)

assertEquals(0, result.length)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.junit.Assert._
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

import language.postfixOps

@RunWith(classOf[JUnit4])
Expand All @@ -19,4 +20,60 @@ class IterableViewLikeTest {
assertEquals(iter, iter.view drop Int.MinValue to Iterable)
assertEquals(iter, iter.view dropRight Int.MinValue to Iterable)
}

@Test
def hasCorrectZipWhenInputHasSameNumberOfElements = {
val result: Iterable[(Int, String)] = Iterable(1, 2, 3).zip(Iterable("a", "b", "c"))

assertEquals(Iterable((1, "a"), (2, "b"), (3, "c")), result)
}

@Test
def hasCorrectZipWhenInputHasMoreElements = {
val result: Iterable[(Int, String)] = Iterable(1, 2).zip(Iterable("a", "b", "c"))

assertEquals(Iterable((1, "a"), (2, "b")), result)
}

@Test
def hasCorrectZipWhenInputHasLessElements = {
val result: Iterable[(Int, String)] = Iterable(1, 2, 3).zip(Iterable("a", "b"))

assertEquals(Iterable((1, "a"), (2, "b")), result)
}

@Test
def hasCorrectZipWhenInputIsEmpty = {
val result: Iterable[(Int, Int)] = Iterable(1, 2, 3).zip(Iterable.empty[Int])

assertTrue(result.isEmpty)
}

@Test
def hasCorrectZipWithWhenInputHasSameNumberOfElements = {
val result: Iterable[Int] = Iterable(1, 2, 3).zipWith(Iterable(4, 5, 6))(_ + _)

assertEquals(Iterable(5, 7, 9), result)
}

@Test
def hasCorrectZipWithWhenInputHasMoreElements = {
val result: Iterable[Int] = Iterable(1, 2).zipWith(Iterable(4, 5, 6))(_ * _)

assertEquals(Iterable(4, 10), result)
}

@Test
def hasCorrectZipWithWhenInputHasLessElements = {
val result: Iterable[Int] = Iterable(1, 2, 3).zipWith(Iterable(4, 5))(_ + _)

assertEquals(Iterable(5, 7), result)
}

@Test
def hasCorrectZipWithWhenInputIsEmpty = {
val result: Iterable[Int] = Iterable(1, 2, 3).zipWith(Iterable.empty[Int])(_ % _)

assertTrue(result.isEmpty)
}
}
84 changes: 84 additions & 0 deletions test/junit/src/test/scala/strawman/collection/IteratorTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,88 @@ class IteratorTest {
// assertEquals(v2, v4)
// assertEquals(Some(v1), v2)
// }

@Test
def hasCorrectZipWhenInputHasSameNumberOfElements = {
val result: Iterator[(Int, String)] = List(1, 2, 3).iterator().zip(List("a", "b", "c"))

assertTrue(result.hasNext)
assertEquals((1, "a"), result.next())
assertTrue(result.hasNext)
assertEquals((2, "b"), result.next())
assertTrue(result.hasNext)
assertEquals((3, "c"), result.next())
assertFalse(result.hasNext)
}

@Test
def hasCorrectZipWhenInputHasMoreElements = {
val result: Iterator[(Int, String)] = List(1, 2).iterator().zip(List("a", "b", "c"))

assertTrue(result.hasNext)
assertEquals((1, "a"), result.next())
assertTrue(result.hasNext)
assertEquals((2, "b"), result.next())
assertFalse(result.hasNext)
}

@Test
def hasCorrectZipWhenInputHasLessElements = {
val result: Iterator[(Int, String)] = List(1, 2, 3).iterator().zip(List("a", "b"))

assertTrue(result.hasNext)
assertEquals((1, "a"), result.next())
assertTrue(result.hasNext)
assertEquals((2, "b"), result.next())
assertFalse(result.hasNext)
}

@Test
def hasCorrectZipWhenInputIsEmpty = {
val result: Iterator[(Int, Int)] = List(1, 2, 3).iterator().zip(List.empty[Int])

assertFalse(result.hasNext)
}

@Test
def hasCorrectZipWithWhenInputHasSameNumberOfElements = {
val result: Iterator[Int] = List(1, 2, 3).iterator().zipWith(List(4, 5, 6))(_ + _)

assertTrue(result.hasNext)
assertEquals(5, result.next())
assertTrue(result.hasNext)
assertEquals(7, result.next())
assertTrue(result.hasNext)
assertEquals(9, result.next())
assertFalse(result.hasNext)
}

@Test
def hasCorrectZipWithWhenInputHasMoreElements = {
val result: Iterator[Int] = List(1, 2).iterator().zipWith(List(4, 5, 6))(_ * _)

assertTrue(result.hasNext)
assertEquals(4, result.next())
assertTrue(result.hasNext)
assertEquals(10, result.next())
assertFalse(result.hasNext)
}

@Test
def hasCorrectZipWithWhenInputHasLessElements = {
val result: Iterator[Int] = List(1, 2, 3).iterator().zipWith(List(4, 5))(_ + _)

assertTrue(result.hasNext)
assertEquals(5, result.next())
assertTrue(result.hasNext)
assertEquals(7, result.next())
assertFalse(result.hasNext)
}

@Test
def hasCorrectZipWithWhenInputIsEmpty = {
val result: Iterator[Int] = List(1, 2, 3).iterator().zipWith(List.empty[Int])(_ % _)

assertFalse(result.hasNext)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package strawman.collection.immutable

import org.junit.Assert._
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(classOf[JUnit4])
class ImmutableArrayTest {

@Test
def hasCorrectZipWhenInputIsImmutableArrayAndHasSameNumberOfElements = {
val result: ImmutableArray[(Int, String)] = ImmutableArray(1, 2, 3).zip(ImmutableArray("a", "b", "c"))

assertEquals(ImmutableArray((1, "a"), (2, "b"), (3, "c")), result)
}

@Test
def hasCorrectZipWhenInputIsImmutableArrayAndHasMoreElements = {
val result: ImmutableArray[(Int, String)] = ImmutableArray(1, 2).zip(ImmutableArray("a", "b", "c"))

assertEquals(ImmutableArray((1, "a"), (2, "b")), result)
}

@Test
def hasCorrectZipWhenInputIsImmutableArrayAndHasLessElements = {
val result: ImmutableArray[(Int, String)] = ImmutableArray(1, 2, 3).zip(ImmutableArray("a", "b"))

assertEquals(ImmutableArray((1, "a"), (2, "b")), result)
}

@Test
def hasCorrectZipWhenInputIsImmutableArrayAndIsEmpty = {
val result: ImmutableArray[(Int, Int)] = ImmutableArray(1, 2, 3).zip(ImmutableArray.empty[Int])

assertTrue(result.isEmpty)
}

@Test
def hasCorrectZipWhenInputIsOtherCollectionTypeAndHasSameNumberOfElements = {
val result: ImmutableArray[(Int, String)] = ImmutableArray(1, 2, 3).zip(List("a", "b", "c"))

assertEquals(ImmutableArray((1, "a"), (2, "b"), (3, "c")), result)
}

@Test
def hasCorrectZipWhenInputIsOtherCollectionTypeAndHasMoreElements = {
val result: ImmutableArray[(Int, String)] = ImmutableArray(1, 2).zip(List("a", "b", "c"))

assertEquals(ImmutableArray((1, "a"), (2, "b")), result)
}

@Test
def hasCorrectZipWhenInputIsOtherCollectionTypeAndHasLessElements = {
val result: ImmutableArray[(Int, String)] = ImmutableArray(1, 2, 3).zip(List("a", "b"))

assertEquals(ImmutableArray((1, "a"), (2, "b")), result)
}

@Test
def hasCorrectZipWhenInputIsOtherCollectionTypeAndIsEmpty = {
val result: ImmutableArray[(Int, Int)] = ImmutableArray(1, 2, 3).zip(List.empty[Int])

assertTrue(result.isEmpty)
}

@Test
def hasCorrectZipWithWhenInputIsImmutableArrayAndHasSameNumberOfElements = {
val result: ImmutableArray[Int] = ImmutableArray(1, 2, 3).zipWith(ImmutableArray(4, 5, 6))(_ + _)

assertEquals(ImmutableArray(5, 7, 9), result)
}

@Test
def hasCorrectZipWithWhenInputIsImmutableArrayAndHasMoreElements = {
val result: ImmutableArray[Int] = ImmutableArray(1, 2).zipWith(ImmutableArray(4, 5, 6))(_ * _)

assertEquals(ImmutableArray(4, 10), result)
}

@Test
def hasCorrectZipWithWhenInputIsImmutableArrayAndHasLessElements = {
val result: ImmutableArray[Int] = ImmutableArray(1, 2, 3).zipWith(ImmutableArray(4, 5))(_ + _)

assertEquals(ImmutableArray(5, 7), result)
}

@Test
def hasCorrectZipWithWhenInputIsImmutableArrayAndIsEmpty = {
val result: ImmutableArray[Int] = ImmutableArray(1, 2, 3).zipWith(ImmutableArray.empty[Int])(_ % _)

assertTrue(result.isEmpty)
}

@Test
def hasCorrectZipWithWhenInputIsOtherCollectionTypeAndHasSameNumberOfElements = {
val result: ImmutableArray[Int] = ImmutableArray(1, 2, 3).zipWith(List(4, 5, 6))(_ + _)

assertEquals(ImmutableArray(5, 7, 9), result)
}

@Test
def hasCorrectZipWithWhenInputIsOtherCollectionTypeAndHasMoreElements = {
val result: ImmutableArray[Int] = ImmutableArray(1, 2).zipWith(List(4, 5, 6))(_ * _)

assertEquals(ImmutableArray(4, 10), result)
}

@Test
def hasCorrectZipWithWhenInputIsOtherCollectionTypeAndHasLessElements = {
val result: ImmutableArray[Int] = ImmutableArray(1, 2, 3).zipWith(List(4, 5))(_ + _)

assertEquals(ImmutableArray(5, 7), result)
}

@Test
def hasCorrectZipWithWhenInputIsOtherCollectionTypeAndIsEmpty = {
val result: ImmutableArray[Int] = ImmutableArray(1, 2, 3).zipWith(List.empty[Int])(_ % _)

assertTrue(result.isEmpty)
}
}
Loading

0 comments on commit 207572c

Please sign in to comment.