|
funkalicious 0.1
|
00001 /* 00002 ** readwrite_dotcodefiles.c 00003 ** 00004 Copyright (C) 2010 Joseph Pingenot 00005 00006 This program is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU Affero General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU Affero General Public License for more details. 00015 00016 You should have received a copy of the GNU Affero General Public License 00017 along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 00019 ** Made by (Johnny Q. Hacker) 00020 ** Login <solarion@borkborkbork> 00021 ** 00022 ** Started on Tue Feb 16 14:35:00 2010 Johnny Q. Hacker 00023 ** Last update Sun May 12 01:17:25 2002 Speed Blue 00024 */ 00025 00026 #include <math.h> 00027 #include <stdio.h> 00028 #include <stdlib.h> 00029 #include <CUnit/CUnit.h> 00030 #include "tests/cunit-local.h" 00031 #include <glib.h> 00032 #include <libdotcode/write_binary_data.h> 00033 #include <libdotcode/read_binary_data.h> 00034 #include "readwrite_dotcodefiles.h" 00035 00036 struct dotcode_grid* grids[4]; 00037 const char* temp_filename = "/tmp/test-file-for-readwrite-dotcodefiles.tmptest"; 00038 00039 int are_equal_typed(enum dotcode_type t, void* store1, void* store2, long unsigned int i) { 00040 switch(t) { 00041 case DOTCODE_INT: 00042 return ((int*)store1)[i] == ((int*)store2)[i]; 00043 case DOTCODE_FLOAT8: 00044 return ((double*)store1)[i] == ((double*)store2)[i]; 00045 case DOTCODE_FLOAT4: 00046 return ((float*)store1)[i] == ((float*)store2)[i]; 00047 default: 00048 return 0; 00049 } 00050 } 00051 00052 int grid_is_consistent(const struct dotcode_grid* g) { 00053 CU_ASSERT(g->data->Nd == 3); 00054 if(!(g->data->Nd == 3)) return 0; 00055 enum dotcode_type t = g->data->t; 00056 for(int i=0; i<3; i++) { 00057 CU_ASSERT(g->gridsites[i]->t == t); 00058 if(!(g->gridsites[i]->t == t)) return 0; 00059 CU_ASSERT(g->gridsites[i]->imin == g->data->indices[i*2]); 00060 if(!(g->gridsites[i]->imin == g->data->indices[i*2])) return 0; 00061 CU_ASSERT(g->gridsites[i]->imax == g->data->indices[i*2+1]); 00062 if(!(g->gridsites[i]->imax == g->data->indices[i*2+1])) return 0; 00063 } 00064 return 1; 00065 } 00066 00067 int grids_are_same(const struct dotcode_grid* g1, const struct dotcode_grid* g2) { 00068 for(int i=0; i<3; i++) { 00069 CU_ASSERT(g1->gridsites[i]->t == g2->gridsites[i]->t); 00070 if(!(g1->gridsites[i]->t == g2->gridsites[i]->t)) return 0; 00071 CU_ASSERT(g1->gridsites[i]->imin == g2->gridsites[i]->imin); 00072 if(!(g1->gridsites[i]->imin == g2->gridsites[i]->imin)) return 0; 00073 CU_ASSERT(g1->gridsites[i]->imax == g2->gridsites[i]->imax); 00074 if(!(g1->gridsites[i]->imax == g2->gridsites[i]->imax)) return 0; 00075 for(int j=0; j<(g1->gridsites[i]->imax - g1->gridsites[i]->imin); j++) { 00076 CU_ASSERT(are_equal_typed(g1->gridsites[i]->t, g1->gridsites[i]->storage, g2->gridsites[i]->storage, j)); 00077 if(!are_equal_typed(g1->gridsites[i]->t, g1->gridsites[i]->storage, g2->gridsites[i]->storage, j)) return 0; 00078 } 00079 } 00080 00081 for(int i=0; i<3; i++) { 00082 CU_ASSERT(g1->Ng[i] == g2->Ng[i]); 00083 if(!(g1->Ng[i] == g2->Ng[i])) return 0; 00084 } 00085 00086 CU_ASSERT(g1->data->t == g2->data->t); 00087 if(!(g1->data->t == g2->data->t)) return 0; 00088 CU_ASSERT(!(g_strcmp0(g1->data->metadata, g2->data->metadata))); 00089 if(g_strcmp0(g1->data->metadata, g2->data->metadata)) return 0; 00090 CU_ASSERT(g1->data->Nd == g2->data->Nd); 00091 if(!(g1->data->Nd == g2->data->Nd)) return 0; 00092 long int size = 1; 00093 for(int i=0; i<(g1->data->Nd); i++) { 00094 CU_ASSERT(g1->data->indices[2*i] == g2->data->indices[2*i]); 00095 if(!(g1->data->indices[2*i] == g2->data->indices[2*i])) return 0; 00096 CU_ASSERT(g1->data->indices[2*i+1] == g2->data->indices[2*i+1]); 00097 if(!(g1->data->indices[2*i+1] == g2->data->indices[2*i+1])) return 0; 00098 size *= (g1->data->indices[2*i+1] == g1->data->indices[2*i]); 00099 } 00100 00101 for(long int i=0; i<size; i++) { 00102 CU_ASSERT(are_equal_typed(g1->data->t, g1->data->data, g2->data->data, i)); 00103 if(!(are_equal_typed(g1->data->t, g1->data->data, g2->data->data, i))) return 0; 00104 } 00105 00106 return 1; 00107 } 00108 00109 struct dotcode_grid* write_specified_10x10x10_float8_grid(double* values) { 00110 struct dotcode_grid *g = (struct dotcode_grid*)malloc(sizeof(struct dotcode_grid)); 00111 CU_ASSERT(g != NULL); 00112 g->data = (struct dotcode_tensor*)malloc(sizeof(struct dotcode_tensor)); 00113 CU_ASSERT(g->data != NULL); 00114 g->data->indices = (int*)malloc(6*sizeof(int)); 00115 CU_ASSERT(g->data->indices != NULL); 00116 g->Ng[0]=g->Ng[1]=g->Ng[2] = 10; 00117 g->data->Nd = 3; 00118 long unsigned int size=1; 00119 for(int i=0; i<3; i++) { 00120 g->gridsites[i] = (struct dotcode_array*)malloc(sizeof(struct dotcode_array)); 00121 CU_ASSERT(g->gridsites[i] != NULL); 00122 g->gridsites[i]->t = DOTCODE_FLOAT8; 00123 g->data->indices[2*i] = g->gridsites[i]->imin = 0; 00124 g->data->indices[2*i+1] = g->gridsites[i]->imax = 9; 00125 g->gridsites[i]->storage = (double*)malloc(sizeof(double)*(g->gridsites[i]->imax-g->gridsites[i]->imin+1)); 00126 CU_ASSERT(g->gridsites[i]->storage != NULL); 00127 for(int j=0; j<(g->gridsites[i]->imax-g->gridsites[i]->imin+1); j++) ((double*)g->gridsites[i]->storage)[j] = 0.5*((double)j); 00128 size*=(g->gridsites[i]->imax-g->gridsites[i]->imin+1); 00129 } 00130 g->data->t = DOTCODE_FLOAT8; 00131 g->data->metadata = "2.0"; 00132 g->data->data = values; 00133 for(int i=0; i<size; i++) ((double*)g->data->data)[i] = values[i]; 00134 GError *e = NULL; 00135 GFile *f = g_file_new_for_path(temp_filename); 00136 e = dotcode_write_typed_file(DOTCODE_GRID, g, f); 00137 if(e != NULL) { 00138 fprintf(stderr, "ERROR writing grid to file (%s): %s", temp_filename, e->message); 00139 g_error_free(e); 00140 } 00141 CU_ASSERT(e == NULL); 00142 return g; 00143 } 00144 00145 struct dotcode_grid* write_valued_10x10x10_float8_grid(double value) { 00146 double* val = (double*)malloc(10*10*10*sizeof(double)); 00147 CU_ASSERT(val != NULL); 00148 for(int i=0; i<(10*10*10); i++) val[i]=value; 00149 struct dotcode_grid *g= write_specified_10x10x10_float8_grid(val); 00150 free(val); 00151 return g; 00152 } 00153 00154 void write_zero_10x10x10_float8_grid(void) { 00155 grids[0] = write_valued_10x10x10_float8_grid(0.0); 00156 //fprintf(stderr, "Allocated grids[0]=%p", (void*)grids[0]); 00157 } 00158 void write_one_10x10x10_float8_grid(void) { 00159 grids[1] = write_valued_10x10x10_float8_grid(1.0); 00160 //fprintf(stderr, "Allocated grids[1]=%p", (void*)grids[1]); 00161 } 00162 void write_mone_10x10x10_float8_grid(void) { 00163 grids[2] = write_valued_10x10x10_float8_grid(-1.0); 00164 //fprintf(stderr, "Allocated grids[2]=%p", (void*)grids[2]); 00165 } 00166 00167 void write_random_10x10x10_float8_grid(void) { 00168 srand48(time(NULL)); 00169 double* value = (double*)malloc(10*10*10*sizeof(double)); 00170 CU_ASSERT(value != NULL); 00171 for(int i=0; i<(10*10*10); i++) value[i]=drand48(); 00172 grids[3] = write_specified_10x10x10_float8_grid(value); 00173 //fprintf(stderr, "Allocated grids[3]=%p", (void*)grids[3]); 00174 } 00175 00176 void read_zero_10x10x10_float8_grid(void) { 00177 enum dotcode_type t; 00178 GFile *f = g_file_new_for_path(temp_filename); 00179 GError *e = NULL; 00180 struct dotcode_grid *g = read_dotcode_generic_file(&t, f, &e); 00181 if(e != NULL) { 00182 fprintf(stderr, "ERROR reading grid from file (%s): %s", temp_filename, e->message); 00183 g_error_free(e); 00184 CU_FAIL("Fail!"); 00185 return; 00186 } 00187 CU_ASSERT(e == NULL); 00188 CU_ASSERT(t == DOTCODE_GRID); 00189 if(t != DOTCODE_GRID) return; 00190 int same = grids_are_same(g, grids[0]); 00191 CU_ASSERT(same != 0); 00192 if(e != NULL) { 00193 fprintf(stderr, "ERROR freeing zero grid: %s\n", e->message); 00194 g_error_free(e); 00195 CU_FAIL("Failed to free zero grid.\n"); 00196 } 00197 } 00198 /*"mone" -> -1*/ 00199 void read_mone_10x10x10_float8_grid(void) { 00200 enum dotcode_type t; 00201 GFile *f = g_file_new_for_path(temp_filename); 00202 GError *e = NULL; 00203 struct dotcode_grid *g = read_dotcode_generic_file(&t, f, &e); 00204 if(e != NULL) { 00205 fprintf(stderr, "ERROR reading grid from file (%s): %s", temp_filename, e->message); 00206 g_error_free(e); 00207 CU_FAIL("Fail!"); 00208 return; 00209 } 00210 CU_ASSERT(e == NULL); 00211 CU_ASSERT(t == DOTCODE_GRID); 00212 if(t != DOTCODE_GRID) return; 00213 int same = grids_are_same(g, grids[2]); 00214 CU_ASSERT(same != 0); 00215 if(e != NULL) { 00216 fprintf(stderr, "ERROR freeing 1 grid: %s\n", e->message); 00217 g_error_free(e); 00218 CU_FAIL("Failed to free 1 grid.\n"); 00219 } 00220 } 00221 void read_one_10x10x10_float8_grid(void) { 00222 enum dotcode_type t; 00223 GFile *f = g_file_new_for_path(temp_filename); 00224 GError *e = NULL; 00225 struct dotcode_grid *g = read_dotcode_generic_file(&t, f, &e); 00226 if(e != NULL) { 00227 fprintf(stderr, "ERROR reading grid from file (%s): %s", temp_filename, e->message); 00228 g_error_free(e); 00229 CU_FAIL("Fail!"); 00230 return; 00231 } 00232 CU_ASSERT(e == NULL); 00233 CU_ASSERT(t == DOTCODE_GRID); 00234 if(t != DOTCODE_GRID) return; 00235 int same = grids_are_same(g, grids[1]); 00236 CU_ASSERT(same != 0); 00237 if(e != NULL) { 00238 fprintf(stderr, "ERROR freeing -1 grid: %s\n", e->message); 00239 g_error_free(e); 00240 CU_FAIL("Failed to free -1 grid.\n"); 00241 } 00242 } 00243 void read_random_10x10x10_float8_grid(void) { 00244 enum dotcode_type t; 00245 GFile *f = g_file_new_for_path(temp_filename); 00246 GError *e = NULL; 00247 struct dotcode_grid *g = read_dotcode_generic_file(&t, f, &e); 00248 if(e != NULL) { 00249 fprintf(stderr, "ERROR reading grid from file (%s): %s", temp_filename, e->message); 00250 g_error_free(e); 00251 CU_FAIL("Fail!"); 00252 return; 00253 } 00254 CU_ASSERT(e == NULL); 00255 CU_ASSERT(t == DOTCODE_GRID); 00256 if(t != DOTCODE_GRID) return; 00257 int same = grids_are_same(g, grids[3]); 00258 CU_ASSERT(same != 0); 00259 dotcode_free_type(DOTCODE_GRID, g, 1, &e, TRUE); 00260 if(e != NULL) { 00261 fprintf(stderr, "ERROR freeing random grid: %s\n", e->message); 00262 g_error_free(e); 00263 CU_FAIL("Failed to free random grid.\n"); 00264 } 00265 } 00266 00267 00268 void libdotcode_readwrite_suite_add_tests(CU_pSuite suite) { 00269 CU_add_test(suite, "Writing zero 10x10x10 Float8 Grid", write_zero_10x10x10_float8_grid); 00270 CU_add_test(suite, "Reading back zero 10x10x10 Float8 Grid", read_zero_10x10x10_float8_grid); 00271 CU_add_test(suite, "Writing one 10x10x10 Float8 Grid", write_one_10x10x10_float8_grid); 00272 CU_add_test(suite, "Reading one zero 10x10x10 Float8 Grid", read_one_10x10x10_float8_grid); 00273 CU_add_test(suite, "Writing -one 10x10x10 Float8 Grid", write_mone_10x10x10_float8_grid); 00274 CU_add_test(suite, "Reading -one zero 10x10x10 Float8 Grid", read_mone_10x10x10_float8_grid); 00275 CU_add_test(suite, "Writing random 10x10x10 Float8 Grid", write_random_10x10x10_float8_grid); 00276 CU_add_test(suite, "Reading back random 10x10x10 Float8 Grid", read_random_10x10x10_float8_grid); 00277 00278 GError *e = NULL; 00279 /*Free the structures*/ 00280 for(int i=0; i<4; i++) { 00281 if(grids[i] == NULL) { 00282 fprintf(stderr, "ERROR: grids[%d] is NULL!\n", i); 00283 continue; 00284 } 00285 dotcode_free_type(DOTCODE_GRID, grids[i], 1, &e, TRUE); 00286 if(e != NULL) { 00287 fprintf(stderr, "ERROR freeing %dth grid: %s\n", i, e->message); 00288 g_error_free(e); 00289 } 00290 } 00291 }