Test.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include <dirent.h>
  2. #include <errno.h>
  3. #include <stdbool.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "Compiler.h"
  8. #include "Script.h"
  9. #include "Tokenizer.h"
  10. static int doneTests = 0;
  11. static int allTests = 0;
  12. static char path[PATH_MAX] = {'\0'};
  13. static int pathIndex = 0;
  14. #define TEST_BUFFER_LENGTH (1024 * 2)
  15. static char testBuffer[TEST_BUFFER_LENGTH];
  16. static int testBufferIndex = 0;
  17. static void tsAddBufferIndex(int length) {
  18. testBufferIndex += length;
  19. if(testBufferIndex > TEST_BUFFER_LENGTH) {
  20. testBufferIndex = TEST_BUFFER_LENGTH;
  21. }
  22. }
  23. static bool tsPrinter(Object* o) {
  24. if(testBufferIndex >= TEST_BUFFER_LENGTH) {
  25. return true;
  26. }
  27. if(o->type == OT_INT) {
  28. int leftBytes = TEST_BUFFER_LENGTH - testBufferIndex;
  29. tsAddBufferIndex(snprintf(testBuffer + testBufferIndex, leftBytes, "%d\n", o->data.intValue));
  30. return false;
  31. }
  32. return true;
  33. }
  34. static void tsAppend(const char* s) {
  35. for(int i = 0; pathIndex < (PATH_MAX - 1) && s[i] != '\0'; i++) {
  36. path[pathIndex++] = s[i];
  37. }
  38. path[pathIndex] = '\0';
  39. }
  40. static int tsEnter(const char* s) {
  41. int marker = pathIndex;
  42. tsAppend("/");
  43. tsAppend(s);
  44. return marker;
  45. }
  46. static void tsReset(int marker) {
  47. path[marker] = '\0';
  48. pathIndex = marker;
  49. }
  50. static void tsCompareResults(FILE* file) {
  51. for(int i = 0; i < testBufferIndex; i++) {
  52. char a = fgetc(file);
  53. char b = testBuffer[i];
  54. if(a != b) {
  55. printf("error in '%s': expected %c, got:\n%s", path, a, testBuffer + i);
  56. return;
  57. }
  58. }
  59. if(fgetc(file) != EOF) {
  60. printf("error in '%s': no full read\n", path);
  61. return;
  62. }
  63. doneTests++;
  64. }
  65. static void tsCheckScript(Script* sc) {
  66. testBufferIndex = 0;
  67. sRun(sc);
  68. tsAppend(".out");
  69. FILE* file = fopen(path, "r");
  70. if(file == NULL) {
  71. printf("cannot open result file '%s'\n", path);
  72. return;
  73. }
  74. tsCompareResults(file);
  75. fclose(file);
  76. }
  77. static void tsCheckFile() {
  78. allTests++;
  79. if(tTokenize(path)) {
  80. puts(tGetError());
  81. return;
  82. }
  83. int codeLength;
  84. unsigned char* code = cCompile(&codeLength);
  85. if(code == NULL) {
  86. puts(cGetError());
  87. return;
  88. }
  89. Script* sc = sInit(code, codeLength);
  90. tsCheckScript(sc);
  91. sDelete(sc);
  92. }
  93. static void tsScanDirectory() {
  94. DIR* dir = opendir(path);
  95. if(dir == NULL) {
  96. printf("cannot open '%s': %s\n", path, strerror(errno));
  97. return;
  98. }
  99. while(true) {
  100. struct dirent* e = readdir(dir);
  101. if(e == NULL) {
  102. return;
  103. } else if(strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0) {
  104. continue;
  105. } else if(e->d_type == DT_DIR) {
  106. int marker = tsEnter(e->d_name);
  107. tsScanDirectory();
  108. tsReset(marker);
  109. } else if(e->d_type == DT_REG && strchr(e->d_name, '.') == NULL) {
  110. int marker = tsEnter(e->d_name);
  111. tsCheckFile();
  112. tsReset(marker);
  113. }
  114. }
  115. if(closedir(dir)) {
  116. printf("cannot close '%s': %s\n", path, strerror(errno));
  117. }
  118. }
  119. /*static String readLine(std::ifstream& f) {
  120. String s;
  121. while(true) {
  122. char c = f.get();
  123. if(!f.good() || c == '\n') {
  124. break;
  125. }
  126. s += c;
  127. }
  128. return s;
  129. }
  130. static bool testTokenizer(const String& input, const String& output) {
  131. tests++;
  132. std::ifstream oStream;
  133. oStream.open(output);
  134. if(!oStream.good()) {
  135. std::cout << "cannot open file '" << output << "'\n";
  136. return true;
  137. }
  138. TokenStream tokenStream;
  139. if(Tokenizer::tokenize(tokenStream, input)) {
  140. return true;
  141. }
  142. while(true) {
  143. String expected = readLine(oStream);
  144. if(expected.getLength() == 0) {
  145. break;
  146. } else if(!tokenStream.hasToken()) {
  147. std::cout << "error in '" << input << "\n'out of tokens\n";
  148. return false;
  149. }
  150. String buffer = tokenStream.nextTokenString();
  151. if(strchr(buffer, '\n') != nullptr) {
  152. expected += '\n';
  153. expected += readLine(oStream);
  154. }
  155. if(strcmp(buffer, expected) != 0) {
  156. std::cout << "error in '" << input << "\n'" << buffer << "' should be '" << expected << "'\n";
  157. return false;
  158. }
  159. }
  160. done++;
  161. return false;
  162. }*/
  163. void tsStart(const char* path) {
  164. sSetPrinter(tsPrinter);
  165. doneTests = 0;
  166. allTests = 0;
  167. tsAppend(path);
  168. tsScanDirectory();
  169. printf("%d / %d tests succeeded\n", doneTests, allTests);
  170. }