2
0

schedule_tests.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * schedule_tests.c
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2004 Steve Underwood
  9. *
  10. * All rights reserved.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2, as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. /*! \page schedule_tests_page Event scheduler tests
  26. \section schedule_tests_page_sec_1 What does it do?
  27. ???.
  28. \section schedule_tests_page_sec_2 How does it work?
  29. ???.
  30. */
  31. #if defined(HAVE_CONFIG_H)
  32. #include "config.h"
  33. #endif
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37. #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
  38. #include "spandsp.h"
  39. uint64_t when1;
  40. uint64_t when2;
  41. static void callback1(span_sched_state_t *s, void *user_data)
  42. {
  43. int id;
  44. uint64_t when;
  45. when = span_schedule_time(s);
  46. printf("1: Callback at %f %" PRId64 "\n", (float) when/1000000.0, when - when1);
  47. if ((when - when1))
  48. {
  49. printf("Callback occured at the wrong time.\n");
  50. exit(2);
  51. }
  52. id = span_schedule_event(s, 500000, callback1, NULL);
  53. when1 = when + 500000;
  54. when = span_schedule_next(s);
  55. printf("1: Event %d, earliest is %" PRId64 "\n", id, when);
  56. }
  57. static void callback2(span_sched_state_t *s, void *user_data)
  58. {
  59. int id;
  60. uint64_t when;
  61. when = span_schedule_time(s);
  62. printf("2: Callback at %f %" PRId64 "\n", (float) when/1000000.0, when - when2);
  63. id = span_schedule_event(s, 550000, callback2, NULL);
  64. if ((when - when2) != 10000)
  65. {
  66. printf("Callback occured at the wrong time.\n");
  67. exit(2);
  68. }
  69. when2 = when + 550000;
  70. when = span_schedule_next(s);
  71. printf("2: Event %d, earliest is %" PRId64 "\n", id, when);
  72. }
  73. int main(int argc, char *argv[])
  74. {
  75. int i;
  76. span_sched_state_t sched;
  77. uint64_t when;
  78. span_schedule_init(&sched);
  79. span_schedule_event(&sched, 500000, callback1, NULL);
  80. span_schedule_event(&sched, 550000, callback2, NULL);
  81. when1 = span_schedule_time(&sched) + 500000;
  82. when2 = span_schedule_time(&sched) + 550000;
  83. //span_schedule_del(&sched, id);
  84. for (i = 0; i < 100000000; i += 20000)
  85. span_schedule_update(&sched, 20000);
  86. when = span_schedule_time(&sched);
  87. if ((when1 - when) < 0 || (when1 - when) > 500000 || (when2 - when) < 0 || (when2 - when) > 550000)
  88. {
  89. printf("Callback failed to occur.\n");
  90. exit(2);
  91. }
  92. span_schedule_release(&sched);
  93. printf("Tests passed.\n");
  94. return 0;
  95. }
  96. /*- End of function --------------------------------------------------------*/
  97. /*- End of file ------------------------------------------------------------*/