Add remaining
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
package testbench
|
||||
|
||||
func IntMin(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
|
||||
return b
|
||||
}
|
||||
|
||||
46
testbench/testbench_test.go
Normal file
46
testbench/testbench_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package testbench
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIntMinBasic(t *testing.T) {
|
||||
ans := IntMin(2, -2)
|
||||
|
||||
if ans != -2 {
|
||||
t.Errorf("IntMain(2, -2) = %d, want -2", ans)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntMinTableDrive(t *testing.T) {
|
||||
var tests = []struct {
|
||||
a, b int
|
||||
want int
|
||||
}{
|
||||
{0, 1, 0},
|
||||
{1, 0, 0},
|
||||
{2, -2, -2},
|
||||
{0, -1, -1},
|
||||
{-1, 0, -1},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
testName := fmt.Sprintf("%d,%d", tt.a, tt.b)
|
||||
t.Run(testName, func(t *testing.T) {
|
||||
ans := IntMin(tt.a, tt.b)
|
||||
if ans != tt.want {
|
||||
t.Errorf("got %d, want %d", ans, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkIntMin(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
IntMin(1, 2)
|
||||
}
|
||||
}
|
||||
|
||||
// go test -v # to run all tests in current project in verbose mode
|
||||
// go test -bench # to run all benchmarks in current project
|
||||
Reference in New Issue
Block a user