Back to Repositories

Testing Recursive Function Implementations in hello-algo

This test suite examines recursive function implementations in Go, focusing on various recursion patterns including standard recursion, iterative simulation, and tail recursion. The tests validate both basic summation algorithms and Fibonacci sequence calculations.

Test Coverage Overview

The test suite provides comprehensive coverage of recursive function implementations.

Key areas tested include:
  • Standard recursive summation
  • Iterative simulation of recursion
  • Tail recursion optimization
  • Fibonacci sequence calculation
Edge cases are verified through different input values, particularly focusing on the base case n=5.

Implementation Analysis

The testing approach utilizes Go’s native testing framework to validate different recursive implementations. The test structure employs a single test function that examines multiple recursive patterns, allowing for direct comparison of different approaches.

Technical patterns include:
  • Direct recursive function testing
  • Output validation through fmt.Println
  • Multiple recursive implementation comparisons

Technical Details

Testing infrastructure includes:
  • Go testing package (testing)
  • Standard output verification (fmt)
  • Single test function architecture
  • Manual result inspection through console output

Best Practices Demonstrated

The test implementation showcases several testing best practices in Go.

Notable practices include:
  • Clear test function naming
  • Consistent test structure
  • Multiple implementation validation
  • Proper package organization
  • Clear output formatting for result verification

krahets/hello-algo

zh-hant/codes/go/chapter_computational_complexity/recursion_test.go

            
// File: recursion_test.go
// Created Time: 2023-08-28
// Author: Reanon ([email protected])

package chapter_computational_complexity

import (
	"fmt"
	"testing"
)

/* Driver Code */
func TestRecursion(t *testing.T) {
	n := 5
	res := recur(n)
	fmt.Println("\n遞迴函式的求和結果 res = ", res)

	res = forLoopRecur(n)
	fmt.Println("\n使用迭代模擬遞迴求和結果 res = ", res)

	res = tailRecur(n, 0)
	fmt.Println("\n尾遞迴函式的求和結果 res = ", res)

	res = fib(n)
	fmt.Println("\n費波那契數列的第", n, "項為", res)
}