1 Star 0 Fork 1

三体自动化技术协会/gogo

forked from xiaochi/gogo 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
db_test.go 3.61 KB
一键复制 编辑 原始数据 按行查看 历史
xiaochi 提交于 2022-10-17 21:38 . j
package gogo
import (
"log"
"math/rand"
"testing"
)
func Test_insertDo(t *testing.T) {
type args struct {
x int
y int
uid int
}
tests := []struct {
name string
args args
wantErr bool
}{
{"first", args{5, 5, 2}, false},
}
f := Init(false, "testdata/1.j")
defer f.Close()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := Insert(tt.args.x, tt.args.y, tt.args.uid)
if (err != nil) != tt.wantErr {
t.Errorf("Insert() error = %v, wantErr %v", err, tt.wantErr)
}
ub := mapXYUB[NewXY(5, 5)]
if ub.Bid != 1 {
t.Errorf("Insert() bid of 5,5 should be 1, real %d", ub.Bid)
}
if ub.Uid != 2 {
t.Errorf("Insert() uid of 5,5 should be 2, real %d", ub.Uid)
}
xys := mapBXYsQ[1].XYs
if len(xys) != 1 {
t.Errorf("Insert() mapBXYs should have 1 element, real %d", len(xys))
}
if mapBXYsQ[1].Qi != 4 {
t.Errorf("Insert() mapBQ should be 4, real %d", mapBXYsQ[1].Qi)
}
})
}
t.Run("eat", func(t *testing.T) {
log.SetFlags(log.LstdFlags | log.Lshortfile)
Insert(5, 4, 3)
_, _, other := neisKinds(findNeis(NewXY(5, 4)), 3)
// fmt.Printf("%x %x %x\n", empty, our, other)
// fmt.Println(allUsAlive(len(empty), len(other)))
// fmt.Printf("%x\n", mapBXYs[2])
obs := ziBids(other).Slice()
if obs[0] != 1 {
t.Errorf("other bid should be 1, real %d", obs[0])
}
if mapBXYsQ[1].Qi != 3 {
t.Errorf("5,5 should have 3 qi, real %d", mapBXYsQ[1].Qi)
}
// DebugState()
Insert(5, 3, 3)
// DebugState()
if len(mapBXYsQ[2].XYs) != 2 {
t.Errorf("block should have 2, real %d", len(mapBXYsQ[2].XYs))
}
if lenBlocks() != len(mapBXYsQ) {
t.Errorf("lenBlocks=%d mapBXYsQ=%d", lenBlocks(), len(mapBXYsQ))
}
Insert(5, 6, 3)
Insert(4, 5, 3)
// DebugState()
if mapBXYsQ[1].Qi != 1 {
t.Errorf("5,5 should have 1 qi, real %d", mapBXYsQ[1].Qi)
}
// dryInsert(6, 5, 3)
// log.Println(needReCal([]int{1}))
Insert(6, 5, 3)
if _, ok := mapXYUB[NewXY(5, 5)]; ok {
t.Error("5,5 should have no, real yes")
}
})
}
func FuzzInsert(f *testing.F) {
fj := Init(false, "testdata/fuzz.j")
defer fj.Close()
testcases := []XY{{1, 2}, {4, -9}, {0, 0}}
for _, tc := range testcases {
f.Add(tc.X, tc.Y)
}
f.Fuzz(func(t *testing.T, x, y int) {
color := rand.Int() % 0xffffff
err := Insert(x, y, color)
if err == nil {
if ub, ok := mapXYUB[NewXY(x, y)]; ok {
qi := countQi(mapBXYsQ[ub.Bid].XYs)
if qi == 0 {
t.Errorf("block %d dead", ub.Bid)
}
}
if lenBlocks() != len(mapBXYsQ) {
t.Errorf("block num not equal mapXYUB=%d mapmapBXYsQ=%d", lenBlocks(), len(mapBXYsQ))
}
if len(mapXYUB) != totalZi() {
t.Errorf("zi num not equal mapXYUB=%d totalZi=%d", len(mapXYUB), totalZi())
}
}
})
}
func lenBlocks() int {
m := map[int]bool{}
for _, ub := range mapXYUB {
b := ub.Bid
m[b] = true
}
return len(m)
}
func totalZi() (n int) {
for _, block := range mapBXYsQ {
n += len(block.XYs)
}
return
}
func Test_updateBids(t *testing.T) {
f := Init(false, "testdata/3.j")
defer f.Close()
t.Run("combine", func(t *testing.T) {
err := Insert(5, 5, 3)
if err != nil {
t.Errorf("should no error")
}
Insert(4, 5, 3)
Insert(4, 5, 3)
if len(mapBXYsQ) != 1 {
t.Errorf("should only 1 block, real %d", len(mapBXYsQ))
}
})
}
func Test_updateBids2(t *testing.T) {
f := Init(false, "testdata/4.j")
defer f.Close()
t.Run("combine 2 2", func(t *testing.T) {
Insert(2, 1, 3)
Insert(2, 2, 3)
Insert(3, 3, 3)
Insert(4, 3, 3)
// DebugState()
Insert(2, 3, 3)
if totalZi() != len(mapXYUB) {
t.Errorf("should equal totalZi()=%d len(mapXYUB)=%d", totalZi(), len(mapXYUB))
}
})
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wu-org/gogo.git
git@gitee.com:wu-org/gogo.git
wu-org
gogo
gogo
master

搜索帮助