|
| 1 | +package com.baeldung.scala.randomfixedsizesample |
| 2 | + |
| 3 | +import org.scalatest.wordspec.AnyWordSpec |
| 4 | +import org.scalatest.matchers.should.Matchers |
| 5 | + |
| 6 | +import scala.annotation.tailrec; |
| 7 | + |
| 8 | +class RandomFixedSizeSampleSpec extends AnyWordSpec with Matchers { |
| 9 | + |
| 10 | + "RandomFixedSizeSample" should { |
| 11 | + "create a random sample out of the initial List" in { |
| 12 | + val list = List.range(0, 100) |
| 13 | + val sampleSize = 10 |
| 14 | + val list_0 = RandomFixedSizeSample.getRandomSampleRec(list, sampleSize) |
| 15 | + val list_1 = RandomFixedSizeSample.getRandomSampleZip(list, sampleSize) |
| 16 | + val list_2 = |
| 17 | + RandomFixedSizeSample.getRandomSampleShuffle(list, sampleSize) |
| 18 | + |
| 19 | + list_0.size shouldBe sampleSize |
| 20 | + list_1.size shouldBe sampleSize |
| 21 | + list_2.size shouldBe sampleSize |
| 22 | + |
| 23 | + list_0.toSet.size shouldBe list_0.size |
| 24 | + list_1.toSet.size shouldBe list_1.size |
| 25 | + list_2.toSet.size shouldBe list_2.size |
| 26 | + |
| 27 | + isSorted(list_0) shouldBe false |
| 28 | + isSorted(list_1) shouldBe false |
| 29 | + isSorted(list_2) shouldBe false |
| 30 | + } |
| 31 | + "ensure getRandomSampleShuffle is the most performant, then goes getRandomSampleZip and then getRandomSampleRec" in { |
| 32 | + val list = List.range(0, 10_000) |
| 33 | + val sampleSize = 100 |
| 34 | + |
| 35 | + val start_0 = System.nanoTime() |
| 36 | + RandomFixedSizeSample.getRandomSampleRec(list, sampleSize) |
| 37 | + val end_0 = System.nanoTime() |
| 38 | + val duration_0 = end_0 - start_0 |
| 39 | + |
| 40 | + val start_1 = System.nanoTime() |
| 41 | + RandomFixedSizeSample.getRandomSampleZip(list, sampleSize) |
| 42 | + val end_1 = System.nanoTime() |
| 43 | + val duration_1 = end_1 - start_1 |
| 44 | + |
| 45 | + val start_2 = System.nanoTime() |
| 46 | + RandomFixedSizeSample.getRandomSampleShuffle(list, sampleSize) |
| 47 | + val end_2 = System.nanoTime() |
| 48 | + val duration_2 = end_2 - start_2 |
| 49 | + |
| 50 | + duration_0 should be > duration_1 |
| 51 | + duration_1 should be > duration_2 |
| 52 | + } |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + def isSorted[T](list: List[T])(implicit o: Ordering[T]): Boolean = { |
| 57 | + @tailrec def iter(head: T, tail: List[T]): Boolean = |
| 58 | + if (tail.isEmpty) true |
| 59 | + else if (o.lt(tail.head, head)) false |
| 60 | + else { |
| 61 | + iter(tail.head, tail.tail) |
| 62 | + } |
| 63 | + |
| 64 | + list match { |
| 65 | + case Nil => true |
| 66 | + case head :: tail => iter(head, tail) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments