-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathtest_jsony.nim
54 lines (42 loc) · 1.13 KB
/
test_jsony.nim
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
import std/[net, strformat, posix]
import jsony
type
Coordinate = tuple
x, y, z: float
CoordinateObject = object
coordinates: seq[Coordinate]
proc notify(msg: string) =
try:
let socket = newSocket()
defer: socket.close()
socket.connect("localhost", Port(9001))
socket.send(msg)
except:
discard
proc calc(text: string): Coordinate =
let obj = text.fromJson(CoordinateObject)
let coordinates = obj.coordinates
let len = float(coordinates.len)
var x, y, z: float
for coord in coordinates:
x += coord.x
y += coord.y
z += coord.z
result = (x: x / len, y: y / len, z: z / len)
when isMainModule:
let right = (x: 2.0, y: 0.5, z: 0.25)
for v in ["""{"coordinates":[{"x":2.0,"y":0.5,"z":0.25}]}""",
"""{"coordinates":[{"y":0.5,"x":2.0,"z":0.25}]}"""]:
let left = calc(v)
if left != right:
stderr.writeLine(&"{left} != {right}")
quit(1)
let text = "/tmp/1.json".readFile()
let compiler = when defined(gcc):
"Nim/gcc (jsony)"
else:
"Nim/clang (jsony)"
notify(&"{compiler}\t{getpid()}")
let results = calc(text)
notify("stop")
echo results