1 |
/* ASCEND modelling environment |
2 |
Copyright (C) 2007 Carnegie Mellon University |
3 |
|
4 |
This program is free software; you can redistribute it and/or modify |
5 |
it under the terms of the GNU General Public License as published by |
6 |
the Free Software Foundation; either version 2, or (at your option) |
7 |
any later version. |
8 |
|
9 |
This program is distributed in the hope that it will be useful, |
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
GNU General Public License for more details. |
13 |
|
14 |
You should have received a copy of the GNU General Public License |
15 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 |
*/ |
17 |
|
18 |
#include <CUnit/CUnit.h> |
19 |
#include <ascend/general/platform.h> |
20 |
#include <ascend/general/ltmatrix.h> |
21 |
|
22 |
#include "test/common.h" |
23 |
|
24 |
//#define LTMATRIX_DEBUG |
25 |
#ifdef LTMATRIX_DEBUG |
26 |
# define TEST_LTMATRIX_PRINT(MTX) ltmatrix_debug_print(MTX) |
27 |
#else |
28 |
# define TEST_LTMATRIX_PRINT(MTX) |
29 |
#endif |
30 |
|
31 |
#ifndef MEMUSED |
32 |
# define MEMUSED(N) CU_TEST(ascmeminuse()==(N)) |
33 |
#endif |
34 |
|
35 |
static void ltmatrix_fill_pattern(ltmatrix* matrix, int pattern); |
36 |
static int ltmatrix_check_pattern(ltmatrix* matrix, int pattern); |
37 |
|
38 |
static void test_ltmatrix(void){ |
39 |
double array[] = {0.1,-1.2,2.3,-3.4,4.5,-5.6,6.7,-7.8,8.9,-9.0}; |
40 |
/** Performing CRUD Operations on LTMatrix library */ |
41 |
ltmatrix *matrix_upper; |
42 |
ltmatrix *matrix_lower; |
43 |
ltmatrix *matrix_full; |
44 |
|
45 |
/** Upper Matrix Operations */ |
46 |
/** Create Operations */ |
47 |
matrix_upper = ltmatrix_create(LTMATRIX_UPPER,4); |
48 |
CU_ASSERT(matrix_upper->access_type == LTMATRIX_UPPER); |
49 |
CU_ASSERT(matrix_upper->dimension == 4); |
50 |
CU_ASSERT(matrix_upper->len == 10); |
51 |
CU_ASSERT(matrix_upper->h!=NULL); |
52 |
/** Read Operations */ |
53 |
/** Done with Range checks in the Access Method. */ |
54 |
TEST_LTMATRIX_PRINT(matrix_upper); /** Success Prints Matrix of 5x5 with all elements 0.0 */ |
55 |
|
56 |
/** Update Operations */ |
57 |
ltmatrix_init(matrix_upper,array); |
58 |
CU_ASSERT(ltmatrix_compare_array(matrix_upper,array)==1); |
59 |
|
60 |
TEST_LTMATRIX_PRINT(matrix_upper); |
61 |
ltmatrix_clear(matrix_upper); |
62 |
TEST_LTMATRIX_PRINT(matrix_upper); |
63 |
|
64 |
/** Lower Matrix Operations */ |
65 |
/** Create Operations */ |
66 |
matrix_lower = ltmatrix_create(LTMATRIX_LOWER,4); |
67 |
CU_ASSERT(matrix_lower->access_type == LTMATRIX_LOWER); |
68 |
CU_ASSERT(matrix_lower->dimension == 4); |
69 |
CU_ASSERT(matrix_lower->len == 10); |
70 |
CU_ASSERT(matrix_lower->h!=NULL); |
71 |
/** Read Operations */ |
72 |
/** Done with Range checks in the Access Method. */ |
73 |
TEST_LTMATRIX_PRINT(matrix_lower); /** Success Prints Matrix of 5x5 with all elements 0.0 */ |
74 |
/** Update Operations */ |
75 |
ltmatrix_init(matrix_lower,array); |
76 |
CU_ASSERT(ltmatrix_compare_array(matrix_lower,array)==1); |
77 |
TEST_LTMATRIX_PRINT(matrix_lower); |
78 |
ltmatrix_clear(matrix_lower); |
79 |
TEST_LTMATRIX_PRINT(matrix_lower); |
80 |
|
81 |
/** Full Matrix Operations */ |
82 |
/** Create Operations */ |
83 |
matrix_full = ltmatrix_create(LTMATRIX_FULL,4); |
84 |
CU_ASSERT(matrix_full->access_type == LTMATRIX_FULL); |
85 |
CU_ASSERT(matrix_full->dimension == 4); |
86 |
CU_ASSERT(matrix_full->len == 16); |
87 |
CU_ASSERT(matrix_full->h!=NULL); |
88 |
/** Read Operations */ |
89 |
/** Done with Range checks in the Access Method. */ |
90 |
TEST_LTMATRIX_PRINT(matrix_full); /** Success Prints Matrix of 5x5 with all elements 0.0 */ |
91 |
|
92 |
|
93 |
/** Pattern all the Hessian Matrices */ |
94 |
ltmatrix_fill_pattern(matrix_upper,0); |
95 |
CU_ASSERT(ltmatrix_check_pattern(matrix_upper,0)==1); |
96 |
|
97 |
ltmatrix_fill_pattern(matrix_lower,0); |
98 |
CU_ASSERT(ltmatrix_check_pattern(matrix_lower,0)==1); |
99 |
|
100 |
ltmatrix_fill_pattern(matrix_full,0); |
101 |
CU_ASSERT(ltmatrix_check_pattern(matrix_full,0)==1); |
102 |
|
103 |
/** Print for one last time the Matrices */ |
104 |
|
105 |
TEST_LTMATRIX_PRINT(matrix_lower); |
106 |
TEST_LTMATRIX_PRINT(matrix_upper); |
107 |
TEST_LTMATRIX_PRINT(matrix_full); |
108 |
|
109 |
|
110 |
/** Delete Operations */ |
111 |
ltmatrix_destroy(matrix_upper); |
112 |
ltmatrix_destroy(matrix_lower); |
113 |
ltmatrix_destroy(matrix_full); |
114 |
} |
115 |
|
116 |
/** |
117 |
Fills patterns in the given matrix |
118 |
@param matrix is the matrix in which a given pattern is stored |
119 |
@param pattern is the type of pattern one wants to store. |
120 |
0 - matrix[row][col] = ((row+1)*(col+1)+1) |
121 |
*/ |
122 |
static void ltmatrix_fill_pattern(ltmatrix* matrix, int pattern){ |
123 |
ltmatrix_test_validity(matrix); |
124 |
unsigned long i, j; |
125 |
switch(pattern){ |
126 |
case 0: |
127 |
for (i = 0; i < matrix->dimension; i++) |
128 |
{ |
129 |
for (j = 0 ; j < matrix->dimension; j++){ |
130 |
ltmatrix_set_element(matrix,i,j,((i+1)*(j+1)+1)); |
131 |
} |
132 |
} |
133 |
break; |
134 |
} |
135 |
} |
136 |
|
137 |
/** |
138 |
Checks the pattern in the given matrix |
139 |
@param matrix is the matrix in which a given pattern is stored |
140 |
@param pattern is the type of pattern one wants to check. |
141 |
0 - matrix[row][col] = ((row+1)*(col+1)+1) |
142 |
*/ |
143 |
static int ltmatrix_check_pattern(ltmatrix* matrix, int pattern){ |
144 |
ltmatrix_test_validity(matrix); |
145 |
unsigned long i, j; |
146 |
switch(pattern){ |
147 |
case 0: |
148 |
for (i = 0; i < matrix->dimension; i++) |
149 |
{ |
150 |
for (j = 0 ; j < matrix->dimension; j++){ |
151 |
if(ltmatrix_get_element(matrix,i,j)!=((i+1)*(j+1)+1)) |
152 |
{ |
153 |
return 0; |
154 |
} |
155 |
} |
156 |
} |
157 |
break; |
158 |
default: |
159 |
return 0; |
160 |
} |
161 |
return 1; |
162 |
} |
163 |
|
164 |
//------------------------- |
165 |
|
166 |
static void test_compare(void){ |
167 |
ltmatrix *m1 = ltmatrix_create(LTMATRIX_UPPER,3); |
168 |
double *r = ltmatrix_get_row_pointer(m1,0); |
169 |
r[0] = 1.; |
170 |
r[1] = 2.; |
171 |
r[2] = 3.; |
172 |
ltmatrix_set_element(m1,1,1,4.); |
173 |
ltmatrix_set_element(m1,2,1,5.); |
174 |
ltmatrix_set_element(m1,2,2,6.); |
175 |
|
176 |
CU_TEST(ltmatrix_get_element(m1,0,0)==1.); |
177 |
CU_TEST(ltmatrix_get_element(m1,1,0)==2.); |
178 |
CU_TEST(ltmatrix_get_element(m1,0,1)==2.); |
179 |
CU_TEST(ltmatrix_get_element(m1,2,0)==3.); |
180 |
CU_TEST(ltmatrix_get_element(m1,0,2)==3.); |
181 |
CU_TEST(ltmatrix_get_element(m1,1,1)==4.); |
182 |
CU_TEST(ltmatrix_get_element(m1,1,2)==5.); |
183 |
CU_TEST(ltmatrix_get_element(m1,2,2)==6.); |
184 |
|
185 |
ltmatrix *m2 = ltmatrix_create(LTMATRIX_UPPER,3); |
186 |
ltmatrix_init(m2,(double[]){1,2,3,4,5,6}); |
187 |
CU_TEST(ltmatrix_compare(m1,m2)==1); |
188 |
TEST_LTMATRIX_PRINT(m1); |
189 |
ltmatrix_destroy(m1); |
190 |
ltmatrix_destroy(m2); |
191 |
MEMUSED(0); |
192 |
} |
193 |
|
194 |
/*===========================================================================*/ |
195 |
/* Registration information */ |
196 |
|
197 |
#define TESTS(T) \ |
198 |
T(ltmatrix) \ |
199 |
T(compare) |
200 |
|
201 |
REGISTER_TESTS_SIMPLE(general_ltmatrix, TESTS); |
202 |
|