Test.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include <dirent.h>
  2. #include <errno.h>
  3. #include <stdarg.h>
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "Compiler.h"
  9. #include "Script.h"
  10. #include "Tokenizer.h"
  11. static int doneTests = 0;
  12. static int allTests = 0;
  13. static char path[PATH_MAX] = {'\0'};
  14. static int pathIndex = 0;
  15. #define TEST_BUFFER_LENGTH (1024 * 2)
  16. static char testBuffer[TEST_BUFFER_LENGTH];
  17. static int testBufferIndex = 0;
  18. static void tsPrintToBuffer(const char* format, ...) {
  19. va_list args;
  20. va_start(args, format);
  21. int leftBytes = TEST_BUFFER_LENGTH - testBufferIndex;
  22. testBufferIndex += vsnprintf(testBuffer + testBufferIndex, leftBytes, format, args);
  23. if(testBufferIndex > TEST_BUFFER_LENGTH) {
  24. testBufferIndex = TEST_BUFFER_LENGTH;
  25. }
  26. va_end(args);
  27. }
  28. static bool tsPrinter(Object* o) {
  29. if(testBufferIndex >= TEST_BUFFER_LENGTH) {
  30. return true;
  31. }
  32. switch(o->type) {
  33. case OT_INT: tsPrintToBuffer("%d\n", o->data.intValue); return false;
  34. case OT_FLOAT: tsPrintToBuffer("%.2f\n", o->data.floatValue); return false;
  35. case OT_CONST_STRING: tsPrintToBuffer("%s\n", o->data.stringValue); return false;
  36. case OT_NULL: tsPrintToBuffer("null\n"); return false;
  37. case OT_BOOL: tsPrintToBuffer(o->data.intValue ? "true\n" : "false\n"); return false;
  38. case OT_ARRAY: tsPrintToBuffer("array\n"); return false;
  39. default: return true;
  40. }
  41. }
  42. static void tsAppend(const char* s) {
  43. for(int i = 0; pathIndex < (PATH_MAX - 1) && s[i] != '\0'; i++) {
  44. path[pathIndex++] = s[i];
  45. }
  46. path[pathIndex] = '\0';
  47. }
  48. static int tsEnter(const char* s) {
  49. int marker = pathIndex;
  50. tsAppend("/");
  51. tsAppend(s);
  52. return marker;
  53. }
  54. static void tsReset(int marker) {
  55. path[marker] = '\0';
  56. pathIndex = marker;
  57. }
  58. static bool tsCompareResults(FILE* file) {
  59. for(int i = 0; i < testBufferIndex; i++) {
  60. char a = fgetc(file);
  61. char b = testBuffer[i];
  62. if(a != b) {
  63. printf("error in '%s': expected %c, got:\n%s", path, a, testBuffer + i);
  64. return true;
  65. }
  66. }
  67. if(fgetc(file) != EOF) {
  68. printf("error in '%s': no full read\n", path);
  69. return true;
  70. }
  71. doneTests++;
  72. return false;
  73. }
  74. static void tsCheckScript(Script* sc) {
  75. testBufferIndex = 0;
  76. sRun(sc);
  77. tsAppend(".out");
  78. FILE* file = fopen(path, "r");
  79. if(file == NULL) {
  80. printf("cannot open result file '%s'\n", path);
  81. return;
  82. }
  83. if(tsCompareResults(file)) {
  84. sPrintCode(sc);
  85. } else if(sc->stackIndex != 0) {
  86. printf("%s has non empty stack: %d\n", path, sc->stackIndex);
  87. sPrintCode(sc);
  88. }
  89. fclose(file);
  90. }
  91. static void tsCheckFile() {
  92. allTests++;
  93. if(tTokenize(path)) {
  94. puts(path);
  95. puts(tGetError());
  96. return;
  97. }
  98. ByteCode* bc = cCompile();
  99. if(bc == NULL) {
  100. puts(path);
  101. puts(cGetError());
  102. return;
  103. }
  104. Script* sc = sInit(bc);
  105. tsCheckScript(sc);
  106. sDelete(sc);
  107. }
  108. static void tsScanDirectory() {
  109. DIR* dir = opendir(path);
  110. if(dir == NULL) {
  111. printf("cannot open '%s': %s\n", path, strerror(errno));
  112. return;
  113. }
  114. while(true) {
  115. struct dirent* e = readdir(dir);
  116. if(e == NULL) {
  117. return;
  118. } else if(strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0) {
  119. continue;
  120. } else if(e->d_type == DT_DIR) {
  121. int marker = tsEnter(e->d_name);
  122. tsScanDirectory();
  123. tsReset(marker);
  124. } else if(e->d_type == DT_REG && strchr(e->d_name, '.') == NULL) {
  125. int marker = tsEnter(e->d_name);
  126. tsCheckFile();
  127. tsReset(marker);
  128. }
  129. }
  130. if(closedir(dir)) {
  131. printf("cannot close '%s': %s\n", path, strerror(errno));
  132. }
  133. }
  134. void tsStart(const char* path) {
  135. sSetPrinter(tsPrinter);
  136. doneTests = 0;
  137. allTests = 0;
  138. tsAppend(path);
  139. tsScanDirectory();
  140. printf("%d / %d tests succeeded\n", doneTests, allTests);
  141. }