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 6e3d460 commit 4e03318
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions test/basic/collection/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,16 @@ func TestSliceCap(t *testing.T) {
// 等于从索引 1 到底层数组末尾的元素数
assert.Equal(t, cap(newSlice), 4)
assert.Equal(t, newSlice, []int{1, 2})

// 访问数组越界
assert.PanicMatches(t, func() {
fmt.Println("newSlice[3]=", newSlice[3])
}, "runtime error: index out of range [3] with length 2")

// newSlice不能随意更改老的slice
newSlice = append(newSlice, 5)
assert.Equal(t, len(oldSlice), 5)
assert.Equal(t, oldSlice, []int{0, 1, 2, 5, 4})
assert.Equal(t, oldSlice[3], 5)
assert.Equal(t, newSlice, []int{1, 2, 5})
}

0 comments on commit 4e03318

Please sign in to comment.