You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gophp/gophp_test.go

99 lines
3.3 KiB

2 years ago
package gophp
import (
"fmt"
5 months ago
"go.dtapp.net/gojson"
2 years ago
"testing"
)
func TestSerialize(t *testing.T) {
t.Log(Unserialize([]byte(`a:1:{s:4:"test";i:34343;}`)))
serialize, _ := Serialize(map[string]interface{}{
"test": 34343,
})
t.Log(string(serialize))
}
func BenchmarkSerialize(b *testing.B) {
for i := 0; i < b.N; i++ {
serialize, _ := Serialize(map[string]interface{}{
"test": 34343,
})
b.Log(string(serialize))
}
}
func TestUnserialize(t *testing.T) {
t.Log(Unserialize([]byte(`a:1:{s:4:"test";i:34343;}`)))
}
func BenchmarkUnserialize(b *testing.B) {
for i := 0; i < b.N; i++ {
b.Log(Unserialize([]byte(`a:1:{s:4:"test";i:34343;}`)))
}
}
type student struct {
Amount string `json:"amount"`
CreateTime string `json:"create_time"`
UpdateTime string `json:"update_time"`
UserID string `json:"user_id"`
}
func TestPhp2(t *testing.T) {
stu := student{
Amount: "1",
}
m := make(map[string]interface{})
// struct 转json
5 months ago
j, _ := gojson.Marshal(stu)
2 years ago
// json 转map
5 months ago
gojson.Unmarshal(j, &m)
2 years ago
fmt.Printf("Serialize %s\n", j)
serialize, err := Serialize(j)
fmt.Printf("Serialize %s\n", serialize)
fmt.Printf("Serialize %s\n", err)
}
func TestUnserialize2(t *testing.T) {
unserialize, _ := Unserialize([]byte(`a:2:{i:0;a:4:{s:7:"user_id";s:5:"10118";s:6:"amount";s:5:"69.00";s:11:"create_time";s:19:"2021-01-04 16:29:03";s:11:"update_time";s:19:"2021-06-15 16:02:46";}i:1;a:4:{s:7:"user_id";s:5:"10088";s:6:"amount";s:5:"10.00";s:11:"create_time";s:19:"2021-01-04 15:46:10";s:11:"update_time";s:19:"2021-06-15 15:50:06";}}`))
fmt.Printf("%s\n", unserialize)
5 months ago
arr, _ := gojson.Marshal(unserialize)
2 years ago
fmt.Printf("arr%s\n", arr)
var stu []student
5 months ago
_ = gojson.Unmarshal(arr, &stu)
2 years ago
fmt.Printf("stu%v\n", stu)
}
func TestUnserialize3(t *testing.T) {
unserialize, err := Unserialize([]byte(`a:3:{s:4:"self";a:5:{s:2:"id";i:32;s:7:"user_id";i:10118;s:16:"merchant_user_id";i:10150;s:5:"level";i:3;s:5:"split";d:2.825;}s:5:"split";a:2:{i:0;a:7:{s:2:"id";i:12;s:7:"user_id";i:10000;s:13:"agent_user_id";i:10088;s:16:"user_golden_bean";d:5;s:5:"level";i:1;s:11:"calculation";d:0.010000000000000002;s:5:"split";d:0.57;}i:1;a:7:{s:2:"id";i:19;s:7:"user_id";i:10088;s:13:"agent_user_id";i:10118;s:16:"user_golden_bean";d:4;s:5:"level";i:2;s:11:"calculation";d:0.04;s:5:"split";d:2.26;}}s:11:"golden_bean";a:6:{s:4:"fans";d:56.5;s:12:"fan_referrer";d:16.95;s:8:"merchant";d:4.5200000000000005;s:5:"agent";d:2.825;s:5:"count";d:80.795;s:2:"bf";a:9:{s:2:"id";i:2;s:7:"user_id";i:10088;s:4:"fans";d:50;s:12:"fan_referrer";d:30;s:8:"merchant";d:8;s:6:"system";d:0;s:5:"agent";d:5;s:11:"create_time";s:19:"2020-11-20 14:47:21";s:11:"update_time";s:19:"2020-11-20 14:47:21";}}}`))
fmt.Printf("%+v\n", unserialize)
if err != nil {
fmt.Printf("err%v\n", err)
return
}
}
5 months ago
func TestEmpty(t *testing.T) {
var emptyString string
var nonEmptyString = "Hello, World"
var emptySlice []int
var nonEmptySlice = []int{1, 2, 3}
var emptyMap map[string]int
var nonEmptyMap = map[string]int{"a": 1}
var emptyInt int
var nonEmptyInt = 42
t.Log(Empty(emptyString)) // true
t.Log(Empty(nonEmptyString)) // false
t.Log(Empty(emptySlice)) // true
t.Log(Empty(nonEmptySlice)) // false
t.Log(Empty(emptyMap)) // true
t.Log(Empty(nonEmptyMap)) // false
t.Log(Empty(emptyInt)) // true
t.Log(Empty(nonEmptyInt)) // false
}