Back to Repositories

Testing Permutation Algorithm Implementation in hello-algo

This test suite validates permutation algorithms implementation in Go, focusing on both unique and duplicate element permutations. It ensures correct generation of all possible permutations for given input arrays using backtracking techniques.

Test Coverage Overview

The test coverage encompasses two main permutation scenarios:
  • TestPermutationI: Tests permutations with unique elements
  • TestPermutationII: Tests permutations with duplicate elements
Key functionality includes array permutation generation and output validation, with edge cases handling duplicate elements in the input array.

Implementation Analysis

The testing approach utilizes Go’s native testing framework with focused test cases for different permutation scenarios. It implements a structured pattern for input preparation, permutation generation, and result verification, leveraging Go’s testing.T structure for assertions and output formatting.

Technical Details

Testing tools and configuration:
  • Go testing package for test execution
  • Custom PrintSlice utility for array visualization
  • External package imports from hello-algo/pkg
  • Standard Go test naming conventions

Best Practices Demonstrated

The test suite demonstrates strong testing practices through clear test case separation, descriptive test names, and proper output formatting. Each test function focuses on a specific permutation scenario, maintaining single responsibility principle while providing comprehensive coverage of the algorithm’s functionality.

krahets/hello-algo

zh-hant/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)
}