Test.c 3.9 KB

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