-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathtest_schema.cr
More file actions
63 lines (50 loc) · 1.06 KB
/
test_schema.cr
File metadata and controls
63 lines (50 loc) · 1.06 KB
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
require "json"
require "socket"
struct Coordinate
include JSON::Serializable
property x : Float64
property y : Float64
property z : Float64
def initialize(@x, @y, @z)
end
end
class Coordinates
include JSON::Serializable
property coordinates : Array(Coordinate)
end
def notify(msg)
begin
TCPSocket.open("localhost", 9001) { |s|
s.puts msg
}
rescue
# standalone usage
end
end
def calc(text)
coordinates = Coordinates.from_json(text).coordinates
len = coordinates.size
x = y = z = 0
coordinates.each do |e|
x += e.x
y += e.y
z += e.z
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 (Schema)\t#{Process.pid}")
results = calc(text)
notify("stop")
p results
end