Test.c 3.7 KB

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