Back to Repositories

Testing Dynamic Programming Climbing Stairs Implementations in hello-algo

This test suite implements and validates various dynamic programming solutions for the classic climbing stairs problem in Go. It explores different approaches including backtracking, DFS, memoization, and optimized DP solutions, while also testing minimum cost variations.

Test Coverage Overview

The test suite provides comprehensive coverage of multiple climbing stairs algorithm implementations:

  • Basic backtracking approach
  • Depth-first search implementation
  • DFS with memoization optimization
  • Standard dynamic programming solution
  • Space-optimized DP variant
  • Constrained DP implementation
  • Minimum cost climbing stairs variations

Implementation Analysis

The testing approach utilizes Go’s native testing framework to validate different algorithmic solutions. Each test function follows a consistent pattern of initializing test cases, executing the algorithm, and outputting results using fmt.Printf. The implementation demonstrates both recursive and iterative approaches to the climbing stairs problem.

Technical Details

Testing Tools & Configuration:

  • Go testing package (testing)
  • Standard fmt package for output formatting
  • Consistent test structure across all implementations
  • Fixed test case with n=9 for comparison
  • Additional cost array test case for minimum cost variants

Best Practices Demonstrated

The test suite exhibits several testing best practices:

  • Clear test function naming conventions
  • Consistent test case usage for comparison
  • Separate test functions for each implementation variant
  • Readable output formatting
  • Comprehensive algorithm coverage
  • Progressive complexity handling from basic to optimized solutions

krahets/hello-algo

zh-hant/codes/go/chapter_dynamic_programming/climbing_stairs_test.go

            
// File: climbing_stairs_test.go
// Created Time: 2023-07-18
// Author: Reanon ([email protected])

package chapter_dynamic_programming

import (
	"fmt"
	"testing"
)

func TestClimbingStairsBacktrack(t *testing.T) {
	n := 9
	res := climbingStairsBacktrack(n)
	fmt.Printf("爬 %d 階樓梯共有 %d 種方案\n", n, res)
}

func TestClimbingStairsDFS(t *testing.T) {
	n := 9
	res := climbingStairsDFS(n)
	fmt.Printf("爬 %d 階樓梯共有 %d 種方案\n", n, res)
}

func TestClimbingStairsDFSMem(t *testing.T) {
	n := 9
	res := climbingStairsDFSMem(n)
	fmt.Printf("爬 %d 階樓梯共有 %d 種方案\n", n, res)
}

func TestClimbingStairsDP(t *testing.T) {
	n := 9
	res := climbingStairsDP(n)
	fmt.Printf("爬 %d 階樓梯共有 %d 種方案\n", n, res)
}

func TestClimbingStairsDPComp(t *testing.T) {
	n := 9
	res := climbingStairsDPComp(n)
	fmt.Printf("爬 %d 階樓梯共有 %d 種方案\n", n, res)
}

func TestClimbingStairsConstraintDP(t *testing.T) {
	n := 9
	res := climbingStairsConstraintDP(n)
	fmt.Printf("爬 %d 階樓梯共有 %d 種方案\n", n, res)
}

func TestMinCostClimbingStairsDPComp(t *testing.T) {
	cost := []int{0, 1, 10, 1, 1, 1, 10, 1, 1, 10, 1}
	fmt.Printf("輸入樓梯的代價串列為 %v\n", cost)

	res := minCostClimbingStairsDP(cost)
	fmt.Printf("爬完樓梯的最低代價為 %d\n", res)

	res = minCostClimbingStairsDPComp(cost)
	fmt.Printf("爬完樓梯的最低代價為 %d\n", res)
}