/[ascend]/trunk/base/generic/utilities/test/test_ascDynaLoad.c
ViewVC logotype

Contents of /trunk/base/generic/utilities/test/test_ascDynaLoad.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 100 - (show annotations) (download) (as text)
Fri Dec 9 23:25:15 2005 UTC (18 years, 9 months ago) by jds
File MIME type: text/x-csrc
File size: 6281 byte(s)
ascMalloc now working on Linux.
Fixed minor bugs in ascPanic.c, test suite functions.
1 /*
2 * Unit test functions for ASCEND: utilities/ascDynaLoad.c
3 *
4 * Copyright (C) 2005 Jerry St.Clair
5 *
6 * This file is part of the Ascend Environment.
7 *
8 * The Ascend Environment is free software; you can redistribute it
9 * and/or modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * The Ascend Environment is distributed in hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with the program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named
21 * COPYING.
22 */
23
24 #include <stdio.h>
25 #include "utilities/ascConfig.h"
26 #ifdef __WIN32__
27 #include <io.h>
28 #endif
29 #include "utilities/ascMalloc.h"
30 #include "utilities/ascDynaLoad.h"
31 #include "CUnit/CUnit.h"
32 #include "test_ascDynaLoad.h"
33 #include "test_ascDynaLoad_shlib.h"
34
35 /*
36 * ascDynaLoad.[ch] has several different platform-dependent
37 * implementations. This test function only tests the platform it
38 * is being run on. At present, testing is not implemented for all
39 * ASCEND platforms.
40 *
41 * This test function attempts to open and execute a shared library
42 * whose path is defined in variable shlib_name. Modify this variable
43 * for the shared library you want to use. Minimally, the shared
44 * library must provide the following public symbols:
45 *
46 * - init function, [int (*)(void)], returns -5.
47 * - isInitialized function, [int (*)(void)], returns TRUE if init()
48 * has been called, FALSE if cleanup or nothing has
49 * been called.
50 * - cleanup function, [void (*)(void)], no return.
51 * - value data [int] == FALSE initially, TRUE after init() has
52 * been called, FALSE after cleanup() has been called.
53 */
54 static void test_ascDynaLoad(void)
55 {
56 FILE *file;
57 initFunc init_func;
58 isInitializedFunc isInitialized_func;
59 cleanupFunc cleanup_func;
60 valuetype *value;
61 unsigned long prior_meminuse;
62
63 #ifdef __WIN32__
64 const char *shlib_name = "..\\..\\..\\generic\\utilities\\test\\test_ascDynaLoad_shlib.dll";
65 #else
66 const char *shlib_name = "../../../generic/utilities/test/test_ascDynaLoad_shlib.so";
67 #endif /* __WIN32__ */
68
69
70 prior_meminuse = ascmeminuse(); /* save meminuse() at start of test function */
71
72 /* test Asc_DynamicLoad(), Asc_DynamicUnLoad() */
73 CU_TEST(1 == Asc_DynamicLoad(NULL, NULL)); /* NULL path */
74 CU_TEST(-3 == Asc_DynamicUnLoad(NULL));
75
76 CU_TEST(1 == Asc_DynamicLoad("dummy", NULL)); /* nonexistent shared lib */
77 CU_TEST(-3 == Asc_DynamicUnLoad("dummy"));
78
79 if (NULL == (file = fopen(shlib_name, "r"))) { /* make sure we can open the test shared library */
80 CU_FAIL("Could not find test shared library. Aborting test.");
81 } else {
82
83 fclose(file);
84
85 if (0 == Asc_DynamicLoad(shlib_name, NULL)) { /* shared lib with no init func */
86 CU_PASS("Opening of shared library succeeded.");
87
88 init_func = (initFunc)Asc_DynamicSymbol(shlib_name, "init");
89 CU_TEST(NULL != init_func);
90 CU_TEST(NULL != (isInitialized_func = (isInitializedFunc)Asc_DynamicSymbol(shlib_name, "isInitialized")));
91 CU_TEST(NULL != (cleanup_func = (cleanupFunc)Asc_DynamicSymbol(shlib_name, "cleanup")));
92 CU_TEST(NULL != (value = (valuetype*)Asc_DynamicSymbol(shlib_name, "value")));
93 if ((NULL != init_func) &&
94 (NULL != isInitialized_func) &&
95 (NULL != cleanup_func) &&
96 (NULL != value)) {
97 CU_TEST(FALSE == (*isInitialized_func)());
98 CU_TEST(FALSE == (*value));
99 CU_TEST(-5 == (*init_func)());
100 CU_TEST(TRUE == (*isInitialized_func)());
101 CU_TEST(TRUE == (*value));
102 (*cleanup_func)();
103 CU_TEST(FALSE == (*isInitialized_func)());
104 CU_TEST(FALSE == (*value));
105 }
106 CU_TEST(0 == Asc_DynamicUnLoad(shlib_name));
107 }
108 else {
109 CU_FAIL("Opening of shared library failed.");
110 }
111
112 if (-5 == Asc_DynamicLoad(shlib_name, "init")) { /* shared lib with init func */
113 CU_PASS("Opening of shared library succeeded.");
114
115 CU_TEST(NULL != (void*)(init_func = (initFunc)Asc_DynamicSymbol(shlib_name, "init")));
116 CU_TEST(NULL != (void*)(isInitialized_func = (isInitializedFunc)Asc_DynamicSymbol(shlib_name, "isInitialized")));
117 CU_TEST(NULL != (void*)(cleanup_func = (cleanupFunc)Asc_DynamicSymbol(shlib_name, "cleanup")));
118 CU_TEST(NULL != (void*)(value = (valuetype*)Asc_DynamicSymbol(shlib_name, "value")));
119 if ((NULL != init_func) &&
120 (NULL != isInitialized_func) &&
121 (NULL != cleanup_func) &&
122 (NULL != value)) {
123 CU_TEST(TRUE == (*isInitialized_func)());
124 CU_TEST(TRUE == (*value));
125 (*cleanup_func)();
126 CU_TEST(FALSE == (*isInitialized_func)());
127 CU_TEST(FALSE == (*value));
128 }
129 CU_TEST(0 == Asc_DynamicUnLoad(shlib_name));
130 }
131 else {
132 CU_FAIL("Opening of shared library failed.");
133 }
134 }
135
136 /* test Asc_DynamicSymbol() - normal operation tested in previous tests */
137
138 CU_TEST(NULL == Asc_DynamicSymbol(NULL, "init")); /* NULL libname */
139 CU_TEST(NULL == Asc_DynamicSymbol(shlib_name, NULL)); /* NULL symbol */
140 CU_TEST(NULL == Asc_DynamicSymbol(shlib_name, "init")); /* library not open */
141
142 CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
143 }
144
145 /*===========================================================================*/
146 /* Registration information */
147
148 static CU_TestInfo ascDynaLoad_test_list[] = {
149 {"test_ascDynaLoad", test_ascDynaLoad},
150 CU_TEST_INFO_NULL
151 };
152
153 static CU_SuiteInfo suites[] = {
154 {"test_utilities_ascDynaLoad", NULL, NULL, ascDynaLoad_test_list},
155 CU_SUITE_INFO_NULL
156 };
157
158 /*-------------------------------------------------------------------*/
159 CU_ErrorCode test_register_utilities_ascDynaLoad(void)
160 {
161 return CU_register_suites(suites);
162 }

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22