File: tst_h4_lendian.c

package info (click to toggle)
netcdf 1:4.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 104,952 kB
  • sloc: ansic: 228,683; sh: 10,980; yacc: 2,561; makefile: 1,319; lex: 1,173; xml: 173; awk: 2
file content (138 lines) | stat: -rw-r--r-- 3,694 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*! Testing for proper read of little-endian variables in an hdf4 file.
 *
 * Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
 * 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014,
 * 2015, 2016, 2017, 2018
 * University Corporation for Atmospheric Research/Unidata.
 *
 * See \ref copyright file for more info.
 * Added to debug issue NCF-332. Based on code submitted by
 * https://github.com/Unidata/netcdf-c/issues/113.
 */

#include <config.h>
#include <unistd.h>
#include <nc_tests.h>
#include "mfhdf.h"
#include "err_macros.h"

#define DIM1 5
#define DIM0 5
#define RANK 2
#define FILENAME "tst_h4_lendian.h4"
#define SDSNAME "data"

/* Read the file. Return -1 if endianness is not little. */
int read_hdf_file(int dtype)
{
   int ncid = 0;
   int le_int16_varid = 0;
   int ed = 0;

   if (nc_open(FILENAME, NC_NETCDF4 | NC_NOWRITE, &ncid)) ERR;
   if (nc_inq_varid(ncid,SDSNAME,&le_int16_varid)) ERR;
   if (nc_inq_var_endian(ncid,le_int16_varid,&ed)) ERR;
   if (nc_close(ncid)) ERR;

   if (ed != NC_ENDIAN_LITTLE)
      return -1;

   return 0;
}

/* Create a HDF4 file with one dataset of type dtype. */
int create_hdf_file(int dtype)
{
   int32 sd_id, sds_id;
   int32 start[2] = {0, 0}, edges[2] = {DIM1, DIM0};
   int16 array_data[DIM0][DIM1];
   int32 array_data_int32[DIM0][DIM1];
   float32 array_data_float32[DIM0][DIM1];
   float64 array_data_float64[DIM0][DIM1];
   void *data;
   intn i, j, count;

   /* Populate data arrays. */
   count = 0;
   for (j = 0; j < DIM0; j++)
   {
      for (i = 0; i < DIM1; i++)
      {
         array_data[j][i] = count;
         array_data_int32[j][i] = count;
         array_data_float32[j][i] = count;
         array_data_float64[j][i] = count++;
      }
   }

   /* Point to the correct data. */
   switch(dtype)
   {
   case DFNT_LINT8:
   case DFNT_LUINT8:
   case DFNT_LINT16:
   case DFNT_LUINT16:
      data = array_data;
      break;
   case DFNT_LINT32:
   case DFNT_LUINT32:
      data = array_data_int32;
      break;
   case DFNT_LFLOAT32:
      data = array_data_float32;
      break;
   case DFNT_LFLOAT64:
      data = array_data_float64;
      break;
   default:
      return -1;
   }

   if ((sd_id = SDstart(FILENAME, DFACC_CREATE)) == -1) ERR;
   if ((sds_id = SDcreate(sd_id, SDSNAME, dtype, RANK, edges)) == -1) ERR;
   if (SDwritedata(sds_id, start, NULL, edges, data)) ERR;
   if (SDend(sd_id)) ERR;

   return 0;
}

/* Create and then read the HDF4 test file. */
int test_read_write(int dtype)
{
   if (create_hdf_file(dtype))
      return -1;
   return read_hdf_file(dtype);
}

int main()
{
   printf("\n***Test reading from an hdf4 file with a little-endian datatype...\n");
   printf("*** testing reading...");
   {
      if (test_read_write(DFNT_LINT8)) ERR;
      if (test_read_write(DFNT_LUINT8)) ERR;
      if (test_read_write(DFNT_LINT16)) ERR;
      if (test_read_write(DFNT_LUINT16)) ERR;
      if (test_read_write(DFNT_LINT32)) ERR;
      if (test_read_write(DFNT_LUINT32)) ERR;
      if (test_read_write(DFNT_LFLOAT32)) ERR;
      /* if (test_read_write(DFNT_LFLOAT64)) ERR; */
   }
   SUMMARIZE_ERR;

   printf("*** testing for True Negatives. these will return error...");
   {
      /* True Negatives. */
      if (test_read_write(DFNT_INT8) != -1) ERR;
      if (test_read_write(DFNT_UINT8) != -1) ERR;
      if (test_read_write(DFNT_INT16) != -1) ERR;
      if (test_read_write(DFNT_UINT16) != -1) ERR;
      if (test_read_write(DFNT_INT32) != -1) ERR;
      if (test_read_write(DFNT_UINT32) != -1) ERR;
      if (test_read_write(DFNT_FLOAT32) != -1) ERR;
      if (test_read_write(DFNT_FLOAT64) != -1) ERR;
   }
   SUMMARIZE_ERR;

   FINAL_RESULTS;
}