-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.dylan
188 lines (165 loc) · 6.18 KB
/
utils.dylan
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
Module: %testworks
Synopsis: Utilities and code that needs to be loaded early.
Copyright: Original Code is Copyright (c) 1995-2004 Functional Objects, Inc.
All rights reserved.
License: See License.txt in this distribution for details.
Warranty: Distributed WITHOUT WARRANTY OF ANY KIND
define function add-times
(sec1 :: <integer>, usec1 :: <integer>, sec2 :: <integer>, usec2 :: <integer>)
=> (sec :: <integer>, usec :: <integer>)
let sec = sec1 + sec2;
let usec = usec1 + usec2;
if (usec >= 1000000)
usec := usec - 1000000;
sec1 := sec1 + 1;
end if;
values(sec, usec)
end function add-times;
define method plural
(n :: <integer>) => (ending :: <string>)
if (n == 1) "" else "s" end if
end;
//// Tags
define class <tag> (<object>)
constant slot tag-name :: <string>, init-keyword: name:;
constant slot tag-negated? :: <boolean>, init-keyword: negated?:;
end;
define method make-tag
(tag :: <tag>) => (tag :: <tag>)
tag
end;
define method make-tag
(spec :: <string>) => (tag :: <tag>)
let negated? = starts-with?(spec, "-");
let name = copy-sequence(spec, start: negated? & 1 | 0);
if (empty?(name))
error("Invalid tag: %=", spec);
end;
make(<tag>, name: name, negated?: negated?)
end method make-tag;
define method print-object
(tag :: <tag>, stream :: <stream>) => ()
format(stream, "#<tag %s%s>", tag.tag-negated? & "-" | "", tag.tag-name);
end;
define function parse-tags
(specs :: <sequence> /* of <string> */)
=> (tags :: <sequence> /* of <tag> */)
map(make-tag, specs)
end;
// If tags match, run the test.
define generic tags-match?
(requested-tags :: <sequence>, component :: <component>)
=> (bool :: <boolean>);
define method tags-match?
(requested-tags :: <sequence>, component :: <component>)
=> (bool :: <boolean>)
#t
end;
define method tags-match?
(requested-tags :: <sequence>, test :: <runnable>)
=> (bool :: <boolean>)
local method match (negated?)
block (return)
for (rtag in requested-tags)
if (rtag.tag-negated? = negated?)
for (ctag in test.test-tags)
if (ctag.tag-name = rtag.tag-name)
return(#t)
end;
end;
end;
end;
end block
end method match;
let negative-rtags? = any?(tag-negated?, requested-tags);
let positive-rtags? = any?(complement(tag-negated?), requested-tags);
block (return)
// Order matters here. Negative tags take precedence.
negative-rtags? & match(#t) & return(#f);
positive-rtags? & return(match(#f));
#t
end block
end method tags-match?;
// Might want to put an extended version of this in the io:format module.
define function format-bytes
(bytes :: <integer>) => (string :: <string>)
let (divisor, units) = case
bytes <= 1024 =>
values(1, "B");
bytes <= ash(1, 20) =>
values(1024, "KiB");
otherwise =>
values(ash(1, 20), "MiB");
// Need more bits in our integers...
end;
concatenate(integer-to-string(round/(bytes, divisor)), units)
end function format-bytes;
define function capitalize
(string :: <string>) => (_ :: <string>)
concatenate(as-uppercase(copy-sequence(string, end: 1)),
copy-sequence(string, start: 1))
end function;
// For --progress and --report=full
define thread variable *indent* :: <string> = "";
define constant $indent-step :: <string> = " ";
define function next-indent () => (indent :: <string>)
concatenate(*indent*, $indent-step)
end function;
// Return a temporary directory unique to the current test or benchmark. The
// directory is created the first time this is called for a given test.
// The directory is _test/<user>-<yyyymmdd-hhmmss>/<full-test-name>/, relative
// to ${DYLAN}/, if defined, or relative to fs/working-directory() otherwise.
define function test-temp-directory () => (d :: false-or(<directory-locator>))
if (instance?(*component*, <runnable>))
let dylan = os/environment-variable("DYLAN");
let base = if (dylan)
as(<directory-locator>, dylan)
else
fs/working-directory()
end;
let uniquifier
= format-to-string("%s-%s", os/login-name() | "unknown",
date/format("%Y%m%d-%H%M%S", date/now()));
let safe-name = map(method (c)
if (c == '\\' | c == '/') '_' else c end
end,
full-component-name(*component*));
let test-directory
= subdirectory-locator(base, "_test", uniquifier, safe-name);
fs/ensure-directories-exist(test-directory);
test-directory
end
end function;
// Create a file in the current test's temp directory with the given contents.
// If the file already exists an error is signaled. `filename` is assumed to be
// a relative pathname; if it contains the path separator, subdirectories are
// created. File contents may be provided with the `contents` parameter,
// otherwise an empty file is created. Returns the full, absolute file path as
// a `<file-locator>`.
define function write-test-file
(filename :: fs/<pathname>, #key contents :: <string> = "")
=> (full-pathname :: <file-locator>)
let locator = merge-locators(as(<file-locator>, filename),
test-temp-directory());
fs/ensure-directories-exist(locator);
fs/with-open-file (stream = locator,
direction: #"output", if-exists: #"signal")
write(stream, contents);
end;
locator
end function;
// For tests to do debugging output.
// TODO(cgay): Collect this and stdio into a log file per test run
// or per test. The Surefire report has a place for stdout, too.
define method test-output
(format-string :: <string>, #rest format-args) => ()
let stream = if (*runner*)
runner-output-stream(*runner*)
else
*standard-output*
end;
with-stream-locked (stream)
apply(format, stream, format-string, format-args);
force-output(stream);
end;
end method;