-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathtest_packedjson.nim
52 lines (41 loc) · 1.15 KB
/
test_packedjson.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
import packedjson
import net
import strformat
import posix
type Coordinate = tuple[x: float, y: float, z: float]
proc notify(msg: string) =
try:
var socket = newSocket()
defer: socket.close()
socket.connect("localhost", Port(9001))
socket.send(msg)
except:
discard
proc calc(text: string): Coordinate =
let jobj = parseJson(text)
let coordinates = jobj["coordinates"]
let len = float(coordinates.len)
var x = 0.0
var y = 0.0
var z = 0.0
for coord in coordinates:
x += coord["x"].getFloat()
y += coord["y"].getFloat()
z += coord["z"].getFloat()
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)
var text = "/tmp/1.json".readFile()
var compiler = "Nim/clang (Packedjson)"
when defined(gcc):
compiler = "Nim/gcc (Packedjson)"
notify(&"{compiler}\t{getpid()}")
let results = calc(text)
notify("stop")
echo results