1
exercism fetch scala parallel-letter-frequency

src/test/scala/FrequencyTest.scala

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import org.scalatest.{Matchers, FunSuite}

/** @version created manually **/
class FrequencyTest extends FunSuite with Matchers {

  // Poem by Friedrich Schiller. The corresponding music is the European
  // Anthem.
  private val ode_an_die_freude = List(
    "Freude schöner Götterfunken"
    , "Tochter aus Elysium,"
    , "Wir betreten feuertrunken,"
    , "Himmlische, dein Heiligtum!"
    , "Deine Zauber binden wieder"
    , "Was die Mode streng geteilt;"
    , "Alle Menschen werden Brüder,"
    , "Wo dein sanfter Flügel weilt.")

  //Dutch national anthem
  private val wilhelmus = List(
    "Wilhelmus van Nassouwe"
    , "ben ik, van Duitsen bloed,"
    , "den vaderland getrouwe"
    , "blijf ik tot in den dood."
    , "Een Prinse van Oranje"
    , "ben ik, vrij, onverveerd,"
    , "den Koning van Hispanje"
    , "heb ik altijd geëerd.")

  //American national anthem
  private val star_spangled_banner = List(
    "O say can you see by the dawn's early light,"
    , "What so proudly we hailed at the twilight's last gleaming,"
    , "Whose broad stripes and bright stars through the perilous fight,"
    , "O'er the ramparts we watched, were so gallantly streaming?"
    , "And the rockets' red glare, the bombs bursting in air,"
    , "Gave proof through the night that our flag was still there;"
    , "O say does that star-spangled banner yet wave,"
    , "O'er the land of the free and the home of the brave?")

  test("Empty texts") {
    Frequency.frequency(1, Seq()) should be (Map())
  }

  test("Single letter") {
    pending
    Frequency.frequency(10, Seq("a")) should be (Map('a' -> 1))
  }

  test("Case insensitivity") {
    pending
    Frequency.frequency(1000, Seq("aA")) should be (Map('a' -> 2))
  }

  test("Many empty texts") {
    pending
    Frequency.frequency(1, Iterator.fill(10000)("  ").toSeq) should be (Map())
  }

  test("many times the same text gives a predictable result") {
    pending
    Frequency.frequency(1, Iterator.fill(1000)("abc").toSeq) should
      be (Map('a' -> 1000, 'b' -> 1000, 'c' -> 1000))
  }

  test("Ignore punctuation") {
    pending
    Frequency.frequency(1, ode_an_die_freude).get(',') should be (None)
  }

  test("Ignore numbers") {
    pending
    Frequency.frequency(1, Seq("Testing 1, 2, 3")).get('1') should be (None)
  }

  test("All three anthems - 1 worker") {
    pending
    val freqs = Frequency.frequency(1, ode_an_die_freude ++ wilhelmus ++ star_spangled_banner)
    freqs.get('a') should be (Some(49))
    freqs.get('t') should be (Some(56))
    freqs.get('ü') should be (Some(2))
  }

  test("All three anthems - 4 workers") {
    pending
    val freqs = Frequency.frequency(4, ode_an_die_freude ++ wilhelmus ++ star_spangled_banner)
    freqs.get('a') should be (Some(49))
    freqs.get('t') should be (Some(56))
    freqs.get('ü') should be (Some(2))
  }
}