Test.c 3.6 KB

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