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

Contents of /trunk/ascend/utilities/test/test_ascDynaLoad.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2011 - (show annotations) (download) (as text)
Tue Apr 28 08:58:48 2009 UTC (15 years, 9 months ago) by jpye
File MIME type: text/x-csrc
File size: 7231 byte(s)
Moving libascend components from #/base/generic into #/ascend
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 *value1, *value2;
61 unsigned long prior_meminuse;
62
63 #ifdef __WIN32__
64 const char *shlib_name = "base\\generic\\utilities\\test\\testdynaload.dll";
65 #else
66 const char *shlib_name = "base/generic/utilities/test/libtestdynaload.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 CU_TEST(NULL != (init_func = (initFunc)Asc_DynamicFunction(shlib_name, "init")));
89 CU_TEST(NULL != (isInitialized_func = (isInitializedFunc)Asc_DynamicFunction(shlib_name, "isInitialized")));
90 CU_TEST(NULL != (cleanup_func = (cleanupFunc)Asc_DynamicFunction(shlib_name, "cleanup")));
91 CU_TEST(NULL != (value1 = (valuetype*)Asc_DynamicVariable(shlib_name, "value")));
92 CU_TEST(NULL != (value2 = (valuetype*)Asc_DynamicSymbol(shlib_name, "value")));
93 if ((NULL != init_func) &&
94 (NULL != isInitialized_func) &&
95 (NULL != cleanup_func) &&
96 (NULL != value1) &&
97 (NULL != value2)) {
98 CU_TEST(FALSE == (*isInitialized_func)());
99 CU_TEST(FALSE == (*value1));
100 CU_TEST(FALSE == (*value2));
101 CU_TEST(-5 == (*init_func)());
102 CU_TEST(TRUE == (*isInitialized_func)());
103 CU_TEST(TRUE == (*value1));
104 CU_TEST(TRUE == (*value2));
105 (*cleanup_func)();
106 CU_TEST(FALSE == (*isInitialized_func)());
107 CU_TEST(FALSE == (*value1));
108 CU_TEST(FALSE == (*value2));
109 }
110 CU_TEST(0 == Asc_DynamicUnLoad(shlib_name));
111 }
112 else {
113 CU_FAIL("Opening of shared library failed.");
114 }
115
116 if (-5 == Asc_DynamicLoad(shlib_name, "init")) { /* shared lib with init func */
117 CU_PASS("Opening of shared library succeeded.");
118
119 CU_TEST(NULL != (init_func = (initFunc)Asc_DynamicFunction(shlib_name, "init")));
120 CU_TEST(NULL != (isInitialized_func = (isInitializedFunc)Asc_DynamicFunction(shlib_name, "isInitialized")));
121 CU_TEST(NULL != (cleanup_func = (cleanupFunc)Asc_DynamicFunction(shlib_name, "cleanup")));
122 CU_TEST(NULL != (value1 = (valuetype*)Asc_DynamicVariable(shlib_name, "value")));
123 CU_TEST(NULL != (value2 = (valuetype*)Asc_DynamicSymbol(shlib_name, "value")));
124 if ((NULL != init_func) &&
125 (NULL != isInitialized_func) &&
126 (NULL != cleanup_func) &&
127 (NULL != value1) &&
128 (NULL != value2)) {
129 CU_TEST(TRUE == (*isInitialized_func)());
130 CU_TEST(TRUE == (*value1));
131 CU_TEST(TRUE == (*value2));
132 (*cleanup_func)();
133 CU_TEST(FALSE == (*isInitialized_func)());
134 CU_TEST(FALSE == (*value1));
135 CU_TEST(FALSE == (*value2));
136 }
137 CU_TEST(0 == Asc_DynamicUnLoad(shlib_name));
138 }
139 else {
140 CU_FAIL("Opening of shared library failed.");
141 }
142 }
143
144 /* test Asc_DynamicVariable(), test Asc_DynamicFunction(), test Asc_DynamicSymbol()
145 - normal operation tested in previous tests */
146
147 CU_TEST(NULL == Asc_DynamicVariable(NULL, "value")); /* NULL libname */
148 CU_TEST(NULL == Asc_DynamicVariable(shlib_name, NULL)); /* NULL symbol */
149 CU_TEST(NULL == Asc_DynamicVariable(shlib_name, "value")); /* library not open */
150
151 CU_TEST(NULL == Asc_DynamicSymbol(NULL, "value")); /* NULL libname */
152 CU_TEST(NULL == Asc_DynamicSymbol(shlib_name, NULL)); /* NULL symbol */
153 CU_TEST(NULL == Asc_DynamicSymbol(shlib_name, "value")); /* library not open */
154
155 CU_TEST(NULL == Asc_DynamicFunction(NULL, "init")); /* NULL libname */
156 CU_TEST(NULL == Asc_DynamicFunction(shlib_name, NULL)); /* NULL symbol */
157 CU_TEST(NULL == Asc_DynamicFunction(shlib_name, "init")); /* library not open */
158
159 CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
160 }
161
162 /*===========================================================================*/
163 /* Registration information */
164
165 static CU_TestInfo ascDynaLoad_test_list[] = {
166 {"dyna;oad", test_ascDynaLoad},
167 CU_TEST_INFO_NULL
168 };
169
170 static CU_SuiteInfo suites[] = {
171 {"utilities_ascDynaLoad", NULL, NULL, ascDynaLoad_test_list},
172 CU_SUITE_INFO_NULL
173 };
174
175 /*-------------------------------------------------------------------*/
176 CU_ErrorCode test_register_utilities_ascDynaLoad(void)
177 {
178 return CU_register_suites(suites);
179 }

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