Test.c 4.1 KB

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