1 | /* $Id: tstTermCallbacks.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Termination Callbacks.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/initterm.h>
|
---|
42 |
|
---|
43 | #include <iprt/test.h>
|
---|
44 | #include <iprt/stream.h>
|
---|
45 | #include <iprt/errcore.h>
|
---|
46 | #include <iprt/initterm.h>
|
---|
47 |
|
---|
48 |
|
---|
49 | /*********************************************************************************************************************************
|
---|
50 | * Global Variables *
|
---|
51 | *********************************************************************************************************************************/
|
---|
52 | static uint32_t g_cCalls;
|
---|
53 | static uint32_t g_fCalled;
|
---|
54 |
|
---|
55 |
|
---|
56 | static DECLCALLBACK(void) tstTermCallback0(RTTERMREASON enmReason, int32_t iStatus, void *pvUser)
|
---|
57 | {
|
---|
58 | RT_NOREF_PV(enmReason); RT_NOREF_PV(iStatus);
|
---|
59 | RTTESTI_CHECK(pvUser == (void *)0);
|
---|
60 | g_cCalls++;
|
---|
61 | g_fCalled |= RT_BIT_32(0);
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | static DECLCALLBACK(void) tstTermCallback1(RTTERMREASON enmReason, int32_t iStatus, void *pvUser)
|
---|
66 | {
|
---|
67 | RT_NOREF_PV(enmReason); RT_NOREF_PV(iStatus);
|
---|
68 | RTTESTI_CHECK(pvUser == (void *)1);
|
---|
69 | g_cCalls++;
|
---|
70 | g_fCalled |= RT_BIT_32(1);
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | static DECLCALLBACK(void) tstTermCallback2(RTTERMREASON enmReason, int32_t iStatus, void *pvUser)
|
---|
75 | {
|
---|
76 | RT_NOREF_PV(enmReason); RT_NOREF_PV(iStatus);
|
---|
77 | RTTESTI_CHECK(pvUser == (void *)2);
|
---|
78 | g_cCalls++;
|
---|
79 | g_fCalled |= RT_BIT_32(2);
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | static DECLCALLBACK(void) tstTermCallback3(RTTERMREASON enmReason, int32_t iStatus, void *pvUser)
|
---|
84 | {
|
---|
85 | RT_NOREF_PV(enmReason); RT_NOREF_PV(iStatus);
|
---|
86 | RTTESTI_CHECK(pvUser == (void *)3);
|
---|
87 | g_cCalls++;
|
---|
88 | g_fCalled |= RT_BIT_32(3);
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | int main()
|
---|
93 | {
|
---|
94 | RTTEST hTest;
|
---|
95 | int rc = RTTestInitAndCreate("tstTermCallback", &hTest);
|
---|
96 | if (rc)
|
---|
97 | return rc;
|
---|
98 | RTTestBanner(hTest);
|
---|
99 |
|
---|
100 | /*
|
---|
101 | * Just some basics.
|
---|
102 | */
|
---|
103 | RTTestSub(hTest, "Uninitialized");
|
---|
104 | RTTESTI_CHECK_RC(RTTermDeregisterCallback(tstTermCallback1, (void *)1), VERR_NOT_FOUND);
|
---|
105 | RTTermRunCallbacks(RTTERMREASON_UNLOAD, 0);
|
---|
106 |
|
---|
107 | RTTestSub(hTest, "One callback");
|
---|
108 | g_cCalls = g_fCalled = 0;
|
---|
109 | RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback0, (void *)0), VINF_SUCCESS);
|
---|
110 | RTTermRunCallbacks(RTTERMREASON_UNLOAD, 0);
|
---|
111 | RTTESTI_CHECK(g_cCalls == 1);
|
---|
112 | RTTESTI_CHECK(g_fCalled == RT_BIT_32(0));
|
---|
113 |
|
---|
114 | RTTestSub(hTest, "Two callbacks");
|
---|
115 | g_cCalls = g_fCalled = 0;
|
---|
116 | RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback0, (void *)0), VINF_SUCCESS);
|
---|
117 | RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback1, (void *)1), VINF_SUCCESS);
|
---|
118 | RTTermRunCallbacks(RTTERMREASON_UNLOAD, 0);
|
---|
119 | RTTESTI_CHECK(g_cCalls == 2);
|
---|
120 | RTTESTI_CHECK(g_fCalled == (RT_BIT_32(0) | RT_BIT_32(1)));
|
---|
121 |
|
---|
122 | RTTestSub(hTest, "Three callbacks");
|
---|
123 | g_cCalls = g_fCalled = 0;
|
---|
124 | RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback0, (void *)0), VINF_SUCCESS);
|
---|
125 | RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback1, (void *)1), VINF_SUCCESS);
|
---|
126 | RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback2, (void *)2), VINF_SUCCESS);
|
---|
127 | RTTermRunCallbacks(RTTERMREASON_UNLOAD, 0);
|
---|
128 | RTTESTI_CHECK(g_cCalls == 3);
|
---|
129 | RTTESTI_CHECK(g_fCalled == (RT_BIT_32(0) | RT_BIT_32(1) | RT_BIT_32(2)));
|
---|
130 |
|
---|
131 | RTTestSub(hTest, "Four callbacks");
|
---|
132 | g_cCalls = g_fCalled = 0;
|
---|
133 | RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback0, (void *)0), VINF_SUCCESS);
|
---|
134 | RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback1, (void *)1), VINF_SUCCESS);
|
---|
135 | RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback2, (void *)2), VINF_SUCCESS);
|
---|
136 | RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback3, (void *)3), VINF_SUCCESS);
|
---|
137 | RTTermRunCallbacks(RTTERMREASON_UNLOAD, 0);
|
---|
138 | RTTESTI_CHECK(g_cCalls == 4);
|
---|
139 | RTTESTI_CHECK(g_fCalled == (RT_BIT_32(0) | RT_BIT_32(1) | RT_BIT_32(2) | RT_BIT_32(3)));
|
---|
140 |
|
---|
141 | RTTestSub(hTest, "Callback deregistration");
|
---|
142 | g_cCalls = g_fCalled = 0;
|
---|
143 | RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback0, (void *)1), VINF_SUCCESS);
|
---|
144 | RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback0, (void *)1), VINF_SUCCESS);
|
---|
145 | RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback0, (void *)0), VINF_SUCCESS);
|
---|
146 | RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback1, (void *)1), VINF_SUCCESS);
|
---|
147 | RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback1, (void *)0), VINF_SUCCESS);
|
---|
148 | RTTESTI_CHECK_RC(RTTermDeregisterCallback(tstTermCallback0, (void *)1), VINF_SUCCESS);
|
---|
149 | RTTESTI_CHECK_RC(RTTermDeregisterCallback(tstTermCallback0, (void *)1), VINF_SUCCESS);
|
---|
150 | RTTESTI_CHECK_RC(RTTermDeregisterCallback(tstTermCallback0, (void *)1), VERR_NOT_FOUND);
|
---|
151 | RTTESTI_CHECK_RC(RTTermDeregisterCallback(tstTermCallback1, (void *)0), VINF_SUCCESS);
|
---|
152 | RTTESTI_CHECK_RC(RTTermDeregisterCallback(tstTermCallback1, (void *)0), VERR_NOT_FOUND);
|
---|
153 | RTTermRunCallbacks(RTTERMREASON_UNLOAD, 0);
|
---|
154 | RTTESTI_CHECK(g_cCalls == 2);
|
---|
155 | RTTESTI_CHECK(g_fCalled == (RT_BIT_32(0) | RT_BIT_32(1)));
|
---|
156 |
|
---|
157 | /*
|
---|
158 | * Summary.
|
---|
159 | */
|
---|
160 | return RTTestSummaryAndDestroy(hTest);
|
---|
161 | }
|
---|
162 |
|
---|