Test.c 4.0 KB

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