about summary refs log tree commit diff
path: root/assessments/semiprimes/server/test/math_test.exs
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-12-11T22·43+0000
committerWilliam Carroll <wpcarro@gmail.com>2020-12-11T22·43+0000
commitf1e458239266dac1d18120c6c3b092a67a739577 (patch)
tree87874e3b4c8a3a7d681fd3995c4fcb80e5652600 /assessments/semiprimes/server/test/math_test.exs
parent6af5e4b82e9d2f025ad388f82495865a195c5218 (diff)
Define Math module
Support `Math.factor` and cover it with tests.
Diffstat (limited to 'assessments/semiprimes/server/test/math_test.exs')
-rw-r--r--assessments/semiprimes/server/test/math_test.exs32
1 files changed, 32 insertions, 0 deletions
diff --git a/assessments/semiprimes/server/test/math_test.exs b/assessments/semiprimes/server/test/math_test.exs
new file mode 100644
index 000000000000..81c60e40667e
--- /dev/null
+++ b/assessments/semiprimes/server/test/math_test.exs
@@ -0,0 +1,32 @@
+defmodule MathTest do
+  use ExUnit.Case
+  doctest Math
+
+  describe "factor" do
+    test "returns the prime factors for an input" do
+      [
+        {15, [3, 5]},
+        {12, [2, 2, 3]},
+        {9, [3, 3]},
+        {21, [3, 7]}
+      ]
+      |> Enum.map(fn {input, expected} ->
+        Math.factor(input) == expected
+      end)
+      |> Enum.all?()
+      |> assert
+    end
+
+    test "handles large numbers" do
+      assert Math.factor(104_023) == [17, 29, 211]
+    end
+
+    test "returns an empty list for 1" do
+      assert Math.factor(1) == []
+    end
+
+    test "returns the prime number itself when the input is prime" do
+      assert Math.factor(7) == [7]
+    end
+  end
+end