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

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