2018-05-31 01:08:42 +02:00
|
|
|
// 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};
|
2018-05-31 01:09:52 +02:00
|
|
|
} // namespace
|
2018-05-31 01:08:42 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-05-31 01:09:52 +02:00
|
|
|
FailureDetailPrinter Test(
|
|
|
|
const char *file, int line, const char *predicate, bool pass) {
|
2018-05-31 01:08:42 +02:00
|
|
|
if (pass) {
|
|
|
|
++passes;
|
|
|
|
return BitBucket;
|
|
|
|
} else {
|
|
|
|
++failures;
|
2018-06-06 20:26:00 +02:00
|
|
|
fprintf(stderr, "%s:%d: FAIL: %s\n", file, line, predicate);
|
2018-05-31 01:08:42 +02:00
|
|
|
return PrintFailureDetails;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-01 20:48:31 +02:00
|
|
|
FailureDetailPrinter Match(const char *file, int line, unsigned long long want,
|
|
|
|
const char *gots, unsigned long long got) {
|
2018-05-31 19:45:41 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 22:00:13 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 01:09:52 +02:00
|
|
|
FailureDetailPrinter Compare(const char *file, int line, const char *xs,
|
|
|
|
const char *rel, const char *ys, unsigned long long x,
|
|
|
|
unsigned long long y) {
|
2018-05-31 01:08:42 +02:00
|
|
|
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;
|
2018-06-09 00:49:06 +02:00
|
|
|
fprintf(stderr, "%s:%d: FAIL: %s[0x%llx] %s %s[0x%llx]\n", file, line, xs,
|
|
|
|
x, rel, ys, y);
|
2018-05-31 01:08:42 +02:00
|
|
|
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
|