1ee5fa1f2SLuke Ireland #include "testing.h"
28670e499SCaroline Concatto #include "llvm/Support/raw_ostream.h"
3ee5fa1f2SLuke Ireland #include <cstdarg>
4ee5fa1f2SLuke Ireland #include <cstdio>
5ee5fa1f2SLuke Ireland #include <cstdlib>
6ee5fa1f2SLuke Ireland
7ee5fa1f2SLuke Ireland namespace testing {
8ee5fa1f2SLuke Ireland
9ee5fa1f2SLuke Ireland namespace {
10ee5fa1f2SLuke Ireland int passes{0};
11ee5fa1f2SLuke Ireland int failures{0};
12*1f879005STim Keith } // namespace
13ee5fa1f2SLuke Ireland
BitBucket(const char *,...)14ee5fa1f2SLuke Ireland static void BitBucket(const char *, ...) {}
15ee5fa1f2SLuke Ireland
PrintFailureDetails(const char * format,...)16ee5fa1f2SLuke Ireland static void PrintFailureDetails(const char *format, ...) {
17ee5fa1f2SLuke Ireland va_list ap;
18ee5fa1f2SLuke Ireland va_start(ap, format);
19ee5fa1f2SLuke Ireland fputs("\t", stderr);
20ee5fa1f2SLuke Ireland vfprintf(stderr, format, ap);
21ee5fa1f2SLuke Ireland va_end(ap);
22ee5fa1f2SLuke Ireland fputc('\n', stderr);
23ee5fa1f2SLuke Ireland }
24ee5fa1f2SLuke Ireland
Test(const char * file,int line,const char * predicate,bool pass)25ee5fa1f2SLuke Ireland FailureDetailPrinter Test(
26ee5fa1f2SLuke Ireland const char *file, int line, const char *predicate, bool pass) {
27ee5fa1f2SLuke Ireland if (pass) {
28ee5fa1f2SLuke Ireland ++passes;
29ee5fa1f2SLuke Ireland return BitBucket;
30ee5fa1f2SLuke Ireland } else {
31ee5fa1f2SLuke Ireland ++failures;
32ee5fa1f2SLuke Ireland fprintf(stderr, "%s:%d: FAIL: %s\n", file, line, predicate);
33ee5fa1f2SLuke Ireland return PrintFailureDetails;
34ee5fa1f2SLuke Ireland }
35ee5fa1f2SLuke Ireland }
36ee5fa1f2SLuke Ireland
Match(const char * file,int line,std::uint64_t want,const char * gots,std::uint64_t got)37ee5fa1f2SLuke Ireland FailureDetailPrinter Match(const char *file, int line, std::uint64_t want,
38ee5fa1f2SLuke Ireland const char *gots, std::uint64_t got) {
39ee5fa1f2SLuke Ireland if (want == got) {
40ee5fa1f2SLuke Ireland ++passes;
41ee5fa1f2SLuke Ireland return BitBucket;
42ee5fa1f2SLuke Ireland } else {
43ee5fa1f2SLuke Ireland ++failures;
44ee5fa1f2SLuke Ireland fprintf(stderr, "%s:%d: FAIL: %s == 0x%jx, not 0x%jx\n", file, line, gots,
45ee5fa1f2SLuke Ireland static_cast<std::uintmax_t>(got), static_cast<std::uintmax_t>(want));
46ee5fa1f2SLuke Ireland return PrintFailureDetails;
47ee5fa1f2SLuke Ireland }
48ee5fa1f2SLuke Ireland }
49ee5fa1f2SLuke Ireland
Match(const char * file,int line,const char * want,const char * gots,const std::string & got)50ee5fa1f2SLuke Ireland FailureDetailPrinter Match(const char *file, int line, const char *want,
51ee5fa1f2SLuke Ireland const char *gots, const std::string &got) {
52ee5fa1f2SLuke Ireland if (want == got) {
53ee5fa1f2SLuke Ireland ++passes;
54ee5fa1f2SLuke Ireland return BitBucket;
55ee5fa1f2SLuke Ireland } else {
56ee5fa1f2SLuke Ireland ++failures;
57ee5fa1f2SLuke Ireland fprintf(stderr, "%s:%d: FAIL: %s == \"%s\", not \"%s\"\n", file, line, gots,
58ee5fa1f2SLuke Ireland got.data(), want);
59ee5fa1f2SLuke Ireland return PrintFailureDetails;
60ee5fa1f2SLuke Ireland }
61ee5fa1f2SLuke Ireland }
62ee5fa1f2SLuke Ireland
Match(const char * file,int line,const std::string & want,const char * gots,const std::string & got)63ee5fa1f2SLuke Ireland FailureDetailPrinter Match(const char *file, int line, const std::string &want,
64ee5fa1f2SLuke Ireland const char *gots, const std::string &got) {
65ee5fa1f2SLuke Ireland return Match(file, line, want.data(), gots, got);
66ee5fa1f2SLuke Ireland }
67ee5fa1f2SLuke Ireland
Compare(const char * file,int line,const char * xs,const char * rel,const char * ys,std::uint64_t x,std::uint64_t y)68ee5fa1f2SLuke Ireland FailureDetailPrinter Compare(const char *file, int line, const char *xs,
69ee5fa1f2SLuke Ireland const char *rel, const char *ys, std::uint64_t x, std::uint64_t y) {
70ee5fa1f2SLuke Ireland while (*rel == ' ') {
71ee5fa1f2SLuke Ireland ++rel;
72ee5fa1f2SLuke Ireland }
73ee5fa1f2SLuke Ireland bool pass{false};
74ee5fa1f2SLuke Ireland if (*rel == '<') {
75ee5fa1f2SLuke Ireland if (rel[1] == '=') {
76ee5fa1f2SLuke Ireland pass = x <= y;
77ee5fa1f2SLuke Ireland } else {
78ee5fa1f2SLuke Ireland pass = x < y;
79ee5fa1f2SLuke Ireland }
80ee5fa1f2SLuke Ireland } else if (*rel == '>') {
81ee5fa1f2SLuke Ireland if (rel[1] == '=') {
82ee5fa1f2SLuke Ireland pass = x >= y;
83ee5fa1f2SLuke Ireland } else {
84ee5fa1f2SLuke Ireland pass = x > y;
85ee5fa1f2SLuke Ireland }
86ee5fa1f2SLuke Ireland } else if (*rel == '=') {
87ee5fa1f2SLuke Ireland pass = x == y;
88ee5fa1f2SLuke Ireland } else if (*rel == '!') {
89ee5fa1f2SLuke Ireland pass = x != y;
90ee5fa1f2SLuke Ireland }
91ee5fa1f2SLuke Ireland if (pass) {
92ee5fa1f2SLuke Ireland ++passes;
93ee5fa1f2SLuke Ireland return BitBucket;
94ee5fa1f2SLuke Ireland } else {
95ee5fa1f2SLuke Ireland ++failures;
96ee5fa1f2SLuke Ireland fprintf(stderr, "%s:%d: FAIL: %s[0x%jx] %s %s[0x%jx]\n", file, line, xs,
97ee5fa1f2SLuke Ireland static_cast<std::uintmax_t>(x), rel, ys,
98ee5fa1f2SLuke Ireland static_cast<std::uintmax_t>(y));
99ee5fa1f2SLuke Ireland return PrintFailureDetails;
100ee5fa1f2SLuke Ireland }
101ee5fa1f2SLuke Ireland }
102ee5fa1f2SLuke Ireland
Complete()103ee5fa1f2SLuke Ireland int Complete() {
104ee5fa1f2SLuke Ireland if (failures == 0) {
105ee5fa1f2SLuke Ireland if (passes == 1) {
1068670e499SCaroline Concatto llvm::outs() << "single test PASSES\n";
107ee5fa1f2SLuke Ireland } else {
1088670e499SCaroline Concatto llvm::outs() << "all " << passes << " tests PASS\n";
109ee5fa1f2SLuke Ireland }
110ee5fa1f2SLuke Ireland passes = 0;
111ee5fa1f2SLuke Ireland return EXIT_SUCCESS;
112ee5fa1f2SLuke Ireland } else {
113ee5fa1f2SLuke Ireland if (passes == 1) {
1148670e499SCaroline Concatto llvm::errs() << "1 test passes, ";
115ee5fa1f2SLuke Ireland } else {
1168670e499SCaroline Concatto llvm::errs() << passes << " tests pass, ";
117ee5fa1f2SLuke Ireland }
118ee5fa1f2SLuke Ireland if (failures == 1) {
1198670e499SCaroline Concatto llvm::errs() << "1 test FAILS\n";
120ee5fa1f2SLuke Ireland } else {
1218670e499SCaroline Concatto llvm::errs() << failures << " tests FAIL\n";
122ee5fa1f2SLuke Ireland }
123ee5fa1f2SLuke Ireland passes = failures = 0;
124ee5fa1f2SLuke Ireland return EXIT_FAILURE;
125ee5fa1f2SLuke Ireland }
126ee5fa1f2SLuke Ireland }
127*1f879005STim Keith } // namespace testing
128