Files
bl/common/utils/sqrt_test.go
昔念 b6e90cf7f5 test(common): 重构 Test_event 测试函数
- 重写了 Test_event 测试函数,移除了未使用的测试代码
- 新增了基于 Task 结构的事件处理逻辑
- 实现了任务计数和完成状态的更新
- 添加了事件发布和订阅的示例代码
2025-08-05 17:01:21 +08:00

163 lines
3.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package utils
import (
"fmt"
"math"
"testing"
"github.com/badu/bus"
)
func Test_fastSqrt(t *testing.T) {
// 测试用例
testCases := []float64{2, 4, 10, 16, 25, 100, 1000, 0.5, 0.01, 1e-20}
fmt.Println("测试卡马克快速平方根算法与标准库的对比:")
fmt.Println("数值\t快速算法结果\t标准库结果\t绝对误差")
fmt.Println("--------------------------------------------------------------")
for _, x := range testCases {
fastResult := fastSqrt(x)
stdResult := math.Sqrt(x)
error := math.Abs(fastResult - stdResult)
fmt.Printf("%.10g\t%.10g\t\t%.10g\t\t%.2e\n",
x, fastResult, stdResult, error)
}
// 测试整数版本
fmt.Println("\n测试整数快速平方根算法")
fmt.Println("数值\t快速算法结果\t标准库转换结果")
fmt.Println("--------------------------------------")
for _, x := range []int{2, 4, 10, 16, 25, 100, 1000} {
fastResult := fastSqrtInt(x)
stdResult := int(math.Sqrt(float64(x)))
fmt.Printf("%d\t%d\t\t%d\n", x, fastResult, stdResult)
}
}
func BenchmarkMathSqrt(b *testing.B) {
testValues := []float64{2, 4, 10, 16, 25, 100, 1000, 0.5, 0.01, 1e-20}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, x := range testValues {
_ = math.Sqrt(x)
}
}
}
func BenchmarkFastSqrt(b *testing.B) {
testValues := []float64{2, 4, 10, 16, 25, 100, 1000, 0.5, 0.01, 1e-20}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, x := range testValues {
_ = fastSqrt(x)
}
}
}
func BenchmarkFastSqrtCarmack(b *testing.B) {
testValues := []float64{2, 4, 10, 16, 25, 100, 1000, 0.5, 0.01, 1e-20}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, x := range testValues {
_ = fastSqrtCarmack(x)
}
}
}
func BenchmarkMathSqrtInt(b *testing.B) {
testValues := []int{2, 4, 10, 16, 25, 100, 1000, 10000, 100000}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, x := range testValues {
_ = int(math.Sqrt(float64(x)))
}
}
}
func BenchmarkFastSqrtInt(b *testing.B) {
testValues := []int{2, 4, 10, 16, 25, 100, 1000, 10000, 100000}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, x := range testValues {
_ = fastSqrtInt(x)
}
}
}
func Test_event(b *testing.T) {
tt := make(map[int]*bus.Listener[*Task], 3)
topic := bus.NewTopic[*Task]() //直接注册到用户
for i := 0; i < 1; i++ {
t := topic.Sub(func(v *Task) { //注册里程碑
fmt.Println(v.id)
if v.cfunc != nil {
v.cont = v.cfunc(v.cont) //增加计数
}
if v.cont > 0 { //如果计数达到,标记任务完成
v.done = true
}
fmt.Println("当前任务计数", v.cont, "是否完成", v.done)
})
tt[i] = t
}
topic.Pub(&Task{id: 1, cfunc: func(t uint32) uint32 { //满足任务1后推定
fmt.Println("当前任务计数", t)
//实现自定义逻辑
//增加用户计数
//增加服务器总计数
return t + 1
}})
topic.Pub(&Task{id: 2, done: true})
for _, v := range tt {
v.Cancel()
}
topic.Pub(&Task{id: 1}) //发送事件
}
// Various event types
const EventA = 0x01
type Task struct {
id uint32
cfunc func(uint32) uint32
cont uint32
done bool
}
// Event type for testing purposes
type Event struct {
Data string
type1 uint32
}
// Type returns the event type
func (ev Event) Type() uint32 {
return ev.type1
}
// newEventA creates a new instance of an event
func newEventA(data string) Event {
return Event{Data: data, type1: EventA}
}
// Various event types
const EventB1 = 0x02
func newEventB(data string) Event {
return Event{Data: data, type1: EventB1}
}