summaryrefslogtreecommitdiff
path: root/factors/factors_test.go
diff options
context:
space:
mode:
authorAdrien Hopkins <adrien.p.hopkins@gmail.com>2023-08-07 15:24:54 -0500
committerAdrien Hopkins <adrien.p.hopkins@gmail.com>2023-08-18 14:26:13 -0500
commit8c6419bde0cb6893cf7e789c8fceaea5638c95ff (patch)
tree234fe0c4249d68bbb402b5bfd4f3d58fea832dd0 /factors/factors_test.go
parent1bef5152dfa454b308946791ad7a8efc747e3fbd (diff)
Add tests for Prime Factorization function
Diffstat (limited to 'factors/factors_test.go')
-rw-r--r--factors/factors_test.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/factors/factors_test.go b/factors/factors_test.go
new file mode 100644
index 0000000..472a2e3
--- /dev/null
+++ b/factors/factors_test.go
@@ -0,0 +1,46 @@
+package factors
+
+import (
+ "fmt"
+ "testing"
+)
+
+var primeFactorCases = map[uint]PrimeFactorization{
+ 0: PrimeFactorization{map[uint]uint{0: 1}},
+ 1: PrimeFactorization{map[uint]uint{}},
+ 2: PrimeFactorization{map[uint]uint{2: 1}},
+ 3: PrimeFactorization{map[uint]uint{3: 1}},
+ 4: PrimeFactorization{map[uint]uint{2: 2}},
+ 6: PrimeFactorization{map[uint]uint{2: 1, 3: 1}},
+ 10: PrimeFactorization{map[uint]uint{2: 1, 5: 1}},
+ 12: PrimeFactorization{map[uint]uint{2: 2, 3: 1}},
+ 33: PrimeFactorization{map[uint]uint{3: 1, 11: 1}},
+ 60: PrimeFactorization{map[uint]uint{2: 2, 3: 1, 5: 1}},
+ 86400: PrimeFactorization{map[uint]uint{2: 7, 3: 3, 5: 2}},
+}
+
+func TestPrimeFactorize(t *testing.T) {
+ for i, expected := range primeFactorCases {
+ testname := fmt.Sprintf("%d", i)
+ t.Run(testname, func(t *testing.T) {
+ actual := PrimeFactorize(i)
+ if !mapEquals(expected.exponents, actual.exponents) {
+ t.Errorf("PrimeFactorize(%d) = %s, want %s", i, actual, expected)
+ }
+ })
+ }
+}
+
+func mapEquals(a, b map[uint]uint) bool {
+ for k := range a {
+ if a[k] != b[k] {
+ return false
+ }
+ }
+ for k := range b {
+ if a[k] != b[k] {
+ return false
+ }
+ }
+ return true
+}