test_atrtp_load.c (6101B)
1 /* Copyright (C) 2022, 2023 |Méso|Star> (contact@meso-star.com) 2 * Copyright (C) 2020, 2021 Centre National de la Recherche Scientifique 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 3 of the License, or 7 * (at your option) 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 #include "atrtp.h" 18 #include <rsys/mem_allocator.h> 19 #include <stdio.h> 20 #include <unistd.h> 21 22 /******************************************************************************* 23 * Helper functions 24 ******************************************************************************/ 25 static void 26 check_atrtp_desc 27 (const struct atrtp_desc* desc, 28 const uint64_t nnodes) 29 { 30 size_t inode; 31 CHK(desc); 32 CHK(nnodes); 33 34 CHK(desc->nnodes == nnodes); 35 FOR_EACH(inode, 0, nnodes) { 36 size_t iprop; 37 const double* props = atrtp_desc_get_node_properties(desc, inode); 38 CHK(props); 39 FOR_EACH(iprop, 0, ATRTP_COUNT__) { 40 CHK(props[iprop] == (double)(10+iprop)); 41 } 42 } 43 } 44 45 static void 46 test_load(struct atrtp* atrtp) 47 { 48 struct atrtp_desc desc = ATRTP_DESC_NULL; 49 FILE* fp = NULL; 50 const char* filename = "test_file.atrtp"; 51 const uint64_t pagesize = 16384; 52 const uint64_t nnodes = 192; 53 size_t i; 54 char byte = 0; 55 56 fp = fopen(filename, "w+"); 57 CHK(fp); 58 59 /* Write the header */ 60 CHK(fwrite(&pagesize, sizeof(pagesize), 1, fp) == 1); 61 CHK(fwrite(&nnodes, sizeof(nnodes), 1, fp) == 1); 62 63 /* Padding */ 64 CHK(fseek(fp, (long)ALIGN_SIZE((size_t)ftell(fp), pagesize), SEEK_SET) == 0); 65 66 /* Write nodes data */ 67 FOR_EACH(i, 0, nnodes) { 68 double props[ATRTP_COUNT__]; 69 int iprop; 70 CHK(ATRTP_COUNT__ < 10); 71 FOR_EACH(iprop, 0, ATRTP_COUNT__) { 72 props[iprop] = (double)(10 + iprop); 73 } 74 CHK(fwrite(props, sizeof(double), ATRTP_COUNT__, fp) == ATRTP_COUNT__); 75 } 76 77 /* Padding. Write one char to position the EOF indicator */ 78 CHK(fseek(fp, (long)ALIGN_SIZE((size_t)ftell(fp), pagesize)-1, SEEK_SET) == 0); 79 CHK(fwrite(&byte, sizeof(byte), 1, fp) == 1); 80 81 rewind(fp); 82 83 CHK(atrtp_load_stream(NULL, fp, filename) == RES_BAD_ARG); 84 CHK(atrtp_load_stream(atrtp, NULL, filename) == RES_BAD_ARG); 85 CHK(atrtp_load_stream(atrtp, fp, NULL) == RES_OK); 86 CHK(atrtp_get_desc(NULL, &desc) == RES_BAD_ARG); 87 CHK(atrtp_get_desc(atrtp, NULL) == RES_BAD_ARG); 88 CHK(atrtp_get_desc(atrtp, &desc) == RES_OK); 89 90 rewind(fp); 91 CHK(atrtp_load_stream(atrtp, fp, filename) == RES_OK); 92 CHK(atrtp_get_desc(atrtp, &desc) == RES_OK); 93 check_atrtp_desc(&desc, nnodes); 94 95 CHK(atrtp_load(NULL, filename) == RES_BAD_ARG); 96 CHK(atrtp_load(atrtp, NULL) == RES_BAD_ARG); 97 CHK(atrtp_load(atrtp, "nop") == RES_IO_ERR); 98 CHK(atrtp_load(atrtp, filename) == RES_OK); 99 CHK(atrtp_get_desc(atrtp, &desc) == RES_OK); 100 check_atrtp_desc(&desc, nnodes); 101 102 fclose(fp); 103 } 104 105 static void 106 test_load_fail(struct atrtp* atrtp) 107 { 108 const char byte = 1; 109 FILE* fp = NULL; 110 uint64_t pagesize; 111 uint64_t nnodes; 112 113 CHK(atrtp); 114 115 /* Wrong pagesize */ 116 fp = tmpfile(); 117 CHK(fp); 118 pagesize = 4097; 119 nnodes = 10; 120 CHK(fwrite(&pagesize, sizeof(pagesize), 1, fp) == 1); 121 CHK(fwrite(&nnodes, sizeof(nnodes), 1, fp) == 1); 122 CHK(fseek(fp, (long)ALIGN_SIZE((size_t)ftell(fp), pagesize), SEEK_SET) == 0); 123 CHK(fseek(fp, (long)(sizeof(double[ATRTP_COUNT__])*nnodes), SEEK_CUR) == 0); 124 CHK(fseek(fp, (long)ALIGN_SIZE((size_t)ftell(fp), pagesize)-1, SEEK_SET) == 0); 125 CHK(fwrite(&byte, sizeof(byte), 1, fp) == 1); /* Positioned the EOF */ 126 rewind(fp); 127 CHK(atrtp_load_stream(atrtp, fp, NULL) == RES_BAD_ARG); 128 fclose(fp); 129 130 /* Wrong size */ 131 fp = tmpfile(); 132 CHK(fp); 133 pagesize = (uint64_t)sysconf(_SC_PAGESIZE); 134 nnodes = 10; 135 CHK(fwrite(&pagesize, sizeof(pagesize), 1, fp) == 1); 136 CHK(fwrite(&nnodes, sizeof(nnodes), 1, fp) == 1); 137 CHK(fseek(fp, (long)ALIGN_SIZE((size_t)ftell(fp), pagesize), SEEK_SET) == 0); 138 CHK(fseek(fp, (long)(sizeof(double[ATRTP_COUNT__])*nnodes), SEEK_CUR) == 0); 139 CHK(fseek(fp, (long)ALIGN_SIZE((size_t)ftell(fp), pagesize)-2, SEEK_SET) == 0); 140 CHK(fwrite(&byte, sizeof(byte), 1, fp) == 1); /* Positioned the EOF */ 141 142 rewind(fp); 143 CHK(atrtp_load_stream(atrtp, fp, NULL) == RES_IO_ERR); 144 fclose(fp); 145 } 146 147 static void 148 test_load_files(struct atrtp* atrtp, int argc, char** argv) 149 { 150 int i; 151 CHK(atrtp); 152 FOR_EACH(i, 1, argc) { 153 struct atrtp_desc desc = ATRTP_DESC_NULL; 154 size_t icell; 155 156 printf("Load %s\n", argv[i]); 157 CHK(atrtp_load(atrtp, argv[i]) == RES_OK); 158 CHK(atrtp_get_desc(atrtp, &desc) == RES_OK); 159 160 FOR_EACH(icell, 0, desc.nnodes) { 161 const double* props = atrtp_desc_get_node_properties(&desc, icell); 162 size_t iprop; 163 FOR_EACH(iprop, 0, ATRTP_COUNT__) { 164 CHK(props[iprop] == props[iprop]); /* !NaN */ 165 } 166 CHK(props[ATRTP_PRESSURE] >= 0); 167 CHK(props[ATRTP_TEMPERATURE] >= 0); 168 CHK(props[ATRTP_XH20] >= 0); 169 CHK(props[ATRTP_XCO2] >= 0); 170 CHK(props[ATRTP_XCO] >= 0); 171 CHK(props[ATRTP_SOOT_VOLFRAC] >= 0); 172 CHK(props[ATRTP_SOOT_VOLFRAC] <= 1); 173 CHK(props[ATRTP_SOOT_PRIMARY_PARTICLES_COUNT] >= 0); 174 CHK(props[ATRTP_SOOT_PRIMARY_PARTICLES_DIAMETER] >= 0); 175 } 176 } 177 } 178 179 /******************************************************************************* 180 * Main function 181 ******************************************************************************/ 182 int 183 main(int argc, char** argv) 184 { 185 struct atrtp* atrtp = NULL; 186 (void)argc, (void)argv; 187 188 CHK(atrtp_create(NULL, &mem_default_allocator, 1, &atrtp) == RES_OK); 189 190 test_load(atrtp); 191 test_load_fail(atrtp); 192 test_load_files(atrtp, argc, argv); 193 194 CHK(atrtp_ref_put(atrtp) == RES_OK); 195 CHK(mem_allocated_size() == 0); 196 return 0; 197 }