Test.c 3.5 KB

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