llvm/flang/test/evaluate/testing.cc
peter klausler 4c11bc07d4 [flang] Prepare for check-in
Original-commit: flang-compiler/f18@2f5b2d5f40
Reviewed-on: https://github.com/flang-compiler/f18/pull/111
Tree-same-pre-rewrite: false
2018-06-22 14:59:28 -07:00

136 lines
3.3 KiB
C++

// Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "testing.h"
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <iostream>
namespace testing {
namespace {
int passes{0};
int failures{0};
} // namespace
static void BitBucket(const char *, ...) {}
static void PrintFailureDetails(const char *format, ...) {
va_list ap;
va_start(ap, format);
fputs("\t", stderr);
vfprintf(stderr, format, ap);
va_end(ap);
fputc('\n', stderr);
}
FailureDetailPrinter Test(
const char *file, int line, const char *predicate, bool pass) {
if (pass) {
++passes;
return BitBucket;
} else {
++failures;
fprintf(stderr, "%s:%d: FAIL: %s\n", file, line, predicate);
return PrintFailureDetails;
}
}
FailureDetailPrinter Match(const char *file, int line, unsigned long long want,
const char *gots, unsigned long long got) {
if (want == got) {
++passes;
return BitBucket;
} else {
++failures;
fprintf(stderr, "%s:%d: FAIL: %s == 0x%llx, not 0x%llx\n", file, line, gots,
got, want);
return PrintFailureDetails;
}
}
FailureDetailPrinter Match(const char *file, int line, const char *want,
const char *gots, const std::string &got) {
if (want == got) {
++passes;
return BitBucket;
} else {
++failures;
fprintf(stderr, "%s:%d: FAIL: %s == \"%s\", not \"%s\"\n", file, line, gots,
got.data(), want);
return PrintFailureDetails;
}
}
FailureDetailPrinter Compare(const char *file, int line, const char *xs,
const char *rel, const char *ys, unsigned long long x,
unsigned long long y) {
while (*rel == ' ') {
++rel;
}
bool pass{false};
if (*rel == '<') {
if (rel[1] == '=') {
pass = x <= y;
} else {
pass = x < y;
}
} else if (*rel == '>') {
if (rel[1] == '=') {
pass = x >= y;
} else {
pass = x > y;
}
} else if (*rel == '=') {
pass = x == y;
} else if (*rel == '!') {
pass = x != y;
}
if (pass) {
++passes;
return BitBucket;
} else {
++failures;
fprintf(stderr, "%s:%d: FAIL: %s[0x%llx] %s %s[0x%llx]\n", file, line, xs,
x, rel, ys, y);
return PrintFailureDetails;
}
}
int Complete() {
if (failures == 0) {
if (passes == 1) {
std::cout << "single test PASSES\n";
} else {
std::cout << "all " << std::dec << passes << " tests PASS\n";
}
passes = 0;
return EXIT_SUCCESS;
} else {
if (passes == 1) {
std::cerr << "1 test passes, ";
} else {
std::cerr << std::dec << passes << " tests pass, ";
}
if (failures == 1) {
std::cerr << "1 test FAILS\n";
} else {
std::cerr << std::dec << failures << " tests FAIL\n";
}
passes = failures = 0;
return EXIT_FAILURE;
}
}
} // namespace testing