-
Notifications
You must be signed in to change notification settings - Fork 255
/
test_pull.cr
61 lines (51 loc) · 1.12 KB
/
test_pull.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
55
56
57
58
59
60
61
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)
len = 0
x = y = z = 0
pull = JSON::PullParser.new(text)
pull.on_key!("coordinates") do
pull.read_array do
len += 1
pull.read_object do |key|
case key
when "x" then x += pull.read_float
when "y" then y += pull.read_float
when "z" then z += pull.read_float
else pull.skip
end
end
end
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 (Pull)\t#{Process.pid}")
results = calc(text)
notify("stop")
p results
end