Skip to content

Commit

Permalink
add: the difference between len and cap of slice
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatDiscovery committed Nov 14, 2024
1 parent 313d216 commit 6e3d460
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions test/basic/collection/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,16 @@ func TestInsertIndex0(t *testing.T) {
assert.Equal(t, updated, []int{1, 2, 3, 4})
fmt.Println(updated) // 输出: [1 2 3 4]
}

// len和cap的区别
func TestSliceCap(t *testing.T) {
oldSlice := []int{0, 1, 2, 3, 4}
assert.Equal(t, len(oldSlice), 5)
assert.Equal(t, cap(oldSlice), 5)

newSlice := oldSlice[1:3]
assert.Equal(t, len(newSlice), 2)
// 等于从索引 1 到底层数组末尾的元素数
assert.Equal(t, cap(newSlice), 4)
assert.Equal(t, newSlice, []int{1, 2})
}

0 comments on commit 6e3d460

Please sign in to comment.