Test.c 4.7 KB

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