VirtualBox

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

Last change on this file since 24639 was 20606, checked in by vboxsync, 15 years ago

IPRT/testcase: Use RTTestInitAndCreate.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.9 KB
Line 
1/* $Id: tstTermCallbacks.cpp 20606 2009-06-15 23:49:07Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Termination Callbacks.
4 */
5
6/*
7 * Copyright (C) 2009 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include <iprt/initterm.h>
36
37#include <iprt/test.h>
38#include <iprt/stream.h>
39#include <iprt/err.h>
40#include <iprt/initterm.h>
41
42/*******************************************************************************
43* Global Variables *
44*******************************************************************************/
45static uint32_t g_cCalls;
46static uint32_t g_fCalled;
47
48
49static DECLCALLBACK(void) tstTermCallback0(RTTERMREASON enmReason, int32_t iStatus, void *pvUser)
50{
51 RTTESTI_CHECK(pvUser == (void *)0);
52 g_cCalls++;
53 g_fCalled |= RT_BIT_32(0);
54}
55
56
57static DECLCALLBACK(void) tstTermCallback1(RTTERMREASON enmReason, int32_t iStatus, void *pvUser)
58{
59 RTTESTI_CHECK(pvUser == (void *)1);
60 g_cCalls++;
61 g_fCalled |= RT_BIT_32(1);
62}
63
64
65static DECLCALLBACK(void) tstTermCallback2(RTTERMREASON enmReason, int32_t iStatus, void *pvUser)
66{
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 RTTESTI_CHECK(pvUser == (void *)3);
76 g_cCalls++;
77 g_fCalled |= RT_BIT_32(3);
78}
79
80
81int main(int argc, char **argv)
82{
83 RTTEST hTest;
84 int rc = RTTestInitAndCreate("tstTermCallback", &hTest);
85 if (rc)
86 return rc;
87 RTTestBanner(hTest);
88
89 /*
90 * Just some basics.
91 */
92 RTTestSub(hTest, "Uninitialized");
93 RTTESTI_CHECK_RC(RTTermDeregisterCallback(tstTermCallback1, (void *)1), VERR_NOT_FOUND);
94 RTTermRunCallbacks(RTTERMREASON_UNLOAD, 0);
95
96 RTTestSub(hTest, "One callback");
97 g_cCalls = g_fCalled = 0;
98 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback0, (void *)0), VINF_SUCCESS);
99 RTTermRunCallbacks(RTTERMREASON_UNLOAD, 0);
100 RTTESTI_CHECK(g_cCalls == 1);
101 RTTESTI_CHECK(g_fCalled == RT_BIT_32(0));
102
103 RTTestSub(hTest, "Two callbacks");
104 g_cCalls = g_fCalled = 0;
105 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback0, (void *)0), VINF_SUCCESS);
106 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback1, (void *)1), VINF_SUCCESS);
107 RTTermRunCallbacks(RTTERMREASON_UNLOAD, 0);
108 RTTESTI_CHECK(g_cCalls == 2);
109 RTTESTI_CHECK(g_fCalled == (RT_BIT_32(0) | RT_BIT_32(1)));
110
111 RTTestSub(hTest, "Three callbacks");
112 g_cCalls = g_fCalled = 0;
113 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback0, (void *)0), VINF_SUCCESS);
114 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback1, (void *)1), VINF_SUCCESS);
115 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback2, (void *)2), VINF_SUCCESS);
116 RTTermRunCallbacks(RTTERMREASON_UNLOAD, 0);
117 RTTESTI_CHECK(g_cCalls == 3);
118 RTTESTI_CHECK(g_fCalled == (RT_BIT_32(0) | RT_BIT_32(1) | RT_BIT_32(2)));
119
120 RTTestSub(hTest, "Four callbacks");
121 g_cCalls = g_fCalled = 0;
122 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback0, (void *)0), VINF_SUCCESS);
123 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback1, (void *)1), VINF_SUCCESS);
124 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback2, (void *)2), VINF_SUCCESS);
125 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback3, (void *)3), VINF_SUCCESS);
126 RTTermRunCallbacks(RTTERMREASON_UNLOAD, 0);
127 RTTESTI_CHECK(g_cCalls == 4);
128 RTTESTI_CHECK(g_fCalled == (RT_BIT_32(0) | RT_BIT_32(1) | RT_BIT_32(2) | RT_BIT_32(3)));
129
130 RTTestSub(hTest, "Callback deregistration");
131 g_cCalls = g_fCalled = 0;
132 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback0, (void *)1), VINF_SUCCESS);
133 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback0, (void *)1), VINF_SUCCESS);
134 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback0, (void *)0), VINF_SUCCESS);
135 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback1, (void *)1), VINF_SUCCESS);
136 RTTESTI_CHECK_RC(RTTermRegisterCallback(tstTermCallback1, (void *)0), VINF_SUCCESS);
137 RTTESTI_CHECK_RC(RTTermDeregisterCallback(tstTermCallback0, (void *)1), VINF_SUCCESS);
138 RTTESTI_CHECK_RC(RTTermDeregisterCallback(tstTermCallback0, (void *)1), VINF_SUCCESS);
139 RTTESTI_CHECK_RC(RTTermDeregisterCallback(tstTermCallback0, (void *)1), VERR_NOT_FOUND);
140 RTTESTI_CHECK_RC(RTTermDeregisterCallback(tstTermCallback1, (void *)0), VINF_SUCCESS);
141 RTTESTI_CHECK_RC(RTTermDeregisterCallback(tstTermCallback1, (void *)0), VERR_NOT_FOUND);
142 RTTermRunCallbacks(RTTERMREASON_UNLOAD, 0);
143 RTTESTI_CHECK(g_cCalls == 2);
144 RTTESTI_CHECK(g_fCalled == (RT_BIT_32(0) | RT_BIT_32(1)));
145
146 /*
147 * Summary.
148 */
149 return RTTestSummaryAndDestroy(hTest);
150}
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