github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/database/leveldb.chai2010/capi/test/test.cc (about) 1 // Copyright 2013 <chaishushan{AT}gmail.com>. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 #include "test.h" 6 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <string.h> 10 #include <math.h> 11 12 struct Test { 13 void (*fn)(void); 14 const char *name; 15 }; 16 17 static Test tests[10000]; 18 static int ntests; 19 static int testid; 20 21 static const char* basename(const char* fname) { 22 int len = strlen(fname); 23 const char* s = fname + len; 24 while(s > fname) { 25 if(s[-1] == '/' || s[-1] == '\\') return s; 26 s--; 27 } 28 return s; 29 } 30 31 void RegisterTest(void (*fn)(void), const char* tname) { 32 tests[ntests].fn = fn; 33 tests[ntests++].name = tname; 34 } 35 36 void TestAssertTrue(bool condition, const char* fname, int lineno) { 37 if(!condition) { 38 fname = basename(fname); 39 printf("fail, %s, line %d: ASSERT_TRUE(false)\n", fname, lineno); 40 exit(-1); 41 } 42 } 43 void TestAssertEQ(int a, int b, const char* fname, int lineno) { 44 if(a != b) { 45 fname = basename(fname); 46 printf("fail, %s, line %d: ASSERT_EQ(%d, %d)\n", fname, lineno, a, b); 47 exit(-1); 48 } 49 } 50 void TestAssertStrEQ(const char* a, const char* b, const char* fname, int lineno) { 51 if(strcmp(a, b) != 0) { 52 fname = basename(fname); 53 printf("fail, %s, line %d: ASSERT_STREQ(\"%s\", \"%s\")\n", fname, lineno, a, b); 54 exit(-1); 55 } 56 } 57 void TestAssertNear(float a, float b, float abs_error, const char* fname, int lineno) { 58 if(abs(a-b) > abs(abs_error)) { 59 fname = basename(fname); 60 printf("fail, %s, line %d: ASSERT_NEAR(%f, %f, %f)\n", fname, lineno, a, b, abs_error); 61 exit(-1); 62 } 63 } 64 65 int main(int argc, char* argv[]) { 66 for(testid = 0; testid < ntests; testid++) { 67 printf("%s ", tests[testid].name); 68 tests[testid].fn(); 69 printf("ok\n"); 70 } 71 printf("PASS\n"); 72 return 0; 73 }