1 #include <gtest/gtest.h>
2 
3 #include <climits>
4 
5 #include "AllocationTestHarness.h"
6 
7 extern "C" {
8 #include "osi/include/data_dispatcher.h"
9 #include "osi/include/fixed_queue.h"
10 #include "osi/include/osi.h"
11 }
12 
13 #define DUMMY_TYPE_0 34
14 #define DUMMY_TYPE_1 42
15 #define TYPE_EDGE_CASE_ZERO 0
16 #define TYPE_EDGE_CASE_MAX INT_MAX
17 
18 #define DUMMY_QUEUE_SIZE 10
19 
20 class DataDispatcherTest : public AllocationTestHarness {};
21 
22 static char dummy_data_0[42] = "please test your code";
23 static char dummy_data_1[42] = "testing is good for your sanity";
24 
TEST_F(DataDispatcherTest,test_new_free_simple)25 TEST_F(DataDispatcherTest, test_new_free_simple) {
26   data_dispatcher_t *dispatcher = data_dispatcher_new("test_dispatcher");
27   ASSERT_TRUE(dispatcher != NULL);
28   data_dispatcher_free(dispatcher);
29 }
30 
TEST_F(DataDispatcherTest,test_dispatch_single_to_nowhere)31 TEST_F(DataDispatcherTest, test_dispatch_single_to_nowhere) {
32   data_dispatcher_t *dispatcher = data_dispatcher_new("test_dispatcher");
33   EXPECT_FALSE(data_dispatcher_dispatch(dispatcher, DUMMY_TYPE_0, dummy_data_0));
34   data_dispatcher_free(dispatcher);
35 }
36 
TEST_F(DataDispatcherTest,test_dispatch_single_to_single)37 TEST_F(DataDispatcherTest, test_dispatch_single_to_single) {
38   data_dispatcher_t *dispatcher = data_dispatcher_new("test_dispatcher");
39 
40   // Register a queue
41   fixed_queue_t *dummy_queue = fixed_queue_new(DUMMY_QUEUE_SIZE);
42   data_dispatcher_register(dispatcher, DUMMY_TYPE_0, dummy_queue);
43   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue));
44 
45   // Send data to the queue
46   EXPECT_TRUE(data_dispatcher_dispatch(dispatcher, DUMMY_TYPE_0, dummy_data_0));
47 
48   // Did we get it?
49   EXPECT_FALSE(fixed_queue_is_empty(dummy_queue));
50   EXPECT_STREQ(dummy_data_0, (char *)fixed_queue_try_dequeue(dummy_queue));
51   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue));
52 
53   fixed_queue_free(dummy_queue, NULL);
54   data_dispatcher_free(dispatcher);
55 }
56 
TEST_F(DataDispatcherTest,test_dispatch_single_to_multiple)57 TEST_F(DataDispatcherTest, test_dispatch_single_to_multiple) {
58   data_dispatcher_t *dispatcher = data_dispatcher_new("test_dispatcher");
59 
60   // Register two queues
61   fixed_queue_t *dummy_queue0 = fixed_queue_new(DUMMY_QUEUE_SIZE);
62   fixed_queue_t *dummy_queue1 = fixed_queue_new(DUMMY_QUEUE_SIZE);
63   data_dispatcher_register(dispatcher, DUMMY_TYPE_0, dummy_queue0);
64   data_dispatcher_register(dispatcher, DUMMY_TYPE_1, dummy_queue1);
65   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue0));
66   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue1));
67 
68   // Send data to one of them
69   EXPECT_TRUE(data_dispatcher_dispatch(dispatcher, DUMMY_TYPE_0, dummy_data_0));
70 
71   // Did we get it?
72   EXPECT_FALSE(fixed_queue_is_empty(dummy_queue0));
73   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue1));
74   EXPECT_STREQ(dummy_data_0, (char *)fixed_queue_try_dequeue(dummy_queue0));
75   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue0));
76 
77   fixed_queue_free(dummy_queue0, NULL);
78   fixed_queue_free(dummy_queue1, NULL);
79   data_dispatcher_free(dispatcher);
80 }
81 
TEST_F(DataDispatcherTest,test_dispatch_single_to_default)82 TEST_F(DataDispatcherTest, test_dispatch_single_to_default) {
83   data_dispatcher_t *dispatcher = data_dispatcher_new("test_dispatcher");
84 
85   // Register two queues, a default and a typed one
86   fixed_queue_t *dummy_queue = fixed_queue_new(DUMMY_QUEUE_SIZE);
87   fixed_queue_t *default_queue = fixed_queue_new(DUMMY_QUEUE_SIZE);
88   data_dispatcher_register(dispatcher, DUMMY_TYPE_0, dummy_queue);
89   data_dispatcher_register_default(dispatcher, default_queue);
90   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue));
91   EXPECT_TRUE(fixed_queue_is_empty(default_queue));
92 
93   // Send data to nowhere
94   EXPECT_TRUE(data_dispatcher_dispatch(dispatcher, DUMMY_TYPE_1, dummy_data_1));
95 
96   // Did we get it?
97   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue));
98   EXPECT_FALSE(fixed_queue_is_empty(default_queue));
99   EXPECT_STREQ(dummy_data_1, (char *)fixed_queue_try_dequeue(default_queue));
100   EXPECT_TRUE(fixed_queue_is_empty(default_queue));
101 
102   fixed_queue_free(dummy_queue, NULL);
103   fixed_queue_free(default_queue, NULL);
104   data_dispatcher_free(dispatcher);
105 }
106 
TEST_F(DataDispatcherTest,test_dispatch_multiple_to_single)107 TEST_F(DataDispatcherTest, test_dispatch_multiple_to_single) {
108   data_dispatcher_t *dispatcher = data_dispatcher_new("test_dispatcher");
109 
110   // Register a queue
111   fixed_queue_t *dummy_queue = fixed_queue_new(DUMMY_QUEUE_SIZE);
112   data_dispatcher_register(dispatcher, DUMMY_TYPE_0, dummy_queue);
113   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue));
114 
115   // Send data to the queue
116   EXPECT_TRUE(data_dispatcher_dispatch(dispatcher, DUMMY_TYPE_0, dummy_data_0));
117   EXPECT_TRUE(data_dispatcher_dispatch(dispatcher, DUMMY_TYPE_0, dummy_data_1));
118 
119   // Did we get it?
120   EXPECT_FALSE(fixed_queue_is_empty(dummy_queue));
121   EXPECT_STREQ(dummy_data_0, (char *)fixed_queue_try_dequeue(dummy_queue));
122   EXPECT_FALSE(fixed_queue_is_empty(dummy_queue));
123   EXPECT_STREQ(dummy_data_1, (char *)fixed_queue_try_dequeue(dummy_queue));
124   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue));
125 
126   fixed_queue_free(dummy_queue, NULL);
127   data_dispatcher_free(dispatcher);
128 }
129 
TEST_F(DataDispatcherTest,test_dispatch_multiple_to_multiple)130 TEST_F(DataDispatcherTest, test_dispatch_multiple_to_multiple) {
131   data_dispatcher_t *dispatcher = data_dispatcher_new("test_dispatcher");
132 
133   // Register two queues
134   fixed_queue_t *dummy_queue0 = fixed_queue_new(DUMMY_QUEUE_SIZE);
135   fixed_queue_t *dummy_queue1 = fixed_queue_new(DUMMY_QUEUE_SIZE);
136   data_dispatcher_register(dispatcher, DUMMY_TYPE_0, dummy_queue0);
137   data_dispatcher_register(dispatcher, DUMMY_TYPE_1, dummy_queue1);
138   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue0));
139   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue1));
140 
141   // Send data to both of them
142   EXPECT_TRUE(data_dispatcher_dispatch(dispatcher, DUMMY_TYPE_0, dummy_data_0));
143   EXPECT_TRUE(data_dispatcher_dispatch(dispatcher, DUMMY_TYPE_1, dummy_data_1));
144 
145   // Did we get it?
146   EXPECT_FALSE(fixed_queue_is_empty(dummy_queue0));
147   EXPECT_FALSE(fixed_queue_is_empty(dummy_queue1));
148   EXPECT_STREQ(dummy_data_0, (char *)fixed_queue_try_dequeue(dummy_queue0));
149   EXPECT_STREQ(dummy_data_1, (char *)fixed_queue_try_dequeue(dummy_queue1));
150   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue0));
151   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue1));
152 
153   fixed_queue_free(dummy_queue0, NULL);
154   fixed_queue_free(dummy_queue1, NULL);
155   data_dispatcher_free(dispatcher);
156 }
157 
TEST_F(DataDispatcherTest,test_dispatch_single_to_single_reregistered)158 TEST_F(DataDispatcherTest, test_dispatch_single_to_single_reregistered) {
159   data_dispatcher_t *dispatcher = data_dispatcher_new("test_dispatcher");
160 
161   // Register a queue, then reregister
162   fixed_queue_t *dummy_queue = fixed_queue_new(DUMMY_QUEUE_SIZE);
163   fixed_queue_t *dummy_queue_reregistered = fixed_queue_new(DUMMY_QUEUE_SIZE);
164   data_dispatcher_register(dispatcher, DUMMY_TYPE_0, dummy_queue);
165   data_dispatcher_register(dispatcher, DUMMY_TYPE_0, dummy_queue_reregistered);
166   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue));
167   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue_reregistered));
168 
169   // Send data to the queue
170   EXPECT_TRUE(data_dispatcher_dispatch(dispatcher, DUMMY_TYPE_0, dummy_data_0));
171 
172   // Did we get it?
173   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue));
174   EXPECT_FALSE(fixed_queue_is_empty(dummy_queue_reregistered));
175   EXPECT_STREQ(dummy_data_0, (char *)fixed_queue_try_dequeue(dummy_queue_reregistered));
176   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue_reregistered));
177 
178   fixed_queue_free(dummy_queue, NULL);
179   fixed_queue_free(dummy_queue_reregistered, NULL);
180   data_dispatcher_free(dispatcher);
181 }
182 
TEST_F(DataDispatcherTest,test_dispatch_single_to_reregistered_null)183 TEST_F(DataDispatcherTest, test_dispatch_single_to_reregistered_null) {
184   data_dispatcher_t *dispatcher = data_dispatcher_new("test_dispatcher");
185 
186   // Register a queue
187   fixed_queue_t *dummy_queue = fixed_queue_new(DUMMY_QUEUE_SIZE);
188   data_dispatcher_register(dispatcher, DUMMY_TYPE_0, dummy_queue);
189   data_dispatcher_register(dispatcher, DUMMY_TYPE_0, NULL);
190   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue));
191 
192   EXPECT_FALSE(data_dispatcher_dispatch(dispatcher, DUMMY_TYPE_0, dummy_data_0));
193   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue));
194 
195   fixed_queue_free(dummy_queue, NULL);
196   data_dispatcher_free(dispatcher);
197 }
198 
TEST_F(DataDispatcherTest,test_dispatch_single_to_default_reregistered_null)199 TEST_F(DataDispatcherTest, test_dispatch_single_to_default_reregistered_null) {
200   data_dispatcher_t *dispatcher = data_dispatcher_new("test_dispatcher");
201 
202   // Register a queue
203   fixed_queue_t *dummy_queue = fixed_queue_new(DUMMY_QUEUE_SIZE);
204   data_dispatcher_register_default(dispatcher, dummy_queue);
205   data_dispatcher_register_default(dispatcher, NULL);
206   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue));
207 
208   EXPECT_FALSE(data_dispatcher_dispatch(dispatcher, DUMMY_TYPE_0, dummy_data_0));
209   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue));
210 
211   fixed_queue_free(dummy_queue, NULL);
212   data_dispatcher_free(dispatcher);
213 }
214 
TEST_F(DataDispatcherTest,test_dispatch_edge_zero)215 TEST_F(DataDispatcherTest, test_dispatch_edge_zero) {
216   data_dispatcher_t *dispatcher = data_dispatcher_new("test_dispatcher");
217 
218   // Register a queue
219   fixed_queue_t *dummy_queue = fixed_queue_new(DUMMY_QUEUE_SIZE);
220   data_dispatcher_register(dispatcher, TYPE_EDGE_CASE_ZERO, dummy_queue);
221   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue));
222 
223   // Send data to the queue
224   EXPECT_TRUE(data_dispatcher_dispatch(dispatcher, TYPE_EDGE_CASE_ZERO, dummy_data_0));
225 
226   // Did we get it?
227   EXPECT_FALSE(fixed_queue_is_empty(dummy_queue));
228   EXPECT_STREQ(dummy_data_0, (char *)fixed_queue_try_dequeue(dummy_queue));
229   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue));
230 
231   fixed_queue_free(dummy_queue, NULL);
232   data_dispatcher_free(dispatcher);
233 }
234 
TEST_F(DataDispatcherTest,test_dispatch_edge_max)235 TEST_F(DataDispatcherTest, test_dispatch_edge_max) {
236   data_dispatcher_t *dispatcher = data_dispatcher_new("test_dispatcher");
237 
238   // Register a queue
239   fixed_queue_t *dummy_queue = fixed_queue_new(DUMMY_QUEUE_SIZE);
240   data_dispatcher_register(dispatcher, TYPE_EDGE_CASE_MAX, dummy_queue);
241   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue));
242 
243   // Send data to the queue
244   EXPECT_TRUE(data_dispatcher_dispatch(dispatcher, TYPE_EDGE_CASE_MAX, dummy_data_0));
245 
246   // Did we get it?
247   EXPECT_FALSE(fixed_queue_is_empty(dummy_queue));
248   EXPECT_STREQ(dummy_data_0, (char *)fixed_queue_try_dequeue(dummy_queue));
249   EXPECT_TRUE(fixed_queue_is_empty(dummy_queue));
250 
251   fixed_queue_free(dummy_queue, NULL);
252   data_dispatcher_free(dispatcher);
253 }
254