Test.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 void tsIntPrinter(int i) {
  31. tsPrintToBuffer("%d\n", i);
  32. }
  33. static void tsFloatPrinter(float f) {
  34. tsPrintToBuffer("%.2f\n", f);
  35. }
  36. static void tsBoolPrinter(bool b) {
  37. tsPrintToBuffer(b ? "true\n" : "false\n");
  38. }
  39. static void tsPointerPrinter(Pointer* p) {
  40. tsPrintToBuffer("(%d, %d)\n", p->array, p->offset);
  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,
  64. testBuffer + i);
  65. return true;
  66. }
  67. }
  68. if(fgetc(file) != EOF) {
  69. printf("error in '%s': no full read\n", path);
  70. return true;
  71. }
  72. doneTests++;
  73. return false;
  74. }
  75. static void tsCheckScript(Script* sc) {
  76. testBufferIndex = 0;
  77. sRun(sc);
  78. tsAppend(".out");
  79. FILE* file = fopen(path, "r");
  80. if(file == NULL) {
  81. printf("cannot open result file '%s'\n", path);
  82. return;
  83. }
  84. if(tsCompareResults(file)) {
  85. btPrint(sc->code);
  86. } else if(sc->stackIndex != 0) {
  87. printf("%s has non empty stack: %d\n", path, sc->stackIndex);
  88. btPrint(sc->code);
  89. }
  90. fclose(file);
  91. }
  92. static void tsCheckFile() {
  93. allTests++;
  94. if(tTokenize(path)) {
  95. puts(path);
  96. puts(tGetError());
  97. return;
  98. }
  99. ByteCode* bc = cCompile();
  100. if(bc == NULL) {
  101. puts(path);
  102. puts(cGetError());
  103. printf("line: %d\n", cGetLine());
  104. return;
  105. }
  106. Script* sc = sInit(bc);
  107. tsCheckScript(sc);
  108. sDelete(sc);
  109. }
  110. static void tsScanDirectory() {
  111. DIR* dir = opendir(path);
  112. if(dir == NULL) {
  113. printf("cannot open '%s': %s\n", path, strerror(errno));
  114. return;
  115. }
  116. while(true) {
  117. struct dirent* e = readdir(dir);
  118. if(e == NULL) {
  119. return;
  120. } else if(strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0) {
  121. continue;
  122. } else if(e->d_type == DT_DIR) {
  123. int marker = tsEnter(e->d_name);
  124. tsScanDirectory();
  125. tsReset(marker);
  126. } else if(e->d_type == DT_REG && strchr(e->d_name, '.') == NULL) {
  127. int marker = tsEnter(e->d_name);
  128. tsCheckFile();
  129. tsReset(marker);
  130. }
  131. }
  132. if(closedir(dir)) {
  133. printf("cannot close '%s': %s\n", path, strerror(errno));
  134. }
  135. }
  136. void tsStart(const char* path) {
  137. sSetIntPrinter(tsIntPrinter);
  138. sSetFloatPrinter(tsFloatPrinter);
  139. sSetBoolPrinter(tsBoolPrinter);
  140. sSetPointerPrinter(tsPointerPrinter);
  141. doneTests = 0;
  142. allTests = 0;
  143. tsAppend(path);
  144. tsScanDirectory();
  145. printf("%d / %d tests succeeded\n", doneTests, allTests);
  146. }