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
|
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#define C_LUCY_TESTBATCH
#include "Lucy/Util/ToolSet.h"
#include "Lucy/Test.h"
TestBatch*
TestBatch_new(int64_t num_tests) {
TestBatch *self = (TestBatch*)VTable_Make_Obj(TESTBATCH);
return TestBatch_init(self, num_tests);
}
TestBatch*
TestBatch_init(TestBatch *self, int64_t num_tests) {
// Assign.
self->num_tests = num_tests;
// Initialize.
self->test_num = 0;
self->num_passed = 0;
self->num_failed = 0;
self->num_skipped = 0;
// Unbuffer stdout. TODO: move this elsewhere.
int check_val = setvbuf(stdout, NULL, _IONBF, 0);
if (check_val != 0) {
fprintf(stderr, "Failed when trying to unbuffer stdout\n");
}
return self;
}
void
TestBatch_plan(TestBatch *self) {
printf("1..%" I64P "\n", self->num_tests);
}
bool_t
TestBatch_test_true(void *vself, bool_t condition, const char *pattern, ...) {
va_list args;
va_start(args, pattern);
bool_t result = TestBatch_VTest_True((TestBatch*)vself, condition,
pattern, args);
va_end(args);
return result;
}
bool_t
TestBatch_test_false(void *vself, bool_t condition, const char *pattern, ...) {
va_list args;
va_start(args, pattern);
bool_t result = TestBatch_VTest_False((TestBatch*)vself, condition,
pattern, args);
va_end(args);
return result;
}
bool_t
TestBatch_test_int_equals(void *vself, long got, long expected,
const char *pattern, ...) {
va_list args;
va_start(args, pattern);
bool_t result = TestBatch_VTest_Int_Equals((TestBatch*)vself, got,
expected, pattern, args);
va_end(args);
return result;
}
bool_t
TestBatch_test_float_equals(void *vself, double got, double expected,
const char *pattern, ...) {
va_list args;
va_start(args, pattern);
bool_t result = TestBatch_VTest_Float_Equals((TestBatch*)vself, got,
expected, pattern, args);
va_end(args);
return result;
}
bool_t
TestBatch_test_string_equals(void *vself, const char *got,
const char *expected, const char *pattern, ...) {
va_list args;
va_start(args, pattern);
bool_t result = TestBatch_VTest_String_Equals((TestBatch*)vself, got,
expected, pattern, args);
va_end(args);
return result;
}
bool_t
TestBatch_pass(void *vself, const char *pattern, ...) {
va_list args;
va_start(args, pattern);
bool_t result = TestBatch_VPass((TestBatch*)vself, pattern, args);
va_end(args);
return result;
}
bool_t
TestBatch_fail(void *vself, const char *pattern, ...) {
va_list args;
va_start(args, pattern);
bool_t result = TestBatch_VFail((TestBatch*)vself, pattern, args);
va_end(args);
return result;
}
void
TestBatch_skip(void *vself, const char *pattern, ...) {
va_list args;
va_start(args, pattern);
TestBatch_VSkip((TestBatch*)vself, pattern, args);
va_end(args);
}
bool_t
TestBatch_vtest_true(TestBatch *self, bool_t condition, const char *pattern,
va_list args) {
// Increment test number.
self->test_num++;
// Test condition and pass or fail.
if (condition) {
self->num_passed++;
printf("ok %" I64P " - ", self->test_num);
vprintf(pattern, args);
printf("\n");
return true;
}
else {
self->num_failed++;
printf("not ok %" I64P " - ", self->test_num);
vprintf(pattern, args);
printf("\n");
return false;
}
}
bool_t
TestBatch_vtest_false(TestBatch *self, bool_t condition,
const char *pattern, va_list args) {
// Increment test number.
self->test_num++;
// Test condition and pass or fail.
if (!condition) {
self->num_passed++;
printf("ok %" I64P " - ", self->test_num);
vprintf(pattern, args);
printf("\n");
return true;
}
else {
self->num_failed++;
printf("not ok %" I64P " - ", self->test_num);
vprintf(pattern, args);
printf("\n");
return false;
}
}
bool_t
TestBatch_vtest_int_equals(TestBatch *self, long got, long expected,
const char *pattern, va_list args) {
// Increment test number.
self->test_num++;
// Test condition and pass or fail.
if (expected == got) {
self->num_passed++;
printf("ok %" I64P " - ", self->test_num);
vprintf(pattern, args);
printf("\n");
return true;
}
else {
self->num_failed++;
printf("not ok %" I64P " - Expected '%ld', got '%ld'\n ",
self->test_num, expected, got);
vprintf(pattern, args);
printf("\n");
return false;
}
}
bool_t
TestBatch_vtest_float_equals(TestBatch *self, double got, double expected,
const char *pattern, va_list args) {
double diff = expected / got;
// Increment test number.
self->test_num++;
// Evaluate condition and pass or fail.
if (diff > 0.00001) {
self->num_passed++;
printf("ok %" I64P " - ", self->test_num);
vprintf(pattern, args);
printf("\n");
return true;
}
else {
self->num_failed++;
printf("not ok %" I64P " - Expected '%f', got '%f'\n ",
self->test_num, expected, got);
vprintf(pattern, args);
printf("\n");
return false;
}
}
bool_t
TestBatch_vtest_string_equals(TestBatch *self, const char *got,
const char *expected, const char *pattern,
va_list args) {
// Increment test number.
self->test_num++;
// Test condition and pass or fail.
if (strcmp(expected, got) == 0) {
self->num_passed++;
printf("ok %" I64P " - ", self->test_num);
vprintf(pattern, args);
printf("\n");
return true;
}
else {
self->num_failed++;
printf("not ok %" I64P " - Expected '%s', got '%s'\n ",
self->test_num, expected, got);
vprintf(pattern, args);
printf("\n");
return false;
}
}
bool_t
TestBatch_vpass(TestBatch *self, const char *pattern, va_list args) {
// Increment test number.
self->test_num++;
// Update counter, indicate pass.
self->num_passed++;
printf("ok %" I64P " - ", self->test_num);
vprintf(pattern, args);
printf("\n");
return true;
}
bool_t
TestBatch_vfail(TestBatch *self, const char *pattern, va_list args) {
// Increment test number.
self->test_num++;
// Update counter, indicate failure.
self->num_failed++;
printf("not ok %" I64P " - ", self->test_num);
vprintf(pattern, args);
printf("\n");
return false;
}
void
TestBatch_vskip(TestBatch *self, const char *pattern, va_list args) {
self->test_num++;
printf("ok %" I64P " # SKIP ", self->test_num);
vprintf(pattern, args);
printf("\n");
self->num_skipped++;
}
|