1
exercism fetch elixir parallel-letter-frequency

parallel_letter_frequency_test.exs

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
if !System.get_env("EXERCISM_TEST_EXAMPLES") do
  Code.load_file("frequency.exs", __DIR__)
end

ExUnit.start
ExUnit.configure exclude: :pending, trace: true

# Your code should contain a frequency(texts, workers) function which accepts a
# list of texts and the number of workers to use in parallel.

defmodule FrequencyTest do
  use ExUnit.Case

  # Poem by Friedrich Schiller. The corresponding music is the European Anthem.
  @ode_an_die_freude """
  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
  @wilhelmus """
  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
  @star_spangled_banner """
  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?
  """

  # Returns the frequencies in a sorted list. This means it doesn't matter if
  # your frequency() function returns a list of pairs or a map, the
  # testing code will handle it.
  defp freq(texts, workers \\ 4) do
    Frequency.frequency(texts, workers) |> Enum.sort() |> Enum.into(%{})
  end

  # @tag :pending
  test "no texts mean no letters" do
    assert freq([]) == %{}
  end

  @tag :pending
  test "one letter" do
    assert freq(["a"]) == %{"a" => 1}
  end

  @tag :pending
  test "case insensitivity" do
    assert freq(["aA"]) == %{"a" => 2}
  end

  @tag :pending
  test "many empty texts still mean no letters" do
    assert freq(List.duplicate("  ", 10000)) == %{}
  end

  @tag :pending
  test "many times the same text gives a predictable result" do
    assert freq(List.duplicate("abc", 1000))
         == %{"a" => 1000, "b" => 1000, "c" => 1000}
  end

  @tag :pending
  test "punctuation doesn't count" do
    assert freq([@ode_an_die_freude])[","] == nil
  end

  @tag :pending
  test "numbers don't count" do
    assert freq(["Testing, 1, 2, 3"])["1"] == nil
  end

  @tag :pending
  test "all three anthems, together, 1 worker" do
    freqs = freq([@ode_an_die_freude, @wilhelmus, @star_spangled_banner], 1)
    assert freqs["a"] == 49
    assert freqs["t"] == 56
    assert freqs["ü"] == 2
  end

  @tag :pending
  test "all three anthems, together, 4 workers" do
    freqs = freq([@ode_an_die_freude, @wilhelmus, @star_spangled_banner], 4)
    assert freqs["a"] == 49
    assert freqs["t"] == 56
    assert freqs["ü"] == 2
  end
end