-
Notifications
You must be signed in to change notification settings - Fork 255
/
test.zig
81 lines (68 loc) · 2.28 KB
/
test.zig
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
const std = @import("std");
const unistd = @cImport(@cInclude("unistd.h"));
const Coordinate = struct {
x: f64,
y: f64,
z: f64,
fn eql(left: Coordinate, right: Coordinate) bool {
return left.x == right.x and left.y == right.y and left.z == right.z;
}
};
const TestStruct = struct {
coordinates: []Coordinate,
};
fn notify(msg: []const u8) void {
const addr = std.net.Address.parseIp("127.0.0.1", 9001) catch unreachable;
if (std.net.tcpConnectToAddress(addr)) |stream| {
defer stream.close();
_ = stream.write(msg) catch unreachable;
} else |_| {}
}
fn readFile(alloc: std.mem.Allocator, filename: []const u8) ![]const u8 {
const file = try std.fs.cwd().openFile(filename, std.fs.File.OpenFlags{});
defer file.close();
const size = try file.getEndPos();
const text = try alloc.alloc(u8, size);
_ = try file.readAll(text);
return text;
}
fn calc(alloc: std.mem.Allocator, text: []const u8) !Coordinate {
const parsed = try std.json.parseFromSlice(TestStruct, alloc, text, .{
.ignore_unknown_fields = true,
});
defer parsed.deinit();
const obj = parsed.value;
var x: f64 = 0.0;
var y: f64 = 0.0;
var z: f64 = 0.0;
for (obj.coordinates) |item| {
x += item.x;
y += item.y;
z += item.z;
}
const len = @as(f64, @floatFromInt(obj.coordinates.len));
return Coordinate{ .x = x / len, .y = y / len, .z = z / len };
}
pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.c_allocator);
defer arena.deinit();
const alloc = arena.allocator();
const right = Coordinate{ .x = 2.0, .y = 0.5, .z = 0.25 };
const vals = [_][]const u8{
"{\"coordinates\":[{\"x\":2.0,\"y\":0.5,\"z\":0.25}]}",
"{\"coordinates\":[{\"y\":0.5,\"x\":2.0,\"z\":0.25}]}",
};
for (vals) |v| {
const left = try calc(alloc, v);
if (!Coordinate.eql(left, right)) {
std.debug.panic("{} != {}\n", .{ left, right });
}
}
const text = try readFile(alloc, "/tmp/1.json");
const pid = unistd.getpid();
const pid_str = try std.fmt.allocPrint(alloc, "Zig\t{d}", .{pid});
notify(pid_str);
const results = try calc(alloc, text);
notify("stop");
std.debug.print("{}\n", .{results});
}