-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_test.go
80 lines (65 loc) · 1.51 KB
/
json_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
package julia
import (
"encoding/json"
"testing"
)
const (
preload = `
using JSON2
parse(x) = JSON2.read(String(x), Vector{String})
serialize(x) = Vector{UInt8}(JSON2.write(x))
`
)
// TestJsonSerialization sends a list of strings to julia
// as json serialized byte buffer which is then read back
// as Vector{String} in julia. Needless to say this method
// should work for arbitrary data types including structs,
// maps, slices etc.
func TestJsonSerializationSendToJulia(t *testing.T) {
Initialize()
defer Finalize()
if _, err := Eval(preload); err != nil {
t.Fatal(err)
}
listOfStrings := []string{"abcd", "12345678"}
jb, _ := json.Marshal(listOfStrings)
arg, err := Marshal(jb)
if err != nil {
t.Fatal(err)
}
resp, err := EvalFunc("parse", ModuleMain, arg)
if err != nil {
t.Fatal(err)
}
respType := resp.Type()
if respType != "Vector{String}" {
t.Fatal("expected Vector{String}, got", respType)
}
}
func TestJsonSerializationReceiveFromJulia(t *testing.T) {
Initialize()
defer Finalize()
if _, err := Eval(preload); err != nil {
t.Fatal(err)
}
resp, err := Eval("serialize([\"abcd\", \"12345679\"])")
if err != nil {
t.Fatal(err)
}
n := Len(resp)
mat, err := NewMat(make([]byte, n))
if err != nil {
t.Fatal(err)
}
if err := Unmarshal(resp, mat); err != nil {
t.Fatal(err)
}
var list []string
if err := json.Unmarshal(mat.GetElms(), &list); err != nil {
t.Fatal(err)
}
if list[0] != "abcd" ||
list[1] != "12345679" {
t.Fatal("did not receive expected values")
}
}