Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / ot-br-posix / repo / tests / unit / test_event_emitter.cpp
1 /*
2  *    Copyright (c) 2017, The OpenThread Authors.
3  *    All rights reserved.
4  *
5  *    Redistribution and use in source and binary forms, with or without
6  *    modification, are permitted provided that the following conditions are met:
7  *    1. Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *    3. Neither the name of the copyright holder nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *    POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "utils/event_emitter.hpp"
30
31 #include <CppUTest/TestHarness.h>
32 #include <stdarg.h>
33
34 #include "common/code_utils.hpp"
35
36 static int   sCounter = 0;
37 static int   sEvent   = 0;
38 static void *sContext = nullptr;
39
40 static void HandleSingleEvent(void *aContext, int aEvent, va_list aArguments)
41 {
42     sCounter++;
43
44     CHECK_EQUAL(sContext, aContext);
45     CHECK_EQUAL(sEvent, aEvent);
46     OTBR_UNUSED_VARIABLE(aArguments);
47 }
48
49 static void HandleTestDifferentContextEvent(void *aContext, int aEvent, va_list aArguments)
50 {
51     void *context1 = va_arg(aArguments, void *);
52     void *context2 = va_arg(aArguments, void *);
53
54     CHECK_EQUAL(sEvent, aEvent);
55
56     int id = *static_cast<int *>(aContext);
57     if (id == 1)
58     {
59         CHECK_EQUAL(context1, aContext);
60     }
61     else if (id == 2)
62     {
63         CHECK_EQUAL(context2, aContext);
64     }
65     else
66     {
67         FAIL("Unexpected id value!");
68     }
69
70     sCounter++;
71 }
72
73 static void HandleTestCallSequenceEvent(void *aContext, int aEvent, va_list aArguments)
74 {
75     CHECK_EQUAL(sEvent, aEvent);
76
77     int id = *static_cast<int *>(aContext);
78
79     ++sCounter;
80
81     CHECK_EQUAL(sCounter, id);
82
83     OTBR_UNUSED_VARIABLE(aArguments);
84 }
85
86 TEST_GROUP(EventEmitter){};
87
88 TEST(EventEmitter, TestSingleHandler)
89 {
90     otbr::EventEmitter ee;
91     int                event = 1;
92     ee.On(event, HandleSingleEvent, nullptr);
93
94     sContext = nullptr;
95     sEvent   = event;
96     sCounter = 0;
97
98     ee.Emit(event);
99
100     CHECK_EQUAL(1, sCounter);
101 }
102
103 TEST(EventEmitter, TestDoubleHandler)
104 {
105     otbr::EventEmitter ee;
106     int                event = 1;
107     ee.On(event, HandleSingleEvent, nullptr);
108     ee.On(event, HandleSingleEvent, nullptr);
109
110     sContext = nullptr;
111     sEvent   = event;
112     sCounter = 0;
113
114     ee.Emit(event);
115
116     CHECK_EQUAL(2, sCounter);
117 }
118
119 TEST(EventEmitter, TestDifferentContext)
120 {
121     otbr::EventEmitter ee;
122     int                event = 2;
123
124     int context1 = 1;
125     int context2 = 2;
126
127     ee.On(event, HandleTestDifferentContextEvent, &context1);
128     ee.On(event, HandleTestDifferentContextEvent, &context2);
129
130     sContext = nullptr;
131     sEvent   = event;
132     sCounter = 0;
133
134     ee.Emit(event, &context1, &context2);
135
136     CHECK_EQUAL(2, sCounter);
137 }
138
139 TEST(EventEmitter, TestCallSequence)
140 {
141     otbr::EventEmitter ee;
142     int                event = 3;
143
144     int context1 = 1;
145     int context2 = 2;
146
147     ee.On(event, HandleTestCallSequenceEvent, &context1);
148     ee.On(event, HandleTestCallSequenceEvent, &context2);
149
150     sContext = nullptr;
151     sEvent   = event;
152     sCounter = 0;
153
154     ee.Emit(event);
155
156     CHECK_EQUAL(2, sCounter);
157 }
158
159 TEST(EventEmitter, TestRemoveHandler)
160 {
161     otbr::EventEmitter ee;
162     int                event = 3;
163
164     ee.On(event, HandleSingleEvent, nullptr);
165     ee.On(event, HandleSingleEvent, nullptr);
166
167     sContext = nullptr;
168     sEvent   = event;
169     sCounter = 0;
170
171     ee.Emit(event);
172     CHECK_EQUAL(2, sCounter);
173
174     ee.Off(event, HandleSingleEvent, nullptr);
175     ee.Emit(event);
176     CHECK_EQUAL(3, sCounter);
177
178     ee.Off(event, HandleSingleEvent, nullptr);
179     ee.Emit(event);
180     CHECK_EQUAL(3, sCounter);
181 }