Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

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. IntMin Function:

    • IntMin is a simple function that returns the minimum of two integers.

  2. TestIntMinBasic:

    • TestIntMinBasic is a basic unit test that checks the result of IntMin with specific inputs.

  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. 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.

PreviousEmbed DirectiveNextCommand-Line Arguments

Last updated 1 year ago

Was this helpful?