Files
bl/common/utils/test_test.go

188 lines
4.2 KiB
Go

package utils
import (
"encoding/json"
"encoding/xml"
"fmt"
"strings"
"testing"
"github.com/apcera/termtables"
"github.com/gogf/gf/v2/os/glog"
"github.com/pointernil/bitset32"
)
func TestThree(t *testing.T) {
fmt.Printf("! \n")
var b bitset32.BitSet32
var a bitset32.BitSet32
// play some Go Fish
// for i := 0; i < 100; i++ {
// card1 := uint(rand.Intn(52))
// card2 := uint(rand.Intn(52))
// b.Set(card1)
// if b.Test(card2) {
// fmt.Println("Go Fish!")
// }
// b.Clear(card1)
// }
// Chaining
b.Set(10).Set(11)
a = *bitset32.New(50)
for i, e := b.NextSet(0); e; i, e = b.NextSet(i + 1) {
fmt.Println("The b bit is set:", i)
}
a.Set(10).Set(9)
f := b.Union(&a)
for i, e := f.NextSet(0); e; i, e = f.NextSet(i + 1) {
fmt.Println("The b+ bit is set:", i)
}
fmt.Println(b.Bytes())
}
func TestMap(t *testing.T) {
// 使用 SyncMap 存储字符串 -> int 类型的键值对
sm1 := &SyncMap[string, int]{}
sm1.Store("apple", 5)
sm1.Store("banana", 10)
// 加载并打印值
if value, ok := sm1.Load("apple"); ok {
fmt.Println("apple:", value)
}
if value, ok := sm1.Load("banana"); ok {
fmt.Println("banana:", value)
}
// 使用 SyncMap 存储 int -> string 类型的键值对
sm2 := &SyncMap[int, string]{}
sm2.Store(1, "one")
sm2.Store(2, "two")
// 遍历所有键值对
sm2.Range(func(key int, value string) bool {
fmt.Printf("%d: %s\n", key, value)
return true
})
}
func TestInit(t *testing.T) {
table := termtables.CreateTable()
table.AddHeaders("Name", "Age")
table.AddRow("John", "30")
table.AddRow("Sam", 18)
table.AddRow("Julie", 20.14)
fmt.Println(glog.GetStack())
fmt.Println(table.Render())
}
// SuperMaps 表示XML根节点
type SuperMaps struct {
XMLName xml.Name `xml:"superMaps" json:"-"`
Maps []Map `xml:"maps" json:"maps"`
}
// Map 表示XML中的每个地图节点
type Map struct {
ID string `xml:"id,attr" json:"id"`
Name string `xml:"name,attr" json:"name"`
X string `xml:"x,attr" json:"x"`
Y string `xml:"y,attr" json:"y"`
Galaxy string `xml:"galaxy,attr" json:"galaxy,omitempty"`
}
func TestXml(t *testing.T) {
// 示例XML数据
xmlData := `<?xml version="1.0" encoding="UTF-8"?>
<superMaps>
<maps id="1" name="传送舱" x="" y=""/>
<maps id="4" name="船长室" x="" y=""/>
<maps id="10 11 12 13" name="克洛斯星" galaxy="1" x="358" y="46"/>
</superMaps>`
// 示例JSON数据
jsonData := `{
"maps": [
{
"id": "1",
"name": "传送舱",
"x": "",
"y": ""
},
{
"id": "4",
"name": "船长室",
"x": "",
"y": ""
},
{
"id": "10 11 12 13",
"name": "克洛斯星",
"galaxy": "1",
"x": "358",
"y": "46"
}
]
}`
// 1. 解析XML并转换为JSON
fmt.Println("=== XML 到 JSON ===")
superMaps := &SuperMaps{}
err := xml.Unmarshal([]byte(xmlData), superMaps)
if err != nil {
fmt.Printf("解析XML失败: %v\n", err)
return
}
jsonOutput, err := json.MarshalIndent(superMaps, "", " ")
if err != nil {
fmt.Printf("转换为JSON失败: %v\n", err)
return
}
fmt.Println(string(jsonOutput))
// 2. 解析JSON并转换为XML
fmt.Println("\n=== JSON 到 XML ===")
newSuperMaps := &SuperMaps{}
err = json.Unmarshal([]byte(jsonData), newSuperMaps)
if err != nil {
fmt.Printf("解析JSON失败: %v\n", err)
return
}
xmlOutput, err := xml.MarshalIndent(newSuperMaps, "", " ")
if err != nil {
fmt.Printf("转换为XML失败: %v\n", err)
return
}
// 添加XML声明
xmlWithHeader := fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?>%s`, xmlOutput)
fmt.Println(string(xmlWithHeader))
// 3. 解析复杂ID的XML并处理
fmt.Println("\n=== 解析复杂ID的XML ===")
parseComplexID(xmlData)
}
// 解析复杂ID的XML并处理
func parseComplexID(xmlData string) {
superMaps := &SuperMaps{}
err := xml.Unmarshal([]byte(xmlData), superMaps)
if err != nil {
fmt.Printf("解析XML失败: %v\n", err)
return
}
// 处理每个地图项
for _, m := range superMaps.Maps {
// 处理包含多个ID的情况
if strings.Contains(m.ID, " ") {
ids := strings.Fields(m.ID)
fmt.Printf("地图名称: %s, 拆分后的ID: %v\n", m.Name, ids)
} else {
fmt.Printf("地图名称: %s, ID: %s\n", m.Name, m.ID)
}
}
}