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