LP-572 Add button_search icon
[librepilot.git] / flight / tests / lednotification / unittest.cpp
blob853d4b9f85078b9aa3857c8ce8f638b6b6647bd4
1 #include "gtest/gtest.h"
3 #include <stdio.h> /* printf */
4 #include <stdlib.h> /* abort */
5 #include <string.h> /* memset */
7 extern "C" {
8 #define xTaskGetTickCount() 1
9 #define portTICK_RATE_MS 1
11 #include "lednotification.c"
13 void PIOS_WS2811_setColorRGB(__attribute__((unused)) Color_t c, __attribute__((unused)) uint8_t led, __attribute__((unused)) bool update) {}
14 void PIOS_WS2811_Update() {}
17 class LedNotificationTest : public testing::Test {};
19 int compare_int8(const void *a, const void *b)
21 const int8_t *da = (const int8_t *)a;
22 const int8_t *db = (const int8_t *)b;
24 return (*da > *db) - (*da < *db);
28 void sort_priorities(int8_t *queued_priorities)
30 qsort(queued_priorities, MAX_BACKGROUND_NOTIFICATIONS, sizeof(int8_t), compare_int8);
33 void set_highest_active(NotifierLedStatus_t *status)
35 int8_t highest = NOTIFY_PRIORITY_BACKGROUND;
37 for (int i = 0; i < MAX_BACKGROUND_NOTIFICATIONS; i++) {
38 if (status->queued_priorities[i] > highest) {
39 status->active_sequence_num = i;
40 highest = status->queued_priorities[i];
45 void insert(NotifierLedStatus_t *status, pios_notify_priority priority)
47 ExtLedNotification_t notification;
49 notification.priority = priority;
50 push_queued_sequence(&notification, status);
53 void init(NotifierLedStatus_t *status, pios_notify_priority priority)
55 for (uint8_t i = 0; i < MAX_BACKGROUND_NOTIFICATIONS; i++) {
56 status->queued_priorities[i] = priority;
60 TEST_F(LedNotificationTest, TestQueueOrder1) {
61 NotifierLedStatus_t status;
63 init(&status, NOTIFY_PRIORITY_BACKGROUND);
65 insert(&status, NOTIFY_PRIORITY_LOW);
66 insert(&status, NOTIFY_PRIORITY_CRITICAL);
67 insert(&status, NOTIFY_PRIORITY_LOW);
68 insert(&status, NOTIFY_PRIORITY_CRITICAL);
70 set_highest_active(&status);
73 EXPECT_EQ(NOTIFY_PRIORITY_CRITICAL, (status.queued_priorities[status.active_sequence_num]));
74 pop_queued_sequence(&status);
75 EXPECT_EQ(NOTIFY_PRIORITY_CRITICAL, (status.queued_priorities[status.active_sequence_num]));
76 pop_queued_sequence(&status);
77 EXPECT_EQ(NOTIFY_PRIORITY_LOW, (status.queued_priorities[status.active_sequence_num]));
78 pop_queued_sequence(&status);
79 EXPECT_EQ(NOTIFY_PRIORITY_LOW, (status.queued_priorities[status.active_sequence_num]));
80 pop_queued_sequence(&status);
81 EXPECT_EQ(NOTIFY_PRIORITY_BACKGROUND, (status.queued_priorities[status.active_sequence_num]));
84 TEST_F(LedNotificationTest, TestQueueOrder2) {
85 NotifierLedStatus_t status;
87 // Fails because insert_point and first_point will both be -1. This will also cause an array-out-of bounds at:
88 // 146 status->queued_priorities[insert_point] = new_notification->priority;
89 // 147 status->queued_sequences[insert_point] = new_notification->sequence;
90 // 148 updated_sequence = insert_point;
92 init(&status, NOTIFY_PRIORITY_LOW);
93 status.queued_priorities[0] = NOTIFY_PRIORITY_BACKGROUND;
95 insert(&status, NOTIFY_PRIORITY_REGULAR);
97 set_highest_active(&status);
99 EXPECT_EQ(NOTIFY_PRIORITY_REGULAR, (status.queued_priorities[status.active_sequence_num]));
100 pop_queued_sequence(&status);
101 EXPECT_EQ(NOTIFY_PRIORITY_LOW, (status.queued_priorities[status.active_sequence_num]));
102 pop_queued_sequence(&status);
103 EXPECT_EQ(NOTIFY_PRIORITY_LOW, (status.queued_priorities[status.active_sequence_num]));
104 pop_queued_sequence(&status);
105 EXPECT_EQ(NOTIFY_PRIORITY_LOW, (status.queued_priorities[status.active_sequence_num]));
106 pop_queued_sequence(&status);
107 EXPECT_EQ(NOTIFY_PRIORITY_LOW, (status.queued_priorities[status.active_sequence_num]));
108 pop_queued_sequence(&status);
109 EXPECT_EQ(NOTIFY_PRIORITY_BACKGROUND, (status.queued_priorities[status.active_sequence_num]));
112 TEST_F(LedNotificationTest, TestQueueOrder3) {
113 NotifierLedStatus_t status;
115 // Fails because queued_priorities[0] _LOW and not _REGULAR. I _thibnk_ this is a bug.
116 init(&status, NOTIFY_PRIORITY_REGULAR);
117 status.queued_priorities[0] = NOTIFY_PRIORITY_BACKGROUND;
119 insert(&status, NOTIFY_PRIORITY_LOW);
121 set_highest_active(&status);
124 for (uint8_t i = 0; i < MAX_BACKGROUND_NOTIFICATIONS - 1; i++) {
125 EXPECT_EQ(NOTIFY_PRIORITY_REGULAR, (status.queued_priorities[status.active_sequence_num]));
126 pop_queued_sequence(&status);
130 TEST_F(LedNotificationTest, TestQueueOrder4) {
131 NotifierLedStatus_t status;
133 init(&status, NOTIFY_PRIORITY_BACKGROUND);
135 insert(&status, NOTIFY_PRIORITY_REGULAR);
136 insert(&status, NOTIFY_PRIORITY_REGULAR);
137 insert(&status, NOTIFY_PRIORITY_REGULAR);
138 insert(&status, NOTIFY_PRIORITY_LOW);
140 set_highest_active(&status);
142 EXPECT_EQ(NOTIFY_PRIORITY_REGULAR, (status.queued_priorities[status.active_sequence_num]));
143 pop_queued_sequence(&status);
144 EXPECT_EQ(NOTIFY_PRIORITY_REGULAR, (status.queued_priorities[status.active_sequence_num]));
145 pop_queued_sequence(&status);
146 EXPECT_EQ(NOTIFY_PRIORITY_REGULAR, (status.queued_priorities[status.active_sequence_num]));
147 pop_queued_sequence(&status);
148 EXPECT_EQ(NOTIFY_PRIORITY_LOW, (status.queued_priorities[status.active_sequence_num]));
149 pop_queued_sequence(&status);
150 EXPECT_EQ(NOTIFY_PRIORITY_BACKGROUND, (status.queued_priorities[status.active_sequence_num]));
151 pop_queued_sequence(&status);
152 EXPECT_EQ(NOTIFY_PRIORITY_BACKGROUND, (status.queued_priorities[status.active_sequence_num]));