Code with Go
Ask or search…
K
Links

Testing and Benchmarking

This Go code defines a simple function (IntMin) and includes tests and a benchmark for it using the testing package. Let's go through the code with inline comments:
package main
import (
"fmt"
"testing"
)
// IntMin returns the minimum of two integers.
func IntMin(a, b int) int {
if a < b {
return a
}
return b
}
// TestIntMinBasic is a basic unit test for IntMin function.
func TestIntMinBasic(t *testing.T) {
// Test with specific inputs and check the result.
ans := IntMin(2, -2)
if ans != -2 {
t.Errorf("IntMin(2, -2) = %d; want -2", ans)
}
}
// TestIntMinTableDriven is a table-driven test for IntMin function.
func TestIntMinTableDriven(t *testing.T) {
// Define a table of test cases with inputs and expected results.
var tests = []struct {
a, b int
want int
}{
{0, 1, 0},
{1, 0, 0},
{2, -2, -2},
{0, -1, -1},
{-1, 0, -1},
}
// Iterate over the test cases and run subtests for each.
for _, tt := range tests {
// Generate a test name based on inputs.
testname := fmt.Sprintf("%d,%d", tt.a, tt.b)
// Run a subtest for each case.
t.Run(testname, func(t *testing.T) {
// Call the IntMin function and check the result.
ans := IntMin(tt.a, tt.b)
if ans != tt.want {
t.Errorf("got %d, want %d", ans, tt.want)
}
})
}
}
// BenchmarkIntMin is a benchmark for the IntMin function.
func BenchmarkIntMin(b *testing.B) {
// Run the IntMin function repeatedly to measure performance.
for i := 0; i < b.N; i++ {
IntMin(1, 2)
}
}
Explanation:
  1. 1.
    IntMin Function:
    • IntMin is a simple function that returns the minimum of two integers.
  2. 2.
    TestIntMinBasic:
    • TestIntMinBasic is a basic unit test that checks the result of IntMin with specific inputs.
  3. 3.
    TestIntMinTableDriven:
    • TestIntMinTableDriven is a table-driven test where multiple test cases are defined in a table, and subtests are created for each case.
  4. 4.
    BenchmarkIntMin:
    • BenchmarkIntMin is a benchmark test that measures the performance of the IntMin function by running it repeatedly.
The testing package in Go provides a convenient way to write unit tests and benchmarks for your code.