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

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