-
Notifications
You must be signed in to change notification settings - Fork 256
/
test.rb
59 lines (48 loc) · 1.18 KB
/
test.rb
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
# frozen_string_literal: true
require 'json'
require 'socket'
Coordinate = Struct.new(:x, :y, :z)
def notify(msg)
Socket.tcp('localhost', 9001) { |s| s.puts msg }
rescue SystemCallError
# standalone usage
end
def calc(text)
jobj = JSON.parse(text)
coordinates = jobj['coordinates']
len = coordinates.length
x = y = z = 0
coordinates.each do |coord|
x += coord['x']
y += coord['y']
z += coord['z']
end
Coordinate.new(x / len, y / len, z / len)
end
if __FILE__ == $PROGRAM_NAME
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 do |v|
left = calc(v)
if left != right
warn "#{left} != #{right}"
exit(1)
end
end
text = File.read('/tmp/1.json')
engine = RUBY_ENGINE
if engine == 'truffleruby'
desc = RUBY_DESCRIPTION
if desc.include?('Native')
engine = 'Ruby/truffleruby'
elsif desc.include?('JVM')
engine = 'Ruby/truffleruby (JVM)'
end
elsif engine == 'ruby' && RubyVM::RJIT.enabled?
engine = 'Ruby (--jit)'
end
notify("#{engine}\t#{Process.pid}")
results = calc(text)
notify('stop')
p results
end