Test.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "Script.h"
  10. #include "tokenizer/Tokenizer.h"
  11. static int doneTests = 0;
  12. static int allTests = 0;
  13. static char path[PATH_MAX] = {'\0'};
  14. static int pathIndex = 0;
  15. #define TEST_BUFFER_LENGTH (1024 * 2)
  16. static char testBuffer[TEST_BUFFER_LENGTH];
  17. static int testBufferIndex = 0;
  18. static void tsPrintToBuffer(const char* format, ...) {
  19. va_list args;
  20. va_start(args, format);
  21. int leftBytes = TEST_BUFFER_LENGTH - testBufferIndex;
  22. testBufferIndex +=
  23. vsnprintf(testBuffer + testBufferIndex, leftBytes, format, args);
  24. if(testBufferIndex > TEST_BUFFER_LENGTH) {
  25. testBufferIndex = TEST_BUFFER_LENGTH;
  26. }
  27. va_end(args);
  28. }
  29. static bool tsPrinter(Object* o) {
  30. if(testBufferIndex >= TEST_BUFFER_LENGTH) {
  31. return true;
  32. }
  33. switch(o->type) {
  34. case OT_INT: tsPrintToBuffer("%d\n", o->as.intValue); return false;
  35. case OT_FLOAT:
  36. tsPrintToBuffer("%.2f\n", o->as.floatValue);
  37. return false;
  38. case OT_CONST_STRING:
  39. tsPrintToBuffer("%s\n", o->as.stringValue);
  40. return false;
  41. case OT_NULL: tsPrintToBuffer("null\n"); return false;
  42. case OT_BOOL:
  43. tsPrintToBuffer(o->as.intValue ? "true\n" : "false\n");
  44. return false;
  45. case OT_ARRAY: tsPrintToBuffer("array\n"); return false;
  46. default: return true;
  47. }
  48. }
  49. static void tsAppend(const char* s) {
  50. for(int i = 0; pathIndex < (PATH_MAX - 1) && s[i] != '\0'; i++) {
  51. path[pathIndex++] = s[i];
  52. }
  53. path[pathIndex] = '\0';
  54. }
  55. static int tsEnter(const char* s) {
  56. int marker = pathIndex;
  57. tsAppend("/");
  58. tsAppend(s);
  59. return marker;
  60. }
  61. static void tsReset(int marker) {
  62. path[marker] = '\0';
  63. pathIndex = marker;
  64. }
  65. static bool tsCompareResults(FILE* file) {
  66. for(int i = 0; i < testBufferIndex; i++) {
  67. char a = fgetc(file);
  68. char b = testBuffer[i];
  69. if(a != b) {
  70. printf("error in '%s': expected %c, got:\n%s", path, a,
  71. testBuffer + i);
  72. return true;
  73. }
  74. }
  75. if(fgetc(file) != EOF) {
  76. printf("error in '%s': no full read\n", path);
  77. return true;
  78. }
  79. doneTests++;
  80. return false;
  81. }
  82. static void tsCheckScript(Script* sc) {
  83. testBufferIndex = 0;
  84. sRun(sc);
  85. tsAppend(".out");
  86. FILE* file = fopen(path, "r");
  87. if(file == NULL) {
  88. printf("cannot open result file '%s'\n", path);
  89. return;
  90. }
  91. if(tsCompareResults(file)) {
  92. sPrintCode(sc);
  93. } else if(sc->stackIndex != 0) {
  94. printf("%s has non empty stack: %d\n", path, sc->stackIndex);
  95. sPrintCode(sc);
  96. }
  97. fclose(file);
  98. }
  99. static void tsCheckFile() {
  100. allTests++;
  101. if(tTokenize(path)) {
  102. puts(path);
  103. puts(tGetError());
  104. return;
  105. }
  106. ByteCode* bc = cCompile();
  107. if(bc == NULL) {
  108. puts(path);
  109. puts(cGetError());
  110. return;
  111. }
  112. Script* sc = sInit(bc);
  113. tsCheckScript(sc);
  114. sDelete(sc);
  115. }
  116. static void tsScanDirectory() {
  117. DIR* dir = opendir(path);
  118. if(dir == NULL) {
  119. printf("cannot open '%s': %s\n", path, strerror(errno));
  120. return;
  121. }
  122. while(true) {
  123. struct dirent* e = readdir(dir);
  124. if(e == NULL) {
  125. return;
  126. } else if(strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0) {
  127. continue;
  128. } else if(e->d_type == DT_DIR) {
  129. int marker = tsEnter(e->d_name);
  130. tsScanDirectory();
  131. tsReset(marker);
  132. } else if(e->d_type == DT_REG && strchr(e->d_name, '.') == NULL) {
  133. int marker = tsEnter(e->d_name);
  134. tsCheckFile();
  135. tsReset(marker);
  136. }
  137. }
  138. if(closedir(dir)) {
  139. printf("cannot close '%s': %s\n", path, strerror(errno));
  140. }
  141. }
  142. void tsStart(const char* path) {
  143. sSetPrinter(tsPrinter);
  144. doneTests = 0;
  145. allTests = 0;
  146. tsAppend(path);
  147. tsScanDirectory();
  148. printf("%d / %d tests succeeded\n", doneTests, allTests);
  149. }