Back to Repositories

Testing Dynamic Array List Implementation in hello-algo

This test suite validates the implementation of a dynamic array list data structure in Go, focusing on core operations and capacity management. The tests verify array manipulation methods including insertion, deletion, access, and automatic resizing functionality.

Test Coverage Overview

The test suite provides comprehensive coverage of array list operations:
  • Basic operations: add, insert, remove, get, set
  • Dynamic capacity management and resizing
  • Edge cases: index-based operations
  • Array state validation: size and capacity tracking

Implementation Analysis

The testing approach utilizes Go’s native testing framework with a focus on functional verification. The test demonstrates sequential operation validation with clear state checking between operations, using fmt.Printf for operation visibility and state tracking.

The implementation follows Go’s testing patterns with a single TestMyList function that progresses through various array operations systematically.

Technical Details

Testing Infrastructure:
  • Go testing package (testing)
  • Standard fmt package for output formatting
  • Custom MyList implementation with array management
  • Integrated capacity tracking and dynamic resizing

Best Practices Demonstrated

The test exhibits several quality testing practices:
  • Comprehensive operation verification
  • Clear state logging between operations
  • Systematic testing progression
  • Performance consideration through capacity testing
  • Readable test structure with clear operation separation

krahets/hello-algo

zh-hant/codes/go/chapter_array_and_linkedlist/my_list_test.go

            
// File: my_list_test.go
// Created Time: 2022-12-18
// Author: msk397 ([email protected])

package chapter_array_and_linkedlist

import (
	"fmt"
	"testing"
)

/* Driver Code */
func TestMyList(t *testing.T) {
	/* 初始化串列 */
	nums := newMyList()
	/* 在尾部新增元素 */
	nums.add(1)
	nums.add(3)
	nums.add(2)
	nums.add(5)
	nums.add(4)
	fmt.Printf("串列 nums = %v ,容量 = %v ,長度 = %v\n", nums.toArray(), nums.capacity(), nums.size())

	/* 在中間插入元素 */
	nums.insert(6, 3)
	fmt.Printf("在索引 3 處插入數字 6 ,得到 nums = %v\n", nums.toArray())

	/* 刪除元素 */
	nums.remove(3)
	fmt.Printf("刪除索引 3 處的元素,得到 nums = %v\n", nums.toArray())

	/* 訪問元素 */
	num := nums.get(1)
	fmt.Printf("訪問索引 1 處的元素,得到 num = %v\n", num)

	/* 更新元素 */
	nums.set(0, 1)
	fmt.Printf("將索引 1 處的元素更新為 0 ,得到 nums = %v\n", nums.toArray())

	/* 測試擴容機制 */
	for i := 0; i < 10; i++ {
		// 在 i = 5 時,串列長度將超出串列容量,此時觸發擴容機制
		nums.add(i)
	}
	fmt.Printf("擴容後的串列 nums = %v ,容量 = %v ,長度 = %v\n", nums.toArray(), nums.capacity(), nums.size())
}