@@ -11,32 +11,16 @@ using Printf
11
11
12
12
function run_part (func, input, expected)
13
13
stats = @timed func (input)
14
- Result (stats. value, expected, stats. time)
14
+ Result (stats. value, expected, stats. time, stats . bytes )
15
15
end
16
16
17
- function run_module (mod, filenames; verbose = false )
17
+ function run_module (mod, filenames; verbose= false )
18
18
name = nameof (mod)
19
19
ok = true
20
20
for fname in filenames
21
21
lines = readlines (fname == " -" ? stdin : fname)
22
22
len = length (lines)
23
- expectfile = replace (fname, r" (.*)\. txt$" => s "\1 .expected" )
24
- expect = Dict (
25
- if expectfile != fname && isfile (expectfile)
26
- open (expectfile) do ef
27
- map (
28
- filter (
29
- ! isnothing,
30
- map (l -> match (r" (part\d ):\s *(.*)" , l), readlines (ef)),
31
- )
32
- ) do m
33
- m. captures[1 ] => replace (m. captures[2 ], r" \\ n" => " \n " )
34
- end
35
- end
36
- else
37
- []
38
- end ,
39
- )
23
+ expect = expectedfor (fname)
40
24
for func in [mod. part1, mod. part2]
41
25
expected = get (expect, string (func), " " )
42
26
if verbose
@@ -46,15 +30,31 @@ function run_module(mod, filenames; verbose = false)
46
30
println (" $func : $(res. got) " )
47
31
if verbose
48
32
time = format_seconds (res. time_sec)
33
+ bytes = Base. format_bytes (res. allocated_bytes)
49
34
print_message (stderr , res)
50
- println (stderr , " $func took $time on $fname " )
35
+ println (stderr , " $func took $time and $bytes on $fname " )
51
36
println (stderr , " =" ^ 40 )
52
37
end
53
38
end
54
39
end
55
40
return ok
56
41
end
57
42
43
+ expectedfile (inputfile) = replace (inputfile, r" (.*)\. txt$" => s "\1 .expected" )
44
+
45
+ function expectedfor (inputfile)
46
+ expectfile = expectedfile (inputfile)
47
+ if expectfile != inputfile && isfile (expectfile)
48
+ open (expectfile) do ef
49
+ map (m -> m. captures[1 ] => replace (m. captures[2 ], r" \\ n" => " \n " ),
50
+ map (l -> match (r" (part\d ):\s *(.*)" , l), readlines (ef)) |>
51
+ filter (! isnothing))
52
+ end |> Dict{String, String}
53
+ else
54
+ Dict {String, String} ()
55
+ end
56
+ end
57
+
58
58
function format_seconds (sec)
59
59
if sec >= 60 * 60
60
60
(m, s) = divrem (sec, 60 )
@@ -66,22 +66,30 @@ function format_seconds(sec)
66
66
elseif sec >= 1
67
67
@sprintf (" %.3fs" , sec)
68
68
elseif sec >= 0.001
69
- @sprintf (" %.3fms" , sec * 1_000 )
69
+ @sprintf (" %.3fms" , sec* 1_000 )
70
70
else
71
- @sprintf (" %.3fµs" , sec * 1_000_000 )
71
+ @sprintf (" %.3fµs" , sec* 1_000_000 )
72
72
end
73
73
end
74
74
75
75
struct Result
76
76
got:: Any
77
77
want:: String
78
78
time_sec:: Float64
79
+ allocated_bytes:: Int
79
80
end
80
81
82
+ const OUTCOME_SYMBOLS = Dict (:success => " ✅" ,
83
+ :failure => " ❌" ,
84
+ :unknown => " ❓" ,
85
+ :todo => " ❗" )
86
+ const OUTCOME_BG_COLOR = Dict (:success => :light_green ,
87
+ :failure => :light_red ,
88
+ :unknown => :magenta ,
89
+ :todo => :cyan )
81
90
function print_message (io, res:: Result )
82
91
o = outcome (res)
83
- sign =
84
- Dict (:success => " ✅" , :failure => " ❌" , :unknown => " ❓" , :todo => " ❗" )[o]
92
+ sign = OUTCOME_SYMBOLS[o]
85
93
msg = if o == :success
86
94
" got $(res. got) "
87
95
elseif o == :failure
@@ -93,14 +101,12 @@ function print_message(io, res::Result)
93
101
elseif o == :todo
94
102
" implement it, want $(res. want) "
95
103
end
96
- bg = Dict (
97
- :success => :light_green ,
98
- :failure => :light_red ,
99
- :unknown => :magenta ,
100
- :todo => :cyan ,
101
- )[o]
104
+ bg = OUTCOME_BG_COLOR[o]
102
105
buf = IOBuffer ()
103
- printstyled (IOContext (buf, :color => true ), uppercase (string (o)), color = bg, reverse = true )
106
+ printstyled (IOContext (buf, :color => true ),
107
+ uppercase (string (o)),
108
+ color= bg,
109
+ reverse= true )
104
110
colored = String (take! (buf))
105
111
println (io, " $sign $colored $msg " )
106
112
end
@@ -122,18 +128,16 @@ end
122
128
macro run_if_main ()
123
129
srcfile = QuoteNode (__source__. file)
124
130
mod = __module__
125
- :(
126
- if abspath (PROGRAM_FILE ) == string ($ srcfile)
127
- args = ARGS
128
- verbose = false
129
- if isempty (ARGS )
130
- args = [" -" ]
131
- elseif first (ARGS ) in [" -v" , " --verbose" ]
132
- verbose = true
133
- args = args[2 : end ]
134
- end
135
- ok = Runner. run_module ($ mod, args, verbose = verbose)
136
- exit (ok ? 0 : 1 )
131
+ :(if abspath (PROGRAM_FILE ) == string ($ srcfile)
132
+ args = ARGS
133
+ verbose = false
134
+ if isempty (ARGS )
135
+ args = [" -" ]
136
+ elseif first (ARGS ) in [" -v" , " --verbose" ]
137
+ verbose = true
138
+ args = args[2 : end ]
137
139
end
138
- )
140
+ ok = Runner. run_module ($ mod, args, verbose= verbose)
141
+ exit (ok ? 0 : 1 )
142
+ end )
139
143
end
0 commit comments