Add more util tests (#3062)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by OHZEKI Naoki · committed by GitHub · parent 2ba68d2 · Browse files · 1 file·+122 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Junegunn Choi <junegunn.c@gmail.com>
Diff stats
1 file+122 added−2 removed
- other:+122 added−2 removed
Provenance
- Commit
6b7a543- Tree
b098111- Author
OHZEKI Naoki <0h23k1.n40k1@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
2ba68d2
1 file changed+122 added−2 removed
modified:src/util/util_test.go+122 added-2 removed
1 package util2 3-import "testing"3+import (4+ "math"5+ "strings"6+ "testing"7+ "time"8+)9 10 func TestMax(t *testing.T) {11+ if Max(10, 1) != 10 {12+ t.Error("Expected", 10)13+ }14 if Max(-2, 5) != 5 {7- t.Error("Invalid result")15+ t.Error("Expected", 5)16 }17 }18 19+func TestMax16(t *testing.T) {20+ if Max16(10, 1) != 10 {21+ t.Error("Expected", 10)22+ }23+ if Max16(-2, 5) != 5 {24+ t.Error("Expected", 5)25+ }26+ if Max16(math.MaxInt16, 0) != math.MaxInt16 {27+ t.Error("Expected", math.MaxInt16)28+ }29+ if Max16(0, math.MinInt16) != 0 {30+ t.Error("Expected", 0)31+ }32+}33+34+func TestMax32(t *testing.T) {35+ if Max32(10, 1) != 10 {36+ t.Error("Expected", 10)37+ }38+ if Max32(-2, 5) != 5 {39+ t.Error("Expected", 5)40+ }41+ if Max32(math.MaxInt32, 0) != math.MaxInt32 {42+ t.Error("Expected", math.MaxInt32)43+ }44+ if Max32(0, math.MinInt32) != 0 {45+ t.Error("Expected", 0)46+ }47+}48+49+func TestMin(t *testing.T) {50+ if Min(10, 1) != 1 {51+ t.Error("Expected", 1)52+ }53+ if Min(-2, 5) != -2 {54+ t.Error("Expected", -2)55+ }56+}57+58+func TestMin32(t *testing.T) {59+ if Min32(10, 1) != 1 {60+ t.Error("Expected", 1)61+ }62+ if Min32(-2, 5) != -2 {63+ t.Error("Expected", -2)64+ }65+ if Min32(math.MaxInt32, 0) != 0 {66+ t.Error("Expected", 0)67+ }68+ if Min32(0, math.MinInt32) != math.MinInt32 {69+ t.Error("Expected", math.MinInt32)70+ }71+}72+73 func TestContrain(t *testing.T) {74 if Constrain(-3, -1, 3) != -1 {75 t.Error("Expected", -1)76 }5 unmodified lines82 t.Error("Expected", 3)83 }84 }85 86+func TestContrain32(t *testing.T) {87+ if Constrain32(-3, -1, 3) != -1 {88+ t.Error("Expected", -1)89+ }90+ if Constrain32(2, -1, 3) != 2 {91+ t.Error("Expected", 2)92+ }93+94+ if Constrain32(5, -1, 3) != 3 {95+ t.Error("Expected", 3)96+ }97+ if Constrain32(0, math.MinInt32, math.MaxInt32) != 0 {98+ t.Error("Expected", 0)99+ }100+}101+102+func TestAsUint16(t *testing.T) {103+ if AsUint16(5) != 5 {104+ t.Error("Expected", 5)105+ }106+ if AsUint16(-10) != 0 {107+ t.Error("Expected", 0)108+ }109+ if AsUint16(math.MaxUint16) != math.MaxUint16 {110+ t.Error("Expected", math.MaxUint16)111+ }112+ if AsUint16(math.MinInt32) != 0 {113+ t.Error("Expected", 0)114+ }115+ if AsUint16(math.MinInt16) != 0 {116+ t.Error("Expected", 0)117+ }118+ if AsUint16(math.MaxUint32) != math.MaxUint16 {119+ t.Error("Expected", math.MaxUint16)120+ }121+}122+123+func TestDurWithIn(t *testing.T) {124+ if DurWithin(time.Duration(5), time.Duration(1), time.Duration(8)) != time.Duration(5) {125+ t.Error("Expected", time.Duration(0))126+ }127+ if DurWithin(time.Duration(0)*time.Second, time.Second, time.Duration(3)*time.Second) != time.Second {128+ t.Error("Expected", time.Second)129+ }130+ if DurWithin(time.Duration(10)*time.Second, time.Duration(0), time.Second) != time.Second {131+ t.Error("Expected", time.Second)132+ }133+}134+135 func TestOnce(t *testing.T) {136 o := Once(false)137 if o() {138 t.Error("Expected: false")35 unmodified lines174 if width != 6 {175 t.Errorf("Expected: 6, actual: %d", width)176 }177 }178+179+func TestRepeatToFill(t *testing.T) {180+ if RepeatToFill("abcde", 10, 50) != strings.Repeat("abcde", 5) {181+ t.Error("Expected:", strings.Repeat("abcde", 5))182+ }183+ if RepeatToFill("abcde", 10, 42) != strings.Repeat("abcde", 4)+"abcde"[:2] {184+ t.Error("Expected:", strings.Repeat("abcde", 4)+"abcde"[:2])185+ }186+}