Test.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. return;
  35. }
  36. tsPrintToBuffer("%d\n", i);
  37. }
  38. static void tsFloatPrinter(Script* sc) {
  39. float f;
  40. if(sPopFloat(sc, &f)) {
  41. return;
  42. }
  43. tsPrintToBuffer("%.2f\n", f);
  44. }
  45. static void tsTextPrinter(Script* sc) {
  46. SnuviArray* array = sGetArray(sc);
  47. if(array == NULL) {
  48. return;
  49. }
  50. for(int i = 0; i < array->realLength; i++) {
  51. tsPrintToBuffer("%c", (char)array->data[i].data.intValue);
  52. }
  53. tsPrintToBuffer("\n");
  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. if(sc->error[0] != '\0') {
  92. printf("script '%s' had error on line %d: %s\n", path, sc->line,
  93. sc->error);
  94. btPrint(sc->code);
  95. return;
  96. }
  97. tsAppend(".out");
  98. FILE* file = fopen(path, "r");
  99. if(file == NULL) {
  100. printf("cannot open result file '%s'\n", path);
  101. return;
  102. }
  103. if(tsCompareResults(file)) {
  104. btPrint(sc->code);
  105. } else if(sc->stackIndex != 0) {
  106. printf("%s has non empty stack: %d\n", path, sc->stackIndex);
  107. btPrint(sc->code);
  108. }
  109. fclose(file);
  110. }
  111. static void tsCheckFile() {
  112. allTests++;
  113. Error e;
  114. tTokenize(path, &e);
  115. if(eHasError(&e)) {
  116. puts(e.message);
  117. printf("line: %d\n", e.line);
  118. printf("path: %s\n", e.paths);
  119. return;
  120. }
  121. ByteCode* bc = cCompile(&e);
  122. if(bc == NULL) {
  123. puts(e.message);
  124. printf("line: %d\n", e.line);
  125. printf("path: %s\n", e.paths);
  126. return;
  127. }
  128. Script* sc = sInit(bc);
  129. tsCheckScript(sc);
  130. sDelete(sc);
  131. }
  132. static void tsScanDirectory() {
  133. DIR* dir = opendir(path);
  134. if(dir == NULL) {
  135. printf("cannot open '%s': %s\n", path, strerror(errno));
  136. return;
  137. }
  138. while(true) {
  139. struct dirent* e = readdir(dir);
  140. if(e == NULL) {
  141. return;
  142. } else if(strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0) {
  143. continue;
  144. } else if(e->d_type == DT_DIR) {
  145. int marker = tsEnter(e->d_name);
  146. tsScanDirectory();
  147. tsReset(marker);
  148. } else if(e->d_type == DT_REG && strchr(e->d_name, '.') == NULL) {
  149. int marker = tsEnter(e->d_name);
  150. tsCheckFile();
  151. tsReset(marker);
  152. }
  153. }
  154. if(closedir(dir)) {
  155. printf("cannot close '%s': %s\n", path, strerror(errno));
  156. }
  157. }
  158. static void tsAddPrinter(DataType in, ScriptFunction sf) {
  159. Function f;
  160. gfInit(&f, "test", dtVoid(), sf);
  161. gfAddArgument(&f, in);
  162. gfsAdd(&f);
  163. }
  164. void tsStart(const char* path) {
  165. tsAddPrinter(dtInt(), tsInt32Printer);
  166. tsAddPrinter(dtFloat(), tsFloatPrinter);
  167. tsAddPrinter(dtText(), tsTextPrinter);
  168. doneTests = 0;
  169. allTests = 0;
  170. tsAppend(path);
  171. tsScanDirectory();
  172. printf("%d / %d tests succeeded\n", doneTests, allTests);
  173. }