summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrien Hopkins <adrien.p.hopkins@gmail.com>2023-08-15 20:01:25 -0500
committerAdrien Hopkins <adrien.p.hopkins@gmail.com>2023-08-21 09:56:06 -0500
commitff5bc533399cb7fc7b83afcc3a20e6abbc05d444 (patch)
treefef566fc518fd6d87273454891f3a7d86e4f98c3
parent58df2b08f57076800745009aa4d562ef77ba6f9c (diff)
Refactor tests to use a common testing function
I can now use the function tableTest() to create a new test in one or two lines of code! No more need to rewrite basically the same code ten times...
-rw-r--r--factors/factors_test.go72
1 files changed, 26 insertions, 46 deletions
diff --git a/factors/factors_test.go b/factors/factors_test.go
index f14dd31..0029d67 100644
--- a/factors/factors_test.go
+++ b/factors/factors_test.go
@@ -6,6 +6,22 @@ import (
"testing"
)
+// Run a test with a table of cases
+func tableTest[IN comparable, OUT any](t *testing.T, toTest func(IN) OUT,
+ cases map[IN]OUT, equal func(OUT, OUT) bool, name string) {
+ for input, expected := range cases {
+ t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
+ actual := toTest(input)
+ if !equal(expected, actual) {
+ t.Errorf("%s(%v) = %v, expect %v", name, input, actual, expected)
+ }
+ })
+ }
+}
+
+// to be used as the equal paramater for tableTest
+func stdEquals[T comparable](a, b T) bool { return a == b }
+
var primeFactorCases = map[uint]PrimeFactorization{
0: PrimeFactorization{map[uint]uint{0: 1}},
1: PrimeFactorization{map[uint]uint{}},
@@ -21,15 +37,10 @@ var primeFactorCases = map[uint]PrimeFactorization{
}
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)
- }
- })
+ equal := func(a, b PrimeFactorization) bool {
+ return mapEquals(a.exponents, b.exponents)
}
+ tableTest(t, PrimeFactorize, primeFactorCases, equal, "PrimeFactorize")
}
var factorCases = map[uint][]uint{
@@ -46,15 +57,7 @@ var factorCases = map[uint][]uint{
}
func TestFactors(t *testing.T) {
- for i, expected := range factorCases {
- testname := fmt.Sprintf("%d", i)
- t.Run(testname, func(t *testing.T) {
- actual := Factors(i)
- if !setEquals(expected, actual) {
- t.Errorf("Factors(%d) = %v, want %v", i, actual, expected)
- }
- })
- }
+ tableTest(t, Factors, factorCases, setEquals, "Factors")
}
var totativeRatioCases = map[uint]float64{
@@ -68,15 +71,8 @@ var totativeRatioCases = map[uint]float64{
}
func TestTotativeRatio(t *testing.T) {
- for i, expected := range totativeRatioCases {
- testname := fmt.Sprintf("%d", i)
- t.Run(testname, func(t *testing.T) {
- actual := TotativeRatio(i)
- if !floatEquals(expected, actual, 1e-15) {
- t.Errorf("TotativeRatio(%d) = %v, want %v", i, actual, expected)
- }
- })
- }
+ equals := func(a, b float64) bool { return floatEquals(a, b, 1e-15) }
+ tableTest(t, TotativeRatio, totativeRatioCases, equals, "TotativeRatio")
}
var factorScoreCases = map[uint]float64{
@@ -92,17 +88,8 @@ var factorScoreCases = map[uint]float64{
}
func TestFactorScore(t *testing.T) {
- for i, expected := range factorScoreCases {
- testname := fmt.Sprintf("%d", i)
- t.Run(testname, func(t *testing.T) {
- actual := Score(i)
- // factors.Score is accurate enough that we can test
- // for exact float values!
- if expected != actual {
- t.Errorf("Score(%d) = %v, want %v", i, actual, expected)
- }
- })
- }
+ // factors.Score is accurate enough that we can test for exact floats!
+ tableTest(t, Score, factorScoreCases, stdEquals[float64], "Score")
}
var basicRankCases = map[uint]string{
@@ -111,15 +98,8 @@ var basicRankCases = map[uint]string{
14: "D~", 15: "E+", 18: "B-", 20: "C+", 24: "A~", 30: "B+", 60: "A+",
}
-func TestBasicRank (t *testing.T) {
- for i, expected := range basicRankCases {
- t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
- actual := BasicRank(i)
- if expected != actual {
- t.Errorf("BasicRank(%d) = %s, want %s", i, actual, expected)
- }
- })
- }
+func TestBasicRank(t *testing.T) {
+ tableTest(t, BasicRank, basicRankCases, stdEquals[string], "BasicRank")
}
func mapEquals[K comparable, V comparable](a, b map[K]V) bool {