Back to Repositories

Testing Space Complexity Implementations in hello-algo

This test suite evaluates space complexity implementations across different algorithmic patterns in Go. It validates various space complexity scenarios including constant, linear, quadratic, and exponential complexity cases through both iterative and recursive approaches.

Test Coverage Overview

The test suite provides comprehensive coverage of space complexity patterns.

Key areas tested include:
  • Constant space complexity operations
  • Linear space complexity in both iterative and recursive implementations
  • Quadratic space complexity patterns
  • Exponential space complexity using tree structures
Edge cases are verified through recursive function calls and tree building operations.

Implementation Analysis

The testing approach utilizes Go’s native testing framework to validate different space complexity scenarios. The implementation employs a systematic verification of space usage patterns, from simple constant-space operations to complex tree-based exponential space requirements.

Key patterns include:
  • Direct function invocation testing
  • Tree structure validation
  • Recursive function testing
  • Visual tree output verification

Technical Details

Testing tools and configuration:
  • Go testing package (testing)
  • Custom tree visualization package (hello-algo/pkg)
  • Single test function architecture
  • Integrated tree printing functionality

Best Practices Demonstrated

The test implementation showcases several testing quality practices in Go.

Notable practices include:
  • Clear test function organization
  • Comprehensive algorithm category coverage
  • Efficient test case structuring
  • Integration with custom utility packages
  • Proper import management

krahets/hello-algo

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

            
// File: space_complexity_test.go
// Created Time: 2022-12-15
// Author: cathay ([email protected])

package chapter_computational_complexity

import (
	"testing"

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

func TestSpaceComplexity(t *testing.T) {
	n := 5
	// 常數階
	spaceConstant(n)
	// 線性階
	spaceLinear(n)
	spaceLinearRecur(n)
	// 平方階
	spaceQuadratic(n)
	spaceQuadraticRecur(n)
	// 指數階
	root := buildTree(n)
	PrintTree(root)
}