test_npl_callout.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_callout api:
  21. void ble_npl_callout_init(struct ble_npl_callout *cf, struct ble_npl_eventq *evq,
  22. ble_npl_event_fn *ev_cb, void *ev_arg);
  23. int ble_npl_callout_reset(struct ble_npl_callout *, int32_t);
  24. int ble_npl_callout_queued(struct ble_npl_callout *c);
  25. void ble_npl_callout_stop(struct ble_npl_callout *c);
  26. */
  27. #include "test_util.h"
  28. #include "nimble/nimble_npl.h"
  29. #define TEST_ARGS_VALUE (55)
  30. #define TEST_INTERVAL (100)
  31. static bool s_tests_running = true;
  32. static struct ble_npl_task s_task;
  33. static struct ble_npl_callout s_callout;
  34. static int s_callout_args = TEST_ARGS_VALUE;
  35. static struct ble_npl_eventq s_eventq;
  36. void on_callout(struct ble_npl_event *ev)
  37. {
  38. VerifyOrQuit(ev->ev_arg == &s_callout_args,
  39. "callout: wrong args passed");
  40. VerifyOrQuit(*(int*)ev->ev_arg == TEST_ARGS_VALUE,
  41. "callout: args corrupted");
  42. s_tests_running = false;
  43. }
  44. /**
  45. * ble_npl_callout_init(struct ble_npl_callout *c, struct ble_npl_eventq *evq,
  46. * ble_npl_event_fn *ev_cb, void *ev_arg)
  47. */
  48. int test_init(void)
  49. {
  50. ble_npl_callout_init(&s_callout,
  51. &s_eventq,
  52. on_callout,
  53. &s_callout_args);
  54. return PASS;
  55. }
  56. int test_queued(void)
  57. {
  58. //VerifyOrQuit(ble_npl_callout_queued(&s_callout),
  59. // "callout: not queued when expected");
  60. return PASS;
  61. }
  62. int test_reset(void)
  63. {
  64. return ble_npl_callout_reset(&s_callout, TEST_INTERVAL);
  65. }
  66. int test_stop(void)
  67. {
  68. return PASS;
  69. }
  70. /**
  71. * ble_npl_callout_init(struct ble_npl_callout *c, struct ble_npl_eventq *evq,
  72. * ble_npl_event_fn *ev_cb, void *ev_arg)
  73. */
  74. void *test_task_run(void *args)
  75. {
  76. SuccessOrQuit(test_init(), "callout_init failed");
  77. SuccessOrQuit(test_queued(), "callout_queued failed");
  78. SuccessOrQuit(test_reset(), "callout_reset failed");
  79. while (s_tests_running)
  80. {
  81. ble_npl_eventq_run(&s_eventq);
  82. }
  83. printf("All tests passed\n");
  84. exit(PASS);
  85. return NULL;
  86. }
  87. int main(void)
  88. {
  89. ble_npl_eventq_init(&s_eventq);
  90. SuccessOrQuit(ble_npl_task_init(&s_task, "s_task", test_task_run,
  91. NULL, 1, 0, NULL, 0),
  92. "task: error initializing");
  93. while (1) {}
  94. }