Test.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 tsInt32Printer(Script* sc) {
  32. int32 i;
  33. if(sPopInt32(sc, &i)) {
  34. tsPrintToBuffer("%d\n", i);
  35. }
  36. }
  37. static void tsInt64Printer(Script* sc) {
  38. int64 i;
  39. if(sPopInt64(sc, &i)) {
  40. tsPrintToBuffer("%ld\n", i);
  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 tsTextPrinter(Script* sc) {
  56. Pointer p;
  57. if(sPopPointer(sc, &p)) {
  58. int32 length;
  59. if(sGetPointerLength(sc, &p, &length)) {
  60. return;
  61. }
  62. for(int i = 0; i < length; i++) {
  63. int32* ip = sCheckAddress(sc, &p, sizeof(int32));
  64. tsPrintToBuffer("%c", (char)*ip);
  65. p.offset += sizeof(int32);
  66. }
  67. tsPrintToBuffer("\n");
  68. }
  69. }
  70. static void tsAppend(const char* s) {
  71. for(int i = 0; pathIndex < (PATH_MAX - 1) && s[i] != '\0'; i++) {
  72. path[pathIndex++] = s[i];
  73. }
  74. path[pathIndex] = '\0';
  75. }
  76. static int tsEnter(const char* s) {
  77. int marker = pathIndex;
  78. tsAppend("/");
  79. tsAppend(s);
  80. return marker;
  81. }
  82. static void tsReset(int marker) {
  83. path[marker] = '\0';
  84. pathIndex = marker;
  85. }
  86. static bool tsCompareResults(FILE* file) {
  87. for(int i = 0; i < testBufferIndex; i++) {
  88. char a = fgetc(file);
  89. char b = testBuffer[i];
  90. if(a != b) {
  91. printf("error in '%s': expected %c, got:\n%s", path, a,
  92. testBuffer + i);
  93. return true;
  94. }
  95. }
  96. if(fgetc(file) != EOF) {
  97. printf("error in '%s': no full read\n", path);
  98. return true;
  99. }
  100. doneTests++;
  101. return false;
  102. }
  103. static void tsCheckScript(Script* sc) {
  104. testBufferIndex = 0;
  105. sRun(sc);
  106. tsAppend(".out");
  107. FILE* file = fopen(path, "r");
  108. if(file == NULL) {
  109. printf("cannot open result file '%s'\n", path);
  110. return;
  111. }
  112. if(tsCompareResults(file)) {
  113. btPrint(sc->code);
  114. } else if(sc->stackIndex != 0) {
  115. printf("%s has non empty stack: %d\n", path, sc->stackIndex);
  116. btPrint(sc->code);
  117. }
  118. fclose(file);
  119. }
  120. static void tsCheckFile() {
  121. allTests++;
  122. if(tTokenize(path)) {
  123. puts(path);
  124. puts(tGetError());
  125. printf("line: %d\n", tGetLine());
  126. return;
  127. }
  128. ByteCode* bc = cCompile();
  129. if(bc == NULL) {
  130. puts(path);
  131. puts(cGetError());
  132. printf("line: %d\n", cGetLine());
  133. return;
  134. }
  135. Script* sc = sInit(bc);
  136. tsCheckScript(sc);
  137. sDelete(sc);
  138. }
  139. static void tsScanDirectory() {
  140. DIR* dir = opendir(path);
  141. if(dir == NULL) {
  142. printf("cannot open '%s': %s\n", path, strerror(errno));
  143. return;
  144. }
  145. while(true) {
  146. struct dirent* e = readdir(dir);
  147. if(e == NULL) {
  148. return;
  149. } else if(strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0) {
  150. continue;
  151. } else if(e->d_type == DT_DIR) {
  152. int marker = tsEnter(e->d_name);
  153. tsScanDirectory();
  154. tsReset(marker);
  155. } else if(e->d_type == DT_REG && strchr(e->d_name, '.') == NULL) {
  156. int marker = tsEnter(e->d_name);
  157. tsCheckFile();
  158. tsReset(marker);
  159. }
  160. }
  161. if(closedir(dir)) {
  162. printf("cannot close '%s': %s\n", path, strerror(errno));
  163. }
  164. }
  165. static void tsAddPrinter(DataType in, ScriptFunction sf) {
  166. Function f;
  167. gfInit(&f, "test", dtVoid(), sf);
  168. gfAddArgument(&f, in);
  169. gfsAdd(&f);
  170. }
  171. void tsStart(const char* path) {
  172. tsAddPrinter(dtConst(dtInt32()), tsInt32Printer);
  173. tsAddPrinter(dtConst(dtInt64()), tsInt64Printer);
  174. tsAddPrinter(dtConst(dtFloat()), tsFloatPrinter);
  175. tsAddPrinter(dtConst(dtBool()), tsBoolPrinter);
  176. tsAddPrinter(dtConst(dtText()), tsTextPrinter);
  177. doneTests = 0;
  178. allTests = 0;
  179. tsAppend(path);
  180. tsScanDirectory();
  181. printf("%d / %d tests succeeded\n", doneTests, allTests);
  182. }