From f7f5d379a90e724e34b70c0755afa11c844bcf25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=82=B2=E6=96=B0?= Date: Mon, 13 Jul 2026 22:46:07 +0800 Subject: [PATCH] =?UTF-8?q?test(testing):=20=E6=B7=BB=E5=8A=A0=E4=B8=AD?= =?UTF-8?q?=E4=BD=8D=E6=95=B0=E8=AE=A1=E7=AE=97=E5=92=8C=E9=80=9A=E9=81=93?= =?UTF-8?q?=E9=80=9A=E4=BF=A1=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 TestNormal: 测试 GetMiddNumber 中位数计算逻辑 - 新增 TestChannel: 模拟通道传球 goroutine 交互 --- go/testing/normal_test.go | 48 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 go/testing/normal_test.go diff --git a/go/testing/normal_test.go b/go/testing/normal_test.go new file mode 100644 index 0000000..345e618 --- /dev/null +++ b/go/testing/normal_test.go @@ -0,0 +1,48 @@ +package testing + +import ( + "sort" + "testing" + "time" +) + +func TestNormal(t *testing.T) { + numbers := map[int][]int{ + 1: {8, 2, 4, 4}, + 2: {0, 5, 3}, + 3: {1}, + } + for _, numbers := range numbers { + middle := GetMiddNumber(numbers) + t.Log(middle) + } + +} + +func GetMiddNumber(numbers []int) float64 { + sort.Ints(numbers) + length := len(numbers) + isOddNUmber := length%2 == 1 + if !isOddNUmber { + return float64(numbers[length/2-1]+numbers[length/2]) / 2.0 + } + return float64(numbers[length/2]) +} + +func TestChannel(t *testing.T) { + var ball = make(chan string) + kickBall := func(playerName string) { + for { + t.Log(<-ball, "传球", "\n") + time.Sleep(time.Second) + ball <- playerName + } + } + go kickBall("张三") + go kickBall("李四") + go kickBall("王二麻子") + go kickBall("刘大") + ball <- "裁判" // 开球 + var c chan bool // 一个零值nil通道 + <-c // 永久阻塞在此 +}