-
Notifications
You must be signed in to change notification settings - Fork 255
/
test.d
85 lines (75 loc) · 1.71 KB
/
test.d
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import core.stdc.stdlib;
import core.thread;
import std.compiler;
import std.conv;
import std.file;
import std.format;
import std.json;
import std.socket;
import std.stdio;
struct Coordinate
{
double x, y, z;
void toString(scope void delegate(const(char)[]) sink) const
{
sink("Coordinate {x: ");
sink(to!string(x));
sink(", y: ");
sink(to!string(y));
sink(", z: ");
sink(to!string(z));
sink("}");
}
}
void notify(string msg)
{
try
{
auto socket = new TcpSocket(new InternetAddress("localhost", 9001));
scope (exit)
socket.close();
socket.send(msg);
}
catch (SocketOSException)
{
// standalone usage
}
}
Coordinate calc(string text)
{
auto jobj = parseJSON(text).object;
auto coordinates = jobj["coordinates"].array;
auto len = coordinates.length;
auto x = 0.0;
auto y = 0.0;
auto z = 0.0;
for (auto i = 0; i < coordinates.length; i++)
{
auto coord = coordinates[i];
x += coord["x"].floating;
y += coord["y"].floating;
z += coord["z"].floating;
}
return Coordinate(x / len, y / len, z / len);
}
void main()
{
immutable right = Coordinate(2.0, 0.5, 0.25);
foreach (v; [
`{"coordinates":[{"x":2.0,"y":0.5,"z":0.25}]}`,
`{"coordinates":[{"y":0.5,"x":2.0,"z":0.25}]}`
])
{
immutable left = calc(v);
if (left != right)
{
stderr.writefln("%s != %s", left, right);
exit(1);
}
}
immutable text = readText("/tmp/1.json");
notify("%s\t%d".format(name, getpid()));
immutable results = calc(text);
notify("stop");
writeln(results);
}