Back to Repositories

Testing Counting Sort Algorithm Implementation in hello-algo

This test suite validates the implementation of counting sort algorithms in Go, examining both naive and optimized approaches for integer array sorting. The tests verify correct sorting behavior and output formatting.

Test Coverage Overview

The test coverage focuses on validating two implementations of counting sort algorithms.

  • Tests basic integer array sorting functionality
  • Verifies both naive and optimized counting sort implementations
  • Validates handling of duplicate values and zero elements
  • Confirms correct output formatting and array transformation

Implementation Analysis

The testing approach employs Go’s native testing framework with direct function invocation and output verification.

The test structure follows Go’s standard testing patterns with:
  • Single test function covering multiple implementations
  • Direct console output validation using fmt.Println
  • Parallel testing of naive and optimized implementations

Technical Details

Testing Infrastructure:

  • Go testing package (testing)
  • Standard fmt package for output formatting
  • Test runner configuration for Go unit tests
  • Direct array manipulation validation

Best Practices Demonstrated

The test implementation showcases several Go testing best practices.

  • Clear test function naming convention
  • Consistent input data across implementation comparisons
  • Direct verification of sorting results
  • Structured output formatting for debugging

krahets/hello-algo

zh-hant/codes/go/chapter_sorting/counting_sort_test.go

            
// File: counting_sort_test.go
// Created Time: 2023-03-20
// Author: Reanon ([email protected])

package chapter_sorting

import (
	"fmt"
	"testing"
)

func TestCountingSort(t *testing.T) {
	nums := []int{1, 0, 1, 2, 0, 4, 0, 2, 2, 4}
	countingSortNaive(nums)
	fmt.Println("計數排序(無法排序物件)完成後 nums = ", nums)

	nums1 := []int{1, 0, 1, 2, 0, 4, 0, 2, 2, 4}
	countingSort(nums1)
	fmt.Println("計數排序完成後 nums1 = ", nums1)
}