/[ascend]/trunk/base/generic/solver/test/test_slv_common.c
ViewVC logotype

Contents of /trunk/base/generic/solver/test/test_slv_common.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 599 - (show annotations) (download) (as text)
Fri May 12 12:53:57 2006 UTC (14 years, 2 months ago) by johnpye
File MIME type: text/x-csrc
File size: 58630 byte(s)
Fixing up MALLOC_DEBUG to use config.h
1 /*
2 * Unit test functions for ASCEND: solver/slv_common.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 <solver/slv_types.h>
31 #include <solver/rel.h>
32 #include <solver/logrel.h>
33 #include <solver/mtx.h>
34 #include <general/list.h>
35 #include <solver/slv_common.h>
36 #include "CUnit/CUnit.h"
37 #include "test_slv_common.h"
38 #include "assertimpl.h"
39 #include "printutil.h"
40
41 /*
42 * Independent calculation of a vector dot product.
43 * Nothing fancy, no validation of input. Assumes valid vectors.
44 */
45 static real64 slow_inner_product(struct vector_data *vec1, struct vector_data *vec2)
46 {
47 int32 i;
48 real64 product = 0.0;
49 real64 *p1 = vec1->vec + vec1->rng->low;
50 real64 *p2 = vec2->vec + vec2->rng->low;
51 int32 len = vec1->rng->high - vec1->rng->low + 1;
52
53 for (i=0 ; i<len ; ++i, ++p1, ++p2)
54 product += *p1 * *p2;
55
56 return product;
57 }
58
59 /*
60 * Independent calculation of an array dot product.
61 * Nothing fancy, no validation of input.
62 * Assumes valid arrays of length at least len.
63 */
64 static real64 slow_dot_product(int32 len, real64 *array1, real64 *array2)
65 {
66 int32 i;
67 real64 product = 0.0;
68
69 for (i=0 ; i<len ; ++i, ++array1, ++array2)
70 product += *array1 * *array2;
71
72 return product;
73 }
74
75 /*
76 * Independent calculation of a vector-matrix product.
77 * Nothing fancy, no validation of input. Assumes valid vector & matrix.
78 */
79 static void slow_vector_matrix_product(mtx_matrix_t mtx,
80 struct vector_data *vec,
81 struct vector_data *prod,
82 real64 scale)
83 {
84 int32 row, col;
85 mtx_coord_t coord;
86 int32 limit = vec->rng->high;
87
88 coord.row = vec->rng->low;
89 for (row=vec->rng->low ; row<=limit ; ++row) {
90 coord.col = vec->rng->low;
91 prod->vec[coord.row] = 0.0;
92 for (col=vec->rng->low ; col<=limit ; ++col) {
93 prod->vec[coord.row] += vec->vec[coord.col] * mtx_value(mtx, &coord);
94 ++coord.col;
95 }
96 prod->vec[coord.row] *= scale;
97 ++coord.row;
98 }
99 }
100
101 /* int comparison function for list searches */
102 static int compare_int32s(CONST VOIDPTR p1, CONST VOIDPTR p2)
103 {
104 assert((NULL != p1) && (NULL != p2));
105 return *((int32*)p1) - *((int32*)p2);
106 }
107
108
109 /*
110 * This function tests the slv_common.c functions and data structures.
111 * Note that some of the implementation declarated in slv_common.h is
112 * defined in slv.c rather than slv_common.c. This subset of slv_common.h
113 * will be tested along with slv.c elsewhere.
114 */
115 static void test_slv_common(void)
116 {
117 struct vector_data *pvec1;
118 struct vector_data *pvec2;
119 struct vector_data *pvec3;
120 mtx_matrix_t mtx;
121 mtx_coord_t coord;
122 mtx_region_t region;
123 real64 rarray[100];
124 real64 rarray2[100];
125 int i;
126 FILE *file_normal;
127 int32 hi[11];
128 int32 hj[11];
129 int32 **lnkmap;
130 int32 *lnkvars;
131 struct gl_list_t *col_list;
132 struct gl_list_t *lnkindex_list;
133 int32 lnkindexes[11];
134 unsigned int pos;
135 unsigned long prior_meminuse;
136 unsigned long cur_meminuse;
137 unsigned long test_meminuse;
138 int i_initialized_lists = FALSE;
139 int i_enabled_printing = FALSE;
140
141 #ifdef NDEBUG
142 CU_FAIL("test_slv_common() compiled with NDEBUG - some features not tested.");
143 #endif
144 #ifndef MALLOC_DEBUG
145 CU_FAIL("test_slv_common() compiled without MALLOC_DEBUG - memory management not tested.");
146 #endif
147
148 prior_meminuse = ascmeminuse();
149
150 /* set up pooling & recycling */
151 if (FALSE == gl_pool_initialized()) {
152 gl_init();
153 gl_init_pool();
154 i_initialized_lists = TRUE;
155 }
156
157 for (i=0 ; i<100 ; ++i) { /* create some reals to use later */
158 rarray[i] = 7/2 * i;
159 }
160
161 /* test slv_create_vector(), slv_destroy_vector() */
162
163 test_meminuse = ascmeminuse();
164
165 cur_meminuse = ascmeminuse();
166 pvec1 = slv_create_vector(-1, 0); /* error - low < 0 */
167 CU_TEST(NULL == pvec1);
168
169 slv_destroy_vector(pvec1);
170 CU_TEST(cur_meminuse == ascmeminuse());
171
172 cur_meminuse = ascmeminuse();
173 pvec1 = slv_create_vector(0, -1); /* error - high < 0 */
174 CU_TEST(NULL == pvec1);
175
176 slv_destroy_vector(pvec1);
177 CU_TEST(cur_meminuse == ascmeminuse());
178
179 cur_meminuse = ascmeminuse();
180 pvec1 = slv_create_vector(10, 0); /* error - low > high */
181 CU_TEST(NULL == pvec1);
182
183 slv_destroy_vector(pvec1);
184 CU_TEST(cur_meminuse == ascmeminuse());
185
186 cur_meminuse = ascmeminuse();
187 pvec1 = slv_create_vector(0, 0); /* ok - low == high */
188 CU_TEST_FATAL(NULL != pvec1);
189 CU_TEST_FATAL(NULL != pvec1->rng);
190 CU_TEST(0 == pvec1->rng->low);
191 CU_TEST(0 == pvec1->rng->high);
192 CU_TEST(NULL != pvec1->vec);
193 CU_TEST(FALSE == pvec1->accurate);
194 #ifdef MALLOC_DEBUG
195 CU_TEST(2 == AllocatedMemory(pvec1, sizeof(struct vector_data)));
196 CU_TEST(2 == AllocatedMemory(pvec1->rng, sizeof(mtx_range_t)));
197 CU_TEST(2 == AllocatedMemory(pvec1->vec, sizeof(real64)));
198 #else
199 CU_TEST(1 == AllocatedMemory(pvec1, sizeof(struct vector_data)));
200 CU_TEST(1 == AllocatedMemory(pvec1->rng, sizeof(mtx_range_t)));
201 CU_TEST(1 == AllocatedMemory(pvec1->vec, sizeof(real64)));
202 #endif
203
204 slv_destroy_vector(pvec1);
205 #ifdef MALLOC_DEBUG
206 CU_TEST(0 == AllocatedMemory(pvec1, 0));
207 #else
208 CU_TEST(1 == AllocatedMemory(pvec1, 0));
209 #endif
210 CU_TEST(cur_meminuse == ascmeminuse());
211
212 cur_meminuse = ascmeminuse();
213 pvec1 = slv_create_vector(0, 10); /* ok - low < high */
214 CU_TEST_FATAL(NULL != pvec1);
215 CU_TEST_FATAL(NULL != pvec1->rng);
216 CU_TEST(0 == pvec1->rng->low);
217 CU_TEST(10 == pvec1->rng->high);
218 CU_TEST(NULL != pvec1->vec);
219 CU_TEST(FALSE == pvec1->accurate);
220 #ifdef MALLOC_DEBUG
221 CU_TEST(2 == AllocatedMemory(pvec1, sizeof(struct vector_data)));
222 CU_TEST(2 == AllocatedMemory(pvec1->rng, sizeof(mtx_range_t)));
223 CU_TEST(2 == AllocatedMemory(pvec1->vec, 11 * sizeof(real64)));
224 #else
225 CU_TEST(1 == AllocatedMemory(pvec1, sizeof(struct vector_data)));
226 CU_TEST(1 == AllocatedMemory(pvec1->rng, sizeof(mtx_range_t)));
227 CU_TEST(1 == AllocatedMemory(pvec1->vec, 11 * sizeof(real64)));
228 #endif
229
230 slv_destroy_vector(pvec1);
231 #ifdef MALLOC_DEBUG
232 CU_TEST(0 == AllocatedMemory(pvec1, 0));
233 #else
234 CU_TEST(1 == AllocatedMemory(pvec1, 0));
235 #endif
236 CU_TEST(cur_meminuse == ascmeminuse());
237
238 CU_TEST(test_meminuse == ascmeminuse());
239
240 /* test slv_init_vector() */
241
242 test_meminuse = ascmeminuse();
243
244 cur_meminuse = ascmeminuse();
245 CU_TEST(2 == slv_init_vector(NULL, 0, 10)); /* error - NULL vec */
246 CU_TEST(cur_meminuse == ascmeminuse());
247
248 cur_meminuse = ascmeminuse();
249 pvec1 = (struct vector_data *)ascmalloc(sizeof(struct vector_data)); /* create a vector with NULL rng, vec */
250 CU_TEST_FATAL(NULL != pvec1);
251 pvec1->rng = NULL;
252 pvec1->vec = NULL;
253 pvec1->accurate = TRUE;
254
255 CU_TEST(1 == slv_init_vector(pvec1, -1, 10)); /* error - low < 0 */
256 CU_TEST(NULL == pvec1->rng);
257 CU_TEST(NULL == pvec1->vec);
258 CU_TEST(TRUE == pvec1->accurate);
259 #ifdef MALLOC_DEBUG
260 CU_TEST(2 == AllocatedMemory(pvec1, sizeof(struct vector_data)));
261 #else
262 CU_TEST(1 == AllocatedMemory(pvec1, sizeof(struct vector_data)));
263 #endif
264
265 slv_destroy_vector(pvec1);
266 #ifdef MALLOC_DEBUG
267 CU_TEST(0 == AllocatedMemory(pvec1, 0));
268 #else
269 CU_TEST(1 == AllocatedMemory(pvec1, 0));
270 #endif
271 CU_TEST(cur_meminuse == ascmeminuse());
272
273 cur_meminuse = ascmeminuse();
274 pvec1 = (struct vector_data *)ascmalloc(sizeof(struct vector_data)); /* create a vector with NULL rng, vec */
275 CU_TEST_FATAL(NULL != pvec1);
276 pvec1->rng = NULL;
277 pvec1->vec = NULL;
278 pvec1->accurate = TRUE;
279
280 CU_TEST(1 == slv_init_vector(pvec1, 10, -1)); /* error - high < 0 */
281 CU_TEST(NULL == pvec1->rng);
282 CU_TEST(NULL == pvec1->vec);
283 CU_TEST(TRUE == pvec1->accurate);
284 #ifdef MALLOC_DEBUG
285 CU_TEST(2 == AllocatedMemory(pvec1, sizeof(struct vector_data)));
286 #else
287 CU_TEST(1 == AllocatedMemory(pvec1, sizeof(struct vector_data)));
288 #endif
289
290 slv_destroy_vector(pvec1);
291 #ifdef MALLOC_DEBUG
292 CU_TEST(0 == AllocatedMemory(pvec1, 0));
293 #else
294 CU_TEST(1 == AllocatedMemory(pvec1, 0));
295 #endif
296 CU_TEST(cur_meminuse == ascmeminuse());
297
298 cur_meminuse = ascmeminuse();
299 pvec1 = (struct vector_data *)ascmalloc(sizeof(struct vector_data)); /* create a vector with NULL rng, vec */
300 CU_TEST_FATAL(NULL != pvec1);
301 pvec1->rng = NULL;
302 pvec1->vec = NULL;
303 pvec1->accurate = TRUE;
304
305 CU_TEST(0 == slv_init_vector(pvec1, 10, 10)); /* ok - low == high */
306 CU_TEST_FATAL(NULL != pvec1->rng);
307 CU_TEST(10 == pvec1->rng->low);
308 CU_TEST(10 == pvec1->rng->high);
309 CU_TEST(NULL != pvec1->vec);
310 CU_TEST(FALSE == pvec1->accurate);
311 #ifdef MALLOC_DEBUG
312 CU_TEST(2 == AllocatedMemory(pvec1, sizeof(struct vector_data)));
313 CU_TEST(2 == AllocatedMemory(pvec1->rng, sizeof(mtx_range_t)));
314 CU_TEST(2 == AllocatedMemory(pvec1->vec, 11 * sizeof(real64)));
315 #else
316 CU_TEST(1 == AllocatedMemory(pvec1, sizeof(struct vector_data)));
317 CU_TEST(1 == AllocatedMemory(pvec1->rng, sizeof(mtx_range_t)));
318 CU_TEST(1 == AllocatedMemory(pvec1->vec, 11 * sizeof(real64)));
319 #endif
320
321 slv_destroy_vector(pvec1);
322 #ifdef MALLOC_DEBUG
323 CU_TEST(0 == AllocatedMemory(pvec1, 0));
324 #else
325 CU_TEST(1 == AllocatedMemory(pvec1, 0));
326 #endif
327 CU_TEST(cur_meminuse == ascmeminuse());
328
329 cur_meminuse = ascmeminuse();
330 pvec1 = (struct vector_data *)ascmalloc(sizeof(struct vector_data)); /* create a vector with NULL rng, vec */
331 CU_TEST_FATAL(NULL != pvec1);
332 pvec1->rng = NULL;
333 pvec1->vec = NULL;
334 pvec1->accurate = TRUE;
335
336 CU_TEST(0 == slv_init_vector(pvec1, 10, 100)); /* ok - low < high */
337 CU_TEST_FATAL(NULL != pvec1->rng);
338 CU_TEST(10 == pvec1->rng->low);
339 CU_TEST(100 == pvec1->rng->high);
340 CU_TEST(NULL != pvec1->vec);
341 CU_TEST(FALSE == pvec1->accurate);
342 #ifdef MALLOC_DEBUG
343 CU_TEST(2 == AllocatedMemory(pvec1, sizeof(struct vector_data)));
344 CU_TEST(2 == AllocatedMemory(pvec1->rng, sizeof(mtx_range_t)));
345 CU_TEST(2 == AllocatedMemory(pvec1->vec, 101 * sizeof(real64)));
346 #else
347 CU_TEST(1 == AllocatedMemory(pvec1, sizeof(struct vector_data)));
348 CU_TEST(1 == AllocatedMemory(pvec1->rng, sizeof(mtx_range_t)));
349 CU_TEST(1 == AllocatedMemory(pvec1->vec, 101 * sizeof(real64)));
350 #endif
351
352 slv_destroy_vector(pvec1);
353 #ifdef MALLOC_DEBUG
354 CU_TEST(0 == AllocatedMemory(pvec1, 0));
355 #else
356 CU_TEST(1 == AllocatedMemory(pvec1, 0));
357 #endif
358 CU_TEST(cur_meminuse == ascmeminuse());
359
360 cur_meminuse = ascmeminuse();
361 pvec1 = slv_create_vector(0,0); /* create a vector with data */
362 CU_TEST_FATAL(NULL != pvec1);
363 #ifdef MALLOC_DEBUG
364 CU_TEST(2 == AllocatedMemory(pvec1, sizeof(struct vector_data)));
365 CU_TEST(2 == AllocatedMemory(pvec1->rng, sizeof(mtx_range_t)));
366 CU_TEST(2 == AllocatedMemory(pvec1->vec, 1 * sizeof(real64)));
367 #else
368 CU_TEST(1 == AllocatedMemory(pvec1, sizeof(struct vector_data)));
369 CU_TEST(1 == AllocatedMemory(pvec1->rng, sizeof(mtx_range_t)));
370 CU_TEST(1 == AllocatedMemory(pvec1->vec, 1 * sizeof(real64)));
371 #endif
372
373 pvec1->accurate = TRUE;
374 pvec1->vec[0] = rarray[0];
375
376 CU_TEST(1 == slv_init_vector(pvec1, -1, 100)); /* error - low < 0 */
377 CU_TEST_FATAL(NULL != pvec1->rng);
378 CU_TEST(0 == pvec1->rng->low);
379 CU_TEST(0 == pvec1->rng->high);
380 CU_TEST_FATAL(NULL != pvec1->vec);
381 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[0], rarray[0], 0.00001);
382 CU_TEST(TRUE == pvec1->accurate);
383 #ifdef MALLOC_DEBUG
384 CU_TEST(2 == AllocatedMemory(pvec1->vec, 1 * sizeof(real64)));
385 #else
386 CU_TEST(1 == AllocatedMemory(pvec1->vec, 1 * sizeof(real64)));
387 #endif
388
389 CU_TEST(1 == slv_init_vector(pvec1, 1, 0)); /* error - high < low */
390 CU_TEST_FATAL(NULL != pvec1->rng);
391 CU_TEST(0 == pvec1->rng->low);
392 CU_TEST(0 == pvec1->rng->high);
393 CU_TEST_FATAL(NULL != pvec1->vec);
394 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[0], rarray[0], 0.00001);
395 CU_TEST(TRUE == pvec1->accurate);
396 #ifdef MALLOC_DEBUG
397 CU_TEST(2 == AllocatedMemory(pvec1->vec, 1 * sizeof(real64)));
398 #else
399 CU_TEST(1 == AllocatedMemory(pvec1->vec, 1 * sizeof(real64)));
400 #endif
401
402 CU_TEST(0 == slv_init_vector(pvec1, 0, 1)); /* ok - high > low */
403 CU_TEST_FATAL(NULL != pvec1->rng);
404 CU_TEST(0 == pvec1->rng->low);
405 CU_TEST(1 == pvec1->rng->high);
406 CU_TEST_FATAL(NULL != pvec1->vec);
407 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[0], rarray[0], 0.00001);
408 CU_TEST(FALSE == pvec1->accurate);
409 #ifdef MALLOC_DEBUG
410 CU_TEST(2 == AllocatedMemory(pvec1->vec, 2 * sizeof(real64)));
411 #else
412 CU_TEST(1 == AllocatedMemory(pvec1->vec, 2 * sizeof(real64)));
413 #endif
414
415 pvec1->accurate = TRUE;
416 pvec1->vec[1] = rarray[1];
417
418 CU_TEST(0 == slv_init_vector(pvec1, 9, 10)); /* ok - high > low */
419 CU_TEST_FATAL(NULL != pvec1->rng);
420 CU_TEST(9 == pvec1->rng->low);
421 CU_TEST(10 == pvec1->rng->high);
422 CU_TEST_FATAL(NULL != pvec1->vec);
423 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[0], rarray[0], 0.00001);
424 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[1], rarray[1], 0.00001);
425 CU_TEST(FALSE == pvec1->accurate);
426 #ifdef MALLOC_DEBUG
427 CU_TEST(2 == AllocatedMemory(pvec1->vec, 11 * sizeof(real64)));
428 #else
429 CU_TEST(1 == AllocatedMemory(pvec1->vec, 11 * sizeof(real64)));
430 #endif
431
432 slv_destroy_vector(pvec1);
433 #ifdef MALLOC_DEBUG
434 CU_TEST(0 == AllocatedMemory(pvec1, 0));
435 #else
436 CU_TEST(1 == AllocatedMemory(pvec1, 0));
437 #endif
438 CU_TEST(cur_meminuse == ascmeminuse());
439
440 CU_TEST(test_meminuse == ascmeminuse());
441
442 /* test slv_zero_vector() */
443
444 test_meminuse = ascmeminuse();
445
446 #ifndef ASC_NO_ASSERTIONS
447 asc_assert_catch(TRUE); /* prepare to test assertions */
448
449 asc_assert_reset();
450 if (0 == setjmp(g_asc_test_env))
451 slv_zero_vector(NULL); /* error - NULL vec */
452 CU_TEST(TRUE == asc_assert_failed());
453
454 pvec1 = (struct vector_data *)ascmalloc(sizeof(struct vector_data)); /* create a vector with NULL rng */
455 CU_TEST_FATAL(NULL != pvec1);
456 pvec1->rng = NULL;
457 pvec1->vec = (real64 *)ascmalloc(10 * sizeof(real64));
458
459 asc_assert_reset();
460 if (0 == setjmp(g_asc_test_env))
461 slv_zero_vector(pvec1); /* error - NULL vec->rng */
462 CU_TEST(TRUE == asc_assert_failed());
463
464 pvec1->rng = (mtx_range_t *)ascmalloc(sizeof(mtx_range_t));
465 ascfree(pvec1->vec);
466 pvec1->vec = NULL;
467
468 asc_assert_reset();
469 if (0 == setjmp(g_asc_test_env))
470 slv_zero_vector(pvec1); /* error - NULL vec->vec */
471 CU_TEST(TRUE == asc_assert_failed());
472
473 pvec1->vec = (real64 *)ascmalloc(10 * sizeof(real64));
474 pvec1->rng->low = -1;
475 pvec1->rng->high = 10;
476
477 asc_assert_reset();
478 if (0 == setjmp(g_asc_test_env))
479 slv_zero_vector(pvec1); /* error - low < 0 */
480 CU_TEST(TRUE == asc_assert_failed());
481
482 pvec1->rng->low = 11;
483
484 asc_assert_reset();
485 if (0 == setjmp(g_asc_test_env))
486 slv_zero_vector(pvec1); /* error - low > high */
487 CU_TEST(TRUE == asc_assert_failed());
488
489 slv_destroy_vector(pvec1);
490
491 asc_assert_catch(FALSE); /* done testing assertions */
492 #endif /* !ASC_NO_ASSERTIONS */
493
494 pvec1 = slv_create_vector(0,0); /* create & initialize a 1-element vector */
495 CU_TEST_FATAL(NULL != pvec1);
496
497 pvec1->vec[0] = rarray[0];
498 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[0], rarray[0], 0.00001);
499
500 slv_zero_vector(pvec1);
501 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[0], 0.0, 0.00001);
502
503 CU_TEST_FATAL(0 == slv_init_vector(pvec1, 0, 9)); /* redimension to larger vector */
504
505 for (i=0 ; i<10 ; ++i) {
506 pvec1->vec[i] = rarray[i]; /* initialize & check the data */
507 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[i], rarray[i], 0.00001);
508 }
509
510 slv_zero_vector(pvec1);
511 for (i=0 ; i<10 ; ++i) {
512 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[i], 0.0, 0.00001); /* all data should now be 0.0 */
513 }
514
515 for (i=0 ; i<10 ; ++i) {
516 pvec1->vec[i] = rarray[i]; /* initialize again */
517 }
518
519 pvec1->rng->low = 5;
520 pvec1->rng->high = 7;
521
522 slv_zero_vector(pvec1);
523 for (i=0 ; i<5 ; ++i) {
524 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[i], rarray[i], 0.00001);
525 }
526 for (i=5 ; i<8 ; ++i) {
527 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[i], 0.0, 0.00001);
528 }
529 for (i=8 ; i<10 ; ++i) {
530 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[i], rarray[i], 0.00001);
531 }
532
533 slv_destroy_vector(pvec1);
534
535 CU_TEST(test_meminuse == ascmeminuse());
536
537 /* test slv_copy_vector() */
538
539 test_meminuse = ascmeminuse();
540
541 #ifndef ASC_NO_ASSERTIONS
542 asc_assert_catch(TRUE); /* prepare to test assertions */
543
544 pvec1 = slv_create_vector(0,10);
545 CU_TEST_FATAL(NULL != pvec1);
546
547 asc_assert_reset();
548 if (0 == setjmp(g_asc_test_env))
549 slv_copy_vector(NULL, pvec1); /* error - NULL srcvec */
550 CU_TEST(TRUE == asc_assert_failed());
551
552 asc_assert_reset();
553 if (0 == setjmp(g_asc_test_env))
554 slv_copy_vector(pvec1, NULL); /* error - NULL destvec */
555 CU_TEST(TRUE == asc_assert_failed());
556
557 pvec2 = (struct vector_data *)ascmalloc(sizeof(struct vector_data)); /* create a vector with NULL rng */
558 CU_TEST_FATAL(NULL != pvec2);
559 pvec2->rng = NULL;
560 pvec2->vec = (real64 *)ascmalloc(10 * sizeof(real64));
561
562 asc_assert_reset();
563 if (0 == setjmp(g_asc_test_env))
564 slv_copy_vector(pvec2, pvec1); /* error - NULL srcvec->rng */
565 CU_TEST(TRUE == asc_assert_failed());
566
567 asc_assert_reset();
568 if (0 == setjmp(g_asc_test_env))
569 slv_copy_vector(pvec1, pvec2); /* error - NULL destvec->rng */
570 CU_TEST(TRUE == asc_assert_failed());
571
572 pvec2->rng = (mtx_range_t *)ascmalloc(sizeof(mtx_range_t));
573 ascfree(pvec2->vec);
574 pvec2->vec = NULL;
575
576 asc_assert_reset();
577 if (0 == setjmp(g_asc_test_env))
578 slv_copy_vector(pvec2, pvec1); /* error - NULL srcvec->vec */
579 CU_TEST(TRUE == asc_assert_failed());
580
581 asc_assert_reset();
582 if (0 == setjmp(g_asc_test_env))
583 slv_copy_vector(pvec1, pvec2); /* error - NULL destvec->vec */
584 CU_TEST(TRUE == asc_assert_failed());
585
586 pvec2->vec = (real64 *)ascmalloc(10 * sizeof(real64));
587 pvec2->rng->low = -1;
588 pvec2->rng->high = 10;
589
590 asc_assert_reset();
591 if (0 == setjmp(g_asc_test_env))
592 slv_copy_vector(pvec2, pvec1); /* error - srcvec->rng->low < 0 */
593 CU_TEST(TRUE == asc_assert_failed());
594
595 asc_assert_reset();
596 if (0 == setjmp(g_asc_test_env))
597 slv_copy_vector(pvec1, pvec2); /* error - destvec->rng->low < 0 */
598 CU_TEST(TRUE == asc_assert_failed());
599
600 pvec2->rng->low = 11;
601
602 asc_assert_reset();
603 if (0 == setjmp(g_asc_test_env))
604 slv_copy_vector(pvec2, pvec1); /* error - srcvec low > high */
605 CU_TEST(TRUE == asc_assert_failed());
606
607 slv_destroy_vector(pvec1);
608 slv_destroy_vector(pvec2);
609
610 asc_assert_catch(FALSE); /* done testing assertions */
611 #endif /* !ASC_NO_ASSERTIONS */
612
613 pvec1 = slv_create_vector(0,0); /* create & initialize a 1-element vectors */
614 pvec2 = slv_create_vector(0,0);
615 CU_TEST_FATAL(NULL != pvec1);
616
617 pvec1->vec[0] = rarray[0];
618 pvec2->vec[0] = rarray[5];
619 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[0], rarray[0], 0.00001);
620 CU_ASSERT_DOUBLE_EQUAL(pvec2->vec[0], rarray[5], 0.00001);
621
622 slv_copy_vector(pvec1, pvec2);
623 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[0], rarray[0], 0.00001);
624 CU_ASSERT_DOUBLE_EQUAL(pvec2->vec[0], rarray[0], 0.00001);
625
626 CU_TEST_FATAL(0 == slv_init_vector(pvec1, 0, 9)); /* redimension pvec1 to larger vector */
627
628 for (i=0 ; i<10 ; ++i) {
629 pvec1->vec[i] = rarray[i]; /* initialize & check the data */
630 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[i], rarray[i], 0.00001);
631 }
632
633 pvec2->vec[0] = rarray[8];
634 slv_copy_vector(pvec2, pvec1); /* copy 1 element*/
635 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[0], rarray[8], 0.00001);
636 CU_ASSERT_DOUBLE_EQUAL(pvec2->vec[0], rarray[8], 0.00001);
637 for (i=1 ; i<10 ; ++i) {
638 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[i], rarray[i], 0.00001); /* rest of data should be intact */
639 }
640
641 pvec2->vec[0] = rarray[3];
642 pvec1->rng->low = 9;
643 pvec1->rng->high = 9;
644 slv_copy_vector(pvec1, pvec2); /* copy 1 element other way*/
645 CU_ASSERT_DOUBLE_EQUAL(pvec2->vec[0], rarray[9], 0.00001);
646 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[0], rarray[8], 0.00001);
647 for (i=1 ; i<10 ; ++i) {
648 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[i], rarray[i], 0.00001); /* data in src should be intact */
649 }
650
651 CU_TEST_FATAL(0 == slv_init_vector(pvec2, 0, 9)); /* redimension pvec2 to larger vector */
652 slv_zero_vector(pvec2); /* zero the destvec */
653 pvec1->rng->low = 0;
654 pvec1->rng->high = 9;
655 slv_copy_vector(pvec1, pvec2); /* copy all elements */
656 for (i=0 ; i<10 ; ++i) {
657 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[i], pvec2->vec[i], 0.00001); /* data should be the same */
658 }
659
660 for (i=0 ; i<10 ; ++i) {
661 pvec2->vec[i] = rarray[9-i]; /* reinitialize & check the data */
662 }
663 pvec2->rng->low = 3;
664 pvec2->rng->high = 6;
665 slv_copy_vector(pvec2, pvec1); /* copy a subset of elements to start of destvec */
666 for (i=3 ; i<7 ; ++i) {
667 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[i-3], rarray[9-i], 0.00001); /* data should be the same */
668 }
669 for (i=4 ; i<10 ; ++i) {
670 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[i], rarray[i], 0.00001); /* data should be the same */
671 }
672
673 slv_destroy_vector(pvec1);
674 slv_destroy_vector(pvec2);
675
676 CU_TEST(test_meminuse == ascmeminuse());
677
678 /* test slv_inner_product() */
679
680 test_meminuse = ascmeminuse();
681
682 #ifndef ASC_NO_ASSERTIONS
683 asc_assert_catch(TRUE); /* prepare to test assertions */
684
685 pvec1 = slv_create_vector(0,10);
686 CU_TEST_FATAL(NULL != pvec1);
687
688 asc_assert_reset();
689 if (0 == setjmp(g_asc_test_env))
690 slv_inner_product(NULL, pvec1); /* error - NULL vec1 */
691 CU_TEST(TRUE == asc_assert_failed());
692
693 asc_assert_reset();
694 if (0 == setjmp(g_asc_test_env))
695 slv_inner_product(pvec1, NULL); /* error - NULL vec2 */
696 CU_TEST(TRUE == asc_assert_failed());
697
698 pvec2 = (struct vector_data *)ascmalloc(sizeof(struct vector_data)); /* create a vector with NULL rng */
699 CU_TEST_FATAL(NULL != pvec2);
700 pvec2->rng = NULL;
701 pvec2->vec = (real64 *)ascmalloc(10 * sizeof(real64));
702
703 asc_assert_reset();
704 if (0 == setjmp(g_asc_test_env))
705 slv_inner_product(pvec2, pvec1); /* error - NULL vec1->rng */
706 CU_TEST(TRUE == asc_assert_failed());
707
708 asc_assert_reset();
709 if (0 == setjmp(g_asc_test_env))
710 slv_inner_product(pvec1, pvec2); /* error - NULL vec2->rng */
711 CU_TEST(TRUE == asc_assert_failed());
712
713 pvec2->rng = (mtx_range_t *)ascmalloc(sizeof(mtx_range_t));
714 ascfree(pvec2->vec);
715 pvec2->vec = NULL;
716
717 asc_assert_reset();
718 if (0 == setjmp(g_asc_test_env))
719 slv_inner_product(pvec2, pvec1); /* error - NULL vec1->vec */
720 CU_TEST(TRUE == asc_assert_failed());
721
722 asc_assert_reset();
723 if (0 == setjmp(g_asc_test_env))
724 slv_inner_product(pvec1, pvec2); /* error - NULL vec2->vec */
725 CU_TEST(TRUE == asc_assert_failed());
726
727 pvec2->vec = (real64 *)ascmalloc(10 * sizeof(real64));
728 pvec2->rng->low = -1;
729 pvec2->rng->high = 10;
730
731 asc_assert_reset();
732 if (0 == setjmp(g_asc_test_env))
733 slv_inner_product(pvec2, pvec1); /* error - vec1->rng->low < 0 */
734 CU_TEST(TRUE == asc_assert_failed());
735
736 asc_assert_reset();
737 if (0 == setjmp(g_asc_test_env))
738 slv_inner_product(pvec1, pvec2); /* error - vec2->rng->low < 0 */
739 CU_TEST(TRUE == asc_assert_failed());
740
741 pvec2->rng->low = 11;
742
743 asc_assert_reset();
744 if (0 == setjmp(g_asc_test_env))
745 slv_inner_product(pvec2, pvec1); /* error - vec1 low > high */
746 CU_TEST(TRUE == asc_assert_failed());
747
748 slv_destroy_vector(pvec1);
749 slv_destroy_vector(pvec2);
750
751 asc_assert_catch(FALSE); /* done testing assertions */
752 #endif /* !ASC_NO_ASSERTIONS */
753
754 pvec1 = slv_create_vector(0,0); /* create & initialize a 1-element vectors */
755 pvec2 = slv_create_vector(0,0);
756 CU_TEST_FATAL(NULL != pvec1);
757 CU_TEST_FATAL(NULL != pvec2);
758
759 pvec1->vec[0] = rarray[0];
760 pvec2->vec[0] = rarray[5];
761 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[0], rarray[0], 0.00001);
762 CU_ASSERT_DOUBLE_EQUAL(pvec2->vec[0], rarray[5], 0.00001);
763
764 CU_ASSERT_DOUBLE_EQUAL(slv_inner_product(pvec1, pvec2), slow_inner_product(pvec1, pvec2), 0.00001);
765 CU_ASSERT_DOUBLE_EQUAL(slv_inner_product(pvec2, pvec1), slow_inner_product(pvec1, pvec2), 0.00001);
766 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[0], rarray[0], 0.00001);
767 CU_ASSERT_DOUBLE_EQUAL(pvec2->vec[0], rarray[5], 0.00001);
768
769 CU_TEST_FATAL(0 == slv_init_vector(pvec1, 0, 9)); /* redimension vectors larger */
770 CU_TEST_FATAL(0 == slv_init_vector(pvec2, 0, 9));
771
772 for (i=0 ; i<10 ; ++i) {
773 pvec1->vec[i] = rarray[i]; /* initialize & check the data */
774 pvec2->vec[i] = 2.0;
775 }
776 /* check entire vectors */
777 CU_ASSERT_DOUBLE_EQUAL(slv_inner_product(pvec1, pvec2), slow_inner_product(pvec1, pvec2), 0.00001);
778 CU_ASSERT_DOUBLE_EQUAL(slv_inner_product(pvec2, pvec1), slow_inner_product(pvec1, pvec2), 0.00001);
779
780 pvec1->rng->low = 9;
781 pvec1->rng->high = 9;
782 pvec2->rng->low = 5;
783 pvec2->rng->high = 5; /* check 1 element subrange */
784 CU_ASSERT_DOUBLE_EQUAL(slv_inner_product(pvec1, pvec2), slow_inner_product(pvec1, pvec2), 0.00001);
785 CU_ASSERT_DOUBLE_EQUAL(slv_inner_product(pvec2, pvec1), slow_inner_product(pvec1, pvec2), 0.00001);
786
787 pvec1->rng->low = 0;
788 pvec1->rng->high = 3;
789 pvec2->rng->low = 2;
790 pvec2->rng->high = 5; /* check 4 element subrange */
791 CU_ASSERT_DOUBLE_EQUAL(slv_inner_product(pvec1, pvec2), slow_inner_product(pvec1, pvec2), 0.00001);
792 CU_ASSERT_DOUBLE_EQUAL(slv_inner_product(pvec2, pvec1), slow_inner_product(pvec1, pvec2), 0.00001);
793
794 for (i=1 ; i<10 ; ++i) {
795 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[i], rarray[i], 0.00001); /* data in vecs should be intact */
796 CU_ASSERT_DOUBLE_EQUAL(pvec2->vec[i], 2.0, 0.00001);
797 }
798
799 for (i=0 ; i<10 ; ++i) {
800 pvec1->vec[i] = rarray[i]; /* initialize & check the data */
801 pvec2->vec[i] = rarray[9-i];
802 }
803 /* check entire vectors */
804 CU_ASSERT_DOUBLE_EQUAL(slv_inner_product(pvec1, pvec2), slow_inner_product(pvec1, pvec2), 0.00001);
805 CU_ASSERT_DOUBLE_EQUAL(slv_inner_product(pvec2, pvec1), slow_inner_product(pvec1, pvec2), 0.00001);
806
807 slv_destroy_vector(pvec1);
808 slv_destroy_vector(pvec2);
809
810 CU_TEST(test_meminuse == ascmeminuse());
811
812 /* test slv_square_norm() */
813
814 test_meminuse = ascmeminuse();
815
816 #ifndef ASC_NO_ASSERTIONS
817 asc_assert_catch(TRUE); /* prepare to test assertions */
818
819 asc_assert_reset();
820 if (0 == setjmp(g_asc_test_env))
821 slv_square_norm(NULL); /* error - NULL vec */
822 CU_TEST(TRUE == asc_assert_failed());
823
824 pvec1 = (struct vector_data *)ascmalloc(sizeof(struct vector_data)); /* create a vector with NULL rng */
825 CU_TEST_FATAL(NULL != pvec1);
826 pvec1->rng = NULL;
827 pvec1->vec = (real64 *)ascmalloc(10 * sizeof(real64));
828
829 asc_assert_reset();
830 if (0 == setjmp(g_asc_test_env))
831 slv_square_norm(pvec1); /* error - NULL vec->rng */
832 CU_TEST(TRUE == asc_assert_failed());
833
834 pvec1->rng = (mtx_range_t *)ascmalloc(sizeof(mtx_range_t));
835 ascfree(pvec1->vec);
836 pvec1->vec = NULL;
837
838 asc_assert_reset();
839 if (0 == setjmp(g_asc_test_env))
840 slv_square_norm(pvec1); /* error - NULL vec->vec */
841 CU_TEST(TRUE == asc_assert_failed());
842
843 pvec1->vec = (real64 *)ascmalloc(10 * sizeof(real64));
844 pvec1->rng->low = -1;
845 pvec1->rng->high = 10;
846
847 asc_assert_reset();
848 if (0 == setjmp(g_asc_test_env))
849 slv_square_norm(pvec1); /* error - vec->rng->low < 0 */
850 CU_TEST(TRUE == asc_assert_failed());
851
852 pvec1->rng->low = 11;
853
854 asc_assert_reset();
855 if (0 == setjmp(g_asc_test_env))
856 slv_square_norm(pvec1); /* error - vec low > high */
857 CU_TEST(TRUE == asc_assert_failed());
858
859 slv_destroy_vector(pvec1);
860
861 asc_assert_catch(FALSE); /* done testing assertions */
862 #endif /* !ASC_NO_ASSERTIONS */
863
864 pvec1 = slv_create_vector(0,0); /* create & initialize a 1-element vector */
865 CU_TEST_FATAL(NULL != pvec1);
866
867 pvec1->vec[0] = 0.0;
868 CU_ASSERT_DOUBLE_EQUAL(slv_square_norm(pvec1), slow_inner_product(pvec1, pvec1), 0.00001);
869 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[0], 0.0, 0.00001);
870
871 pvec1->vec[0] = rarray[7];
872 CU_ASSERT_DOUBLE_EQUAL(slv_square_norm(pvec1), slow_inner_product(pvec1, pvec1), 0.00001);
873 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[0], rarray[7], 0.00001);
874
875 CU_TEST_FATAL(0 == slv_init_vector(pvec1, 0, 9)); /* redimension vectors larger */
876
877 for (i=0 ; i<10 ; ++i) {
878 pvec1->vec[i] = rarray[i]; /* initialize the data */
879 }
880 /* check entire vectors */
881 CU_ASSERT_DOUBLE_EQUAL(slv_square_norm(pvec1), slow_inner_product(pvec1, pvec1), 0.00001);
882
883 pvec1->rng->low = 9;
884 pvec1->rng->high = 9;
885 CU_ASSERT_DOUBLE_EQUAL(slv_square_norm(pvec1), slow_inner_product(pvec1, pvec1), 0.00001);
886
887 pvec1->rng->low = 0;
888 pvec1->rng->high = 3;
889 CU_ASSERT_DOUBLE_EQUAL(slv_square_norm(pvec1), slow_inner_product(pvec1, pvec1), 0.00001);
890
891 for (i=1 ; i<10 ; ++i) {
892 CU_ASSERT_DOUBLE_EQUAL(pvec1->vec[i], rarray[i], 0.00001); /* data in vecs should be intact */
893 }
894
895 slv_destroy_vector(pvec1);
896
897 CU_TEST(test_meminuse == ascmeminuse());
898
899 /* test slv_matrix_product() */
900
901 test_meminuse = ascmeminuse();
902
903 #ifndef ASC_NO_ASSERTIONS
904 asc_assert_catch(TRUE); /* prepare to test assertions */
905
906 mtx = mtx_create();
907 CU_TEST_FATAL(NULL != mtx);
908 mtx_set_order(mtx, 10);
909 pvec1 = (struct vector_data *)ascmalloc(sizeof(struct vector_data));
910 CU_TEST_FATAL(NULL != pvec1);
911 pvec1->rng = (mtx_range_t *)ascmalloc(sizeof(mtx_range_t));
912 pvec1->rng->low = 0;
913 pvec1->rng->high = 10;
914 pvec1->vec = (real64 *)ascmalloc(11 * sizeof(real64));
915 pvec2 = (struct vector_data *)ascmalloc(sizeof(struct vector_data));
916 CU_TEST_FATAL(NULL != pvec2);
917 pvec2->rng = (mtx_range_t *)ascmalloc(sizeof(mtx_range_t));
918 pvec2->rng->low = 0;
919 pvec2->rng->high = 10;
920 pvec2->vec = (real64 *)ascmalloc(11 * sizeof(real64));
921
922 asc_assert_reset();
923 if (0 == setjmp(g_asc_test_env))
924 slv_matrix_product(NULL, pvec1, pvec2, 1.0, FALSE); /* error - NULL mtx */
925 CU_TEST(TRUE == asc_assert_failed());
926
927 asc_assert_reset();
928 if (0 == setjmp(g_asc_test_env))
929 slv_matrix_product(mtx, NULL, pvec2, 1.0, FALSE); /* error - NULL vec */
930 CU_TEST(TRUE == asc_assert_failed());
931
932 asc_assert_reset();
933 if (0 == setjmp(g_asc_test_env))
934 slv_matrix_product(mtx, pvec1, NULL, 1.0, FALSE); /* error - NULL prod */
935 CU_TEST(TRUE == asc_assert_failed());
936
937 ascfree(pvec1->rng);
938 pvec1->rng = NULL;
939
940 asc_assert_reset();
941 if (0 == setjmp(g_asc_test_env))
942 slv_matrix_product(mtx, pvec1, pvec2, 1.0, FALSE); /* error - NULL vec->rng */
943 CU_TEST(TRUE == asc_assert_failed());
944
945 pvec1->rng = (mtx_range_t *)ascmalloc(sizeof(mtx_range_t));
946 pvec1->rng->low = 0;
947 pvec1->rng->high = 10;
948 ascfree(pvec2->rng);
949 pvec2->rng = NULL;
950
951 asc_assert_reset();
952 if (0 == setjmp(g_asc_test_env))
953 slv_matrix_product(mtx, pvec1, pvec2, 1.0, FALSE); /* error - NULL prod->rng */
954 CU_TEST(TRUE == asc_assert_failed());
955
956 pvec2->rng = (mtx_range_t *)ascmalloc(sizeof(mtx_range_t));
957 pvec2->rng->low = 0;
958 pvec2->rng->high = 10;
959 ascfree(pvec1->vec);
960 pvec1->vec = NULL;
961
962 asc_assert_reset();
963 if (0 == setjmp(g_asc_test_env))
964 slv_matrix_product(mtx, pvec1, pvec2, 1.0, FALSE); /* error - NULL vec->vec */
965 CU_TEST(TRUE == asc_assert_failed());
966
967 pvec1->vec = (real64 *)ascmalloc(11 * sizeof(real64));
968 ascfree(pvec2->vec);
969 pvec2->vec = NULL;
970
971 asc_assert_reset();
972 if (0 == setjmp(g_asc_test_env))
973 slv_matrix_product(mtx, pvec1, pvec2, 1.0, FALSE); /* error - NULL prod->vec */
974 CU_TEST(TRUE == asc_assert_failed());
975
976 pvec2->vec = (real64 *)ascmalloc(11 * sizeof(real64));
977 pvec1->rng->low = -1;
978 pvec1->rng->high = 10;
979
980 asc_assert_reset();
981 if (0 == setjmp(g_asc_test_env))
982 slv_matrix_product(mtx, pvec1, pvec2, 1.0, FALSE); /* error - vec low < 0 */
983 CU_TEST(TRUE == asc_assert_failed());
984
985 pvec1->rng->low = 11;
986
987 asc_assert_reset();
988 if (0 == setjmp(g_asc_test_env))
989 slv_matrix_product(mtx, pvec1, pvec2, 1.0, FALSE); /* error - vec low > high */
990 CU_TEST(TRUE == asc_assert_failed());
991
992 pvec1->rng->low = 0;
993 pvec1->rng->high = 10;
994 pvec2->rng->low = -1;
995 pvec2->rng->high = 10;
996
997 asc_assert_reset();
998 if (0 == setjmp(g_asc_test_env))
999 slv_matrix_product(mtx, pvec1, pvec2, 1.0, FALSE); /* error - prod low < 0 */
1000 CU_TEST(TRUE == asc_assert_failed());
1001
1002 pvec2->rng->low = 11;
1003
1004 asc_assert_reset();
1005 if (0 == setjmp(g_asc_test_env))
1006 slv_matrix_product(mtx, pvec1, pvec2, 1.0, FALSE); /* error - prod low > high */
1007 CU_TEST(TRUE == asc_assert_failed());
1008
1009 mtx_destroy(mtx);
1010 slv_destroy_vector(pvec1);
1011 slv_destroy_vector(pvec2);
1012
1013 asc_assert_catch(FALSE); /* done testing assertions */
1014 #endif /* !ASC_NO_ASSERTIONS */
1015
1016 mtx = mtx_create();
1017 mtx_set_order(mtx, 10);
1018
1019 pvec1 = slv_create_vector(0,0);
1020 pvec1->vec[0] = 10.0;
1021
1022 pvec2 = slv_create_vector(0,0);
1023 pvec3 = slv_create_vector(0,0);
1024
1025 slv_matrix_product(mtx, pvec1, pvec2, 1.0, FALSE); /* mtx with all zero's */
1026 slow_vector_matrix_product(mtx, pvec1, pvec3, 1.0); /* 1-element vector */
1027 CU_ASSERT_DOUBLE_EQUAL(pvec2->vec[0], pvec3->vec[0], 0.000001);
1028 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[0], 0.000001);
1029
1030 mtx_fill_value(mtx, mtx_coord(&coord,0,0), 20.0);
1031
1032 slv_matrix_product(mtx, pvec1, pvec2, 1.0, FALSE); /* normal mtx */
1033 slow_vector_matrix_product(mtx, pvec1, pvec3, 1.0); /* 1-element vector */
1034 CU_ASSERT_DOUBLE_EQUAL(pvec2->vec[0], pvec3->vec[0], 0.000001);
1035 CU_ASSERT_DOUBLE_EQUAL(200.0, pvec2->vec[0], 0.000001);
1036
1037 slv_matrix_product(mtx, pvec1, pvec2, 1.0, TRUE); /* transpose should have no effect */
1038 CU_ASSERT_DOUBLE_EQUAL(pvec2->vec[0], pvec3->vec[0], 0.000001);
1039 CU_ASSERT_DOUBLE_EQUAL(200.0, pvec2->vec[0], 0.000001);
1040
1041 mtx_clear(mtx);
1042
1043 slv_init_vector(pvec1,0,1);
1044 pvec1->vec[0] = 10.0;
1045 pvec1->vec[1] = 20.5;
1046
1047 slv_init_vector(pvec2, 0,1);
1048 slv_init_vector(pvec3, 0,1);
1049
1050 slv_matrix_product(mtx, pvec1, pvec2, 1.0, FALSE); /* empty mtx */
1051 slow_vector_matrix_product(mtx, pvec1, pvec3, 1.0); /* 2-element vector */
1052 CU_ASSERT_DOUBLE_EQUAL(pvec2->vec[0], pvec3->vec[0], 0.000001);
1053 CU_ASSERT_DOUBLE_EQUAL(pvec2->vec[1], pvec3->vec[1], 0.000001);
1054 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[0], 0.000001);
1055 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[1], 0.000001);
1056
1057 mtx_fill_value(mtx, mtx_coord(&coord,0,0), 20.0);
1058 mtx_fill_value(mtx, mtx_coord(&coord,0,1), 0.5);
1059 mtx_fill_value(mtx, mtx_coord(&coord,1,1), -0.455);
1060
1061 slv_matrix_product(mtx, pvec1, pvec2, 1.0, FALSE); /* normal mtx, but not all non-zeros */
1062 slow_vector_matrix_product(mtx, pvec1, pvec3, 1.0); /* 2-element vector */
1063 CU_ASSERT_DOUBLE_EQUAL(pvec2->vec[0], pvec3->vec[0], 0.000001);
1064 CU_ASSERT_DOUBLE_EQUAL(pvec2->vec[1], pvec3->vec[1], 0.000001);
1065
1066 slv_matrix_product(mtx, pvec1, pvec2, 1.0, TRUE); /* transpose of normal mtx, not all non-zeros */
1067
1068 mtx_clear(mtx);
1069 mtx_fill_value(mtx, mtx_coord(&coord,0,0), 20.0);
1070 mtx_fill_value(mtx, mtx_coord(&coord,1,0), 0.5);
1071 mtx_fill_value(mtx, mtx_coord(&coord,1,1), -0.455);
1072
1073 slow_vector_matrix_product(mtx, pvec1, pvec3, 1.0); /* confirm transpose works */
1074 CU_ASSERT_DOUBLE_EQUAL(pvec2->vec[0], pvec3->vec[0], 0.000001);
1075 CU_ASSERT_DOUBLE_EQUAL(pvec2->vec[1], pvec3->vec[1], 0.000001);
1076
1077 slv_init_vector(pvec1,0,10);
1078 pvec1->vec[0] = 10.0;
1079 pvec1->vec[1] = 20.0;
1080 pvec1->vec[2] = 30.0;
1081 pvec1->vec[3] = 40.0;
1082 pvec1->vec[4] = 50.0;
1083 pvec1->vec[5] = 60.0;
1084 pvec1->vec[6] = 70.0;
1085 pvec1->vec[7] = 80.0;
1086 pvec1->vec[8] = 90.0;
1087 pvec1->vec[9] = 100.0;
1088 pvec1->vec[10] = 110.0;
1089 pvec1->rng->low = 2; /* only use a subset of vector */
1090 pvec1->rng->high = 4;
1091
1092 slv_init_vector(pvec2, 0,10);
1093 slv_init_vector(pvec3, 0,10);
1094 for (i=0 ; i<11 ; ++i) { /* zero product vecs so can detect subset */
1095 pvec2->vec[i] = 0.0;
1096 pvec3->vec[i] = 0.0;
1097 }
1098
1099 mtx_clear(mtx);
1100 mtx_fill_value(mtx, mtx_coord(&coord,2,2), 1.0); /* only give values in vector range */
1101 mtx_fill_value(mtx, mtx_coord(&coord,2,3), 1.0);
1102 mtx_fill_value(mtx, mtx_coord(&coord,2,4), 1.0);
1103 mtx_fill_value(mtx, mtx_coord(&coord,3,2), 2.0);
1104 mtx_fill_value(mtx, mtx_coord(&coord,3,3), 2.0);
1105 mtx_fill_value(mtx, mtx_coord(&coord,3,4), 2.0);
1106 mtx_fill_value(mtx, mtx_coord(&coord,4,2), 3.0);
1107 mtx_fill_value(mtx, mtx_coord(&coord,4,3), 3.0);
1108 mtx_fill_value(mtx, mtx_coord(&coord,4,4), 3.0);
1109
1110 slv_matrix_product(mtx, pvec1, pvec2, 1.0, FALSE); /* normal mtx */
1111 slow_vector_matrix_product(mtx, pvec1, pvec3, 1.0); /* vector subset*/
1112
1113 for (i=0 ; i<11 ; ++i) {
1114 CU_ASSERT_DOUBLE_EQUAL(pvec2->vec[i], pvec3->vec[i], 0.000001);
1115 }
1116 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[0], 0.000001);
1117 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[1], 0.000001);
1118 CU_ASSERT_DOUBLE_EQUAL(120.0, pvec2->vec[2], 0.000001);
1119 CU_ASSERT_DOUBLE_EQUAL(240.0, pvec2->vec[3], 0.000001);
1120 CU_ASSERT_DOUBLE_EQUAL(360.0, pvec2->vec[4], 0.000001);
1121 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[5], 0.000001);
1122 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[6], 0.000001);
1123 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[7], 0.000001);
1124 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[8], 0.000001);
1125 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[9], 0.000001);
1126 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[10], 0.000001);
1127
1128 slv_matrix_product(mtx, pvec1, pvec2, 0.5, FALSE); /* different scale */
1129
1130 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[0], 0.000001);
1131 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[1], 0.000001);
1132 CU_ASSERT_DOUBLE_EQUAL(60.0, pvec2->vec[2], 0.000001);
1133 CU_ASSERT_DOUBLE_EQUAL(120.0, pvec2->vec[3], 0.000001);
1134 CU_ASSERT_DOUBLE_EQUAL(180.0, pvec2->vec[4], 0.000001);
1135 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[5], 0.000001);
1136 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[6], 0.000001);
1137 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[7], 0.000001);
1138 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[8], 0.000001);
1139 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[9], 0.000001);
1140 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[10], 0.000001);
1141
1142 slv_matrix_product(mtx, pvec1, pvec2, 1.0, TRUE); /* transpose */
1143
1144 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[0], 0.000001);
1145 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[1], 0.000001);
1146 CU_ASSERT_DOUBLE_EQUAL(260.0, pvec2->vec[2], 0.000001);
1147 CU_ASSERT_DOUBLE_EQUAL(260.0, pvec2->vec[3], 0.000001);
1148 CU_ASSERT_DOUBLE_EQUAL(260.0, pvec2->vec[4], 0.000001);
1149 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[5], 0.000001);
1150 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[6], 0.000001);
1151 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[7], 0.000001);
1152 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[8], 0.000001);
1153 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[9], 0.000001);
1154 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[10], 0.000001);
1155
1156 slv_matrix_product(mtx, pvec1, pvec2, 2.0, TRUE); /* transpose */
1157
1158 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[0], 0.000001);
1159 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[1], 0.000001);
1160 CU_ASSERT_DOUBLE_EQUAL(520.0, pvec2->vec[2], 0.000001);
1161 CU_ASSERT_DOUBLE_EQUAL(520.0, pvec2->vec[3], 0.000001);
1162 CU_ASSERT_DOUBLE_EQUAL(520.0, pvec2->vec[4], 0.000001);
1163 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[5], 0.000001);
1164 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[6], 0.000001);
1165 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[7], 0.000001);
1166 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[8], 0.000001);
1167 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[9], 0.000001);
1168 CU_ASSERT_DOUBLE_EQUAL(0.0, pvec2->vec[10], 0.000001);
1169
1170 mtx_destroy(mtx);
1171 slv_destroy_vector(pvec1);
1172 slv_destroy_vector(pvec2);
1173 slv_destroy_vector(pvec3);
1174
1175 CU_TEST(test_meminuse == ascmeminuse());
1176
1177 /* test slv_write_vector() - not much to do but make sure something gets written */
1178
1179 test_meminuse = ascmeminuse();
1180
1181 pvec1 = slv_create_vector(0,10);
1182
1183 if (FALSE == test_printing_enabled()) {
1184 test_enable_printing();
1185 i_enabled_printing = TRUE;
1186 }
1187
1188 if (NULL != (file_normal = fopen("slvcommontempfile1.tmp", "w+"))) {
1189
1190 slv_write_vector(file_normal, pvec1);/* write to normal open file */
1191 rewind(file_normal);
1192 CU_TEST(EOF != fgetc(file_normal)); /* test that file is not empty */
1193 fclose(file_normal);
1194 remove("slvcommontempfile1.tmp");
1195 }
1196 else {
1197 CU_FAIL("Error opening output file 1 in test_slv_common.c");
1198 }
1199
1200 if (TRUE == i_enabled_printing) {
1201 test_disable_printing();
1202 i_enabled_printing = FALSE;
1203 }
1204
1205 slv_destroy_vector(pvec1);
1206
1207 CU_TEST(test_meminuse == ascmeminuse());
1208
1209 /* test slv_dot() */
1210
1211 test_meminuse = ascmeminuse();
1212
1213 #ifndef ASC_NO_ASSERTIONS
1214 asc_assert_catch(TRUE); /* prepare to test assertions */
1215
1216 asc_assert_reset();
1217 if (0 == setjmp(g_asc_test_env))
1218 slv_dot(10, NULL, rarray2); /* error - NULL a1 */
1219 CU_TEST(TRUE == asc_assert_failed());
1220
1221 asc_assert_reset();
1222 if (0 == setjmp(g_asc_test_env))
1223 slv_dot(10, rarray, NULL); /* error - NULL a2 */
1224 CU_TEST(TRUE == asc_assert_failed());
1225
1226 asc_assert_reset();
1227 if (0 == setjmp(g_asc_test_env))
1228 slv_dot(-10, rarray, rarray2); /* error - len < 0 */
1229 CU_TEST(TRUE == asc_assert_failed());
1230
1231 asc_assert_catch(FALSE); /* done testing assertions */
1232 #endif /* !ASC_NO_ASSERTIONS */
1233
1234 rarray2[0] = rarray[5];
1235
1236 CU_ASSERT_DOUBLE_EQUAL(slv_dot(1, rarray, rarray2), slow_dot_product(1, rarray, rarray2), 0.00001);
1237 CU_ASSERT_DOUBLE_EQUAL(slv_dot(1, rarray2, rarray), slow_dot_product(1, rarray, rarray2), 0.00001);
1238
1239 for (i=0 ; i<10 ; ++i) {
1240 rarray2[i] = 2.0;
1241 }
1242
1243 CU_ASSERT_DOUBLE_EQUAL(slv_dot(11, rarray, rarray2), slow_dot_product(11, rarray, rarray2), 0.00001);
1244 CU_ASSERT_DOUBLE_EQUAL(slv_dot(11, rarray2, rarray), slow_dot_product(11, rarray, rarray2), 0.00001);
1245
1246 CU_ASSERT_DOUBLE_EQUAL(slv_dot(5, rarray, rarray2), slow_dot_product(5, rarray, rarray2), 0.00001);
1247 CU_ASSERT_DOUBLE_EQUAL(slv_dot(5, rarray2, rarray), slow_dot_product(5, rarray, rarray2), 0.00001);
1248
1249 CU_ASSERT_DOUBLE_EQUAL(slv_dot(0, rarray, rarray2), slow_dot_product(0, rarray, rarray2), 0.00001);
1250 CU_ASSERT_DOUBLE_EQUAL(slv_dot(0, rarray2, rarray), slow_dot_product(0, rarray, rarray2), 0.00001);
1251
1252 for (i=1 ; i<10 ; ++i) {
1253 CU_ASSERT_DOUBLE_EQUAL(7/2 * i, rarray[i], 0.00001); /* data in arrays should be intact */
1254 CU_ASSERT_DOUBLE_EQUAL(rarray2[i], 2.0, 0.00001);
1255 }
1256
1257 for (i=0 ; i<10 ; ++i) {
1258 rarray2[i] = rarray[9-i];
1259 }
1260
1261 CU_ASSERT_DOUBLE_EQUAL(slv_dot(11, rarray, rarray2), slow_dot_product(11, rarray, rarray2), 0.00001);
1262 CU_ASSERT_DOUBLE_EQUAL(slv_dot(11, rarray2, rarray), slow_dot_product(11, rarray, rarray2), 0.00001);
1263
1264 CU_TEST(test_meminuse == ascmeminuse());
1265
1266 /* test slv_get_output_file() */
1267
1268 test_meminuse = ascmeminuse();
1269
1270 file_normal = (FILE *)100;
1271 CU_TEST(file_normal == slv_get_output_file(file_normal)); /* non-NULL fp */
1272 CU_TEST(NULL != slv_get_output_file(NULL)); /* NULL fp */
1273 fprintf(slv_get_output_file(NULL), "\n If you see this then test_slv_common:slv_get_output_file() failed!");
1274
1275 /* MIF(), LIF(), PMIF(), PLIF() - macros accessing members - not tested */
1276
1277 /* not tested - revisit later:
1278 * - slv_print_obj_name()
1279 * - slv_print_rel_name()
1280 * - slv_print_var_name()
1281 * - slv_print_logrel_name()
1282 * - slv_print_dis_name()
1283 * - slv_print_obj_index()
1284 * - slv_print_rel_sindex()
1285 * - slv_print_var_sindex()
1286 * - slv_print_logrel_sindex()
1287 * - slv_print_dis_sindex()
1288 * - slv_print_obj_index()
1289 */
1290
1291 test_meminuse = ascmeminuse();
1292
1293 CU_FAIL("slv_print_*_name() and slv_print_*_sindex() not tested.");
1294
1295 CU_TEST(test_meminuse == ascmeminuse());
1296
1297 /* test slv_direct_solve() */
1298
1299 test_meminuse = ascmeminuse();
1300
1301 CU_FAIL("slv_direct_solve() test not implemented.");
1302
1303 CU_TEST(test_meminuse == ascmeminuse());
1304
1305 /* test slv_direct_log_solve() */
1306
1307 test_meminuse = ascmeminuse();
1308
1309 CU_FAIL("slv_direct_log_solve() test not implemented.");
1310
1311 CU_TEST(test_meminuse == ascmeminuse());
1312
1313 /* test slv_create_lnkmap(), slv_write_lnkmap(), slv_destroy_lnkmap() */
1314
1315 test_meminuse = ascmeminuse();
1316
1317 hi[0] = 100;
1318 hj[0] = 1;
1319
1320 CU_TEST(NULL == slv_create_lnkmap(10, 10, 1, hi, hj)); /* error - hi contains invalid index */
1321
1322 hi[0] = 1;
1323 hj[0] = 100;
1324
1325 CU_TEST(NULL == slv_create_lnkmap(10, 10, 1, hi, hj)); /* error - hj contains invalid index */
1326
1327 lnkmap = slv_create_lnkmap(10, 10, 0, hi, hj); /* 0 element arrays */
1328 CU_TEST_FATAL(NULL != lnkmap);
1329 for (i=0 ; i<10 ; ++i) {
1330 CU_TEST(0 == *lnkmap[i]);
1331 }
1332
1333 CU_TEST(0 != AllocatedMemory((VOIDPTR)lnkmap, 0));
1334
1335 slv_destroy_lnkmap(lnkmap);
1336
1337 #ifdef MALLOC_DEBUG
1338 CU_TEST(0 == AllocatedMemory((VOIDPTR)lnkmap, 0));
1339 #else
1340 CU_TEST(1 == AllocatedMemory((VOIDPTR)lnkmap, 0));
1341 #endif
1342
1343 hi[0] = 1;
1344 hj[0] = 1;
1345
1346 lnkmap = slv_create_lnkmap(10, 10, 1, hi, hj); /* 1 element arrays */
1347 CU_TEST_FATAL(NULL != lnkmap);
1348 for (i=0 ; i<10 ; ++i) {
1349 lnkvars = lnkmap[i];
1350 if (i == 1) {
1351 CU_TEST_FATAL(1 == lnkvars[0]); /* number of non-zero elements */
1352 CU_TEST(1 == lnkvars[1]); /* column # of 1st element */
1353 CU_TEST(0 == lnkvars[2]); /* link map index */
1354 } else {
1355 CU_TEST(0 == lnkvars[0]);
1356 }
1357 }
1358
1359 CU_TEST(0 != AllocatedMemory((VOIDPTR)lnkmap, 0));
1360
1361 slv_destroy_lnkmap(lnkmap);
1362
1363 #ifdef MALLOC_DEBUG
1364 CU_TEST(0 == AllocatedMemory((VOIDPTR)lnkmap, 0));
1365 #else
1366 CU_TEST(1 == AllocatedMemory((VOIDPTR)lnkmap, 0));
1367 #endif
1368
1369 /* link map order: (5,1) (3,0) (3,8) (2,10) (2,0) (2,4) (2,7) (2,2) (1,10) (1,5) (1,1) */
1370 hi[0] = 5; /* row 5: (5,1) */
1371 hj[0] = 1;
1372 hi[1] = 3; /* row 3: (3,0) (3,8) */
1373 hj[1] = 0;
1374 hi[2] = 3;
1375 hj[2] = 8;
1376 hi[3] = 2; /* row 2: (2,0) (2,2) (2,4) (2,7) (2,10) */
1377 hj[3] = 10;
1378 hi[4] = 2;
1379 hj[4] = 0;
1380 hi[5] = 2;
1381 hj[5] = 4;
1382 hi[6] = 2;
1383 hj[6] = 7;
1384 hi[7] = 2;
1385 hj[7] = 2;
1386 hi[8] = 1; /* row 1: (1,1) (1,5) (1,10) */
1387 hj[8] = 10;
1388 hi[9] = 1;
1389 hj[9] = 5;
1390 hi[10] = 1;
1391 hj[10] = 1;
1392
1393 lnkindex_list = gl_create(11);
1394 for (i=0 ; i<11 ; ++i) {
1395 lnkindexes[i] = i;
1396 gl_append_ptr(lnkindex_list, &lnkindexes[i]);
1397 }
1398
1399 lnkmap = slv_create_lnkmap(6, 11, 11, hi, hj); /* multi element arrays */
1400 CU_TEST_FATAL(NULL != lnkmap);
1401
1402 lnkvars = lnkmap[0];
1403 CU_TEST(0 == lnkvars[0]); /* number of non-zero elements */
1404
1405 col_list = gl_create(10);
1406 CU_TEST_FATAL(NULL != col_list);
1407 gl_append_ptr(col_list, &hj[8]);
1408 gl_append_ptr(col_list, &hj[9]);
1409 gl_append_ptr(col_list, &hj[10]);
1410
1411 lnkvars = lnkmap[1];
1412 CU_TEST(3 == lnkvars[0]); /* number of non-zero elements */
1413 for (i=0 ; i<lnkvars[0] ; ++i) {
1414 CU_TEST(0 != (pos = gl_search(col_list, &lnkvars[2*i+1], compare_int32s)));
1415 gl_delete(col_list, pos, FALSE);
1416 CU_TEST(0 != (pos = gl_search(lnkindex_list, &lnkvars[2*i+2], compare_int32s)));
1417 gl_delete(lnkindex_list, pos, FALSE);
1418 }
1419
1420 gl_reset(col_list);
1421 gl_append_ptr(col_list, &hj[3]);
1422 gl_append_ptr(col_list, &hj[4]);
1423 gl_append_ptr(col_list, &hj[5]);
1424 gl_append_ptr(col_list, &hj[6]);
1425 gl_append_ptr(col_list, &hj[7]);
1426
1427 lnkvars = lnkmap[2];
1428 CU_TEST(5 == lnkvars[0]); /* number of non-zero elements */
1429 for (i=0 ; i<lnkvars[0] ; ++i) {
1430 CU_TEST(0 != (pos = gl_search(col_list, &lnkvars[2*i+1], compare_int32s)));
1431 gl_delete(col_list, pos, FALSE);
1432 CU_TEST(0 != (pos = gl_search(lnkindex_list, &lnkvars[2*i+2], compare_int32s)));
1433 gl_delete(lnkindex_list, pos, FALSE);
1434 }
1435
1436 gl_reset(col_list);
1437 gl_append_ptr(col_list, &hj[1]);
1438 gl_append_ptr(col_list, &hj[2]);
1439
1440 lnkvars = lnkmap[3];
1441 CU_TEST(2 == lnkvars[0]); /* number of non-zero elements */
1442 for (i=0 ; i<lnkvars[0] ; ++i) {
1443 CU_TEST(0 != (pos = gl_search(col_list, &lnkvars[2*i+1], compare_int32s)));
1444 gl_delete(col_list, pos, FALSE);
1445 CU_TEST(0 != (pos = gl_search(lnkindex_list, &lnkvars[2*i+2], compare_int32s)));
1446 gl_delete(lnkindex_list, pos, FALSE);
1447 }
1448
1449 lnkvars = lnkmap[4];
1450 CU_TEST(0 == lnkvars[0]); /* number of non-zero elements */
1451
1452 gl_reset(col_list);
1453 gl_append_ptr(col_list, &hj[0]);
1454
1455 lnkvars = lnkmap[5];
1456 CU_TEST_FATAL(1 == lnkvars[0]); /* number of non-zero elements */
1457 for (i=0 ; i<lnkvars[0] ; ++i) {
1458 CU_TEST(0 != (pos = gl_search(col_list, &lnkvars[2*i+1], compare_int32s)));
1459 gl_delete(col_list, pos, FALSE);
1460 CU_TEST(0 != (pos = gl_search(lnkindex_list, &lnkvars[2*i+2], compare_int32s)));
1461 gl_delete(lnkindex_list, pos, FALSE);
1462 }
1463
1464 CU_TEST(0 == gl_length(lnkindex_list)); /* all lnkindexes should have been used */
1465
1466 if (FALSE == test_printing_enabled()) {
1467 test_enable_printing();
1468 i_enabled_printing = TRUE;
1469 }
1470
1471 if (NULL != (file_normal = fopen("slvcommontempfile2.tmp", "w+"))) {
1472
1473 slv_write_lnkmap(file_normal, 6, lnkmap); /* write to normal open file */
1474 rewind(file_normal);
1475 CU_TEST(EOF != fgetc(file_normal)); /* test that file is not empty */
1476 fclose(file_normal);
1477 remove("slvcommontempfile2.tmp");
1478 }
1479 else {
1480 CU_FAIL("Error opening output file 2 in test_slv_common.c");
1481 }
1482
1483 if (TRUE == i_enabled_printing) {
1484 test_disable_printing();
1485 i_enabled_printing = FALSE;
1486 }
1487
1488 gl_destroy(col_list);
1489 gl_destroy(lnkindex_list);
1490 slv_destroy_lnkmap(lnkmap);
1491
1492 gl_emptyrecycler();
1493 CU_TEST(test_meminuse == ascmeminuse());
1494
1495 /* test slv_lnkmap_from_mtx() */
1496
1497 test_meminuse = ascmeminuse();
1498
1499 mtx = mtx_create();
1500 CU_TEST_FATAL(NULL != mtx);
1501 mtx_set_order(mtx, 11);
1502
1503 region.row.low = 0;
1504 region.row.high = 10;
1505 region.col.low = 0;
1506 region.col.high = 10;
1507
1508 CU_TEST(NULL == slv_lnkmap_from_mtx(NULL, &region)); /* error - NULL mtx */
1509
1510 region.row.low = -1;
1511 region.row.high = 10;
1512 region.col.low = 0;
1513 region.col.high = 10;
1514
1515 CU_TEST(NULL == slv_lnkmap_from_mtx(mtx, &region)); /* error - region.row.low < 0 */
1516
1517 region.row.low = 0;
1518 region.row.high = 11;
1519
1520 CU_TEST(NULL == slv_lnkmap_from_mtx(mtx, &region)); /* error - region.row.high >= order */
1521
1522 region.col.low = -1;
1523 region.col.high = 10;
1524
1525 CU_TEST(NULL == slv_lnkmap_from_mtx(mtx, &region)); /* error - region.col.low < 0 */
1526
1527 region.col.low = 0;
1528 region.col.high = 11;
1529
1530 CU_TEST(NULL == slv_lnkmap_from_mtx(mtx, &region)); /* error - region.col.high >= order */
1531
1532 region.row.low = 0;
1533 region.row.high = 10;
1534 region.col.low = 0;
1535 region.col.high = 10;
1536
1537 lnkmap = slv_lnkmap_from_mtx(mtx, &region); /* empty matrix */
1538 CU_TEST_FATAL(NULL != lnkmap);
1539 for (i=0 ; i<11 ; ++i) {
1540 CU_TEST(0 == *lnkmap[i]);
1541 }
1542
1543 slv_destroy_lnkmap(lnkmap);
1544
1545 mtx_fill_value(mtx, mtx_coord(&coord,5,1),10.0); /* row 5: (5,1) (5,7) */
1546 mtx_fill_value(mtx, mtx_coord(&coord,2,0),20.1); /* row 2: (2,0) (2,6) */
1547 mtx_fill_value(mtx, mtx_coord(&coord,6,4),30.2); /* row 6: (6,4) (6,5) */
1548 mtx_fill_value(mtx, mtx_coord(&coord,2,6),40.3);
1549 mtx_fill_value(mtx, mtx_coord(&coord,0,2),50.4); /* row 0: (0,2) */
1550 mtx_fill_value(mtx, mtx_coord(&coord,5,7),59.5);
1551 mtx_fill_value(mtx, mtx_coord(&coord,6,5),69.6);
1552 mtx_fill_value(mtx, mtx_coord(&coord,3,8),79.7); /* row 3: (3,8) */
1553 mtx_fill_value(mtx, mtx_coord(&coord,9,9),89.8); /* row 9: (9,9) (9,10) */
1554 mtx_fill_value(mtx, mtx_coord(&coord,9,10),99.9);
1555
1556 region.row.low = 3;
1557 region.row.high = 4;
1558 region.col.low = 0;
1559 region.col.high = 10;
1560 lnkmap = slv_lnkmap_from_mtx(mtx, &region); /* region with 1 non-zero */
1561 CU_TEST_FATAL(NULL != lnkmap);
1562 lnkvars = lnkmap[3];
1563 CU_TEST(1 == lnkvars[0]);
1564 CU_TEST(8 == lnkvars[1]);
1565 CU_TEST(80 == lnkvars[2]);
1566 for (i=0 ; i<3 ; ++i) {
1567 CU_TEST(0 == *lnkmap[i]);
1568 }
1569 for (i=4 ; i<11 ; ++i) {
1570 CU_TEST(0 == *lnkmap[i]);
1571 }
1572
1573 slv_destroy_lnkmap(lnkmap);
1574
1575 lnkmap = slv_lnkmap_from_mtx(mtx, mtx_ENTIRE_MATRIX); /* entire matrix */
1576 CU_TEST_FATAL(NULL != lnkmap);
1577 CU_TEST(0 == *lnkmap[1]);
1578 CU_TEST(0 == *lnkmap[4]);
1579 CU_TEST(0 == *lnkmap[7]);
1580 CU_TEST(0 == *lnkmap[8]);
1581 CU_TEST(0 == *lnkmap[10]);
1582
1583 lnkvars = lnkmap[0];
1584 CU_TEST(1 == lnkvars[0]);
1585 CU_TEST(2 == lnkvars[1]);
1586 CU_TEST(50 == lnkvars[2]);
1587
1588 lnkvars = lnkmap[2];
1589 CU_TEST(2 == lnkvars[0]);
1590 if (0 == lnkvars[1]) {
1591 CU_TEST(20 == lnkvars[2]);
1592 CU_TEST(6 == lnkvars[3]);
1593 CU_TEST(40 == lnkvars[4]);
1594 } else if (6 == lnkvars[1]) {
1595 CU_TEST(40 == lnkvars[2]);
1596 CU_TEST(0 == lnkvars[3]);
1597 CU_TEST(20 == lnkvars[4]);
1598 } else {
1599 CU_FAIL("Unexpected col for lnkmap row 2.");
1600 }
1601
1602 lnkvars = lnkmap[3];
1603 CU_TEST(1 == lnkvars[0]);
1604 CU_TEST(8 == lnkvars[1]);
1605 CU_TEST(80 == lnkvars[2]);
1606
1607 lnkvars = lnkmap[5];
1608 CU_TEST(2 == lnkvars[0]);
1609 if (1 == lnkvars[1]) {
1610 CU_TEST(10 == lnkvars[2]);
1611 CU_TEST(7 == lnkvars[3]);
1612 CU_TEST(60 == lnkvars[4]);
1613 } else if (7 == lnkvars[1]) {
1614 CU_TEST(60 == lnkvars[2]);
1615 CU_TEST(1 == lnkvars[3]);
1616 CU_TEST(10 == lnkvars[4]);
1617 } else {
1618 CU_FAIL("Unexpected col for lnkmap row 5.");
1619 }
1620
1621 lnkvars = lnkmap[6];
1622 CU_TEST(2 == lnkvars[0]);
1623 if (4 == lnkvars[1]) {
1624 CU_TEST(30 == lnkvars[2]);
1625 CU_TEST(5 == lnkvars[3]);
1626 CU_TEST(70 == lnkvars[4]);
1627 } else if (5 == lnkvars[1]) {
1628 CU_TEST(70 == lnkvars[2]);
1629 CU_TEST(4 == lnkvars[3]);
1630 CU_TEST(30 == lnkvars[4]);
1631 } else {
1632 CU_FAIL("Unexpected col for lnkmap row 6.");
1633 }
1634
1635 lnkvars = lnkmap[9];
1636 CU_TEST(2 == lnkvars[0]);
1637 if (9 == lnkvars[1]) {
1638 CU_TEST(90 == lnkvars[2]);
1639 CU_TEST(10 == lnkvars[3]);
1640 CU_TEST(100 == lnkvars[4]);
1641 } else if (10 == lnkvars[1]) {
1642 CU_TEST(100 == lnkvars[2]);
1643 CU_TEST(9 == lnkvars[3]);
1644 CU_TEST(90 == lnkvars[4]);
1645 } else {
1646 CU_FAIL("Unexpected col for lnkmap row 9.");
1647 }
1648
1649 slv_destroy_lnkmap(lnkmap);
1650
1651 mtx_destroy(mtx);
1652
1653 CU_TEST(test_meminuse == ascmeminuse());
1654
1655 /*
1656
1657 extern int slv_direct_solve(slv_system_t server,
1658 struct rel_relation *rel,
1659 struct var_variable *var,
1660 FILE *file,
1661 real64 epsilon,
1662 int ignore_bounds,
1663 int scaled);
1664 *<
1665 * Attempts to directly solve the given relation (equality constraint) for
1666 * the given variable, leaving the others fixed. Returns an integer
1667 * signifying the status as one of the following three:
1668 * <pre>
1669 * 0 ==> Unable to determine anything.
1670 * Not symbolically invertible.
1671 * 1 ==> Solution(s) found.
1672 * Variable value set to first found if more than one.
1673 * -1 ==> No solution found.
1674 * Function invertible, but no solution exists satisfying
1675 * var bounds (if active) and the epsilon given.
1676 * </pre>
1677 * The variable bounds will be upheld, unless ignore_bounds=FALSE.
1678 * Residual testing will be against epsilon and either scaled or
1679 * unscaled residual according to scaled (no scale -> 0).
1680 * If file != NULL and there are leftover possible solutions, we
1681 * will write about them to file.
1682 *
1683 * @param server The slv_system_t (mostly ignored).
1684 * @param rel The relation to attempt to solve.
1685 * @param var The variable for which to solve.
1686 * @param file File stream to receive other possible solutions.
1687 * @param epsilon Tolerance for testing convergence.
1688 * @param ignore_bounds If TRUE, ignore bounds on variable.
1689 * @param scaled If TRUE, test scaled residuals against epsilon.
1690
1691
1692 extern int slv_direct_log_solve(slv_system_t sys,
1693 struct logrel_relation *lrel,
1694 struct dis_discrete *dvar,
1695 FILE *file,
1696 int perturb,
1697 struct gl_list_t *instances);
1698 *<
1699 * Attempt to directly solve the given logrelation for the given
1700 * discrete variable, leaving the others fixed. Returns an integer
1701 * signifying the status as one of the following three:
1702 * <pre>
1703 * 0 ==> Unable to determine anything. Bad logrelation or dvar
1704 * 1 ==> Solution found.
1705 * 2 ==> More than one solution found. It does not modify the value
1706 * of dvar. Conflicting.
1707 * -1 ==> No solution found. Inconsistency
1708 * </pre>
1709 * If file != NULL and there are leftover possible solutions, we
1710 * will write about them to file.
1711 * The flag perturb and the gl_list are used to change the truth
1712 * value of some boundaries. This is sometimes useful in
1713 * conditional modeling.
1714 *
1715 * @param sys The slv_system_t (mostly ignored).
1716 * @param lrel The logical relation to attempt to solve.
1717 * @param dvar The discrete variable for which to solve.
1718 * @param file File stream to receive other possible solutions.
1719 * @param perturb If TRUE, perturbs the truth values if necessary to find the solution.
1720 * @param instances List of instances.
1721
1722 */
1723
1724 if (TRUE == i_initialized_lists) { /* clean up list system if necessary */
1725 gl_destroy_pool();
1726 }
1727 CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
1728 }
1729
1730 /*===========================================================================*/
1731 /* Registration information */
1732
1733 static CU_TestInfo slv_common_test_list[] = {
1734 {"test_slv_common", test_slv_common},
1735 CU_TEST_INFO_NULL
1736 };
1737
1738 static CU_SuiteInfo suites[] = {
1739 {"test_solver_slv_common", NULL, NULL, slv_common_test_list},
1740 CU_SUITE_INFO_NULL
1741 };
1742
1743 /*-------------------------------------------------------------------*/
1744 CU_ErrorCode test_register_solver_slv_common(void)
1745 {
1746 return CU_register_suites(suites);
1747 }

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