File: tsdsprops.c

package info (click to toggle)
libhdf4 4.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 30,384 kB
  • sloc: ansic: 128,700; sh: 15,015; fortran: 12,444; java: 5,863; xml: 1,205; makefile: 794; yacc: 678; pascal: 418; perl: 360; javascript: 203; lex: 163; csh: 41
file content (770 lines) | stat: -rw-r--r-- 27,001 bytes parent folder | download | duplicates (2)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF.  The full HDF copyright notice, including       *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF/releases/.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/****************************************************************************
 * tsdsprops.c - tests functionality regarding an SDS' properties, such
 *		 name, dimensions, type, size,...
 * Structure of the file:
 *    test_SDSprops - test driver
 *	  test_SDSnames    - tests that an SDS name can be more than
 *		previously hardcoded at 64 characters; now length is variable.
 *	  test_unlim_dim - tests that writing two unlimited 1D datasets,
 *		without closing the file between writes, will not cause the
 *		second dataset to contain garbage anymore. (bug 525)
 *	  test_unlim_inloop - tests that appending unlimited data to more
 *		than one dataset within a loop stores data correctly. (bug 801)
 *	  test_valid_args - tests that when some invalid arguments were passed
 *		into an API, they can be caught and handled properly.
 *		(bugzilla 150)
 ****************************************************************************/

#include <stdlib.h>
#include <string.h>

#include "mfhdf.h"

#include "hdftest.h"

/***************************************************************************
   Name: test_SDSnames() - tests that SDS name is no longer restricted to
                           64 characters only. (bugzilla 516)
   Description:
        The main contents include:
        - create data sets with very short and very long names
        - close the file, then reopen it, get SDS' info, and verify the names

   Return value:
        The number of errors occurred in this routine.

****************************************************************************/

#define FILE_NAME "SDSlongname.hdf" /* data file to test SDS names */
#define X_LENGTH  10
#define Y_LENGTH  10
#define RANK      2
#define NAME_LEN1 93 /* the length of longname_ds below */
#define NAME_LEN2 1  /* the length of shortname_ds below */

#define longname_ds                                                                                          \
    "The name of this dataset is long, and it is used to test the new variable length name feature"
#define shortname_ds "A"

static intn
test_SDSnames()
{
    int32  fid, dset1, dset2;
    int32  rank, dtype, nattrs, dimsizes[RANK];
    char  *ds_name;
    uint16 name_len;
    intn   status;
    intn   num_errs = 0; /* number of errors so far */

    /* Create a file */
    fid = SDstart(FILE_NAME, DFACC_CREATE);
    CHECK(fid, FAIL, "SDstart");

    /* Create an X_LENGTH by Y_LENGTH dataset, called DataSet_1 */
    dimsizes[0] = X_LENGTH;
    dimsizes[1] = Y_LENGTH;
    dset1       = SDcreate(fid, longname_ds, DFNT_INT32, RANK, dimsizes);
    CHECK(dset1, FAIL, "SDcreate");

    dset2 = SDcreate(fid, shortname_ds, DFNT_INT32, RANK, dimsizes);
    CHECK(dset2, FAIL, "SDcreate");

    /* Close the datasets */
    status = SDendaccess(dset1);
    CHECK(status, FAIL, "SDendaccess");

    status = SDendaccess(dset2);
    CHECK(status, FAIL, "SDendaccess");

    /* Close the file */
    status = SDend(fid);
    CHECK(status, FAIL, "SDend");

    /* Re-open the file to check on the SDS name */
    fid = SDstart(FILE_NAME, DFACC_RDWR);
    CHECK(fid, FAIL, "SDstart");

    /* Get access to the first dataset */
    dset1 = SDselect(fid, 0);
    CHECK(dset1, FAIL, "SDselect");

    /* Get and verify the length of its name */
    status = SDgetnamelen(dset1, &name_len);
    CHECK(status, FAIL, "SDgetnamelen");
    VERIFY(name_len, NAME_LEN1, "SDgetnamelen");

    /* Allocate buffer to get its name */
    ds_name = (char *)malloc(name_len + 1);
    CHECK_ALLOC(ds_name, "ds_name", "test_SDSnames");

    /* Get information of the first dataset, and verify its name */
    status = SDgetinfo(dset1, ds_name, &rank, dimsizes, &dtype, &nattrs);
    CHECK(status, FAIL, "SDgetinfo");
    VERIFY_CHAR(ds_name, longname_ds, "SDgetinfo");

    /* Close the datasets */
    status = SDendaccess(dset1);
    CHECK(status, FAIL, "SDendaccess");

    /* Release allocated memory */
    free(ds_name);

    /* Get access to the second dataset */
    dset2 = SDselect(fid, 1);
    CHECK(dset2, FAIL, "SDselect");

    /* Get and verify the length of its name */
    name_len = 0;
    status   = SDgetnamelen(dset2, &name_len);
    CHECK(status, FAIL, "SDgetnamelen");
    VERIFY(name_len, NAME_LEN2, "SDgetnamelen");

    /* Allocate buffer to get its name */
    ds_name = (char *)malloc(name_len + 1);
    CHECK_ALLOC(ds_name, "ds_name", "test_SDSnames");

    /* Get information of the second dataset, and verify its name */
    status = SDgetinfo(dset2, ds_name, &rank, dimsizes, &dtype, &nattrs);
    CHECK(status, FAIL, "SDgetinfo");
    VERIFY_CHAR(ds_name, shortname_ds, "SDgetinfo");

    /* Close the datasets */
    status = SDendaccess(dset2);
    CHECK(status, FAIL, "SDendaccess");

    /* Release allocated memory */
    free(ds_name);

    /* Close the file */
    status = SDend(fid);
    CHECK(status, FAIL, "SDend");

    /* Return the number of errors that's been kept track of, so far */
    return num_errs;
} /* test_SDSnames */

/***************************************************************************
   Name: test_unlim_dim() - tests that writing two unlimited 1D datasets,
                            without closing the file between writes, will
                            not cause the second dataset to contain garbage
                            anymore. (bug 525)
   Description:
        The main contents include:
        - create and write to two datasets
        - append data to both without closing anything
        - close the datasets and the file
        - reopen the file, get the data sets and their info
        - read and verify the data

   Return value:
        The number of errors occurred in this routine.

****************************************************************************/

#define UD_FILE_NAME "Unlim_dim.hdf" /* data file to test unlimited dim */
#define DS1_NAME     "data 1"
#define DS2_NAME     "data 2"
#define DIM0         10

static intn
test_unlim_dim()
{
    int32 fid, dset1, dset2;
    int32 rank, start[1], edges[1], dtype, nattrs, dimsizes[1];
    int16 array_data[DIM0],                          /* data to be written to both datasets */
        append_data[DIM0],                           /* data to be appended to both datasets */
        outdata[DIM0 + DIM0], outdata1[DIM0 + DIM0]; /* data read */
    char ds_name[20];
    intn idx, status;
    intn num_errs = 0; /* number of errors so far */

    /* Create a file */
    fid = SDstart(UD_FILE_NAME, DFACC_CREATE);
    CHECK(fid, FAIL, "SDstart");

    /* Define the rank and dimensions of the data set to be created. */
    rank        = 1;
    dimsizes[0] = SD_UNLIMITED;

    /* Create two array data sets. */
    dset1 = SDcreate(fid, DS1_NAME, DFNT_INT16, rank, dimsizes);
    CHECK(dset1, FAIL, "SDcreate");

    dset2 = SDcreate(fid, DS2_NAME, DFNT_INT16, rank, dimsizes);
    CHECK(dset2, FAIL, "SDcreate");

    /* Fill the stored-data array with values. */
    for (idx = 0; idx < DIM0; idx++) {
        array_data[idx] = idx + 1;
    }

    /* Define the location, pattern, and size of the data sets */
    start[0] = 0;
    edges[0] = DIM0;

    status = SDwritedata(dset1, start, NULL, edges, (void *)array_data);
    CHECK(status, FAIL, "SDwritedata");
    status = SDwritedata(dset2, start, NULL, edges, (void *)array_data);
    CHECK(status, FAIL, "SDwritedata");

    /* Store array values to be appended */
    for (idx = 0; idx < DIM0; idx++)
        append_data[idx] = DIM0 + idx;

    /* Define the location of the append */
    start[0] = DIM0; /* start from the end of the previous write */
    edges[0] = DIM0; /* append the same number of elements as the write */

    /* Append data to file */
    status = SDwritedata(dset1, start, NULL, edges, (void *)append_data);
    CHECK(status, FAIL, "SDwritedata");
    status = SDwritedata(dset2, start, NULL, edges, (void *)append_data);
    CHECK(status, FAIL, "SDwritedata");

    /* Close the datasets */
    status = SDendaccess(dset1);
    CHECK(status, FAIL, "SDendaccess");

    status = SDendaccess(dset2);
    CHECK(status, FAIL, "SDendaccess");

    /* Close the file */
    status = SDend(fid);
    CHECK(status, FAIL, "SDend");

    /* Re-open the file to check on the SDS names */
    fid = SDstart(UD_FILE_NAME, DFACC_RDWR);
    CHECK(fid, FAIL, "SDstart");

    /* Get access to the datasets */
    dset1 = SDselect(fid, 0);
    CHECK(dset1, FAIL, "SDselect");
    dset2 = SDselect(fid, 1);
    CHECK(dset1, FAIL, "SDselect");

    /* Get information of the first dataset, and verify its name */
    status = SDgetinfo(dset1, ds_name, &rank, dimsizes, &dtype, &nattrs);
    CHECK(status, FAIL, "SDgetinfo");

    /* Define the location to read */
    start[0] = 0;
    edges[0] = DIM0 + DIM0;

    /* Read and check first dataset */
    status = SDreaddata(dset1, start, NULL, edges, (void *)outdata);
    CHECK(status, FAIL, "SDreaddata");

    for (idx = 0; idx < DIM0; idx++) {
        VERIFY(outdata[idx], array_data[idx], "SDreaddata first dataset");
    }
    for (idx = DIM0; idx < edges[0]; idx++) {
        VERIFY(outdata[idx], append_data[idx - DIM0], "SDreaddata first dataset");
    }

    /* Read and check second dataset */
    status = SDreaddata(dset2, start, NULL, edges, (void *)outdata1);
    CHECK(status, FAIL, "SDreaddata");

    for (idx = 0; idx < DIM0; idx++) {
        VERIFY(outdata1[idx], array_data[idx], "SDreaddata first dataset");
    }
    for (idx = DIM0; idx < edges[0]; idx++) {
        VERIFY(outdata1[idx], append_data[idx - DIM0], "SDreaddata first dataset");
    }

    /* Close the datasets */
    status = SDendaccess(dset1);
    CHECK(status, FAIL, "SDendaccess");

    /* Close the datasets */
    status = SDendaccess(dset2);
    CHECK(status, FAIL, "SDendaccess");

    /* Close the file */
    status = SDend(fid);
    CHECK(status, FAIL, "SDend");

    /* Return the number of errors that's been kept track of, so far */
    return num_errs;
} /* test_unlim_dim */

/***************************************************************************
   Name: test_unlim_inloop() - tests that appending unlimited data to more
                            than one dataset within a loop stores data
                            correctly. (bug 801)
   Description:
        The main contents include:
        - create two data sets
        - write to the data sets in a loop
        - close the file, then reopen it
        - in another loop, read the data sets and verify their data

   Return value:
        The number of errors occurred in this routine.

****************************************************************************/

#define UDIL_FILE_NAME "Unlim_inloop.hdf" /* file to test unlim dims in loop */
#define SIZE           5
#define N_DSETS        2
#define RANK1          1

static intn
test_unlim_inloop()
{
    int32 fid;
    int32 sds_id[N_DSETS];
    int32 start[RANK1], edges[RANK1], rank, dimsizes[RANK1];
    int32 dtype, nattrs;
    char  sds_name[20];
    int   i, n_writes; /* number of times writing to the data sets */
    /*int32   n,m, mm;*/
    intn    status;
    float64 array_data[SIZE];
    intn    num_errs = 0; /* number of errors so far */

    /* Create a file */
    fid = SDstart(UDIL_FILE_NAME, DFACC_CREATE);
    CHECK(fid, FAIL, "SDstart");

    dimsizes[0] = SD_UNLIMITED;

    /* Create N_DSETS data sets in a loop */
    for (i = 0; i < N_DSETS; i++) {
        sprintf(sds_name, "test%d", i);
        sds_id[i] = SDcreate(fid, sds_name, DFNT_FLOAT64, 1, dimsizes);
        CHECK(sds_id[i], FAIL, "SDcreate");

        status = SDendaccess(sds_id[i]);
        CHECK(status, FAIL, "SDendaccess");
    }

    /* Making up data to write */
    for (i = 0; i < SIZE; i++) {
        array_data[i] = i + 1;
    }

    /* write and append to the data sets */
    n_writes = 0;
    edges[0] = SIZE;
    while (n_writes < 2) {
        /* i.e, start at 0 first time and at SIZE second time*/
        start[0] = SIZE * n_writes;

        for (i = 0; i < N_DSETS; i++) {
            sds_id[i] = SDselect(fid, i);
            CHECK(sds_id[i], FAIL, "SDselect");

            status = SDwritedata(sds_id[i], start, NULL, edges, (void *)array_data);
            CHECK(status, FAIL, "SDwritedata");

            status = SDendaccess(sds_id[i]);
            CHECK(status, FAIL, "SDendaccess");
        }
        n_writes++;
    }

    /* close the file */
    status = SDend(fid);
    CHECK(status, FAIL, "SDcreate");

    /* Re-open the file to check on the SDS name */
    fid = SDstart(UDIL_FILE_NAME, DFACC_RDWR);
    CHECK(fid, FAIL, "SDstart");

    for (i = 0; i < N_DSETS; i++) {
        float64 outdata[SIZE * 2];

        /* Get access to the datasets */
        sds_id[i] = SDselect(fid, i);

        /* Get information of the first dataset, and verify its name */
        status = SDgetinfo(sds_id[i], sds_name, &rank, dimsizes, &dtype, &nattrs);
        CHECK(status, FAIL, "SDgetinfo");

        /* Define the location to read */
        start[0] = 0;
        edges[0] = SIZE * n_writes;

        /* Read and check first dataset */
        status = SDreaddata(sds_id[i], start, NULL, edges, (void *)outdata);
        CHECK(status, FAIL, "SDreaddata");

        /* Verify the read data */
        n_writes = 0;
        while (n_writes < 2) {
            intn in, out;
            for (in = 0, out = 0 + (SIZE * n_writes); in < SIZE; in++, out++) {
                VERIFY(outdata[out], array_data[in], "SDreaddata");
            }
            n_writes++;
        }

        /* Close the datasets */
        status = SDendaccess(sds_id[i]);
        CHECK(status, FAIL, "SDendaccess");
    }

    /* close the file */
    status = SDend(fid);
    CHECK(status, FAIL, "SDcreate");

    /* Return the number of errors that's been kept track of, so far */
    return num_errs;
} /* test_unlim_inloop */

/***************************************************************************
   Name: test_valid_args() - tests that when some invalid arguments were passed
                            into an API, they can be caught and handled
                            properly. (bugzilla 150)
   Description:
        The main contents include:
        - create a data set of size X_LENGTH x Y_LENGTH
        - write to the data set
        - close the file, then reopen it
        - read the dataset giving a stride value, that goes beyond the
          dimension size
        - when SDreaddata failed, try to check for error code DFE_ARGS and
          handle the failure properly
        - close all.

   Return value:
        The number of errors occurred in this routine.

****************************************************************************/

#define ARGS_FILE_NAME "test_arguments.hdf" /* file to test validating args */

static intn
test_valid_args()
{
    int32 fid, dset1, dset2;
    int32 start[2], edges[2], dtype, nattrs, dimsizes[2], rank, strides[2];
    int16 array_data[X_LENGTH][Y_LENGTH], /* data to be written to datasets */
        outdata[X_LENGTH][Y_LENGTH];      /* data read */
    char ds_name[20];
    intn idxx, idxy, status;
    intn num_errs = 0; /* number of errors so far */

    /* Create a file */
    fid = SDstart(ARGS_FILE_NAME, DFACC_CREATE);
    CHECK(fid, FAIL, "SDstart");

    /* Create two array data sets. */
    dimsizes[0] = X_LENGTH;
    dimsizes[1] = Y_LENGTH;
    dset1       = SDcreate(fid, DS1_NAME, DFNT_INT16, RANK, dimsizes);
    CHECK(dset1, FAIL, "SDcreate");

    dimsizes[0] = SD_UNLIMITED;
    dset2       = SDcreate(fid, DS2_NAME, DFNT_INT16, RANK, dimsizes);
    CHECK(dset2, FAIL, "SDcreate");

    /* Fill the stored-data array with values. */
    for (idxx = 0; idxx < X_LENGTH; idxx++) {
        for (idxy = 0; idxy < Y_LENGTH; idxy++) {
            array_data[idxx][idxy] = idxx * idxy + 1;
        }
    }

    /* Define the location, pattern, and size of the data sets */
    start[0] = 0;
    start[1] = 0;
    edges[0] = X_LENGTH;
    edges[1] = Y_LENGTH;

    status = SDwritedata(dset1, start, NULL, edges, (void *)array_data);
    CHECK(status, FAIL, "SDwritedata");
    status = SDwritedata(dset2, start, NULL, edges, (void *)array_data);
    CHECK(status, FAIL, "SDwritedata");

    /* Close the datasets */
    status = SDendaccess(dset1);
    CHECK(status, FAIL, "SDendaccess");

    status = SDendaccess(dset2);
    CHECK(status, FAIL, "SDendaccess");

    /* Close the file */
    status = SDend(fid);
    CHECK(status, FAIL, "SDend");

    /* Re-open the file to check on the SDS names */
    fid = SDstart(ARGS_FILE_NAME, DFACC_RDWR);
    CHECK(fid, FAIL, "SDstart");

    /* Get access to the datasets */
    dset1 = SDselect(fid, 0);
    CHECK(dset1, FAIL, "SDselect");
    dset2 = SDselect(fid, 1);
    CHECK(dset1, FAIL, "SDselect");

    /* Get information of the first dataset, and verify its name */
    status = SDgetinfo(dset1, ds_name, &rank, dimsizes, &dtype, &nattrs);
    CHECK(status, FAIL, "SDgetinfo");

    /* Define the location to read */
    strides[0] = X_LENGTH; /* simulate a mistake here, should have -1 */
    strides[1] = Y_LENGTH - 1;

    /* Attempt to read first dataset, it should fail with invalid an
        argument error */
    status = SDreaddata(dset1, start, strides, edges, (void *)outdata);
    VERIFY(status, FAIL, "SDreaddata");
    VERIFY(HEvalue(1), DFE_ARGS, "SDreaddata");

    /* Show that the error was also caught for the dataset with unlimited
        dimension */
    status = SDreaddata(dset2, start, strides, edges, (void *)outdata);
    VERIFY(status, FAIL, "SDreaddata");
    VERIFY(HEvalue(1), DFE_ARGS, "SDreaddata");

    /* Close the datasets */
    status = SDendaccess(dset1);
    CHECK(status, FAIL, "SDendaccess");

    /* Close the datasets */
    status = SDendaccess(dset2);
    CHECK(status, FAIL, "SDendaccess");

    /* Close the file */
    status = SDend(fid);
    CHECK(status, FAIL, "SDend");

    /* Return the number of errors that's been kept track of, so far */
    return num_errs;
} /* test_valid_args */

/*******************************************************************
   Name: test_valid_args2() - tests that when some invalid arguments were passed
                            into SDreaddata, causing the function to attempt
                            to read beyond the dimension size, can be caught
                            and handled properly. (more for bugzilla 150)
   Description:
        The main contents include:
        - creates a file and three datasets:
          + "data1" dimsizes=[1] int32 read the one element
          + "data2" dimsizes=[202,271]
          + "data3" dimsizes=[10,3,2]
        - close the file and reopen it to check reading data
        - read the datasets as followed to test error checkings on parameters
          + "data1" - attempts to read with incorrect stride value
          + "data1" - attempts to read with incorrect count value
          + "data1" - reads again with all arguments in bounds
          + "data2" - attempts to read with strides[1]=D2_Y (correct: D2_Y-1)
          + "data2" - attempts to read with count[1]=3 (correct: 2)
          + "data3" - attempts to read with incorrect combination of stride and count
          + "data3" - reads again with all arguments in bounds

********************************************************************/
#define D1_X 1
#define D2_X 202
#define D2_Y 271
#define D3_X 10
#define D3_Y 3
#define D3_Z 2

/* Helper function to test_valid_args2 creates and writes to a dataset */
static intn
makeSDS(int32 sd_id, char *name, int32 dtype, int32 rank, int32 *dimsizes, int32 *start, int32 *strides,
        int32 *count, void *data)
{
    int32 sds_id;
    intn  status;
    intn  num_errs = 0; /* number of errors so far */

    /* Create the named dataset */
    sds_id = SDcreate(sd_id, name, dtype, rank, dimsizes);
    CHECK(sds_id, FAIL, "SDcreate");

    /* Write to it */
    status = SDwritedata(sds_id, start, strides, count, data);
    CHECK(status, FAIL, "SDwritedata");

    /* Terminate access to the array. */
    status = SDendaccess(sds_id);
    CHECK(status, FAIL, "SDendaccess");

    return (status);
}

static intn
test_valid_args2()
{
    int32   sd_id, sds_id;
    int32   dim[1], dims2[2], dims3[3], d1start[1], d2start[2], d3start[3];
    int32   d1count[1], d2count[2], d3count[3];
    int32   d1stride[1], d2stride[2], d3stride[3];
    float32 data1 = 32.0, outdata1;
    int32   data2[D2_X][D2_Y], outdata2[D2_X][D2_Y];
    int16   data3[D3_X][D3_Y][D3_Z], outdata3[D3_X][D3_Y][D3_Z];
    intn    i, j, k, status;
    intn    num_errs = 0; /* number of errors so far */

    /* Create and open the file and initiate the SD interface. */
    sd_id = SDstart("b150.hdf", DFACC_CREATE);
    CHECK(sd_id, FAIL, "SDstart");

    /* Create the first dataset */
    dim[0]     = D1_X;
    d1start[0] = 0;
    d1count[0] = 1;
    data1      = 32.0;
    status     = makeSDS(sd_id, "data1", DFNT_FLOAT32, 1, dim, d1start, NULL, d1count, &data1);
    CHECK(status, FAIL, "makeSDS data1");

    /* Create and write to second dataset */
    dims2[0]   = D2_X;
    dims2[1]   = D2_Y;
    d2start[0] = d2start[1] = 0;
    d2count[0]              = dims2[0];
    d2count[1]              = dims2[1];
    for (i = 0; i < D2_X; i++)
        for (j = 0; j < D2_Y; j++)
            data2[i][j] = i * j;

    status = makeSDS(sd_id, "data2", DFNT_INT32, 2, dims2, d2start, NULL, d2count, data2);
    CHECK(status, FAIL, "makeSDS data2");

    /* Create and write to third dataset */
    dims3[0]   = D3_X;
    dims3[1]   = D3_Y;
    dims3[2]   = D3_Z;
    d3start[0] = d3start[1] = d3start[2] = 0;
    d3count[0]                           = dims3[0];
    d3count[1]                           = dims3[1];
    d3count[2]                           = dims3[2];
    for (i = 0; i < D3_X; i++)
        for (j = 0; j < D3_Y; j++)
            for (k = 0; k < D3_Z; k++)
                data3[i][j][k] = i * j * k;

    status = makeSDS(sd_id, "data3", DFNT_INT16, 3, dims3, d3start, NULL, d3count, data3);
    CHECK(status, FAIL, "makeSDS data3");

    /* Terminate access to the SD interface and close the file. */
    status = SDend(sd_id);
    CHECK(status, FAIL, "SDend");

    /* Reopen the file and read in some of the data */
    sd_id = SDstart("b150.hdf", DFACC_READ);
    CHECK(sd_id, FAIL, "SDstart");

    /* Read first dataset with incorrect stride value, but shouldn't fail */
    sds_id      = SDselect(sd_id, 0);
    d1start[0]  = 0;
    d1stride[0] = 2; /* irrelevant because only 1 value to be read */
    d1count[0]  = 1;
    status      = SDreaddata(sds_id, d1start, d1stride, d1count, &outdata1);
    CHECK(status, FAIL, "SDreaddata");

    /* Read first dataset with incorrect count value */
    d1start[0]  = 0;
    d1stride[0] = 1;
    d1count[0]  = 2; /* array only has 1 value */
    status      = SDreaddata(sds_id, d1start, d1stride, d1count, &outdata1);
    VERIFY(status, FAIL, "SDreaddata");

    /* Read again with "correct" arguments */
    d1count[0] = 1;
    status     = SDreaddata(sds_id, d1start, d1stride, d1count, &outdata1);
    CHECK(status, FAIL, "SDreaddata");
    VERIFY(outdata1, data1, "SDreaddata first dataset");

    /* Terminate access to the first dataset */
    status = SDendaccess(sds_id);
    CHECK(status, FAIL, "SDendaccess");

    /* Read second dataset with out of bound stride */
    sds_id     = SDselect(sd_id, 1);
    d2start[0] = d2start[1] = 0;
    d2stride[0]             = 1;
    d2stride[1]             = D2_Y; /* should be D2_Y - 1 */
    d2count[0]              = D2_X;
    d2count[1]              = 2;
    status                  = SDreaddata(sds_id, d2start, d2stride, d2count, (void *)outdata2);
    VERIFY(status, FAIL, "SDreaddata");

    /* Read second dataset with too many values requested */
    d2stride[1] = D2_Y - 1;
    d2count[1]  = 3; /* should be 2 */
    status      = SDreaddata(sds_id, d2start, d2stride, d2count, (void *)outdata2);
    VERIFY(status, FAIL, "SDreaddata");

    /* Terminate access to the second dataset */
    status = SDendaccess(sds_id);
    CHECK(status, FAIL, "SDendaccess");

    /* Read third dataset with too many values requested */
    sds_id     = SDselect(sd_id, 2);
    d3start[0] = 1;
    d3start[1] = d3start[2] = 0;
    d3stride[0]             = 3;
    d3stride[1]             = 2;
    d3stride[2]             = 1;
    d3count[0]              = 4; /* should be 3 max or smaller stride */
    d3count[1]              = 2;
    d3count[2]              = 2;
    status                  = SDreaddata(sds_id, d3start, d3stride, d3count, (void *)outdata3);
    VERIFY(status, FAIL, "SDreaddata");

    /* Read again with "correct" arguments */
    d3start[0] = 1;
    d3start[1] = d3start[2] = 0;
    d3stride[0]             = 3;
    d3stride[1]             = 2;
    d3stride[2]             = 1;
    d3count[0]              = 3;
    d3count[1]              = 2;
    d3count[2]              = 2;
    status                  = SDreaddata(sds_id, d3start, d3stride, d3count, (void *)outdata3);
    CHECK(status, FAIL, "SDreaddata");

    /* Terminate access to the third dataset */
    status = SDendaccess(sds_id);
    CHECK(status, FAIL, "SDendaccess");

    /* Terminate access to the SD interface and close the file. */
    status = SDend(sd_id);
    CHECK(status, FAIL, "SDend");

    /* Return the number of errors that's been kept track of, so far */
    return num_errs;
} /* test_valid_args2 */

/* Test driver for testing various SDS' properties. */
extern int
test_SDSprops()
{
    intn num_errs = 0; /* number of errors */

    /* Output message about test being performed */
    TESTING("various SDS' properties (tsdsprops.c)");

    num_errs = num_errs + test_SDSnames();
    num_errs = num_errs + test_unlim_dim();
    num_errs = num_errs + test_unlim_inloop();
    num_errs = num_errs + test_valid_args();
    num_errs = num_errs + test_valid_args2();

    if (num_errs == 0)
        PASSED();
    else
        H4_FAILED();

    /* Return the number of errors that's been kept track of so far */
    return num_errs;
}