test_npl_eventq.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. Unit tests for the ble_npl_eventq api:
  21. void ble_npl_eventq_init(struct ble_npl_eventq *);
  22. void ble_npl_eventq_put(struct ble_npl_eventq *, struct ble_npl_event *);
  23. struct ble_npl_event *ble_npl_eventq_get_no_wait(struct ble_npl_eventq *evq);
  24. struct ble_npl_event *ble_npl_eventq_get(struct ble_npl_eventq *);
  25. void ble_npl_eventq_run(struct ble_npl_eventq *evq);
  26. struct ble_npl_event *ble_npl_eventq_poll(struct ble_npl_eventq **, int, ble_npl_time_t);
  27. void ble_npl_eventq_remove(struct ble_npl_eventq *, struct ble_npl_event *);
  28. struct ble_npl_eventq *ble_npl_eventq_dflt_get(void);
  29. */
  30. #include <assert.h>
  31. #include <pthread.h>
  32. #include "test_util.h"
  33. #include "nimble/nimble_npl.h"
  34. #define TEST_ARGS_VALUE (55)
  35. #define TEST_STACK_SIZE (1024)
  36. static bool s_tests_running = true;
  37. static struct ble_npl_task s_task_runner;
  38. static struct ble_npl_task s_task_dispatcher;
  39. static struct ble_npl_eventq s_eventq;
  40. static struct ble_npl_event s_event;
  41. static int s_event_args = TEST_ARGS_VALUE;
  42. void on_event(struct ble_npl_event *ev)
  43. {
  44. VerifyOrQuit(ev->ev_arg == &s_event_args,
  45. "callout: wrong args passed");
  46. VerifyOrQuit(*(int*)ev->ev_arg == TEST_ARGS_VALUE,
  47. "callout: args corrupted");
  48. s_tests_running = false;
  49. }
  50. int test_init(void)
  51. {
  52. //VerifyOrQuit(!ble_npl_eventq_inited(&s_eventq), "eventq: empty q initialized");
  53. ble_npl_eventq_init(&s_eventq);
  54. //VerifyOrQuit(ble_npl_eventq_inited(&s_eventq), "eventq: not initialized");
  55. return PASS;
  56. }
  57. int test_run(void)
  58. {
  59. while (s_tests_running)
  60. {
  61. ble_npl_eventq_run(&s_eventq);
  62. }
  63. return PASS;
  64. }
  65. int test_put(void)
  66. {
  67. s_event.ev_cb = on_event;
  68. s_event.ev_arg = &s_event_args;
  69. ble_npl_eventq_put(&s_eventq, &s_event);
  70. return PASS;
  71. }
  72. int test_get_no_wait(void)
  73. {
  74. //struct ble_npl_event *ev = ble_npl_eventq_get_no_wait(&s_eventq);
  75. return FAIL;
  76. }
  77. int test_get(void)
  78. {
  79. struct ble_npl_event *ev = ble_npl_eventq_get(&s_eventq,
  80. BLE_NPL_TIME_FOREVER);
  81. VerifyOrQuit(ev == &s_event,
  82. "callout: wrong event passed");
  83. return PASS;
  84. }
  85. void *task_test_runner(void *args)
  86. {
  87. int count = 1000000000;
  88. SuccessOrQuit(test_init(), "eventq_init failed");
  89. SuccessOrQuit(test_put(), "eventq_put failed");
  90. SuccessOrQuit(test_get(), "eventq_get failed");
  91. SuccessOrQuit(test_put(), "eventq_put failed");
  92. SuccessOrQuit(test_run(), "eventq_run failed");
  93. printf("All tests passed\n");
  94. exit(PASS);
  95. return NULL;
  96. }
  97. int main(void)
  98. {
  99. SuccessOrQuit(ble_npl_task_init(&s_task_runner,
  100. "task_test_runner",
  101. task_test_runner,
  102. NULL, 1, 0, NULL, 0),
  103. "task: error initializing");
  104. while (1) {}
  105. }