testcjdroute.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <https://www.gnu.org/licenses/>.
  14. */
  15. #include "util/Assert.h"
  16. #include "util/events/Time.h"
  17. #include "util/events/EventBase.h"
  18. #include "util/CString.h"
  19. #include "memory/MallocAllocator.h"
  20. #include <stdio.h>
  21. #ifdef SUBNODE
  22. #define testcjdroute_SUBNODE 1
  23. #else
  24. #define testcjdroute_SUBNODE 0
  25. #endif
  26. <?js require("./test/testcjdroute.js").generate(
  27. file, builder, testcjdroute_SUBNODE, this.async()); ?>
  28. <?js return file.testcjdroute_prototypes; ?>
  29. typedef int (* Test)(int argc, char** argv);
  30. static struct {
  31. Test func;
  32. char* name;
  33. } TESTS[] = {
  34. <?js return file.testcjdroute_tests ?>
  35. };
  36. static uint64_t runTest(Test test,
  37. char* name,
  38. uint64_t startTime,
  39. int argc,
  40. char** argv,
  41. struct EventBase* base)
  42. {
  43. fprintf(stderr, "Running test %s", name);
  44. Assert_true(!test(argc, argv));
  45. uint64_t now = Time_hrtime();
  46. char* seventySpaces = " ";
  47. int count = CString_strlen(name);
  48. if (count > 69) { count = 69; }
  49. fprintf(stderr, "%s%d.%d ms\n",
  50. &seventySpaces[count],
  51. (int)((now - startTime)/1000000),
  52. (int)((now - startTime)/1000)%1000);
  53. return now;
  54. }
  55. static void usage(char* appName)
  56. {
  57. printf("%s <test> run one test\n", appName);
  58. printf("%s all run every test\n\n", appName);
  59. printf("Available Tests:\n");
  60. for (int i = 0; i < (int)(sizeof(TESTS)/sizeof(*TESTS)); i++) {
  61. printf("%s\n", TESTS[i].name);
  62. }
  63. }
  64. int main(int argc, char** argv)
  65. {
  66. struct Allocator* alloc = MallocAllocator_new(4096);
  67. struct EventBase* base = EventBase_new(alloc);
  68. uint64_t now = Time_hrtime();
  69. uint64_t startTime = now;
  70. if (argc < 2) {
  71. Assert_true(argc > 0);
  72. usage(argv[0]);
  73. return 100;
  74. }
  75. if (CString_strcmp("all", argv[1])) {
  76. for (int i = 0; i < (int)(sizeof(TESTS)/sizeof(*TESTS)); i++) {
  77. if (!CString_strcmp(TESTS[i].name, argv[1])) {
  78. TESTS[i].func(argc, argv);
  79. return 0;
  80. }
  81. }
  82. usage(argv[0]);
  83. return 100;
  84. }
  85. for (int i = 0; i < (int)(sizeof(TESTS)/sizeof(*TESTS)); i++) {
  86. now = runTest(TESTS[i].func, TESTS[i].name, now, argc, argv, base);
  87. }
  88. fprintf(stderr, "Total test time %d.%d ms\n",
  89. (int)((now - startTime)/1000000),
  90. (int)((now - startTime)/1000)%1000);
  91. Allocator_free(alloc);
  92. return 0;
  93. }