test(slice): 添加 slice append 行为测试
新增 go/testing/slice/slice_test.go,验证将 slice 作为参数传入函数并在函数内 append 时不会影响原 slice 的 len/cap,借此说明 slice 值传递时的扩容行为。
This commit is contained in:
parent
b218c52d6e
commit
f870a1b224
18
go/testing/slice/slice_test.go
Normal file
18
go/testing/slice/slice_test.go
Normal file
@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func appendSlice(s []int, n int) {
|
||||
s = append(s, n)
|
||||
fmt.Printf("len=%d,cap=%d\n", len(s), cap(s))
|
||||
}
|
||||
func TestSliceAppend(t *testing.T) {
|
||||
s1 := make([]int, 5)
|
||||
appendSlice(s1, 10)
|
||||
if len(s1) != 5 || cap(s1) != 5 {
|
||||
t.Errorf("Expected len=5, cap=5; got len=%d, cap=%d", len(s1), cap(s1))
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user