Test.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. if(cGetError() == NULL) {
  87. printf("'%s' has error but none was set\n", path);
  88. } else {
  89. puts(cGetError());
  90. }
  91. return;
  92. }
  93. Script* sc = sInit(code, codeLength);
  94. tsCheckScript(sc);
  95. sDelete(sc);
  96. }
  97. static void tsScanDirectory() {
  98. DIR* dir = opendir(path);
  99. if(dir == NULL) {
  100. printf("cannot open '%s': %s\n", path, strerror(errno));
  101. return;
  102. }
  103. while(true) {
  104. struct dirent* e = readdir(dir);
  105. if(e == NULL) {
  106. return;
  107. } else if(strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0) {
  108. continue;
  109. } else if(e->d_type == DT_DIR) {
  110. int marker = tsEnter(e->d_name);
  111. tsScanDirectory();
  112. tsReset(marker);
  113. } else if(e->d_type == DT_REG && strchr(e->d_name, '.') == NULL) {
  114. int marker = tsEnter(e->d_name);
  115. tsCheckFile();
  116. tsReset(marker);
  117. }
  118. }
  119. if(closedir(dir)) {
  120. printf("cannot close '%s': %s\n", path, strerror(errno));
  121. }
  122. }
  123. /*static String readLine(std::ifstream& f) {
  124. String s;
  125. while(true) {
  126. char c = f.get();
  127. if(!f.good() || c == '\n') {
  128. break;
  129. }
  130. s += c;
  131. }
  132. return s;
  133. }
  134. static bool testTokenizer(const String& input, const String& output) {
  135. tests++;
  136. std::ifstream oStream;
  137. oStream.open(output);
  138. if(!oStream.good()) {
  139. std::cout << "cannot open file '" << output << "'\n";
  140. return true;
  141. }
  142. TokenStream tokenStream;
  143. if(Tokenizer::tokenize(tokenStream, input)) {
  144. return true;
  145. }
  146. while(true) {
  147. String expected = readLine(oStream);
  148. if(expected.getLength() == 0) {
  149. break;
  150. } else if(!tokenStream.hasToken()) {
  151. std::cout << "error in '" << input << "\n'out of tokens\n";
  152. return false;
  153. }
  154. String buffer = tokenStream.nextTokenString();
  155. if(strchr(buffer, '\n') != nullptr) {
  156. expected += '\n';
  157. expected += readLine(oStream);
  158. }
  159. if(strcmp(buffer, expected) != 0) {
  160. std::cout << "error in '" << input << "\n'" << buffer << "' should be '" << expected << "'\n";
  161. return false;
  162. }
  163. }
  164. done++;
  165. return false;
  166. }*/
  167. void tsStart(const char* path) {
  168. sSetPrinter(tsPrinter);
  169. doneTests = 0;
  170. allTests = 0;
  171. tsAppend(path);
  172. tsScanDirectory();
  173. printf("%d / %d tests succeeded\n", doneTests, allTests);
  174. }