1
0

testcjdroute.c 3.0 KB

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