Test.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 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. printf("line: %d\n", tGetLine());
  111. return;
  112. }
  113. ByteCode* bc = cCompile();
  114. if(bc == NULL) {
  115. puts(path);
  116. puts(cGetError());
  117. printf("line: %d\n", cGetLine());
  118. return;
  119. }
  120. Script* sc = sInit(bc);
  121. tsCheckScript(sc);
  122. sDelete(sc);
  123. }
  124. static void tsScanDirectory() {
  125. DIR* dir = opendir(path);
  126. if(dir == NULL) {
  127. printf("cannot open '%s': %s\n", path, strerror(errno));
  128. return;
  129. }
  130. while(true) {
  131. struct dirent* e = readdir(dir);
  132. if(e == NULL) {
  133. return;
  134. } else if(strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0) {
  135. continue;
  136. } else if(e->d_type == DT_DIR) {
  137. int marker = tsEnter(e->d_name);
  138. tsScanDirectory();
  139. tsReset(marker);
  140. } else if(e->d_type == DT_REG && strchr(e->d_name, '.') == NULL) {
  141. int marker = tsEnter(e->d_name);
  142. tsCheckFile();
  143. tsReset(marker);
  144. }
  145. }
  146. if(closedir(dir)) {
  147. printf("cannot close '%s': %s\n", path, strerror(errno));
  148. }
  149. }
  150. static void tsAddPrinter(Structs* sts, DataType in, ScriptFunction sf) {
  151. Function f;
  152. gfInit(&f, "test", dtVoid(), sf);
  153. fAddArgument(&f, in, sts);
  154. gfsAdd(&f);
  155. }
  156. void tsStart(const char* path) {
  157. Structs sts;
  158. stsInit(&sts);
  159. tsAddPrinter(&sts, dtInt32(), tsInt32Printer);
  160. tsAddPrinter(&sts, dtInt64(), tsInt64Printer);
  161. tsAddPrinter(&sts, dtFloat(), tsFloatPrinter);
  162. tsAddPrinter(&sts, dtBool(), tsBoolPrinter);
  163. doneTests = 0;
  164. allTests = 0;
  165. tsAppend(path);
  166. tsScanDirectory();
  167. printf("%d / %d tests succeeded\n", doneTests, allTests);
  168. }