diff --git a/src/util/util_test.go b/src/util/util_test.go index ca12aa759ab..8479367187c 100644 --- a/src/util/util_test.go +++ b/src/util/util_test.go @@ -8,179 +8,304 @@ import ( ) func TestMax(t *testing.T) { - if Max(10, 1) != 10 { - t.Error("Expected", 10) - } - if Max(-2, 5) != 5 { - t.Error("Expected", 5) + testcases := []struct { + first int + second int + expect int + }{ + {10, 1, 10}, + {-2, 5, 5}, + } + for _, testcase := range testcases { + if actual := Max(testcase.first, testcase.second); actual != testcase.expect { + t.Errorf("Expected: %d, actual: %d", testcase.expect, actual) + } } } func TestMax16(t *testing.T) { - if Max16(10, 1) != 10 { - t.Error("Expected", 10) - } - if Max16(-2, 5) != 5 { - t.Error("Expected", 5) - } - if Max16(math.MaxInt16, 0) != math.MaxInt16 { - t.Error("Expected", math.MaxInt16) - } - if Max16(0, math.MinInt16) != 0 { - t.Error("Expected", 0) + testcases := []struct { + first int16 + second int16 + expect int16 + }{ + {10, 1, 10}, + {-2, 5, 5}, + {math.MaxInt16, 0, math.MaxInt16}, + {0, math.MinInt16, 0}, + } + for _, testcase := range testcases { + if actual := Max16(testcase.first, testcase.second); actual != testcase.expect { + t.Errorf("Expected: %d, actual: %d", testcase.expect, actual) + } } } func TestMax32(t *testing.T) { - if Max32(10, 1) != 10 { - t.Error("Expected", 10) - } - if Max32(-2, 5) != 5 { - t.Error("Expected", 5) - } - if Max32(math.MaxInt32, 0) != math.MaxInt32 { - t.Error("Expected", math.MaxInt32) - } - if Max32(0, math.MinInt32) != 0 { - t.Error("Expected", 0) + testcases := []struct { + first int32 + second int32 + expect int32 + }{ + {10, 1, 10}, + {-2, 5, 5}, + {math.MaxInt32, 0, math.MaxInt32}, + {0, math.MinInt32, 0}, + } + for _, testcase := range testcases { + if actual := Max32(testcase.first, testcase.second); actual != testcase.expect { + t.Errorf("Expected: %d, actual: %d", testcase.expect, actual) + } } } func TestMin(t *testing.T) { - if Min(10, 1) != 1 { - t.Error("Expected", 1) + testcases := []struct { + first int + second int + expect int + }{ + {10, 1, 1}, + {-2, 5, -2}, + } + for _, testcase := range testcases { + if actual := Min(testcase.first, testcase.second); actual != testcase.expect { + t.Errorf("Expected: %d, actual: %d", testcase.expect, actual) + } } - if Min(-2, 5) != -2 { - t.Error("Expected", -2) +} + +func TestMin16(t *testing.T) { + testcases := []struct { + first int16 + second int16 + expect int16 + }{ + {10, 1, 1}, + {-2, 5, -2}, + {math.MaxInt16, 0, 0}, + {0, math.MinInt16, math.MinInt16}, + } + for _, testcase := range testcases { + if actual := Min16(testcase.first, testcase.second); actual != testcase.expect { + t.Errorf("Expected: %d, actual: %d", testcase.expect, actual) + } } } func TestMin32(t *testing.T) { - if Min32(10, 1) != 1 { - t.Error("Expected", 1) - } - if Min32(-2, 5) != -2 { - t.Error("Expected", -2) - } - if Min32(math.MaxInt32, 0) != 0 { - t.Error("Expected", 0) - } - if Min32(0, math.MinInt32) != math.MinInt32 { - t.Error("Expected", math.MinInt32) + testcases := []struct { + first int32 + second int32 + expect int32 + }{ + {10, 1, 1}, + {-2, 5, -2}, + {math.MaxInt32, 0, 0}, + {0, math.MinInt32, math.MinInt32}, + } + for _, testcase := range testcases { + if actual := Min32(testcase.first, testcase.second); actual != testcase.expect { + t.Errorf("Expected: %d, actual: %d", testcase.expect, actual) + } } } func TestContrain(t *testing.T) { - if Constrain(-3, -1, 3) != -1 { - t.Error("Expected", -1) - } - if Constrain(2, -1, 3) != 2 { - t.Error("Expected", 2) + testcases := []struct { + val int + min int + max int + expect int + }{ + {-3, -1, 3, -1}, + {2, -1, 3, 2}, + {5, -1, 3, 3}, + } + for _, testcase := range testcases { + if actual := Constrain(testcase.val, testcase.min, testcase.max); actual != testcase.expect { + t.Errorf("Expected: %d, actual: %d", testcase.expect, actual) + } } +} - if Constrain(5, -1, 3) != 3 { - t.Error("Expected", 3) +func TestContrain16(t *testing.T) { + testcases := []struct { + val int16 + min int16 + max int16 + expect int16 + }{ + {-3, -1, 3, -1}, + {2, -1, 3, 2}, + {5, -1, 3, 3}, + {0, math.MinInt16, math.MaxInt16, 0}, + } + for _, testcase := range testcases { + if actual := Constrain16(testcase.val, testcase.min, testcase.max); actual != testcase.expect { + t.Errorf("Expected: %d, actual: %d", testcase.expect, actual) + } } } func TestContrain32(t *testing.T) { - if Constrain32(-3, -1, 3) != -1 { - t.Error("Expected", -1) - } - if Constrain32(2, -1, 3) != 2 { - t.Error("Expected", 2) - } - - if Constrain32(5, -1, 3) != 3 { - t.Error("Expected", 3) - } - if Constrain32(0, math.MinInt32, math.MaxInt32) != 0 { - t.Error("Expected", 0) + testcases := []struct { + val int32 + min int32 + max int32 + expect int32 + }{ + {-3, -1, 3, -1}, + {2, -1, 3, 2}, + {5, -1, 3, 3}, + {0, math.MinInt32, math.MaxInt32, 0}, + } + for _, testcase := range testcases { + if actual := Constrain32(testcase.val, testcase.min, testcase.max); actual != testcase.expect { + t.Errorf("Expected: %d, actual: %d", testcase.expect, actual) + } } } func TestAsUint16(t *testing.T) { - if AsUint16(5) != 5 { - t.Error("Expected", 5) - } - if AsUint16(-10) != 0 { - t.Error("Expected", 0) - } - if AsUint16(math.MaxUint16) != math.MaxUint16 { - t.Error("Expected", math.MaxUint16) - } - if AsUint16(math.MinInt32) != 0 { - t.Error("Expected", 0) - } - if AsUint16(math.MinInt16) != 0 { - t.Error("Expected", 0) - } - if AsUint16(math.MaxUint32) != math.MaxUint16 { - t.Error("Expected", math.MaxUint16) + testcases := []struct { + val int + expect uint16 + }{ + {5, 5}, + {-10, 0}, + {math.MaxUint16, math.MaxUint16}, + {math.MaxUint32, math.MaxUint16}, + {math.MinInt16, 0}, + {math.MinInt32, 0}, + } + for _, testcase := range testcases { + if actual := AsUint16(testcase.val); actual != testcase.expect { + t.Errorf("Expected: %d, actual: %d", testcase.expect, actual) + } } } -func TestDurWithIn(t *testing.T) { - if DurWithin(time.Duration(5), time.Duration(1), time.Duration(8)) != time.Duration(5) { - t.Error("Expected", time.Duration(0)) - } - if DurWithin(time.Duration(0)*time.Second, time.Second, time.Duration(3)*time.Second) != time.Second { - t.Error("Expected", time.Second) - } - if DurWithin(time.Duration(10)*time.Second, time.Duration(0), time.Second) != time.Second { - t.Error("Expected", time.Second) +func TestAsUint32(t *testing.T) { + testcases := []struct { + val int + expect uint32 + }{ + {5, 5}, + {-10, 0}, + {math.MaxInt32, math.MaxInt32}, + {math.MinInt32, 0}, + } + for _, testcase := range testcases { + if actual := AsUint32(testcase.val); actual != testcase.expect { + t.Errorf("Expected: %d, actual: %d", testcase.expect, actual) + } } } func TestOnce(t *testing.T) { - o := Once(false) - if o() { - t.Error("Expected: false") - } - if o() { - t.Error("Expected: false") + testcases := []struct { + nextResponse bool + expect1 bool + expect2 bool + }{ + {false, false, false}, + {true, true, false}, + } + for _, testcase := range testcases { + o := Once(testcase.nextResponse) + if actual := o(); actual != testcase.expect1 { + t.Errorf("Expected (1): %T actual: %T", testcase.expect1, actual) + } + if actual := o(); actual != testcase.expect2 { + t.Errorf("Expected (2): %T actual: %T", testcase.expect2, actual) + } } +} - o = Once(true) - if !o() { - t.Error("Expected: true") - } - if o() { - t.Error("Expected: false") +func TestDurWithIn(t *testing.T) { + sec := time.Second + min := time.Minute + hour := time.Hour + testcases := []struct { + val time.Duration + min time.Duration + max time.Duration + expect time.Duration + }{ + {5, 1, 8, 5}, + {0, sec, 3 * sec, sec}, + {10 * sec, 0, sec, sec}, + {min, sec, hour, min}, + {24 * hour, 10 * hour, 20 * hour, 20 * hour}, + } + for _, testcase := range testcases { + if actual := DurWithin(testcase.val, testcase.min, testcase.max); actual != testcase.expect { + t.Errorf("Expected: %s, actual: %s", testcase.expect, actual) + } } } func TestRunesWidth(t *testing.T) { - for _, args := range [][]int{ - {100, 5, -1}, - {3, 4, 3}, - {0, 1, 0}, - } { - width, overflowIdx := RunesWidth([]rune("hello"), 0, 0, args[0]) - if width != args[1] { - t.Errorf("Expected width: %d, actual: %d", args[1], width) - } - if overflowIdx != args[2] { - t.Errorf("Expected overflow index: %d, actual: %d", args[2], overflowIdx) + testcases := []struct { + runes []rune + prefixWidth int + tabstop int + limit int + expectWidth int + expectIndex int + }{ + {[]rune("hello"), 0, 0, 100, 5, -1}, + {[]rune("hello"), 0, 0, 3, 4, 3}, + {[]rune("hello"), 0, 0, 0, 1, 0}, + } + for _, testcase := range testcases { + if actualWidth, actualIndex := RunesWidth( + testcase.runes, testcase.prefixWidth, testcase.tabstop, testcase.limit, + ); actualWidth != testcase.expectWidth || actualIndex != testcase.expectIndex { + t.Errorf( + "Expected (width: %d, index: %d), actual (width: %d, index: %d)", + testcase.expectWidth, testcase.expectIndex, actualWidth, actualIndex, + ) } } } func TestTruncate(t *testing.T) { - truncated, width := Truncate("가나다라마", 7) - if string(truncated) != "가나다" { - t.Errorf("Expected: 가나다, actual: %s", string(truncated)) - } - if width != 6 { - t.Errorf("Expected: 6, actual: %d", width) + testcases := []struct { + input string + limit int + expectTruncated string + expectWidth int + }{ + {"가나다라마", 7, "가나다", 6}, + {"123456789", 4, "1234", 4}, + } + for _, testcase := range testcases { + actualRunes, actualWidth := Truncate(testcase.input, testcase.limit) + actualTruncated := string(actualRunes) + if actualTruncated != testcase.expectTruncated || actualWidth != testcase.expectWidth { + t.Errorf( + "Expected: (truncated: %s, width: %d), actual (truncated: %s, width: %d)", + testcase.expectTruncated, testcase.expectWidth, actualTruncated, actualWidth, + ) + } } } func TestRepeatToFill(t *testing.T) { - if RepeatToFill("abcde", 10, 50) != strings.Repeat("abcde", 5) { - t.Error("Expected:", strings.Repeat("abcde", 5)) - } - if RepeatToFill("abcde", 10, 42) != strings.Repeat("abcde", 4)+"abcde"[:2] { - t.Error("Expected:", strings.Repeat("abcde", 4)+"abcde"[:2]) + testcases := []struct { + str string + length int + limit int + expect string + }{ + {"abcde", 10, 50, strings.Repeat("abcde", 5)}, + {"abcde", 10, 42, strings.Repeat("abcde", 4) + "abcde"[:2]}, + } + for _, testcase := range testcases { + if actual := RepeatToFill(testcase.str, testcase.length, testcase.limit); actual != testcase.expect { + t.Errorf("Expected: %s, actual: %s", testcase.expect, actual) + } } }