1 defmodule SieveFilterTest do
5 test "less_than? compares the counter first" do
6 assert SieveFilter.less_than?({10, 5}, {12, 3})
7 assert not SieveFilter.less_than?({12, 3}, {10, 5})
10 test "less_than? compares the prime second" do
11 assert SieveFilter.less_than?({10, 2}, {10, 5})
12 assert not SieveFilter.less_than?({10, 5}, {10, 2})
15 test "get_next adds the prime to the counter" do
16 assert SieveFilter.get_next({21, 7}) == {28, 7}
20 defmodule SieveTest do
24 test "run_item_through_sieve returns empty list and all filters for prime" do
25 sieve = [{8, 2}, {9, 3}, {10, 5}]
26 {matched, unmatched} = Sieve.run_item_through_sieve(sieve, 7)
29 assert unmatched == sieve
32 test "run_item_through_sieve returns matched and unmatched filters for composite" do
33 sieve = [{10, 2}, {10, 5}, {12, 3}, {14, 7}]
34 {matched, unmatched} = Sieve.run_item_through_sieve(sieve, 10)
36 assert matched == [{10, 2}, {10, 5}]
37 assert unmatched == [{12, 3}, {14, 7}]
40 test "merge merges sieves" do
41 sieve_a = [{12, 2}, {15, 5}]
42 sieve_b = [{12, 3}, {14, 7}]
43 merged = Sieve.merge(sieve_a, sieve_b)
45 assert merged == [{12, 2}, {12, 3}, {14, 7}, {15, 5}]
48 test "get_next updates filters" do
49 sieve = [{42, 2}, {42, 3}, {42, 7}]
51 assert Sieve.get_next(sieve) == [{44, 2}, {45, 3}, {49, 7}]
55 defmodule PrimesTest do
59 test "transform updates sieve and next item" do
60 sieve = [{8, 2}, {9, 3}, {10, 5}, {14, 7}]
63 {next_sieve, next_item} = Primes.transform(sieve, item)
65 assert next_sieve == [{12, 2}, {12, 3}, {14, 7}, {15, 5}, {22, 11}]
66 assert next_item == 11
69 test "generate iterates updating state and yielding items" do
70 fibonacci = Primes.generate(0, 1, fn(prev, curr) -> {curr, prev + curr} end)
72 assert Enum.take(fibonacci, 10) == [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
75 test "primes generates primes" do
76 assert Enum.take(Primes.primes(), 10) == [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
79 test "primes is fast" do
80 assert 1 == Enum.sum(Stream.take_while(Primes.primes(), &(&1 < 2_000_000)))