Test.c 4.5 KB

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