Files
bl/common/utils/sqrt_test.go
昔念 0f6c8d9329 refactor(sqrt_test): 重构事件测试逻辑
- 修改了事件订阅和发布机制,增加了任务完成的判断和处理
- 优化了代码结构,提高了可读性和可维护性
- 移除了不必要的
2025-08-05 17:33:38 +08:00

170 lines
3.6 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) {
topic := bus.NewTopic[*Task]() //直接注册到用户
for i := 0; i < 1; i++ {
isdone := false
t := topic.Sub(func(v *Task) { //用户初始化时注册里程碑
if v.done { //如果任务完成
isdone = true
fmt.Println(v.id, "任务完成")
return
}
if v.cfunc != nil {
v.cont = v.cfunc(v.cont) //增加计数
}
if v.cont > 0 { //如果计数达到,标记任务完成
v.done = true
}
fmt.Println(v.id, "当前任务计数", v.cont, "是否完成", v.done)
})
if isdone { //如果任务完成,取消注册
t.Cancel()
}
}
task1 := &Task{id: 1, cfunc: func(t uint32) uint32 { //满足任务1后推定
fmt.Println(1, "当前任务计数", t)
//实现自定义逻辑
//增加用户计数
//增加服务器总计数
return t + 1
}}
topic.Pub(task1)
topic.Pub(&Task{id: 2, done: true})
topic.Pub(task1) //发送事件
}
// 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}
}