Test.c 4.8 KB

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