PageRenderTime 21ms CodeModel.GetById 12ms app.highlight 7ms RepoModel.GetById 0ms app.codeStats 1ms

/npk/cli/tests/testutil.h

http://npk.googlecode.com/
C++ Header | 107 lines | 90 code | 15 blank | 2 comment | 13 complexity | f1379dfaa8fdadbd293b1129ad76d0cd MD5 | raw file
  1/* testutil.h from haje01's GX library */
  2#ifndef __TESTUTIL_H__
  3#define __TESTUTIL_H__
  4
  5#include <iostream>
  6#include <fcntl.h>
  7#include <stdio.h>
  8#if defined( WIN32 )
  9#include <io.h>
 10#include <sys/utime.h>
 11#pragma warning ( disable : 4819 )
 12#pragma warning ( disable : 4996 )
 13#else
 14#include <stdio.h>
 15#include <unistd.h>
 16#include <string.h>
 17#include <stdlib.h>
 18#endif
 19
 20#ifndef O_BINARY
 21#define O_BINARY 0
 22#endif
 23
 24#if defined( WIN32 )
 25#define CHECK(x) \
 26    {if(!(x)) { printf("Assertion Failed : %s, %d\n",__FILE__,__LINE__); \
 27    __debugbreak(); }}
 28#define CMD(x) \
 29    { \
 30        char buf[512]; \
 31        strcpy( buf, x ); \
 32        size_t l = strlen(buf); \
 33        for( size_t i = 0; i < l; ++i ) \
 34            if( buf[i] == '/' ) buf[i] = '\\'; \
 35        system(buf); \
 36    }
 37#define CP(x,y) \
 38    { \
 39        char buf[512]; \
 40        sprintf( buf, "copy %s %s %%Y", x, y ); \
 41        size_t l = strlen(buf); \
 42        for( size_t i = 0; i < l; ++i ) { \
 43            if( buf[i] == '/' ) buf[i] = '\\'; \
 44            if( buf[i] == '%' ) buf[i] = '/'; \
 45		} \
 46        system(buf); \
 47		_utime(y, NULL); \
 48    }
 49#else
 50#define CHECK(x) \
 51    {if(!(x)) { printf("Assertion Failed : %s, %d\n",__FILE__,__LINE__); \
 52        __asm__("int $0x03");}}
 53#define CMD(x) \
 54    system(x);
 55#define CP(x,y) \
 56    { \
 57        char buf[512]; \
 58        sprintf( buf, "cp %s %s", x, y ); \
 59        CMD(buf); \
 60    }
 61#endif
 62
 63#ifdef WIN32
 64#include <windows.h>
 65#else
 66#include <unistd.h>
 67#define Sleep(x) usleep((x)*1000)
 68#endif
 69
 70#define CHECK_CLOSE( M_A, M_B, M_EPSILON ) \
 71    CHECK( fabs( M_A - M_B ) < M_EPSILON );
 72#define CHECK_EQUAL( M_A, M_B ) \
 73    CHECK( M_A == M_B )
 74#define CHECK_EQUAL_STR( M_A, M_B ) \
 75    CHECK( strcmp((M_A), (M_B)) == 0 )
 76#define CHECK_EQUAL_STR_SIZE( M_A, M_B, S ) \
 77    CHECK( strncmp((M_A), (M_B), (S)) == 0 )
 78
 79static void CHECK_EQUAL_STR_WITH_FILE( const char* src, const char* filename )
 80{
 81    int     h;
 82    char*   buf;
 83    size_t  size;
 84    
 85    h = open( filename, O_RDONLY | O_BINARY );
 86    CHECK( h >= 0 );
 87
 88    size = lseek( h, 0, SEEK_END );
 89    CHECK( size != 0 );
 90
 91    lseek( h, 0, SEEK_SET );
 92
 93    buf = (char*)malloc( size+1 );
 94    CHECK( buf != NULL );
 95
 96    // put '\0' at last
 97    ((char*)buf)[size] = '\0';
 98
 99    CHECK_EQUAL( size, read( h, buf, size ) );
100
101    CHECK_EQUAL_STR_SIZE( buf, src, size );
102
103    free( buf );
104    close( h );
105}
106
107#endif // __TESTUTIL_H__