| 1 | <HTML> |
| 2 | <HEAD> |
| 3 | <TITLE>CUnit - The Test Registry</TITLE> |
| 4 | <LINK REL=StyleSheet HREF="CUnit_doc.css" TYPE="text/css" TITLE="CUnit Basic Style" /> |
| 5 | </HEAD> |
| 6 | <BODY> |
| 7 | <DIV CLASS="NAVHEADER" > |
| 8 | <TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0"> |
| 9 | <TR> |
| 10 | <TH COLSPAN="3" ALIGN="center"><H3>CUnit Progammers Guide</H3></TH> |
| 11 | </TR> |
| 12 | <TR> |
| 13 | <TD WIDTH="10%" ALIGN="left" VALIGN="bottom"><A HREF="writing_tests.html" ACCESSKEY="P" >Prev</A></TD> |
| 14 | <TD WIDTH="80%" ALIGN="center" VALIGN="bottom"><A HREF="index.html" ACCESSKEY="H" >Home</A></TD> |
| 15 | <TD WIDTH="10%" ALIGN="right" VALIGN="bottom"><A HREF="managing_tests.html" ACCESSKEY="N" >Next</A></TD> |
| 16 | </TR> |
| 17 | </TABLE> |
| 18 | <HR ALIGN="LEFT" WIDTH="100%"> |
| 19 | |
| 20 | <H2>3. The Test Registry</H2> |
| 21 | |
| 22 | <H3 ID="synopsis">3.1. Synopsis</H3> |
| 23 | #include <<A HREF="headers/TestDB.h">CUnit/TestDB.h</A>> |
| 24 | (included automatically by <<A HREF="headers/CUnit.h">CUnit/CUnit.h</A>>) |
| 25 | <P /> |
| 26 | <PRE> |
| 27 | typedef struct <A HREF="#CU_TestRegistry">CU_TestRegistry</A> |
| 28 | typedef CU_TestRegistry* <A HREF="#CU_TestRegistry">CU_pTestRegistry</A> |
| 29 | |
| 30 | CU_ErrorCode <A HREF="#init">CU_initialize_registry</A>(void) |
| 31 | |
| 32 | void <A HREF="#cleanup">CU_cleanup_registry</A>(void) |
| 33 | |
| 34 | CU_BOOL <A HREF="#init">CU_registry_initialized</A>(void) |
| 35 | |
| 36 | CU_pTestRegistry <A HREF="#otherfuncs">CU_get_registry</A>(void) |
| 37 | |
| 38 | CU_pTestRegistry <A HREF="#otherfuncs">CU_set_registry</A>(CU_pTestRegistry pTestRegistry) |
| 39 | |
| 40 | CU_pTestRegistry <A HREF="#otherfuncs">CU_create_new_registry</A>(void) |
| 41 | |
| 42 | void <A HREF="#otherfuncs">CU_destroy_existing_registry</A>(CU_pTestRegistry* ppRegistry) |
| 43 | </PRE> |
| 44 | |
| 45 | <H3 ID="struct">3.2. Internal Structure</H3> |
| 46 | The test registry is the repository for suites and associated tests. |
| 47 | CUnit maintains an active test registry which is updated when the user |
| 48 | adds a suite or test. The suites in this active registry are the ones |
| 49 | run when the user chooses to run all tests. |
| 50 | <P /> |
| 51 | The CUnit test registry is a data structure <CITE>CU_TestRegistry</CITE> |
| 52 | declared in <A HREF="headers/TestDB.h"><CUnit/TestDB.h></A>. It |
| 53 | includes fields for the total numbers of suites and tests stored in the |
| 54 | registry, as well as a pointer to the head of the linked list of |
| 55 | registered suites. |
| 56 | <PRE ID="CU_TestRegistry"> |
| 57 | typedef struct CU_TestRegistry |
| 58 | { |
| 59 | unsigned int uiNumberOfSuites; |
| 60 | unsigned int uiNumberOfTests; |
| 61 | CU_pSuite pSuite; |
| 62 | } <CITE><B>CU_TestRegistry</B></CITE>; |
| 63 | |
| 64 | typedef CU_TestRegistry* <CITE><B>CU_pTestRegistry</B></CITE>; |
| 65 | </PRE> |
| 66 | <P /> |
| 67 | The user normally only needs to initialize the registry before use and |
| 68 | clean up afterwards. However, other functions are provided to |
| 69 | manipulate the registry when necessary. |
| 70 | |
| 71 | <H3 ID="init">3.3. Initialization</H3> |
| 72 | <P CLASS="indent2"><CITE>CU_ErrorCode <B>CU_initialize_registry</B>(void)</CITE></P> |
| 73 | <P CLASS="indent5">The active CUnit test registry must be initialized |
| 74 | before use. The user should call <CITE>CU_initialize_registry()</CITE> |
| 75 | before calling any other CUnit functions. Failure to do so will |
| 76 | likely result in a crash. If this function is called more than once, |
| 77 | any existing registry will be cleaned up (i.e. destroyed!) before |
| 78 | creating the new registry. This function may not be called during a |
| 79 | test run (i.e. from a test function or suite initialization/cleanup |
| 80 | function). |
| 81 | <BR /><BR /> |
| 82 | An error status code is returned: |
| 83 | <P CLASS="indent10"> |
| 84 | <TABLE CELLPADDING=1> |
| 85 | <TR> |
| 86 | <TD><CITE>CUE_SUCCESS</CITE></TD> |
| 87 | <TD>initialization was successful.</TD> |
| 88 | </TR> |
| 89 | <TR> |
| 90 | <TD><CITE>CUE_NOMEMORY</CITE></TD> |
| 91 | <TD>memory allocation failed.</TD> |
| 92 | </TR> |
| 93 | </TABLE> |
| 94 | </P> |
| 95 | <P CLASS="indent2"><CITE>CU_BOOL <B>CU_registry_initialized</B>(void)</CITE></P> |
| 96 | <P CLASS="indent5">This function can be used to check whether the registry has |
| 97 | been initialized. This may be useful if the registry setup is distributed over |
| 98 | multiple files that need to make sure the registry is ready for test registration. |
| 99 | </P> |
| 100 | |
| 101 | <H3 ID="cleanup">3.4. Cleanup</H3> |
| 102 | <P CLASS="indent2"><CITE>void <B>CU_cleanup_registry</B>(void)</CITE></P> |
| 103 | <P CLASS="indent5">When testing is complete, the user should call this |
| 104 | function to clean up and release memory used by the framework. This |
| 105 | should be the last CUnit function called (except for restoring the |
| 106 | test registry using <A HREF="#init">CU_initialize_registry()</A> or |
| 107 | <A HREF="#setreg">CU_set_registry()</A>). |
| 108 | <BR /><BR /> |
| 109 | Failure to call <CITE>CU_cleanup_registry()</CITE> will result in |
| 110 | memory leaks. It may be called more than once without creating an |
| 111 | error condition. <I>Note that this function will destroy all |
| 112 | suites (and associated tests) in the registry.</I> Pointers to |
| 113 | registered suites and tests should not be dereferenced after |
| 114 | cleaning up the registry. This function may not be called during a |
| 115 | test run (i.e. from a test function or suite initialization/cleanup |
| 116 | function). |
| 117 | <BR /><BR /> |
| 118 | Calling <CITE>CU_cleanup_registry()</CITE> will only affect the internal |
| 119 | <A HREF="#CU_TestRegistry">CU_TestRegistry</A> maintained by the CUnit |
| 120 | framework. Destruction of any other test registries owned by the user |
| 121 | are the responsibility of the user. This can be done explictly by |
| 122 | calling <A HREF="#destroy">CU_destroy_existing_registry()</A>, or |
| 123 | implicitly by making the registry active using |
| 124 | <A HREF="#setreg">CU_set_registry()</A> and calling |
| 125 | <CITE>CU_cleanup_registry()</CITE> again. |
| 126 | </P> |
| 127 | |
| 128 | <H3 ID="otherfuncs">3.5. Other Registry Functions</H3> |
| 129 | Other registry functions are provided primarily for internal and |
| 130 | testing purposes. However, general users may find use for them and |
| 131 | should be aware of them. |
| 132 | <P /> |
| 133 | These include: |
| 134 | <P /> |
| 135 | <P CLASS="indent2"><CITE ID="getreg">CU_pTestRegistry <B>CU_get_registry</B>(void)</CITE></P> |
| 136 | <P CLASS="indent5">Returns a pointer to the active test registry. The registry is a |
| 137 | variable of data type <A HREF="#CU_TestRegistry">CU_TestRegistry</A>. |
| 138 | Direct manipulation of the internal test registry is not |
| 139 | recommended - API functions should be used instead. The framework |
| 140 | maintains ownership of the registry, so the returned pointer will be |
| 141 | invalidated by a call to <A HREF="#cleanup">CU_cleanup_registry()</A> |
| 142 | or <A HREF="#init">CU_initialize_registry()</A>. |
| 143 | </P> |
| 144 | <P CLASS="indent2"><CITE ID="setreg">CU_pTestRegistry <B>CU_set_registry</B>(CU_pTestRegistry pTestRegistry)</CITE></P> |
| 145 | <P CLASS="indent5">Replaces the active registry with the specified one. A pointer to the |
| 146 | previous registry is returned. <B><I>It is the caller's responsibility |
| 147 | to destroy the old registry</I></B>. This can be done explictly by |
| 148 | calling <A HREF="#destroy">CU_destroy_existing_registry()</A> for the |
| 149 | returned pointer. Alternatively, the registry can be made active using |
| 150 | <A HREF="#setreg">CU_set_registry()</A> and destroyed implicitly when |
| 151 | <A HREF="#cleanup">CU_cleanup_registry()</A> is called. Care should be |
| 152 | taken not to explicitly destroy a registry that is set as the active one. |
| 153 | This can result in multiple frees of the same memory and a likely crash. |
| 154 | </P> |
| 155 | <P CLASS="indent2"><CITE ID="create">CU_pTestRegistry <B>CU_create_new_registry</B>(void)</CITE></P> |
| 156 | <P CLASS="indent5">Creates a new registry and returns a pointer to it. |
| 157 | The new registry will not contain any suites or tests. It is the |
| 158 | caller's responsibility to destroy the new registry by one of the |
| 159 | mechanisms described previously. |
| 160 | </P> |
| 161 | <P CLASS="indent2"><CITE ID="destroy">void <B>CU_destroy_existing_registry</B>(CU_pTestRegistry* ppRegistry)</CITE></P> |
| 162 | <P CLASS="indent5">Destroys and frees all memory for the specified test |
| 163 | registry, including any registered suites and tests. This function |
| 164 | should not be called for a registry which is set as the active test |
| 165 | registry (e.g. a <CITE>CU_pTestRegistry</CITE> pointer retrieved using |
| 166 | <A HREF="#getreg">CU_get_registry()</A>). This will result in a |
| 167 | multiple free of the same memory when |
| 168 | <A HREF="#cleanup">CU_cleanup_registry()</A> is called. ppRegistry |
| 169 | may not be <code>NULL</code>, but the pointer it points to may be. In |
| 170 | that case, the function has no effect. Note that *ppRegistry will be |
| 171 | set to <code>NULL</code> upon return. |
| 172 | </P> |
| 173 | |
| 174 | <H3 ID="deprecated">3.6. Deprecated v1 Data Types & Functions</H3> |
| 175 | The following data types and functions are deprecated as of version 2. |
| 176 | To use these deprecated names, user code must be compiled with |
| 177 | <CITE>USE_DEPRECATED_CUNIT_NAMES</CITE> defined. |
| 178 | <P /> |
| 179 | <B>#include <<A HREF="headers/TestDB.h">CUnit/TestDB.h</A>></B> |
| 180 | (included automatically by <A HREF="headers/CUnit.h">CUnit/CUnit.h</A>>). |
| 181 | <P /> |
| 182 | <TABLE CELLPADDING=5 BORDER=2> |
| 183 | <TR VALIGN="top"> |
| 184 | <TD><B>Deprecated Name</B></TD> |
| 185 | <TD><B>Equivalent New Name</B></TD> |
| 186 | </TR> |
| 187 | <TR VALIGN="top"> |
| 188 | <TD><CODE>_TestRegistry</CODE></TD> |
| 189 | <TD><A HREF="#CU_TestRegistry">CU_TestRegistry</A></TD> |
| 190 | </TR> |
| 191 | <TR VALIGN="top"> |
| 192 | <TD> |
| 193 | <CODE> |
| 194 | _TestRegistry.uiNumberOfGroups<BR /> |
| 195 | PTestRegistry->uiNumberOfGroups |
| 196 | </CODE> |
| 197 | </TD> |
| 198 | <TD> |
| 199 | <CODE> |
| 200 | CU_TestRegistry.uiNumberOfSuites<BR /> |
| 201 | CU_pTestRegistry->uiNumberOfSuites<BR /> |
| 202 | </CODE> |
| 203 | </TD> |
| 204 | </TR> |
| 205 | <TR VALIGN="top"> |
| 206 | <TD> |
| 207 | <CODE> |
| 208 | _TestRegistry.pGroup<BR /> |
| 209 | PTestRegistry->pGroup |
| 210 | </CODE> |
| 211 | </TD> |
| 212 | <TD> |
| 213 | <CODE> |
| 214 | CU_TestRegistry.pSuite<BR /> |
| 215 | CU_pTestRegistry->pSuite |
| 216 | </CODE> |
| 217 | </TD> |
| 218 | </TR> |
| 219 | <TR VALIGN="top"> |
| 220 | <TD><CODE>PTestRegistry</CODE></TD> |
| 221 | <TD><A HREF="#CU_TestRegistry">CU_pTestRegistry</A></TD> |
| 222 | </TR> |
| 223 | <TR VALIGN="top"> |
| 224 | <TD><CODE>initialize_registry()</CODE></TD> |
| 225 | <TD><A HREF="#init">CU_initialize_registry()</A></TD> |
| 226 | </TR> |
| 227 | <TR VALIGN="top"> |
| 228 | <TD><CODE>cleanup_registry()</CODE></TD> |
| 229 | <TD><A HREF="#cleanup">CU_cleanup_registry()</A></TD> |
| 230 | </TR> |
| 231 | <TR VALIGN="top"> |
| 232 | <TD><CODE>get_registry()</CODE></TD> |
| 233 | <TD><A HREF="#getreg">CU_get_registry()</A></TD> |
| 234 | </TR> |
| 235 | <TR VALIGN="top"> |
| 236 | <TD><CODE>set_registry()</CODE></TD> |
| 237 | <TD><A HREF="#setreg">CU_set_registry()</A></TD> |
| 238 | </TR> |
| 239 | </TABLE> |
| 240 | |
| 241 | <DIV CLASS="NAVFOOTER"> |
| 242 | <HR ALIGN="LEFT" WIDTH="100%"> |
| 243 | <TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0"> |
| 244 | <TR> |
| 245 | <TD WIDTH="33%" ALIGN="left" VALIGN="top"><A HREF="writing_tests.html" ACCESSKEY="P">Prev</A></TD> |
| 246 | <TD WIDTH="34%" ALIGN="center" VALIGN="top"><A HREF="index.html" ACCESSKEY="H" >Home</A></TD> |
| 247 | <TD WIDTH="33%" ALIGN="right" VALIGN="top"><A HREF="managing_tests.html" ACCESSKEY="N" >Next</A></TD> |
| 248 | </TR> |
| 249 | <TR> |
| 250 | <TD WIDTH="33%" ALIGN="left" VALIGN="top">Introduction</TD> |
| 251 | <TD WIDTH="34%" ALIGN="center" VALIGN="top"> </TD> |
| 252 | <TD WIDTH="33%" ALIGN="right" VALIGN="top">Managing Tests & Suites</TD> |
| 253 | </TR> |
| 254 | </TABLE> |
| 255 | </DIV> |
| 256 | |
| 257 | </BODY> |
| 258 | </HTML> |