Back to Repositories

Testing Dynamic Programming Implementations for Climbing Stairs in hello-algo

This test suite implements and validates various dynamic programming solutions for the classic climbing stairs problem in Go. The tests cover different approaches including backtracking, DFS, memoization, and dynamic programming, along with cost optimization variants.

Test Coverage Overview

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

  • Tests both basic climbing stairs problem and minimum cost variants
  • Validates different solution approaches: backtracking, DFS, memoized DFS, and DP
  • Covers edge cases with n=9 steps consistently across implementations
  • Includes cost optimization tests with variable step costs

Implementation Analysis

The testing approach uses Go’s native testing framework with focused unit tests for each algorithm variant.

  • Implements table-driven tests using testing.T
  • Uses fmt.Printf for output validation
  • Consistent test structure across all implementations
  • Separate test functions for each algorithm variation

Technical Details

  • Testing Framework: Go testing package
  • Output Validation: fmt package for result verification
  • Test Structure: Individual test functions with consistent input cases
  • Input Parameters: Fixed test case of n=9 for comparison
  • Cost Testing: Array-based input for cost optimization tests

Best Practices Demonstrated

The test suite exhibits strong testing practices with clear organization and consistent methodology.

  • Consistent naming convention for test functions
  • Isolated test cases for each implementation
  • Clear output formatting for result verification
  • Comprehensive coverage of algorithm variations
  • Structured approach to testing both basic and complex scenarios

krahets/hello-algo

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)
}