summaryrefslogtreecommitdiff
path: root/factors/factors_test.go
diff options
context:
space:
mode:
authorAdrien Hopkins <adrien.p.hopkins@gmail.com>2023-09-13 19:38:39 -0500
committerAdrien Hopkins <adrien.p.hopkins@gmail.com>2023-09-13 19:38:39 -0500
commitf657d4257e6b39a3899a2ebef8f30f99ebfb313f (patch)
tree16c117ba1ce8c10fd95230fb372c439c1ec6c613 /factors/factors_test.go
parent345f6bae7b005cdd386833aa496aa71ca11c7b97 (diff)
factors: Remove most panics
Panics are not the best way of handling errors in Go. I've replaced panics with default values whenever a sensible one exists. Factors(0) does not have a sensible default value (as every number is a factor of zero), so it still panics.
Diffstat (limited to 'factors/factors_test.go')
-rw-r--r--factors/factors_test.go30
1 files changed, 26 insertions, 4 deletions
diff --git a/factors/factors_test.go b/factors/factors_test.go
index dec651c..a18d30b 100644
--- a/factors/factors_test.go
+++ b/factors/factors_test.go
@@ -68,10 +68,6 @@ var totativeRatioCases = map[uint]float64{
}
func totativeRatio(n uint) float64 {
- if n == 0 {
- panic("0 has no totative ratio!")
- }
-
return float64(Totient(n)) / float64(n)
}
@@ -214,6 +210,8 @@ var (
pt = DigitType{0, Opaque} // 0P - opaque totatives
)
var digitMapCases = map[uint][]DigitType{
+ 0: []DigitType{},
+ 1: []DigitType{zt},
2: []DigitType{zt, ot},
3: []DigitType{zt, ot, wt},
4: []DigitType{zt, ot, ft, wt},
@@ -263,14 +261,38 @@ func TestGetDigitType(t *testing.T) {
}
}
+// A few test cases related to radix zero
+func TestGetDigitTypeMisc(t *testing.T) {
+ t.Run("GetDigitType(0, 0)", func(t *testing.T) {
+ if GetDigitType(0, 0) != ft {
+ t.Errorf("GetDigitType(0, 0) = %s, expected 1R", GetDigitType(0, 0))
+ }
+ })
+ t.Run("GetDigitType(1, 0)", func(t *testing.T) {
+ if GetDigitType(1, 0) != ot {
+ t.Errorf("GetDigitType(1, 0) = %s, expected 0R", GetDigitType(1, 0))
+ }
+ })
+ t.Run("GetDigitType(2, 0)", func(t *testing.T) {
+ if GetDigitType(2, 0) != pt {
+ t.Errorf("GetDigitType(2, 0) = %s, expected 0P", GetDigitType(2, 0))
+ }
+ })
+}
+
var splitCases = map[uintPair]uintPair{
// digit, radix, regular part, totative part
uintPair{0, 0}: uintPair{0, 1},
+ uintPair{0, 1}: uintPair{1, 0},
uintPair{0, 2}: uintPair{1, 0},
uintPair{0, 12}: uintPair{1, 0},
uintPair{1, 0}: uintPair{1, 1},
uintPair{20, 0}: uintPair{1, 20},
uintPair{360, 0}: uintPair{1, 360},
+ uintPair{1, 1}: uintPair{1, 1},
+ uintPair{12, 1}: uintPair{1, 12},
+ uintPair{87, 1}: uintPair{1, 87},
+ uintPair{120, 1}: uintPair{1, 120},
uintPair{1, 2}: uintPair{1, 1},
uintPair{1, 3}: uintPair{1, 1},
uintPair{1, 7}: uintPair{1, 1},