furi_pubsub_test.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "flipper_v2.h"
  4. #include "log.h"
  5. #include "minunit.h"
  6. const uint32_t context_value = 0xdeadbeef;
  7. const uint32_t notify_value_0 = 0x12345678;
  8. const uint32_t notify_value_1 = 0x11223344;
  9. uint32_t pubsub_value = 0;
  10. uint32_t pubsub_context_value = 0;
  11. void test_pubsub_handler(const void* arg, void* ctx) {
  12. pubsub_value = *(uint32_t*)arg;
  13. pubsub_context_value = *(uint32_t*)ctx;
  14. }
  15. void test_furi_pubsub() {
  16. bool result;
  17. PubSub test_pubsub;
  18. PubSubItem* test_pubsub_item;
  19. // init pubsub case
  20. result = init_pubsub(&test_pubsub);
  21. mu_assert(result, "init pubsub failed");
  22. // subscribe pubsub case
  23. test_pubsub_item = subscribe_pubsub(&test_pubsub, test_pubsub_handler, (void*)&context_value);
  24. mu_assert_pointers_not_eq(test_pubsub_item, NULL);
  25. /// notify pubsub case
  26. result = notify_pubsub(&test_pubsub, (void*)&notify_value_0);
  27. mu_assert(result, "notify pubsub failed");
  28. mu_assert_int_eq(pubsub_value, notify_value_0);
  29. mu_assert_int_eq(pubsub_context_value, context_value);
  30. // unsubscribe pubsub case
  31. result = unsubscribe_pubsub(test_pubsub_item);
  32. mu_assert(result, "unsubscribe pubsub failed");
  33. result = unsubscribe_pubsub(test_pubsub_item);
  34. mu_assert(!result, "unsubscribe pubsub not failed");
  35. /// notify unsubscribed pubsub case
  36. result = notify_pubsub(&test_pubsub, (void*)&notify_value_1);
  37. mu_assert(result, "notify pubsub failed");
  38. mu_assert_int_not_eq(pubsub_value, notify_value_1);
  39. // delete pubsub case
  40. result = delete_pubsub(&test_pubsub);
  41. mu_assert(result, "unsubscribe pubsub failed");
  42. // TODO test case that the pubsub_delete will remove pubsub from heap
  43. }