-
Notifications
You must be signed in to change notification settings - Fork 256
/
test.c3
83 lines (69 loc) · 1.5 KB
/
test.c3
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
module json_c3;
import std::io;
import std::os::posix;
import std::encoding::json;
import std::collections::object;
import std::collections::list;
import std::net::tcp;
struct Coordinate
{
double x, y, z;
}
struct TestStruct
{
Coordinate[] coordinates;
}
fn bool Coordinate.equals(&self, Coordinate other)
{
return self.x == other.x && self.y == other.y && self.z == other.z;
}
fn usz! Coordinate.to_format(&self, Formatter *formatter) @dynamic
{
return formatter.printf(`Coordinate{x: %.17e, y: %.17e, z: %.17e}`, self.x, self.y, self.z)!;
}
fn void notify(String msg)
{
TcpSocket! sock = tcp::connect("127.0.0.1", 9001);
if (catch excuse = sock)
{
return;
}
defer(void) sock.close();
sock.write(msg)!!;
}
fn Coordinate! calc(String s)
{
Object *o = json::parse_string(s)!;
defer o.free();
List(<Object *>) coords = o.get("coordinates")!.array;
double x, y, z;
foreach(c : coords)
{
x += c.get("x")!.f;
y += c.get("y")!.f;
z += c.get("z")!.f;
}
return Coordinate{
x / coords.size,
y / coords.size,
z / coords.size,
};
}
fn void main(String[] args)
{
Coordinate right = {2.0, 0.5, 0.25};
String[*] vals = {
`{"coordinates":[{"x":2.0,"y":0.5,"z":0.25}]}`,
`{"coordinates":[{"y":0.5,"x":2.0,"z":0.25}]}`,
};
foreach(v : vals)
{
Coordinate left = calc(v)!!;
assert(left.equals(right));
}
char[] json_str = file::load_temp("/tmp/1.json")!!;
notify(string::tformat("C3\t%d", posix::getpid()));
Coordinate results = calc((String)json_str)!!;
notify("end");
io::printn(results);
}