htcp

Properties of water suspended in clouds
git clone git://git.meso-star.fr/htcp.git
Log | Files | Refs | README | LICENSE

test_htcp_load.c (5524B)


      1 /* Copyright (C) 2018, 2020-2023, 2025, 2025 |Méso|Star> (contact@meso-star.com)
      2  * Copyright (C) 2018 Centre National de la Recherche Scientifique
      3  * Copyright (C) 2018 Université Paul Sabatier
      4  *
      5  * This program is free software: you can redistribute it and/or modify
      6  * it under the terms of the GNU General Public License as published by
      7  * the Free Software Foundation, either version 3 of the License, or
      8  * (at your option) any later version.
      9  *
     10  * This program is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     13  * GNU General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU General Public License
     16  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     17 
     18 #include "htcp.h"
     19 #include "test_htcp_utils.h"
     20 
     21 #include <rsys/double3.h>
     22 
     23 #include <unistd.h>
     24 
     25 int
     26 main(int argc, char** argv)
     27 {
     28   int64_t pagesize;
     29   struct htcp* htcp = NULL;
     30   struct htcp_desc desc = HTCP_DESC_NULL;
     31   FILE* stream = NULL;
     32   int8_t i8;
     33   int32_t i32[4];
     34   double dbl[3];
     35   size_t i, n;
     36   size_t x, y, z, t;
     37   (void)argc, (void)argv;
     38 
     39   stream = tmpfile();
     40 
     41   pagesize = (int64_t)sysconf(_SC_PAGESIZE); /* pagesize */
     42   CHK(fwrite(&pagesize, sizeof(pagesize), 1, stream) == 1);
     43   i8 = 0; /* irregular Z */
     44   CHK(fwrite(&i8, sizeof(i8), 1, stream) == 1);
     45   i32[0] = 2; /* Definition */
     46   i32[1] = 2;
     47   i32[2] = 3;
     48   i32[3] = 1;
     49   CHK(fwrite(i32, sizeof(i32[0]), 4, stream) == 4);
     50   dbl[0] = 0; /* Lower position */
     51   dbl[1] = 0;
     52   dbl[2] = 0;
     53   CHK(fwrite(dbl, sizeof(dbl[0]), 3, stream) == 3);
     54   dbl[0] = 1; /* Voxel size */
     55   dbl[1] = 2;
     56   dbl[2] = 3;
     57   CHK(fwrite(dbl, sizeof(dbl[0]), 3, stream) == 3);
     58 
     59   n = (size_t)(i32[0]*i32[1]*i32[2]*i32[3]);
     60   fseek(stream, ALIGN_SIZE(ftell(stream), pagesize), SEEK_SET); /* padding */
     61   FOR_EACH(i, 0, n) { /* RVT */
     62     dbl[0] = (double)i;
     63     CHK(fwrite(dbl, sizeof(dbl[0]), 1, stream) == 1);
     64   }
     65 
     66   fseek(stream, ALIGN_SIZE(ftell(stream), pagesize), SEEK_SET); /* padding */
     67   FOR_EACH(i, 0, n) { /* RCT */
     68     dbl[0] =-(double)i;
     69     CHK(fwrite(dbl, sizeof(dbl[0]), 1, stream) == 1);
     70   }
     71   fseek(stream, ALIGN_SIZE(ftell(stream), pagesize), SEEK_SET); /* padding */
     72 
     73   FOR_EACH(i, 0, n) { /* PABST */
     74     dbl[0] =(double)(i*2);
     75     CHK(fwrite(dbl, sizeof(dbl[0]), 1, stream) == 1);
     76   }
     77   fseek(stream, ALIGN_SIZE(ftell(stream), pagesize), SEEK_SET); /* padding */
     78 
     79   FOR_EACH(i, 0, n) { /* T */
     80     dbl[0] =-(double)(i*2);
     81     CHK(fwrite(dbl, sizeof(dbl[0]), 1, stream) == 1);
     82   }
     83   if(!IS_ALIGNED(ftell(stream), (size_t)pagesize)) {
     84     /* Padding to ensure that the size is aligned on the page size. Note that
     85      * one char is written to positioned the EOF indicator */
     86     const char byte = 0;
     87     fseek(stream, ALIGN_SIZE(ftell(stream), pagesize)-1, SEEK_SET);
     88     CHK(fwrite(&byte, 1, 1, stream) == 1);
     89   }
     90 
     91   rewind(stream);
     92 
     93   CHK(htcp_create(NULL, &mem_default_allocator, 1, &htcp) == RES_OK);
     94 
     95   CHK(htcp_get_desc(NULL, &desc) == RES_BAD_ARG);
     96   CHK(htcp_get_desc(htcp, NULL) == RES_BAD_ARG);
     97   CHK(htcp_get_desc(htcp, &desc) == RES_BAD_ARG);
     98 
     99   CHK(htcp_load_stream(NULL, stream) == RES_BAD_ARG);
    100   CHK(htcp_load_stream(htcp, NULL) == RES_BAD_ARG);
    101   CHK(htcp_load_stream(htcp, stream) == RES_OK);
    102   fclose(stream);
    103 
    104   CHK(htcp_get_desc(NULL, &desc) == RES_BAD_ARG);
    105   CHK(htcp_get_desc(htcp, NULL) == RES_BAD_ARG);
    106   CHK(htcp_get_desc(htcp, &desc) == RES_OK);
    107 
    108   CHK(desc.pagesize == 4096);
    109   CHK(desc.irregular_z == 0);
    110   CHK(desc.spatial_definition[0] == 2);
    111   CHK(desc.spatial_definition[1] == 2);
    112   CHK(desc.spatial_definition[2] == 3);
    113   CHK(desc.time_definition == 1);
    114   CHK(desc.lower[0] == 0);
    115   CHK(desc.lower[1] == 0);
    116   CHK(desc.lower[2] == 0);
    117   CHK(desc.upper[0] == desc.vxsz_x * (double)desc.spatial_definition[0]);
    118   CHK(desc.upper[1] == desc.vxsz_y * (double)desc.spatial_definition[1]);
    119   CHK(desc.upper[2] == desc.vxsz_z[0] * (double)desc.spatial_definition[2]);
    120   CHK(desc.vxsz_x == 1);
    121   CHK(desc.vxsz_y == 2);
    122   CHK(desc.vxsz_z[0] == 3);
    123   CHK(desc.coord_z == NULL);
    124 
    125   FOR_EACH(x, 0, desc.spatial_definition[0]) {
    126     double low[3];
    127     low[0] = (double)x * desc.vxsz_x;
    128     FOR_EACH(y, 0, desc.spatial_definition[1]) {
    129       low[1] = (double)y * desc.vxsz_y;
    130       FOR_EACH(z, 0, desc.spatial_definition[2]) {
    131         double vox_low[3], vox_upp[3];
    132         double upp[3];
    133         low[2] = (double)z * desc.vxsz_z[0];
    134         upp[0] = low[0] + desc.vxsz_x;
    135         upp[1] = low[1] + desc.vxsz_y;
    136         upp[2] = low[2] + desc.vxsz_z[0];
    137         htcp_desc_get_voxel_aabb(&desc, x, y, z, vox_low, vox_upp);
    138         CHK(d3_eq_eps(vox_low, low, 1.e-6));
    139         CHK(d3_eq_eps(vox_upp, upp, 1.e-6));
    140       }
    141     }
    142   }
    143 
    144   n = desc.spatial_definition[0]
    145     * desc.spatial_definition[1]
    146     * desc.spatial_definition[2]
    147     * desc.time_definition;
    148   FOR_EACH(i, 0,n) {
    149     CHK(desc.RVT[i] == (double)i);
    150     CHK(desc.RCT[i] ==-(double)i);
    151   }
    152   i = 0;
    153   FOR_EACH(t, 0, desc.time_definition) {
    154   FOR_EACH(z, 0, desc.spatial_definition[2]) {
    155   FOR_EACH(y, 0, desc.spatial_definition[1]) {
    156   FOR_EACH(x, 0, desc.spatial_definition[0]) {
    157     CHK(htcp_desc_RVT_at(&desc, x, y, z, t) == (double)i);
    158     CHK(htcp_desc_RCT_at(&desc, x, y, z, t) ==-(double)i);
    159     CHK(htcp_desc_PABST_at(&desc, x, y, z, t) ==(double)(i*2));
    160     CHK(htcp_desc_T_at(&desc, x, y, z, t) ==-(double)(i*2));
    161     ++i;
    162   }}}}
    163   CHK(htcp_ref_put(htcp) == RES_OK);
    164 
    165   CHK(mem_allocated_size() == 0);
    166   return 0;
    167 }
    168