fork download
  1. #include <stdio.h>
  2.  
  3. // these macros rely on the fact that errorCode is defined in the the caller
  4.  
  5. #define FUNC_RETURN_ISNT(func, unexpected) \
  6.   ((errorCode = func) == unexpected) ? printf("error: call %s shouldn't have returned %d but did (from function %s at line %d of file %s)\n", #func, unexpected, __func__, __LINE__, __FILE__), 0 : 1
  7.  
  8. #define FUNC_RETURN_IS(func, expected) \
  9.   ((errorCode = func) != expected) ? printf("error: call %s returned %d instead of %d (from function %s at line %d of file %s)\n", #func, errorCode, expected, __func__, __LINE__, __FILE__), 0 : 1
  10.  
  11. #define ERROR_CHECK(func) \
  12.   (errorCode = func) ? printf("error: call %s returned %d (from function %s at line %d of file %s)\n", #func, errorCode, __func__, __LINE__, __FILE__), errorCode : 0
  13.  
  14. int func(int a, int b, int c)
  15. {
  16. return a^b^c;
  17. }
  18.  
  19. int func2(void)
  20. {
  21. return -1;
  22. }
  23.  
  24. int func3(void)
  25. {
  26. static int i = 3;
  27.  
  28. return i--;
  29. }
  30.  
  31. int main(void)
  32. {
  33. int a = 0, b = 0, c = 0;
  34. int errorCode;
  35.  
  36. int (*funcPoint)(void) = func2;
  37.  
  38. FUNC_RETURN_ISNT(func(1,1,1), 1);
  39.  
  40. FUNC_RETURN_IS(func(a,b,c), 1);
  41.  
  42. ERROR_CHECK(func(a,b,1));
  43.  
  44. if(ERROR_CHECK(func2()))
  45. {
  46. printf("func2 failed error check\n");
  47. }
  48. else
  49. {
  50. printf("func2 passed error check\n");
  51. }
  52.  
  53. if(ERROR_CHECK(funcPoint()))
  54. {
  55. printf("funcPoint failed error check\n");
  56. }
  57. else
  58. {
  59. printf("funcPoint passed error check\n");
  60. }
  61.  
  62. if(ERROR_CHECK(func(0,0,0)))
  63. {
  64. printf("func failed error check\n");
  65. }
  66. else
  67. {
  68. printf("func passed error check\n");
  69. }
  70.  
  71. while(ERROR_CHECK(func3()))
  72. {
  73. printf("retry...\n");
  74. }
  75.  
  76. switch(ERROR_CHECK(func(1,2,4)))
  77. {
  78. case 0:
  79. printf("okay\n");
  80. break;
  81. case 1:
  82. printf("non-fatal error 1\n");
  83. break;
  84. case 7:
  85. printf("fatal error 7\n");
  86. return 1;
  87. }
  88.  
  89. return 0;
  90. }
Runtime error #stdin #stdout 0s 1832KB
stdin
Standard input is empty
stdout
error: call func(1,1,1) shouldn't have returned 1 but did (from function main at line 38 of file prog.c)
error: call func(a,b,c) returned 0 instead of 1 (from function main at line 40 of file prog.c)
error: call func(a,b,1) returned 1 (from function main at line 42 of file prog.c)
error: call func2() returned -1 (from function main at line 44 of file prog.c)
func2 failed error check
error: call funcPoint() returned -1 (from function main at line 53 of file prog.c)
funcPoint failed error check
func passed error check
error: call func3() returned 3 (from function main at line 71 of file prog.c)
retry...
error: call func3() returned 2 (from function main at line 71 of file prog.c)
retry...
error: call func3() returned 1 (from function main at line 71 of file prog.c)
retry...
error: call func(1,2,4) returned 7 (from function main at line 76 of file prog.c)
fatal error 7