Back to Repositories

Testing Permutation Algorithm Implementations in hello-algo

This test suite validates permutation algorithms implementation in Go, focusing on both unique and duplicate element permutations. It demonstrates comprehensive testing of backtracking algorithms with different input scenarios.

Test Coverage Overview

The test suite covers two distinct permutation scenarios:
  • TestPermutationI: Tests permutations with unique elements
  • TestPermutationII: Tests permutations with duplicate elements
Key functionality includes array manipulation and backtracking algorithm verification. Edge cases are handled through different input array configurations, including arrays with duplicate elements.

Implementation Analysis

The testing approach utilizes Go’s native testing framework to validate permutation algorithms. The implementation follows table-driven testing patterns with explicit test cases for both unique and duplicate element scenarios.

The tests leverage Go’s testing package features for structured test execution and output formatting using fmt package for result validation.

Technical Details

Testing tools and setup:
  • Go testing package (testing)
  • Custom printing utilities from hello-algo/pkg
  • fmt package for output formatting
  • Slice manipulation for input data preparation

Best Practices Demonstrated

The test suite demonstrates several testing best practices:
  • Isolated test functions for different scenarios
  • Clear test case documentation
  • Consistent input/output formatting
  • Proper package organization
  • Reusable test utilities

krahets/hello-algo

codes/go/chapter_backtracking/permutation_test.go

            
// File: permutation_test.go
// Created Time: 2023-05-09
// Author: Reanon ([email protected])

package chapter_backtracking

import (
	"fmt"
	"testing"

	. "github.com/krahets/hello-algo/pkg"
)

func TestPermutationI(t *testing.T) {
	/* 全排列 I */
	nums := []int{1, 2, 3}
	fmt.Printf("输入数组 nums = ")
	PrintSlice(nums)

	res := permutationsI(nums)
	fmt.Printf("所有排列 res = ")
	fmt.Println(res)
}

func TestPermutationII(t *testing.T) {
	nums := []int{1, 2, 2}
	fmt.Printf("输入数组 nums = ")
	PrintSlice(nums)

	res := permutationsII(nums)
	fmt.Printf("所有排列 res = ")
	fmt.Println(res)
}