-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain_test.go
100 lines (87 loc) · 1.63 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"bytes"
"encoding/json"
"github.com/mitchellh/mapstructure"
"strconv"
"strings"
"testing"
"time"
)
func TestUtilizationRate(t *testing.T) {
var a int64 = 1
var b int64 = 3000
var c float32 = float32(a) / float32(b)
t.Logf("%f", c)
}
func TestVersion(t *testing.T) {
ver := "0.1.1"
digs := strings.Split(ver, ".")
a, _ := strconv.Atoi(digs[0])
b, _ := strconv.Atoi(digs[1])
t.Logf("%d", a*10+b)
}
func TestTime(t *testing.T) {
s := 2 * time.Second.Nanoseconds()
start := time.Now()
time.Sleep(300 * time.Microsecond)
t.Logf("%d %d", time.Since(start).Nanoseconds(), s)
}
func TestFnv32(t *testing.T) {
key := "ddffvfgfgf"
hash := uint32(2166136261)
const prime32 = uint32(16777619)
for i := 0; i < len(key); i++ {
hash *= prime32
hash ^= uint32(key[i])
}
t.Log(hash)
}
type a struct {
A string `json:"a"`
}
func TestMap(t *testing.T) {
var result a
m := map[string]string{
"a": "12",
}
mapstructure.Decode(m, &result)
t.Logf("%+v", result)
}
func TestJson(t *testing.T) {
type obj struct {
Name string `json:"name"`
Age int `json:"age"`
}
json1 := obj{
Name: "x",
Age: 20,
}
json2 := obj{
Name: "t",
Age: 33,
}
b1, _ := json.Marshal(json1)
b2, _ := json.Marshal(json2)
var buf bytes.Buffer
//enc := json.NewEncoder(&buf)
buf.WriteByte('[')
buf.Write(b1)
buf.WriteByte(',')
buf.Write(b2)
//enc.Encode(b1)
//buf.WriteByte(',')
//buf.UnreadByte()
//enc.Encode(b2)
buf.WriteByte(']')
result := buf.Bytes()
t.Log(string(result))
type objs []obj
var ret objs
err := json.Unmarshal(result, &ret)
if err != nil {
t.Error(err)
}
t.Log(ret)
t.Log(ret[0].Name)
}