1
0

testcjdroute.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #define string_strlen
  16. #define string_strcmp
  17. #include "util/Assert.h"
  18. #include "util/platform/libc/string.h"
  19. #include "util/events/Time.h"
  20. #include "util/events/EventBase.h"
  21. #include "memory/MallocAllocator.h"
  22. #include <stdio.h>
  23. <?js
  24. var done = this.async();
  25. var Tests = require("./Tests");
  26. Tests.get(function (tests) {
  27. var prototypes = [];
  28. var listContent = [];
  29. tests.forEach(function (test) {
  30. var main = /^.*\/([^\/]+)\.c$/.exec(test)[1] + '_main';
  31. (builder.config['cflags'+test] =
  32. builder.config['cflags'+test] || []).push('-D', 'main='+main);
  33. file.links.push(test);
  34. listContent.push('{ .func = '+main+', .name = "'+test.replace(/^.*\/|.c$/g, '')+'" },');
  35. prototypes.push('int '+main+'(int argc, char** argv);');
  36. });
  37. file.testcjdroute_tests = listContent.join('\n');
  38. file.testcjdroute_prototypes = prototypes.join('\n');
  39. done();
  40. });
  41. ?>
  42. <?js return file.testcjdroute_prototypes; ?>
  43. typedef int (* Test)(int argc, char** argv);
  44. static struct {
  45. Test func;
  46. char* name;
  47. } TESTS[] = {
  48. <?js return file.testcjdroute_tests ?>
  49. };
  50. static uint64_t runTest(Test test,
  51. char* name,
  52. uint64_t startTime,
  53. int argc,
  54. char** argv,
  55. struct EventBase* base)
  56. {
  57. fprintf(stderr, "Running test %s", name);
  58. Assert_always(!test(argc, argv));
  59. uint64_t now = Time_hrtime();
  60. char* seventySpaces = " ";
  61. int count = strlen(name);
  62. if (count > 69) { count = 69; }
  63. fprintf(stderr, "%s%d.%d ms\n",
  64. &seventySpaces[count],
  65. (int)((now - startTime)/1000000),
  66. (int)((now - startTime)/1000)%1000);
  67. return now;
  68. }
  69. static void usage(char* appName)
  70. {
  71. printf("%s <test> run one test\n", appName);
  72. printf("%s all run every test\n\n", appName);
  73. printf("Available Tests:\n");
  74. for (int i = 0; i < (int)(sizeof(TESTS)/sizeof(*TESTS)); i++) {
  75. printf("%s\n", TESTS[i].name);
  76. }
  77. }
  78. int main(int argc, char** argv)
  79. {
  80. struct Allocator* alloc = MallocAllocator_new(4096);
  81. struct EventBase* base = EventBase_new(alloc);
  82. uint64_t now = Time_hrtime();
  83. uint64_t startTime = now;
  84. if (argc < 2) {
  85. Assert_always(argc > 0);
  86. usage(argv[0]);
  87. return 100;
  88. }
  89. if (strcmp("all", argv[1])) {
  90. for (int i = 0; i < (int)(sizeof(TESTS)/sizeof(*TESTS)); i++) {
  91. if (!strcmp(TESTS[i].name, argv[1])) {
  92. TESTS[i].func(argc, argv);
  93. return 0;
  94. }
  95. }
  96. usage(argv[0]);
  97. return 100;
  98. }
  99. for (int i = 0; i < (int)(sizeof(TESTS)/sizeof(*TESTS)); i++) {
  100. now = runTest(TESTS[i].func, TESTS[i].name, now, argc, argv, base);
  101. }
  102. fprintf(stderr, "Total test time %d.%d ms\n",
  103. (int)((now - startTime)/1000000),
  104. (int)((now - startTime)/1000)%1000);
  105. Allocator_free(alloc);
  106. return 0;
  107. }