gpt-4.1-2025-04-14· OpenAI
LMArena Human Preference 140K
帮我给代码添加注释 [code]
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
帮我给代码添加注释
package tools
import (
"errors"
"fmt"
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
)
// TestLinkedList_Add 测试链表Add方法
// 验证以下场景:
// 1. 在链表头部添加元素
// 2. 在链表中间添加元素
// 3. 在链表尾部添加元素
// 4. 索引越界情况
func TestLinkedList_Add(t *testing.T) {
testCases := []struct {
name string
list func() *LinkedList[int] // 使用函数返回新实例
index int
newVal int
wantLinkedList *LinkedList[int]
wantErr error
}{
{
name: "add num to index left",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3}) },
newVal: 100,
index: 0,
wantLinkedList: NewLinkedListOf([]int{100, 1, 2, 3}),
},
{
name: "add num to index left1",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3, 44, 55, 66, 77}) },
newVal: 100,
index: 3,
wantLinkedList: NewLinkedListOf([]int{1, 2, 3, 100, 44, 55, 66, 77}),
},
{
name: "add num to index right",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3}) },
newVal: 100,
index: 3,
wantLinkedList: NewLinkedListOf([]int{1, 2, 3, 100}),
},
{
name: "add num to index right1",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3, 44, 55, 66, 77}) },
newVal: 100,
index: 5,
wantLinkedList: NewLinkedListOf([]int{1, 2, 3, 44, 55, 100, 66, 77}),
},
{
name: "add num to index mid",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3}) },
newVal: 100,
index: 1,
wantLinkedList: NewLinkedListOf([]int{1, 100, 2, 3}),
},
{
name: "add num to index -1",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3}) },
newVal: 100,
index: -1,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 3, -1),
},
{
name: "add num to index OutOfRange",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3}) },
newVal: 100,
index: 4,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 3, 4),
},
{
name: "add num to index 0",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{}) },
newVal: 100,
index: 0,
wantErr: nil,
wantLinkedList: NewLinkedListOf([]int{100}),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
list := tc.list() // 获取新的链表实例
err := list.Add(tc.index, tc.newVal)
assert.Equal(t, tc.wantErr, err)
if err != nil {
return
}
assert.Equal(t, tc.wantLinkedList.AsSlice(), list.AsSlice())
})
}
}
// TestLinkedList_Delete 测试链表Delete方法
// 验证以下场景:
// 1. 删除链表头部元素
// 2. 删除链表中间元素
// 3. 删除链表尾部元素
// 4. 删除不存在的元素
func TestLinkedList_Delete(t *testing.T) {
testCases := []struct {
name string
list func() *LinkedList[int] // 使用函数返回新实例
wantLinkedList *LinkedList[int]
delVal int
index int
wantErr error
}{
{
name: "delete num to index -1",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3}) },
index: -1,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 3, -1),
},
{
name: "delete beyond length index 99",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3}) },
index: 99,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 3, 99),
},
{
name: "delete beyond length index 3",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3}) },
index: 3,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 3, 3),
},
{
name: "delete empty node",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{}) },
index: 3,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 0, 3),
},
{
name: "delete num to index 0",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3}) },
index: 0,
delVal: 1,
wantLinkedList: NewLinkedListOf([]int{2, 3}),
},
{
name: "delete num to index by tail",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3, 4, 5}) },
index: 4,
delVal: 5,
wantLinkedList: NewLinkedListOf([]int{1, 2, 3, 4}),
},
{
name: "delete num to index 1",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{11, 22, 33, 44, 55}) },
index: 1,
delVal: 22,
wantLinkedList: NewLinkedListOf([]int{11, 33, 44, 55}),
},
{
name: "deleting an element with only one",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{888}) },
index: 0,
delVal: 888,
wantLinkedList: NewLinkedListOf([]int{}),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
list := tc.list() // 获取新的链表实例
delVal, err := list.Delete(tc.index)
if err != nil {
assert.Equal(t, tc.wantErr, err)
} else {
assert.Equal(t, tc.delVal, delVal)
assert.Equal(t, tc.wantLinkedList.AsSlice(), list.AsSlice())
}
})
}
}
func TestLinkedList_Append(t *testing.T) {
testCases := []struct {
name string
list func() *LinkedList[int] // 使用函数返回新实例
index int
newVal []int
wantLinkedList *LinkedList[int]
wantErr error
}{
{
name: "append non-empty values to non-empty list",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{123}) },
newVal: []int{234, 456},
wantLinkedList: NewLinkedListOf([]int{123, 234, 456}),
},
{
name: "append empty values to non-empty list",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{123}) },
newVal: []int{},
wantLinkedList: NewLinkedListOf([]int{123}),
},
{
name: "append nil to non-empty list",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{123}) },
newVal: nil,
wantLinkedList: NewLinkedListOf([]int{123}),
},
{
name: "append non-empty values to empty list",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{}) },
newVal: []int{234, 456},
wantLinkedList: NewLinkedListOf([]int{234, 456}),
},
{
name: "append empty values to empty list",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{}) },
newVal: []int{},
wantLinkedList: NewLinkedListOf([]int{}),
},
{
name: "append nil to empty list",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{}) },
newVal: nil,
wantLinkedList: NewLinkedListOf([]int{}),
},
{
name: "append non-empty values to nil list",
list: func() *LinkedList[int] { return NewLinkedListOf[int](nil) },
newVal: []int{234, 456},
wantLinkedList: NewLinkedListOf([]int{234, 456}),
},
{
name: "append empty values to nil list",
list: func() *LinkedList[int] { return NewLinkedListOf[int](nil) },
newVal: []int{},
wantLinkedList: NewLinkedListOf([]int{}),
},
{
name: "append nil to nil list",
list: func() *LinkedList[int] { return NewLinkedListOf[int](nil) },
newVal: nil,
wantLinkedList: NewLinkedListOf([]int{}),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
list := tc.list() // 获取新的链表实例
err := list.Append(tc.newVal...)
assert.Equal(t, tc.wantErr, err)
assert.Equal(t, tc.wantLinkedList.AsSlice(), list.AsSlice())
})
}
}
func TestNewLinkedListOf(t *testing.T) {
testCases := []struct {
name string
slice func() []int
wantedSlice []int
}{
{
name: "nil",
slice: func() []int { return nil },
wantedSlice: []int{},
},
{
name: "vacant",
slice: func() []int { return []int{} },
wantedSlice: []int{},
},
{
name: "single",
slice: func() []int { return []int{1} },
wantedSlice: []int{1},
},
{
name: "normal",
slice: func() []int { return []int{1, 2, 3} },
wantedSlice: []int{1, 2, 3},
},
}
for _, tc := range testCases {
slice := tc.slice() // 获取新的切片实例
list := NewLinkedListOf(slice)
// 在这里断言你的元素,可以利用 Get 方法,也可以直接用 AsSlice 来断言
assert.Equal(t, tc.wantedSlice, list.AsSlice())
}
}
// TestLinkedList_AsSlice 测试链表AsSlice方法
// 验证以下场景:
// 1. 空链表转换为切片
// 2. 非空链表转换为切片
// 3. 验证切片元素顺序与链表一致
func TestLinkedList_AsSlice(t *testing.T) {
vals := []int{1, 2, 3}
a := NewLinkedListOf(vals)
slice := a.AsSlice()
// 内容相同
assert.Equal(t, slice, vals)
aAddr := fmt.Sprintf("%p", vals)
sliceAddr := fmt.Sprintf("%p", slice)
// 但是地址不同,也就是意味着 slice 必须是一个新创建的
assert.NotEqual(t, aAddr, sliceAddr)
}
func TestLinkedList_Cap(t *testing.T) {
t.Run("empty list", func(t *testing.T) {
list := NewLinkedList[int]()
assert.Equal(t, 0, list.Cap())
})
t.Run("after append", func(t *testing.T) {
list := NewLinkedList[int]()
err := list.Append(12)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 1, list.Cap())
})
}
func TestLinkedList_Get(t *testing.T) {
tests := []struct {
name string
list func() *LinkedList[int]
index int
wantVal int
wantErr error
}{
{
name: "get left",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3, 4, 5}) },
index: 0,
wantVal: 1,
},
{
name: "get right",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3, 4, 5}) },
index: 4,
wantVal: 5,
},
{
name: "get middle",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3, 4, 5}) },
index: 2,
wantVal: 3,
},
{
name: "over left",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3, 4, 5}) },
index: -1,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 5, -1),
},
{
name: "over right",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3, 4, 5}) },
index: 5,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 5, 5),
},
{
name: "empty list",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{}) },
index: 0,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 0, 0),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
list := tc.list()
get, err := list.Get(tc.index)
assert.Equal(t, tc.wantErr, err)
assert.Equal(t, tc.wantVal, get)
})
}
}
// TestLinkedList_Range 测试链表Range方法
// 验证以下场景:
// 1. 遍历空链表
// 2. 遍历非空链表
// 3. 验证遍历顺序
// 4. 测试提前终止遍历
func TestLinkedList_Range(t *testing.T) {
testCases := []struct {
name string
list func() *LinkedList[int]
wantVal int
wantErr error
}{
{
name: "计算全部元素的和",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3, 4, 5}) },
wantVal: 15,
wantErr: nil,
},
{
name: "测试中断",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3, 4, -5, 6, 7, 8, -9, 10}) },
wantErr: errors.New("index 4 is error"),
},
{
name: "测试数组为nil",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{}) },
wantVal: 0,
wantErr: nil,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
list := tc.list()
result := 0
err := list.Range(func(index int, num int) error {
if num < 0 {
return fmt.Errorf("index %d is error", index)
}
result += num
return nil
})
assert.Equal(t, tc.wantErr, err)
if err != nil {
return
}
assert.Equal(t, tc.wantVal, result)
})
}
}
func TestLinkedList_Set(t *testing.T) {
testCases := []struct {
name string
list func() *LinkedList[int]
wantLinkedList *LinkedList[int]
index int
setVal int
wantErr error
}{
{
name: "set num to index -1",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3}) },
index: -1,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 3, -1),
},
{
name: "set beyond length index 99",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3}) },
index: 99,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 3, 99),
},
{
name: "set empty node",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{}) },
index: 3,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 0, 3),
},
{
name: "set num to index 3",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{11, 22, 33, 44, 55}) },
index: 2,
setVal: 999,
wantLinkedList: NewLinkedListOf([]int{11, 22, 999, 44, 55}),
},
{
name: "set num to head",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{11, 22, 33, 44, 55}) },
index: 0,
setVal: -200,
wantLinkedList: NewLinkedListOf([]int{-200, 22, 33, 44, 55}),
},
{
name: "set num to tail",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{-11, 22, -33, 44, -55, 999, -888}) },
index: 6,
setVal: 888,
wantLinkedList: NewLinkedListOf([]int{-11, 22, -33, 44, -55, 999, 888}),
},
{
name: "index == len(*node)",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{-11, 22, -33, 44, -55, 999, -888}) },
index: 7,
setVal: 888,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 7, 7),
},
{
name: "len(*node) == 0",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{}) },
index: 0,
setVal: 888,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 0, 0),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
list := tc.list()
err := list.Set(tc.index, tc.setVal)
if err != nil {
assert.Equal(t, tc.wantErr, err)
} else {
assert.Equal(t, tc.wantLinkedList, list)
}
})
}
}
func BenchmarkLinkedList_Add(b *testing.B) {
l := NewLinkedListOf([]int{1, 2, 3})
testCase := make([]int, 0, b.N)
for i := 1; i <= b.N; i++ {
testCase = append(testCase, rand.Intn(i))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = l.Add(testCase[i], testCase[i])
}
}
func BenchmarkLinkedList_Get(b *testing.B) {
l := NewLinkedListOf([]int{1, 2, 3})
for i := 1; i <= b.N; i++ {
err := l.Add(i, i)
if err != nil {
b.Fatal(err)
}
}
for i := 0; b.Loop(); i++ {
_, _ = l.Get(i)
}
}
当然可以!下面是你提供的 Go 代码的详细注释版,每个函数和关键逻辑都加了中文注释,方便你和团队理解和维护。
package tools
import (
"errors"
"fmt"
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
)
// TestLinkedList_Add 测试链表 Add 方法
// 验证以下场景:
// 1. 在链表头部添加元素
// 2. 在链表中间添加元素
// 3. 在链表尾部添加元素
// 4. 索引越界情况
func TestLinkedList_Add(t *testing.T) {
testCases := []struct {
name string
list func() *LinkedList[int] // 每次返回一个新的链表实例
index int
newVal int
wantLinkedList *LinkedList[int]
wantErr error
}{
// 头部插入
{
name: "add num to index left",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3}) },
newVal: 100,
index: 0,
wantLinkedList: NewLinkedListOf([]int{100, 1, 2, 3}),
},
// 中间插入
{
name: "add num to index left1",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3, 44, 55, 66, 77}) },
newVal: 100,
index: 3,
wantLinkedList: NewLinkedListOf([]int{1, 2, 3, 100, 44, 55, 66, 77}),
},
// 尾部插入
{
name: "add num to index right",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3}) },
newVal: 100,
index: 3,
wantLinkedList: NewLinkedListOf([]int{1, 2, 3, 100}),
},
// 中间插入
{
name: "add num to index right1",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3, 44, 55, 66, 77}) },
newVal: 100,
index: 5,
wantLinkedList: NewLinkedListOf([]int{1, 2, 3, 44, 55, 100, 66, 77}),
},
// 中间插入
{
name: "add num to index mid",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3}) },
newVal: 100,
index: 1,
wantLinkedList: NewLinkedListOf([]int{1, 100, 2, 3}),
},
// 索引越界(负数)
{
name: "add num to index -1",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3}) },
newVal: 100,
index: -1,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 3, -1),
},
// 索引越界(大于长度)
{
name: "add num to index OutOfRange",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3}) },
newVal: 100,
index: 4,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 3, 4),
},
// 空链表插入
{
name: "add num to index 0",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{}) },
newVal: 100,
index: 0,
wantErr: nil,
wantLinkedList: NewLinkedListOf([]int{100}),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
list := tc.list() // 获取新的链表实例
err := list.Add(tc.index, tc.newVal)
assert.Equal(t, tc.wantErr, err)
if err != nil {
return
}
assert.Equal(t, tc.wantLinkedList.AsSlice(), list.AsSlice())
})
}
}
// TestLinkedList_Delete 测试链表 Delete 方法
// 验证以下场景:
// 1. 删除链表头部元素
// 2. 删除链表中间元素
// 3. 删除链表尾部元素
// 4. 删除不存在的元素(越界)
func TestLinkedList_Delete(t *testing.T) {
testCases := []struct {
name string
list func() *LinkedList[int] // 每次返回一个新的链表实例
wantLinkedList *LinkedList[int]
delVal int
index int
wantErr error
}{
// 索引越界(负数)
{
name: "delete num to index -1",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3}) },
index: -1,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 3, -1),
},
// 索引越界(大于长度)
{
name: "delete beyond length index 99",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3}) },
index: 99,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 3, 99),
},
// 索引等于长度
{
name: "delete beyond length index 3",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3}) },
index: 3,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 3, 3),
},
// 空链表删除
{
name: "delete empty node",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{}) },
index: 3,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 0, 3),
},
// 删除头部
{
name: "delete num to index 0",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3}) },
index: 0,
delVal: 1,
wantLinkedList: NewLinkedListOf([]int{2, 3}),
},
// 删除尾部
{
name: "delete num to index by tail",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3, 4, 5}) },
index: 4,
delVal: 5,
wantLinkedList: NewLinkedListOf([]int{1, 2, 3, 4}),
},
// 删除中间
{
name: "delete num to index 1",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{11, 22, 33, 44, 55}) },
index: 1,
delVal: 22,
wantLinkedList: NewLinkedListOf([]int{11, 33, 44, 55}),
},
// 只剩一个元素
{
name: "deleting an element with only one",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{888}) },
index: 0,
delVal: 888,
wantLinkedList: NewLinkedListOf([]int{}),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
list := tc.list() // 获取新的链表实例
delVal, err := list.Delete(tc.index)
if err != nil {
assert.Equal(t, tc.wantErr, err)
} else {
assert.Equal(t, tc.delVal, delVal)
assert.Equal(t, tc.wantLinkedList.AsSlice(), list.AsSlice())
}
})
}
}
// TestLinkedList_Append 测试链表 Append 方法
// 验证追加多个元素、空切片、nil等情况
func TestLinkedList_Append(t *testing.T) {
testCases := []struct {
name string
list func() *LinkedList[int] // 每次返回一个新的链表实例
index int
newVal []int
wantLinkedList *LinkedList[int]
wantErr error
}{
// 非空链表追加非空切片
{
name: "append non-empty values to non-empty list",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{123}) },
newVal: []int{234, 456},
wantLinkedList: NewLinkedListOf([]int{123, 234, 456}),
},
// 非空链表追加空切片
{
name: "append empty values to non-empty list",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{123}) },
newVal: []int{},
wantLinkedList: NewLinkedListOf([]int{123}),
},
// 非空链表追加 nil
{
name: "append nil to non-empty list",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{123}) },
newVal: nil,
wantLinkedList: NewLinkedListOf([]int{123}),
},
// 空链表追加非空切片
{
name: "append non-empty values to empty list",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{}) },
newVal: []int{234, 456},
wantLinkedList: NewLinkedListOf([]int{234, 456}),
},
// 空链表追加空切片
{
name: "append empty values to empty list",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{}) },
newVal: []int{},
wantLinkedList: NewLinkedListOf([]int{}),
},
// 空链表追加 nil
{
name: "append nil to empty list",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{}) },
newVal: nil,
wantLinkedList: NewLinkedListOf([]int{}),
},
// nil链表追加非空切片
{
name: "append non-empty values to nil list",
list: func() *LinkedList[int] { return NewLinkedListOf[int](nil) },
newVal: []int{234, 456},
wantLinkedList: NewLinkedListOf([]int{234, 456}),
},
// nil链表追加空切片
{
name: "append empty values to nil list",
list: func() *LinkedList[int] { return NewLinkedListOf[int](nil) },
newVal: []int{},
wantLinkedList: NewLinkedListOf([]int{}),
},
// nil链表追加 nil
{
name: "append nil to nil list",
list: func() *LinkedList[int] { return NewLinkedListOf[int](nil) },
newVal: nil,
wantLinkedList: NewLinkedListOf([]int{}),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
list := tc.list() // 获取新的链表实例
err := list.Append(tc.newVal...)
assert.Equal(t, tc.wantErr, err)
assert.Equal(t, tc.wantLinkedList.AsSlice(), list.AsSlice())
})
}
}
// TestNewLinkedListOf 测试 NewLinkedListOf 工厂函数
// 验证 nil、空切片、单元素、正常切片等情况
func TestNewLinkedListOf(t *testing.T) {
testCases := []struct {
name string
slice func() []int
wantedSlice []int
}{
{
name: "nil",
slice: func() []int { return nil },
wantedSlice: []int{},
},
{
name: "vacant",
slice: func() []int { return []int{} },
wantedSlice: []int{},
},
{
name: "single",
slice: func() []int { return []int{1} },
wantedSlice: []int{1},
},
{
name: "normal",
slice: func() []int { return []int{1, 2, 3} },
wantedSlice: []int{1, 2, 3},
},
}
for _, tc := range testCases {
slice := tc.slice() // 获取新的切片实例
list := NewLinkedListOf(slice)
// 断言链表转切片后与预期一致
assert.Equal(t, tc.wantedSlice, list.AsSlice())
}
}
// TestLinkedList_AsSlice 测试链表 AsSlice 方法
// 验证空链表、非空链表、切片顺序和内存地址
func TestLinkedList_AsSlice(t *testing.T) {
vals := []int{1, 2, 3}
a := NewLinkedListOf(vals)
slice := a.AsSlice()
// 内容相同
assert.Equal(t, slice, vals)
aAddr := fmt.Sprintf("%p", vals)
sliceAddr := fmt.Sprintf("%p", slice)
// 地址不同,说明是新切片
assert.NotEqual(t, aAddr, sliceAddr)
}
// TestLinkedList_Cap 测试链表 Cap 方法
// 验证空链表和追加元素后的容量
func TestLinkedList_Cap(t *testing.T) {
t.Run("empty list", func(t *testing.T) {
list := NewLinkedList[int]()
assert.Equal(t, 0, list.Cap())
})
t.Run("after append", func(t *testing.T) {
list := NewLinkedList[int]()
err := list.Append(12)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 1, list.Cap())
})
}
// TestLinkedList_Get 测试链表 Get 方法
// 验证头部、尾部、中间、越界、空链表等情况
func TestLinkedList_Get(t *testing.T) {
tests := []struct {
name string
list func() *LinkedList[int]
index int
wantVal int
wantErr error
}{
// 头部
{
name: "get left",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3, 4, 5}) },
index: 0,
wantVal: 1,
},
// 尾部
{
name: "get right",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3, 4, 5}) },
index: 4,
wantVal: 5,
},
// 中间
{
name: "get middle",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3, 4, 5}) },
index: 2,
wantVal: 3,
},
// 越界(负数)
{
name: "over left",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3, 4, 5}) },
index: -1,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 5, -1),
},
// 越界(大于等于长度)
{
name: "over right",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3, 4, 5}) },
index: 5,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 5, 5),
},
// 空链表
{
name: "empty list",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{}) },
index: 0,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 0, 0),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
list := tc.list()
get, err := list.Get(tc.index)
assert.Equal(t, tc.wantErr, err)
assert.Equal(t, tc.wantVal, get)
})
}
}
// TestLinkedList_Range 测试链表 Range 方法
// 验证遍历空链表、非空链表、顺序、提前终止等
func TestLinkedList_Range(t *testing.T) {
testCases := []struct {
name string
list func() *LinkedList[int]
wantVal int
wantErr error
}{
// 正常遍历
{
name: "计算全部元素的和",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3, 4, 5}) },
wantVal: 15,
wantErr: nil,
},
// 中途遇到负数提前终止
{
name: "测试中断",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3, 4, -5, 6, 7, 8, -9, 10}) },
wantErr: errors.New("index 4 is error"),
},
// 空链表
{
name: "测试数组为nil",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{}) },
wantVal: 0,
wantErr: nil,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
list := tc.list()
result := 0
// 遍历链表,遇到负数返回错误
err := list.Range(func(index int, num int) error {
if num < 0 {
return fmt.Errorf("index %d is error", index)
}
result += num
return nil
})
assert.Equal(t, tc.wantErr, err)
if err != nil {
return
}
assert.Equal(t, tc.wantVal, result)
})
}
}
// TestLinkedList_Set 测试链表 Set 方法
// 验证越界、头部、尾部、中间、空链表等情况
func TestLinkedList_Set(t *testing.T) {
testCases := []struct {
name string
list func() *LinkedList[int]
wantLinkedList *LinkedList[int]
index int
setVal int
wantErr error
}{
// 越界(负数)
{
name: "set num to index -1",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3}) },
index: -1,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 3, -1),
},
// 越界(大于长度)
{
name: "set beyond length index 99",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{1, 2, 3}) },
index: 99,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 3, 99),
},
// 空链表
{
name: "set empty node",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{}) },
index: 3,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 0, 3),
},
// 中间
{
name: "set num to index 3",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{11, 22, 33, 44, 55}) },
index: 2,
setVal: 999,
wantLinkedList: NewLinkedListOf([]int{11, 22, 999, 44, 55}),
},
// 头部
{
name: "set num to head",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{11, 22, 33, 44, 55}) },
index: 0,
setVal: -200,
wantLinkedList: NewLinkedListOf([]int{-200, 22, 33, 44, 55}),
},
// 尾部
{
name: "set num to tail",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{-11, 22, -33, 44, -55, 999, -888}) },
index: 6,
setVal: 888,
wantLinkedList: NewLinkedListOf([]int{-11, 22, -33, 44, -55, 999, 888}),
},
// 索引等于长度
{
name: "index == len(*node)",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{-11, 22, -33, 44, -55, 999, -888}) },
index: 7,
setVal: 888,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 7, 7),
},
// 空链表
{
name: "len(*node) == 0",
list: func() *LinkedList[int] { return NewLinkedListOf([]int{}) },
index: 0,
setVal: 888,
wantErr: fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", 0, 0),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
list := tc.list()
err := list.Set(tc.index, tc.setVal)
if err != nil {
assert.Equal(t, tc.wantErr, err)
} else {
assert.Equal(t, tc.wantLinkedList, list)
}
})
}
}
// BenchmarkLinkedList_Add 基准测试 Add 方法
func BenchmarkLinkedList_Add(b *testing.B) {
l := NewLinkedListOf([]int{1, 2, 3})
testCase := make([]int, 0, b.N)
for i := 1; i <= b.N; i++ {
testCase = append(testCase, rand.Intn(i))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = l.Add(testCase[i], testCase[i])
}
}
// BenchmarkLinkedList_Get 基准测试 Get 方法
func BenchmarkLinkedList_Get(b *testing.B) {
l := NewLinkedListOf([]int{1, 2, 3})
for i := 1; i <= b.N; i++ {
err := l.Add(i, i)
if err != nil {
b.Fatal(err)
}
}
for i := 0; b.Loop(); i++ {
_, _ = l.Get(i)
}
}
注释说明:
如需对链表实现本身加注释,也可以贴出实现部分,我会帮你详细注释!