VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstTermCallbacks.cpp@ 83743

Last change on this file since 83743 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 KB
Line 
1/* $Id: tstTermCallbacks.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Termination Callbacks.
4 */
5
6/*
7 * Copyright (C) 2009-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/initterm.h>
32
33#include <iprt/test.h>
34#include <iprt/stream.h>
35#include <iprt/errcore.h>
36#include <iprt/initterm.h>
37
38
39/*********************************************************************************************************************************
40* Global Variables *
41*********************************************************************************************************************************/
42static uint32_t g_cCalls;
43static uint32_t g_fCalled;
44
45
46static DECLCALLBACK(void) tstTermCallback0(RTTERMREASON enmReason, int32_t iStatus, void *pvUser)
47{
48 RT_NOREF_PV(enmReason); RT_NOREF_PV(iStatus);
49 RTTESTI_CHECK(pvUser == (void *)0);
50 g_cCalls++;
51 g_fCalled |= RT_BIT_32(0);
52}
53
54
55static DECLCALLBACK(void) tstTermCallback1(RTTERMREASON enmReason, int32_t iStatus, void *pvUser)
56{
57 RT_NOREF_PV(enmReason); RT_NOREF_PV(iStatus);
58 RTTESTI_CHECK(pvUser == (void *)1);
59 g_cCalls++;
60 g_fCalled |= RT_BIT_32(1);
61}
62
63
64static DECLCALLBACK(void) tstTermCallback2(RTTERMREASON enmReason, int32_t iStatus, void *pvUser)
65{
66 RT_NOREF_PV(enmReason); RT_NOREF_PV(iStatus);
67 RTTESTI_CHECK(pvUser == (void *)2);
68 g_cCalls++;
69 g_fCalled |= RT_BIT_32(2);
70}
71
72
73static DECLCALLBACK(void) tstTermCallback3(RTTERMREASON enmReason, int32_t iStatus, void *pvUser)
74{
75 RT_NOREF_PV(enmReason); RT_NOREF_PV(iStatus);
76 RTTESTI_CHECK(pvUser == (void *)3);
77 g_cCalls++;
78 g_fCalled |= RT_BIT_32(3);
79}
80
81
82int main()
83{
84 RTTEST hTest;
85 int rc = RTTestInitAndCreate("tstTermCallback", &hTest);
86 if (rc)
87 return rc;
88 RTTestBanner(hTest);
89
90 /*
91 * Just some basics.
92 */
93 RTTestSub(hTest, "Uninitialized");
94 RTTESTI_CHECK_RC(RTTermDeregisterCallback(tstTermCallback1, (void *)1), VERR_NOT_FOUND);
95 RTTermRunCallbacks(RTTERMREASON_UNLOAD, 0);
96
97 RTTestSub(hTest, "One callback");
98 g_cCalls = g_fCalled = 0;
99 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback0, (void *)0), VINF_SUCCESS);
100 RTTermRunCallbacks(RTTERMREASON_UNLOAD, 0);
101 RTTESTI_CHECK(g_cCalls == 1);
102 RTTESTI_CHECK(g_fCalled == RT_BIT_32(0));
103
104 RTTestSub(hTest, "Two callbacks");
105 g_cCalls = g_fCalled = 0;
106 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback0, (void *)0), VINF_SUCCESS);
107 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback1, (void *)1), VINF_SUCCESS);
108 RTTermRunCallbacks(RTTERMREASON_UNLOAD, 0);
109 RTTESTI_CHECK(g_cCalls == 2);
110 RTTESTI_CHECK(g_fCalled == (RT_BIT_32(0) | RT_BIT_32(1)));
111
112 RTTestSub(hTest, "Three callbacks");
113 g_cCalls = g_fCalled = 0;
114 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback0, (void *)0), VINF_SUCCESS);
115 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback1, (void *)1), VINF_SUCCESS);
116 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback2, (void *)2), VINF_SUCCESS);
117 RTTermRunCallbacks(RTTERMREASON_UNLOAD, 0);
118 RTTESTI_CHECK(g_cCalls == 3);
119 RTTESTI_CHECK(g_fCalled == (RT_BIT_32(0) | RT_BIT_32(1) | RT_BIT_32(2)));
120
121 RTTestSub(hTest, "Four callbacks");
122 g_cCalls = g_fCalled = 0;
123 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback0, (void *)0), VINF_SUCCESS);
124 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback1, (void *)1), VINF_SUCCESS);
125 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback2, (void *)2), VINF_SUCCESS);
126 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback3, (void *)3), VINF_SUCCESS);
127 RTTermRunCallbacks(RTTERMREASON_UNLOAD, 0);
128 RTTESTI_CHECK(g_cCalls == 4);
129 RTTESTI_CHECK(g_fCalled == (RT_BIT_32(0) | RT_BIT_32(1) | RT_BIT_32(2) | RT_BIT_32(3)));
130
131 RTTestSub(hTest, "Callback deregistration");
132 g_cCalls = g_fCalled = 0;
133 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback0, (void *)1), VINF_SUCCESS);
134 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback0, (void *)1), VINF_SUCCESS);
135 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback0, (void *)0), VINF_SUCCESS);
136 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback1, (void *)1), VINF_SUCCESS);
137 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback1, (void *)0), VINF_SUCCESS);
138 RTTESTI_CHECK_RC(RTTermDeregisterCallback(tstTermCallback0, (void *)1), VINF_SUCCESS);
139 RTTESTI_CHECK_RC(RTTermDeregisterCallback(tstTermCallback0, (void *)1), VINF_SUCCESS);
140 RTTESTI_CHECK_RC(RTTermDeregisterCallback(tstTermCallback0, (void *)1), VERR_NOT_FOUND);
141 RTTESTI_CHECK_RC(RTTermDeregisterCallback(tstTermCallback1, (void *)0), VINF_SUCCESS);
142 RTTESTI_CHECK_RC(RTTermDeregisterCallback(tstTermCallback1, (void *)0), VERR_NOT_FOUND);
143 RTTermRunCallbacks(RTTERMREASON_UNLOAD, 0);
144 RTTESTI_CHECK(g_cCalls == 2);
145 RTTESTI_CHECK(g_fCalled == (RT_BIT_32(0) | RT_BIT_32(1)));
146
147 /*
148 * Summary.
149 */
150 return RTTestSummaryAndDestroy(hTest);
151}
152
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette