-
Notifications
You must be signed in to change notification settings - Fork 255
/
test.cr
54 lines (44 loc) · 982 Bytes
/
test.cr
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
require "json"
require "socket"
struct Coordinate
property x, y, z
def initialize(@x : Float64, @y : Float64, @z : Float64)
end
end
def notify(msg)
begin
TCPSocket.open("localhost", 9001) { |s|
s.puts msg
}
rescue
# standalone usage
end
end
def calc(text)
jobj = JSON.parse(text)
coordinates = jobj["coordinates"].as_a
len = coordinates.size
x = y = z = 0
coordinates.each do |coord|
x += coord["x"].as_f
y += coord["y"].as_f
z += coord["z"].as_f
end
Coordinate.new(x / len, y / len, z / len)
end
class EntryPoint
right = Coordinate.new(2.0, 0.5, 0.25)
["{\"coordinates\":[{\"x\":2.0,\"y\":0.5,\"z\":0.25}]}",
"{\"coordinates\":[{\"y\":0.5,\"x\":2.0,\"z\":0.25}]}"].each { |v|
left = calc(v)
if left != right
STDERR.puts "#{left} != #{right}"
exit(1)
end
}
text = File.read("/tmp/1.json")
notify("Crystal\t#{Process.pid}")
results = calc(text)
notify("stop")
p results
end