Repository navigation
Expand file tree
/
Copy pathmtest.cpp
More file actions
1512 lines (1410 loc) · 56.6 KB
/
Copy pathmtest.cpp
File metadata and controls
1512 lines (1410 loc) · 56.6 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// The runtime of mcpplibs.mtest: the program's cases, the runner's requests
// (spec/protocol.md P2) and the two reports, the events and the terminal's.
module mcpplibs.mtest;
import std;
import mcpplibs.os;
import nlohmann.json;
namespace mcpplibs::mtest {
namespace os = mcpplibs::os;
// P2.3: a runner that reads this program's file finds the protocol here.
constinit const char protocol_marker[] = "mtest-protocol=1";
namespace {
using clock = std::chrono::steady_clock;
struct fatal_signal {};
struct skip_signal {
std::string reason;
};
struct running_case {
std::string id;
bool scope = false; // a suite: a prefix of identifiers and attributes, not a case
attributes attrs; // what the cases within it have
bool failed = false; // an expectation of its own did not hold
bool case_failed = false; // a case it declared failed
std::optional<std::filesystem::path> dir;
std::map<std::string, std::pair<double, std::string>> metrics; // name -> value, unit
std::size_t declared = 0; // the cases it declared that ran
std::size_t declared_failed = 0; // of those, the ones that failed
// Its failures as a person reads them, printed under its line when it ends.
std::vector<std::string> failures;
std::uint64_t held_before = 0; // the checks held in the program when it began
bool stopped = false; // a fatal expectation ended it
// SPEC-017 O8: what it writes, kept while it runs under a runner and stated as `output`
// events when it ends; a case it declares keeps its own, and this one's resumes after it.
std::optional<os::stdio::capture> out_capture, err_capture;
std::string out_text, err_text;
bool capturing = false;
};
// The expectations that held, in the whole program: a case's share is the difference over its
// run. Counted without the lock, as a passing check is the common one.
std::atomic<std::uint64_t> g_held{0};
struct declared_suite {
std::string name;
std::source_location where;
attributes attrs;
std::function<void()> body;
};
struct state {
std::recursive_mutex lock;
const clock::time_point began = clock::now();
bool protocol = false;
bool listing = false;
std::optional<std::set<std::string>> selection;
std::vector<std::string> patterns; // a person's, with `*`
std::set<std::string> patterns_met;
bool alone = false; // the selection is a person's, given as the program's arguments
std::set<std::string> seen;
std::ofstream events;
std::filesystem::path tmp_root;
std::uint64_t seed = 0;
bool seed_given = false;
std::vector<running_case> stack;
std::vector<declared_suite> suites;
std::size_t suites_run = 0;
std::map<std::string, int> counts;
// Of `counts`, the cases that declared cases: the console's tally is of the cases that check.
std::map<std::string, int> container_counts;
std::set<std::string> clauses;
bool any_failed = false;
bool program_written = false;
bool done = false;
// P10: how closely a runner asked for use to be measured; a case may ask for more.
mtest::monitor_level monitor = mtest::monitor_level::off;
// P11: the run fixtures and components this program declares.
std::map<std::string, std::function<void(serving&)>> run_fixtures;
// The children this program's files declare, by name.
std::map<std::string, std::function<int(std::vector<std::string>)>, std::less<>> children;
struct component_decl {
std::string name;
std::vector<resource_pool> pools;
};
std::vector<component_decl> components;
state() {
const auto protocol_version = os::env::get("MTEST_PROTOCOL");
const auto report = os::env::get("MTEST_REPORT");
protocol = protocol_version && !protocol_version->empty() && report && !report->empty();
if (protocol) {
events.open(os::fs::from_utf8(*report), std::ios::app | std::ios::binary);
listing = os::env::get("MTEST_MODE").value_or("run") == "list";
if (auto select = os::env::get("MTEST_SELECT"); select && !select->empty()) {
std::set<std::string> ids;
if (auto text = os::fs::read(os::fs::from_utf8(*select))) {
std::string_view rest = *text;
while (!rest.empty()) {
auto nl = rest.find('\n');
auto line = rest.substr(0, nl);
if (line.ends_with('\r'))
line.remove_suffix(1);
if (!line.empty())
ids.emplace(line);
rest =
nl == std::string_view::npos ? std::string_view{} : rest.substr(nl + 1);
}
}
selection = std::move(ids);
}
}
if (auto m = os::env::get("MTEST_MONITOR"); m && !m->empty())
monitor = *m == "trace" ? mtest::monitor_level::trace
: *m == "detailed" ? mtest::monitor_level::detailed
: *m == "basic" ? mtest::monitor_level::basic
: mtest::monitor_level::off;
if (auto t = os::env::get("MTEST_TMPDIR"); t && !t->empty())
tmp_root = os::fs::from_utf8(*t);
if (auto s = os::env::get("MTEST_SEED"); s && !s->empty()) {
std::uint64_t v = 0;
auto [p, ec] = std::from_chars(s->data(), s->data() + s->size(), v);
if (ec == std::errc{} && p == s->data() + s->size()) {
seed = v;
seed_given = true;
}
}
if (!seed_given)
seed = (std::uint64_t(std::random_device{}()) << 32) ^ std::random_device{}();
}
};
state& the() {
static state s;
return s;
}
// An event of the report (P5): an object whose fields keep the order they are written in.
using event = nlohmann::ordered_json;
double since_start() {
return std::chrono::duration<double, std::milli>(clock::now() - the().began).count();
}
// ---- use of the machine (P10) -------------------------------------------------
// The largest resident set of this process since its last reset, where the kernel keeps one that
// can be reset (Linux: /proc/self/status VmHWM, reset by writing 5 to /proc/self/clear_refs).
std::optional<std::uint64_t> resident_peak() {
auto text = os::fs::read(std::filesystem::path("/proc/self/status"));
if (!text)
return std::nullopt;
const auto at = text->find("VmHWM:");
if (at == std::string::npos)
return std::nullopt;
std::uint64_t kb = 0;
std::string_view rest = std::string_view(*text).substr(at + 6);
while (!rest.empty() && (rest.front() == ' ' || rest.front() == '\t'))
rest.remove_prefix(1);
std::from_chars(rest.data(), rest.data() + rest.size(), kb);
return kb * 1024;
}
bool reset_resident_peak() {
return bool(os::fs::write(std::filesystem::path("/proc/self/clear_refs"), "5"));
}
// The resident set now, where the kernel tells it (Linux: /proc/self/status VmRSS).
std::optional<std::uint64_t> resident_now() {
auto text = os::fs::read(std::filesystem::path("/proc/self/status"));
if (!text)
return std::nullopt;
const auto at = text->find("VmRSS:");
if (at == std::string::npos)
return std::nullopt;
std::uint64_t kb = 0;
std::string_view rest = std::string_view(*text).substr(at + 6);
while (!rest.empty() && (rest.front() == ' ' || rest.front() == '\t'))
rest.remove_prefix(1);
std::from_chars(rest.data(), rest.data() + rest.size(), kb);
return kb * 1024;
}
// `trace`: the processor's load and the resident set over the case, sampled every `every`.
class sampler {
public:
explicit sampler(std::chrono::milliseconds every)
: thread_([this, every](std::stop_token stop) {
auto last = os::process::resources(os::process::of::self);
auto then = clock::now();
while (!stop.stop_requested()) {
std::this_thread::sleep_for(every);
auto now = os::process::resources(os::process::of::self);
const auto t = clock::now();
const double wall = std::chrono::duration<double, std::milli>(t - then).count();
std::lock_guard g(m_);
if (now && last && wall > 0)
load_.push_back(std::chrono::duration<double, std::milli>(
(now->user + now->system) - (last->user + last->system))
.count() /
wall);
if (auto rss = resident_now())
rss_.push_back(double(*rss));
last = now;
then = t;
}
}) {}
std::pair<std::vector<double>, std::vector<double>> stop() {
thread_.request_stop();
thread_.join();
std::lock_guard g(m_);
return {load_, rss_};
}
private:
std::mutex m_;
std::vector<double> load_, rss_;
std::jthread thread_;
};
std::chrono::milliseconds sample_every() {
if (auto v = os::env::get("MTEST_MONITOR_SAMPLE"); v && !v->empty()) {
int ms = 0;
if (std::from_chars(v->data(), v->data() + v->size(), ms).ec == std::errc{} && ms > 0)
return std::chrono::milliseconds(ms);
}
return std::chrono::milliseconds(50);
}
struct use_mark {
std::optional<os::process::usage> at;
bool peak_reset = false;
clock::time_point when = clock::now();
std::shared_ptr<sampler> samples;
};
use_mark mark_use(mtest::monitor_level level) {
use_mark m;
if (level == mtest::monitor_level::off)
return m;
if (auto u = os::process::resources(os::process::of::self))
m.at = *u;
if (level >= mtest::monitor_level::detailed)
m.peak_reset = reset_resident_peak();
if (level == mtest::monitor_level::trace)
m.samples = std::make_shared<sampler>(sample_every());
return m;
}
void write_event(const event& e);
// The case's use since `m`, as metrics of the case with their precision (P6.6).
void report_use(std::string_view id, const use_mark& m, mtest::monitor_level level) {
if (level == mtest::monitor_level::off || !m.at)
return;
auto now = os::process::resources(os::process::of::self);
if (!now)
return;
auto put = [&](std::string_view name, double value, std::string_view unit,
std::string_view precision) {
write_event({{"e", "metric"},
{"id", id},
{"name", name},
{"value", value},
{"unit", unit},
{"better", "lower"},
{"precision", precision}});
};
auto ms = [](std::chrono::nanoseconds d) {
return std::chrono::duration<double, std::milli>(d).count();
};
if (m.samples) {
auto [load, rss] = m.samples->stop();
auto series = [&](std::string_view name, const std::vector<double>& v,
std::string_view unit) {
if (v.empty())
return;
event ev = {{"e", "metric"}, {"id", id},
{"name", name}, {"value", *std::ranges::max_element(v)},
{"unit", unit}, {"samples", v},
{"better", "lower"}, {"precision", "sampled"}};
write_event(ev);
};
series("cpu.load", load, "1");
series("mem.rss", rss, "B");
}
put("cpu.user", ms(now->user - m.at->user), "ms", "exact");
put("cpu.sys", ms(now->system - m.at->system), "ms", "exact");
if (level < mtest::monitor_level::detailed)
return;
if (m.peak_reset) {
if (auto peak = resident_peak())
put("mem.peak", double(*peak), "B", "exact");
} else if (now->peak_memory) {
put("mem.peak", double(*now->peak_memory), "B", "process");
}
if (now->peak_memory && m.at->peak_memory)
put("mem.delta", double(*now->peak_memory) - double(*m.at->peak_memory), "B", "process");
if (now->read_bytes && m.at->read_bytes)
put("io.read", double(*now->read_bytes - *m.at->read_bytes), "B", "exact");
if (now->written_bytes && m.at->written_bytes)
put("io.write", double(*now->written_bytes - *m.at->written_bytes), "B", "exact");
}
void write_event(const event& e) {
auto& s = the();
if (!s.protocol || !s.events)
return;
// Text that is not UTF-8 (a program's output) is written with the replacement character.
s.events << e.dump(-1, ' ', false, event::error_handler_t::replace) << "\n";
s.events.flush();
}
void write_program() {
auto& s = the();
if (s.program_written)
return;
s.program_written = true;
// Keeps the marker in a link that discards what nothing reaches.
const char* volatile keep = protocol_marker;
(void)keep;
write_event(
{{"e", "program"},
{"protocol", 1},
{"level", 3},
{"framework", "mcpplibs.mtest"},
{"version", "0.8.5"},
{"isolation", {"process", "case"}},
{"guide", "https://github.com/mcpp-community/mtest/blob/main/lib/docs/writing-tests.md"}});
}
std::string_view kind_name(kind k) {
switch (k) {
case kind::unit:
return "unit";
case kind::e2e:
return "e2e";
case kind::cts:
return "cts";
case kind::bench:
return "bench";
}
return "unit";
}
std::string full_id(std::string_view name) {
auto& s = the();
return s.stack.empty() || s.stack.back().id.empty()
? std::string(name)
: s.stack.back().id + "/" + std::string(name);
}
// What a case declared here has besides its own attributes: its suite's and its enclosing cases'.
attributes inherited() {
auto& s = the();
return s.stack.empty() ? attributes{} : s.stack.back().attrs;
}
// Whether an identifier is what a pattern says: `*` is any run of characters, the rest itself.
// `begins`: whether an identifier that begins so could be.
bool matches(std::string_view pattern, std::string_view id, bool begins = false) {
std::size_t at = 0, in = 0, star = std::string_view::npos, resumed = 0;
while (in < id.size()) {
if (at < pattern.size() && pattern[at] == '*') {
star = at++;
resumed = in;
} else if (at < pattern.size() && pattern[at] == id[in]) {
++at;
++in;
} else if (star != std::string_view::npos) {
at = star + 1;
in = ++resumed;
} else {
return false;
}
}
if (begins)
return true;
while (at < pattern.size() && pattern[at] == '*')
++at;
return at == pattern.size();
}
// The pattern a case answers, itself or by a case it is declared in; met from then on.
bool answers_pattern(const std::string& id) {
auto& s = the();
bool any = false;
for (const auto& pattern : s.patterns) {
bool is = matches(pattern, id);
for (auto slash = id.find('/'); !is && slash != std::string::npos;
slash = id.find('/', slash + 1))
is = matches(pattern, std::string_view(id).substr(0, slash));
if (is) {
s.patterns_met.insert(pattern);
any = true;
}
}
return any;
}
// Whether a case declared inside this one could answer a pattern.
bool may_declare_pattern(const std::string& id) {
return std::ranges::any_of(the().patterns, [&](const std::string& pattern) {
return matches(pattern, id + "/", true);
});
}
// Whether a case is reported: it is selected, or declared inside a selected case (P4.1). A case
// that runs only because it declares a selected one is the scope of that case (P3.4).
bool reported(const std::string& id) {
auto& s = the();
if (!s.selection)
return true;
for (const auto& want : *s.selection)
if (want == id ||
(id.starts_with(want) && id.size() > want.size() && id[want.size()] == '/'))
return true;
return answers_pattern(id);
}
// A case runs when it is selected, or is an ancestor or a descendant of a selected case (P4).
bool selected(const std::string& id) {
auto& s = the();
if (!s.selection)
return true;
for (const auto& want : *s.selection) {
if (want == id)
return true;
if (want.starts_with(id) && want.size() > id.size() && want[id.size()] == '/')
return true;
if (id.starts_with(want) && id.size() > want.size() && id[want.size()] == '/')
return true;
}
return answers_pattern(id) || may_declare_pattern(id);
}
std::string sanitized(std::string_view id) {
std::string out;
for (char c : id)
out += (std::isalnum(static_cast<unsigned char>(c)) || c == '-' || c == '_') ? c : '_';
if (out.size() > 80)
out.resize(80);
return out;
}
// SPEC-017 O8: a case's own output. What the streams hold is written first, so that what the
// case writes is all that the capture keeps.
void flush_streams() {
std::cout.flush();
std::cerr.flush();
std::fflush(nullptr);
}
void begin_capture(running_case& c) {
flush_streams();
if (auto o = os::stdio::capture::begin(os::stdio::stream::output))
c.out_capture.emplace(std::move(*o));
if (auto e = os::stdio::capture::begin(os::stdio::stream::error))
c.err_capture.emplace(std::move(*e));
}
// The capture ended and its text kept; the text is written to the stream it was taken from, so
// a person at the terminal reads what the case wrote where it wrote it.
void end_capture(running_case& c) {
flush_streams();
if (c.err_capture) {
auto t = c.err_capture->text();
c.err_capture.reset();
if (!t.empty()) {
(void)os::stdio::write_error(t);
c.err_text += t;
}
}
if (c.out_capture) {
auto t = c.out_capture->text();
c.out_capture.reset();
if (!t.empty()) {
(void)os::stdio::write_output(t);
(void)os::stdio::flush_output();
c.out_text += t;
}
}
}
void say(std::string_view line) {
std::println("{}", line);
std::cout.flush();
}
// The statement at `line` of `file` and the lines it continues on, as one line: what a check
// without operands states. Empty when the source is not readable here.
std::string statement_at(const std::string& file, std::uint_least32_t line) {
std::ifstream in(os::fs::from_utf8(file), std::ios::binary);
if (!in || line == 0)
return {};
std::string text, one;
for (std::uint_least32_t n = 1; n < line && std::getline(in, one); ++n) {}
int depth = 0;
for (int taken = 0; taken < 8 && std::getline(in, one); ++taken) {
if (one.ends_with('\r'))
one.pop_back();
const auto first = one.find_first_not_of(" \t");
if (first == std::string::npos)
continue;
if (!text.empty())
text += ' ';
text += one.substr(first);
for (char c : one)
depth += c == '(' ? 1 : c == ')' ? -1 : 0;
if (depth <= 0 && text.find(';') != std::string::npos)
break;
}
while (!text.empty() && (text.back() == ' ' || text.back() == '\t'))
text.pop_back();
if (text.size() > 240)
text = text.substr(0, 237) + "...";
return text;
}
// A file under the working directory by its name there; any other by its own.
std::string shown_path(const std::string& file) {
std::error_code ec;
const auto p = os::fs::from_utf8(file);
if (!p.is_absolute())
return file;
const auto rel = std::filesystem::proximate(p, std::filesystem::current_path(ec), ec);
if (ec || rel.empty() || rel.begin()->string() == "..")
return file;
return os::fs::generic_utf8(rel);
}
void record_failure(std::string_view message, const std::source_location& where,
const comparison* value, bool is_fatal) {
auto& s = the();
std::lock_guard g(s.lock);
const bool in_case = !s.stack.empty() && !s.stack.back().scope;
const std::string id = in_case ? s.stack.back().id : std::string{};
if (in_case)
s.stack.back().failed = true;
else
s.any_failed = true;
const std::string file = where.file_name();
const bool compared = value && !value->expected.empty();
std::string text = std::string(message);
if (value && !value->expression.empty())
text = text.empty() ? value->expression : value->expression + ": " + text;
if (text.empty())
text = "the expectation did not hold";
// As a person reads it: where, the statement, the two sides or that it did not hold, the
// message the check added, and whether the case went on.
const std::string source = value ? statement_at(file, where.line()) : std::string{};
std::string block = std::format("{}:{}", shown_path(file), where.line());
if (!source.empty())
block += "\n " + source;
if (compared) {
block +=
std::format("\n expected {}\n actual {}", value->expected, value->actual);
if (!message.empty())
block += std::format("\n note {}", message);
} else if (value) {
block += source.empty() || !message.empty() || !value->expression.empty()
? "\n " + text
: std::string("\n did not hold");
} else {
block += "\n " + text;
}
if (is_fatal) {
block += "\n fatal: the rest of the case did not run";
if (in_case)
s.stack.back().stopped = true;
}
if (in_case)
s.stack.back().failures.push_back(std::move(block));
else {
std::string shown;
for (std::string_view rest = block; !rest.empty();) {
const auto nl = rest.find('\n');
shown += " " + std::string(rest.substr(0, nl)) + "\n";
rest = nl == std::string_view::npos ? std::string_view{} : rest.substr(nl + 1);
}
shown.pop_back();
say(shown);
}
event ev = {{"e", "failure"},
{"id", id},
{"file", file},
{"line", where.line()},
{"message", text}};
if (!source.empty())
ev["source"] = source;
if (compared) {
ev["expected"] = value->expected;
ev["actual"] = value->actual;
}
if (is_fatal)
ev["fatal"] = true;
write_event(ev);
}
event bounds_json(const std::vector<metric_bound>& bounds) {
event out = event::array();
for (const auto& b : bounds) {
event one = {{"metric", b.metric}};
if (b.max)
one["max"] = *b.max;
if (b.min)
one["min"] = *b.min;
out.push_back(std::move(one));
}
return out;
}
// P6.5: each bound of the case on the metrics it reported, as an expectation at its end.
void check_bounds(const std::vector<metric_bound>& bounds, const std::source_location& where) {
auto& s = the();
const auto& reported = s.stack.back().metrics;
for (const auto& b : bounds) {
const auto it = reported.find(b.metric);
if (it == reported.end()) {
record_failure(std::format("the case reported no metric `{}`, which it bounds",
b.metric),
where, nullptr, false);
continue;
}
const auto [value, unit] = it->second;
// A unit of 1 (a ratio, a count) is not written.
const auto in = [&](double v) {
return unit.empty() || unit == "1" ? std::format("{:g}", v)
: std::format("{:g} {}", v, unit);
};
if (b.max && value > *b.max)
record_failure(std::format("metric {} = {}, over its bound {} (at_most)", b.metric,
in(value), in(*b.max)),
where, nullptr, false);
if (b.min && value < *b.min)
record_failure(std::format("metric {} = {}, under its bound {} (at_least)", b.metric,
in(value), in(*b.min)),
where, nullptr, false);
}
}
void run_suites() {
auto& s = the();
while (s.suites_run < s.suites.size()) {
auto [name, where, attrs, f] = s.suites[s.suites_run++];
if (!name.empty()) {
// A named suite is a case: listed with its attributes, run when it is selected.
test(name, std::move(attrs)) = body(std::move(f), where);
continue;
}
s.stack.push_back({s.stack.empty() ? std::string{} : s.stack.back().id, true,
within(inherited(), attrs)});
try {
f();
} catch (const std::exception& e) {
record_failure(std::format("an exception escaped the suite: {}", e.what()),
std::source_location::current(), nullptr, false);
} catch (...) {
record_failure("an exception escaped the suite", std::source_location::current(),
nullptr, false);
}
s.stack.pop_back();
}
}
} // namespace
// ---- expectations ------------------------------------------------------------
comparison contains(std::string_view text, std::string_view part) {
comparison c;
c.held = text.find(part) != std::string_view::npos;
if (!c.held) {
c.actual = detail::show(text);
c.expected = detail::show(part);
c.expression =
std::format("{} does not contain {}",
c.actual.size() > 200 ? c.actual.substr(0, 200) + "..." : c.actual,
c.expected);
}
return c;
}
expectation::expectation(comparison value, bool is_fatal, std::source_location where)
: held_(value.held), fatal_(is_fatal), value_(std::move(value)), where_(where) {}
expectation::~expectation() noexcept(false) {
if (held_) {
g_held.fetch_add(1, std::memory_order_relaxed);
return;
}
record_failure(message_, where_, &value_, fatal_);
if (fatal_ && std::uncaught_exceptions() == 0)
throw fatal_signal{};
}
expectation expect(bool held, std::source_location where) {
comparison c;
c.held = held;
return expectation(std::move(c), false, where);
}
expectation expect(comparison value, std::source_location where) {
return expectation(std::move(value), false, where);
}
expectation expect(fatal_condition value, std::source_location where) {
return expectation(std::move(value.value), true, where);
}
void skip(std::string reason) {
throw skip_signal{std::move(reason)};
}
void fail(std::string message, std::source_location where) {
record_failure(message, where, nullptr, true);
throw fatal_signal{};
}
// ---- cases --------------------------------------------------------------------
void test::operator=(body b) {
auto& s = the();
std::unique_lock g(s.lock);
write_program();
const std::string id = full_id(name_);
attributes attrs = within(inherited(), attrs_);
attrs.skip = skip_reason(attrs);
s.seen.insert(id);
s.clauses.insert(attrs.clauses.begin(), attrs.clauses.end());
const std::string file = b.where.file_name();
if (s.listing) {
event ev = {{"e", "case"},
{"id", id},
{"name", name_},
{"file", file},
{"line", b.where.line()}};
// A kind no attribute states is the runner's to give (the program's, by its directory).
if (attrs.kind)
ev["kind"] = kind_name(*attrs.kind);
ev["tags"] = attrs.tags;
ev["clauses"] = attrs.clauses;
ev["resources"] = attrs.resources;
ev["artifacts"] = attrs.artifacts;
if (attrs.only_platforms || attrs.only_architectures) {
auto words = attrs.only_platforms.value_or(std::vector<std::string>{});
for (const auto& w : attrs.only_architectures.value_or(std::vector<std::string>{}))
words.push_back(w);
ev["only"] = words;
}
if (!attrs.excepted.empty())
ev["except"] = attrs.excepted;
if (!attrs.capabilities.empty()) {
event answered = event::object();
for (const auto& c : attrs.capabilities)
answered[c.name()] = c.answer().present;
ev["capabilities"] = answered;
}
if (attrs.limit)
ev["timeout"] = std::chrono::duration<double>(*attrs.limit).count();
if (!attrs.bounds.empty())
ev["bounds"] = bounds_json(attrs.bounds);
if (attrs.isolated)
ev["isolation"] = "case";
if (!attrs.uses.empty())
ev["uses"] = attrs.uses;
if (attrs.retries)
ev["retries"] = *attrs.retries;
if (attrs.diagnosis) {
event d = {{"env", event::object()}};
for (const auto& [k, v] : attrs.diagnosis->env)
d["env"][k] = v;
if (attrs.diagnosis->limit.count() > 0)
d["timeout"] = std::chrono::duration<double>(attrs.diagnosis->limit).count();
ev["diagnose"] = d;
}
if (attrs.skip) {
ev["expect"] = "skip";
ev["reason"] = *attrs.skip;
} else if (attrs.expected_failure)
ev["expect"] = "fail";
write_event(ev);
return;
}
if (!selected(id))
return;
auto finish = [&](std::string_view status, double ms, std::string_view reason,
const running_case* done = nullptr) {
s.counts[std::string(status)]++;
const bool container = done && done->declared > 0;
if (container)
s.container_counts[std::string(status)]++;
if (status == "fail" || status == "xpass")
s.any_failed = true;
event ev = {{"e", "end"}, {"id", id}, {"status", status}, {"ms", ms}};
if (!reason.empty())
ev["reason"] = reason;
// SPEC-017 O3: of the checks the case ran, those that held and those that did not.
if (done && status != "skip")
ev["checks"] = {{"held", g_held.load(std::memory_order_relaxed) - done->held_before},
{"failed", done->failures.size()}};
write_event(ev);
const char* mark = status == "pass" ? " ok "
: status == "skip" ? " skip"
: status == "xfail" ? " xfail"
: " FAIL";
// What it amounts to: of a case that declared cases, how many of them failed; of one that
// checks, how many of its checks failed, and whether a fatal one ended it.
std::string tally;
if (done && status != "skip") {
if (container && done->declared_failed > 0)
tally = std::format("{} of {} cases failed", done->declared_failed, done->declared);
const std::size_t failed = done->failures.size();
if (failed > 0) {
const auto held = g_held.load(std::memory_order_relaxed) - done->held_before;
if (!tally.empty())
tally += "; ";
tally += std::format("{} of {} checks failed", failed, failed + held);
if (done->stopped)
tally += ", stopped";
}
}
say(std::format("{} {}{}{}{}", mark, id, reason.empty() ? "" : " -- ", reason,
tally.empty() ? "" : " (" + tally + ")"));
if (done)
for (const auto& block : done->failures) {
std::string shown;
for (std::string_view rest = block; !rest.empty();) {
const auto nl = rest.find('\n');
shown += " " + std::string(rest.substr(0, nl)) + "\n";
rest = nl == std::string_view::npos ? std::string_view{} : rest.substr(nl + 1);
}
shown.pop_back();
say(shown);
}
};
const bool own = reported(id);
if (attrs.skip) {
// A skipped scope skips the selected cases it declares.
std::vector<std::string> skipped{id};
if (!own) {
skipped.clear();
for (const auto& want : *s.selection)
if (want.starts_with(id + "/")) {
skipped.push_back(want);
s.seen.insert(want);
}
// What a pattern would have found in it is not declared: the scope stands for it.
for (const auto& pattern : s.patterns)
if (matches(pattern, id + "/", true)) {
s.patterns_met.insert(pattern);
if (!std::ranges::contains(skipped, id))
skipped.push_back(id);
}
}
for (const auto& one : skipped) {
write_event({{"e", "begin"}, {"id", one}, {"t", since_start()}});
event ev = {{"e", "end"},
{"id", one},
{"status", "skip"},
{"ms", 0},
{"reason", *attrs.skip}};
s.counts["skip"]++;
write_event(ev);
say(std::format(" skip {} -- {}", one, *attrs.skip));
}
return;
}
// P3.4: a case declared inside a case is not listed; its begin states what it is.
event begun = {{"e", "begin"}, {"id", id}, {"t", since_start()}};
if (attrs.kind)
begun["kind"] = kind_name(*attrs.kind);
if (!attrs.tags.empty())
begun["tags"] = attrs.tags;
if (!attrs.clauses.empty())
begun["clauses"] = attrs.clauses;
if (!attrs.bounds.empty())
begun["bounds"] = bounds_json(attrs.bounds);
if (own)
write_event(begun);
if (!s.stack.empty() && !s.stack.back().scope)
++s.stack.back().declared;
const bool capturing = s.protocol && !s.listing && own;
if (capturing && !s.stack.empty() && s.stack.back().capturing)
end_capture(s.stack.back()); // the declaring case's output so far is its own
s.stack.push_back({id, false, attrs});
s.stack.back().held_before = g_held.load(std::memory_order_relaxed);
if (capturing) {
s.stack.back().capturing = true;
begin_capture(s.stack.back());
}
const auto started = clock::now();
std::string status = "pass";
std::string reason;
const auto level = std::max(s.monitor, attrs.monitoring.value_or(mtest::monitor_level::off));
const auto use = own ? mark_use(level) : use_mark{};
g.unlock();
try {
b.run();
} catch (const fatal_signal&) {
} catch (const skip_signal& sk) {
status = "skip";
reason = sk.reason;
} catch (const std::exception& e) {
record_failure(std::format("an exception escaped the case: {}", e.what()), b.where, nullptr,
false);
} catch (...) {
record_failure("an exception that is not a std::exception escaped the case", b.where,
nullptr, false);
}
g.lock();
// A case that declared cases is judged by them; one that declared none, by its own metrics.
if (status != "skip" && s.stack.back().declared == 0)
check_bounds(attrs.bounds, b.where);
const double ms = std::chrono::duration<double, std::milli>(clock::now() - started).count();
if (own)
report_use(id, use, level);
if (capturing)
end_capture(s.stack.back());
running_case done = std::move(s.stack.back());
s.stack.pop_back();
if (capturing) {
if (!done.out_text.empty())
write_event({{"e", "output"}, {"id", id}, {"stream", "out"}, {"text", done.out_text}});
if (!done.err_text.empty())
write_event({{"e", "output"}, {"id", id}, {"stream", "err"}, {"text", done.err_text}});
}
if (done.dir) {
std::error_code ec;
std::filesystem::remove_all(*done.dir, ec);
}
if (!own) {
// A scope is reported only when a statement of its own failed.
if (!done.failed)
return;
write_event(begun);
}
if (status != "skip")
status = done.failed || done.case_failed ? "fail" : "pass";
if (attrs.expected_failure) {
if (status == "fail") {
status = "xfail";
reason = *attrs.expected_failure;
} else if (status == "pass") {
status = "xpass";
reason = "expected to fail: " + *attrs.expected_failure;
}
}
if (status == "fail" && !s.stack.empty() && !s.stack.back().scope) {
s.stack.back().case_failed = true;
++s.stack.back().declared_failed;
}
finish(status, ms, reason, &done);
// The declaring case's capture resumes, after this case's own lines.
if (capturing && !s.stack.empty() && s.stack.back().capturing)
begin_capture(s.stack.back());
}
void Suite::add(std::string name, std::source_location where, attributes attrs,
std::function<void()> f) {
the().suites.push_back({std::move(name), where, std::move(attrs), std::move(f)});
}
namespace {
// MTEST_SERVE: this program serves one of its run fixtures for a runner (P11.3) and runs no case.
std::optional<int> serve_if_asked() {
auto name = os::env::get("MTEST_SERVE");
if (!name || name->empty())
return std::nullopt;
auto& s = the();
auto it = s.run_fixtures.find(*name);
if (it == s.run_fixtures.end()) {
std::println(std::cerr, "mtest: this program serves no run fixture named {}", *name);
return 2;
}
serving sv;
try {
it->second(sv);
} catch (const std::exception& e) {
std::println(std::cerr, "mtest: serving {} failed: {}", *name, e.what());
return 1;
}
return 0;
}
void list_components() {
auto& s = the();
for (const auto& c : s.components) {
event ev = {{"e", "component"}, {"name", c.name}};
auto pools = event::array();
for (const auto& p : c.pools)
pools.push_back({{"name", p.name}, {"capacity", p.capacity}});
ev["pools"] = pools;
write_event(ev);
}
for (const auto& [name, f] : s.run_fixtures)
write_event({{"e", "component"}, {"fixture", name}, {"scope", "run"}});
}