-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_parser.exs
50 lines (43 loc) · 1.29 KB
/
binary_parser.exs
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
defmodule LogEntry do
defstruct [:id, :timestamp, :x, :y, :z, :dx, :dy, :dz]
end
defmodule BinaryParser do
# Return a filled in LogEntry structs
def parse_line(line) do
# TODO
end
end
defmodule LogParser do
def parse(logfile) do
File.stream!(logfile, [], 14) # read 14 bytes at a time
|> Enum.map(fn(line) -> BinaryParser.parse_line(line) end)
|> print_last_positions
end
def print_last_positions(log_entries) do
last_known_positions = Enum.reduce(log_entries, %{}, fn(entry, results) ->
%{timestamp: ts, x: x, y: y, z: z} = entry
Map.put(results, entry.id, {ts, x, y, z})
end)
IO.inspect last_known_positions
end
end
ExUnit.start
defmodule LogParserTest do
use ExUnit.Case, async: true
test "returns map containing tuple of last known positions correctly" do
positions = LogParser.parse("./example_data/good.log")
assert positions == %{
1 => {1_450_000_009, 10, 10, 10},
2 => {1_450_000_009, 20, 20, 20},
3 => {1_450_000_009, 30, 30, 30}
}
end
# test "what happens with bad data" do
# positions = LogParser.parse("./example_data/bad.log")
# assert positions == %{
# 1 => {1_450_000_009, 10, 10, 10},
# 2 => {1_450_000_009, 20, 20, 20},
# 3 => {1_450_000_009, 30, 30, 30}
# }
# end
end