-
Notifications
You must be signed in to change notification settings - Fork 1
/
cgolua_test.go
92 lines (82 loc) · 1.92 KB
/
cgolua_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
package lua
import (
"encoding/json"
"testing"
"time"
)
func test_sum(args ...interface{}) (interface{}, error) {
sum := 0
for _, arg := range args {
sum = sum + int(arg.(int64))
}
return sum, nil
}
func json_decode(args ...interface{}) (interface{}, error) {
raw := args[0].(string)
var res map[string]interface{}
err := json.Unmarshal([]byte(raw), &res)
return res, err
}
func TestLuaFib(t *testing.T) {
start_t := time.Now()
res, err := Call("script.lua", "fib", 35)
end_t := time.Now()
if err != nil {
t.Error("fib", err)
} else {
delta_t := end_t.Sub(start_t)
t.Log(res, "cost", delta_t.Seconds(), "seconds")
}
}
func TestLuaRun(t *testing.T) {
RegisterExternMethod("json_decode", json_decode)
RegisterExternMethod("test_sum", test_sum)
start_t := time.Now()
res, err := Call("script.lua", "async_json_encode", nil)
end_t := time.Now()
if err != nil {
t.Error("async_json_encode", err)
} else {
delta_t := end_t.Sub(start_t)
t.Log(res, "cost", delta_t.Seconds(), "seconds")
}
start_t = time.Now()
res, err = Call("", "test_args", 69)
end_t = time.Now()
if err != nil {
t.Log("ok!! test_args", err)
} else {
t.Log("shouldnot found function or maybe only one Core")
}
err = DoFile("script.lua")
if err != nil {
t.Error("Loadfile", err)
}
start_t = time.Now()
res, err = Call("", "test_args", 69)
end_t = time.Now()
if err != nil {
t.Error("test_args", err)
} else {
delta_t := end_t.Sub(start_t)
t.Log(res, "cost", delta_t.Seconds(), "seconds")
}
start_t = time.Now()
res, err = Call("", "test_pull_table", "ok.?ok")
end_t = time.Now()
if err != nil {
t.Error("pull_table", err)
} else {
delta_t := end_t.Sub(start_t)
t.Log(res, "cost", delta_t.Seconds(), "seconds")
}
start_t = time.Now()
res, err = Call("", "fib", 35)
end_t = time.Now()
if err != nil {
t.Error("fib", err)
} else {
delta_t := end_t.Sub(start_t)
t.Log(res, "cost", delta_t.Seconds(), "seconds")
}
}