Test.c 4.4 KB

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