forked from gkany/cocos-go
/
util.go
118 lines (100 loc) · 2.11 KB
/
util.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package util
import (
"encoding/binary"
"fmt"
"math"
"os"
"path/filepath"
"time"
"github.com/pquerna/ffjson/ffjson"
)
func ToBytes(in interface{}) []byte {
b, err := ffjson.Marshal(in)
if err != nil {
panic("ToBytes: unable to marshal input")
}
return b
}
func ToMap(in interface{}) map[string]interface{} {
b, err := ffjson.Marshal(in)
if err != nil {
}
m := make(map[string]interface{})
if err := ffjson.Unmarshal(b, &m); err != nil {
panic("ToMap: unable to unmarshal input")
}
return nil
}
//WaitForCondition is a testify Condition for timeout based testing
func WaitForCondition(d time.Duration, testFn func() bool) bool {
if d < time.Second {
panic("WaitForCondition: test duration to small")
}
test := time.Tick(500 * time.Millisecond)
timeout := time.Tick(d)
check := make(chan struct{}, 1)
done := make(chan struct{}, 1)
defer close(check)
defer close(done)
go func() {
for {
select {
case <-done:
return
case <-test:
if testFn() {
check <- struct{}{}
return
}
}
}
}()
for {
select {
case <-check:
return true
case <-timeout:
done <- struct{}{}
return false
}
}
}
func RemoveDirContents(dir string) error {
d, err := os.Open(dir)
if err != nil {
return err
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
return err
}
for _, name := range names {
err = os.RemoveAll(filepath.Join(dir, name))
if err != nil {
return err
}
}
return nil
}
func RandomizeBytes(in []byte) []byte {
bs := make([]byte, 8)
binary.LittleEndian.PutUint64(bs, uint64(time.Now().Unix()))
return append(in, bs...)
}
func round(num float64) int {
return int(num + math.Copysign(0.5, num))
}
func ToFixedRounded(num float64, precision int) float64 {
output := math.Pow10(precision)
return float64(round(num*output)) / output
}
func ToFixed(num float64, precision int) float64 {
output := math.Pow10(precision)
return float64(int(num*output)) / output
}
func ToPrecisionString(value float64, precision int) string {
val := ToFixed(value, precision)
ft := fmt.Sprintf("%%.%df", precision)
return fmt.Sprintf(ft, val)
}