summaryrefslogtreecommitdiffstats
path: root/hl/test/gen_test_ds.c
blob: 573fe20caa4b864cf37b2a89cdf9810eca11fb51 (plain)
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdfgroup.org/HDF5/doc/Copyright.html.  If you do not have          *
 * access to either file, you may request a copy from help@hdfgroup.org.     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * Purpose:     This program is run to generate an HDF5 data file with datasets
 *              that use dimension scales.
 *
 *              Compile and run this program to generate the "test_ds_xx.h5" 
 *              file, where xx is "le" on a little-endian machine and "be"
 *              on a big-endian machine.
 *              Move it to the test directory in the current branch.
 *              The test: test_foreign_scaleattached(const char *fileforeign) 
 *              in test_ds.c will read them.
 */

#include <stdlib.h>
#include <string.h>
#include "h5hltest.h"
#include "H5DSpublic.h"
#include "H5LTpublic.h"

/* prototypes */
static hid_t open_test_file(const char *fileext);
herr_t create_long_dataset(hid_t fid, const char *dsname, const char *dsidx); 
herr_t test_attach_scale(hid_t fid, hid_t did, const char *name, unsigned int idx);
herr_t test_detach_scale(hid_t fid, hid_t did, const char *name, unsigned int idx);
herr_t test_set_scalename(hid_t fid, hid_t did, const char *name, const char *scalename, unsigned int idx);
herr_t test_cmp_scalename(hid_t fid, hid_t did, const char *name, const char *scalename, unsigned int idx);

static int test_long_attachscales(const char *filename);
static int test_duplicatelong_attachscales(const char *filename);
static int test_long_scalenames(const char *filename);
static int test_samelong_scalenames(const char *filename);
static int test_foreign_scaleattached(const char *filename);


#define DIM_DATA      12
#define DIM1_SIZE     3
#define DIM2_SIZE     4
#define DIM3_SIZE     12
#define DIM4_SIZE     2
#define DIM0          0
#define DIM1          1
#define DIM2          2
#define DIM3          3

#define DATASET_NAME   "dset_"
#define DS_1_NAME      "ds_1_"
#define DS_2_NAME      "ds_2_"
#define DS_3_NAME      "ds_3_"
#define DS_4_NAME      "ds_4_"

#define SCALE_1_NAME   "scalename_1_"
#define SCALE_2_NAME   "scalename_2_"
#define SCALE_3_NAME   "scalename_3_"
#define SCALE_4_NAME   "scalename_4_"

#define FILENAME       "test_ds_"
#define FILEEXT        ".h5"

/*-------------------------------------------------------------------------
 * the main program
 *-------------------------------------------------------------------------
 */
int main(int argc , char **argv)
{
    int nerrors=0;
    char filename[65];

    
    if (argc < 2) {
        printf("Usage: gen_test [le | be]\n");
        return 1;
    }

    if ( argv[1] && (strcmp("le",argv[1])!=0) && (strcmp("be",argv[1])!=0) ) {
        printf("Usage: gen_test [le | be]\n");
        return 1;
    }

    /* create file to be used in following tests */
    strcpy(filename, FILENAME);
    strcat(filename, argv[1]);
    strcat(filename, FILEEXT);
    if(H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT) < 0) {
        nerrors = 1;
        goto error;
    }
    nerrors += test_long_attachscales(filename) < 0  ? 1 : 0;
    nerrors += test_duplicatelong_attachscales(filename) < 0  ? 1 : 0;
    nerrors += test_samelong_scalenames(filename) < 0  ? 1 : 0;
    nerrors += test_foreign_scaleattached(filename) < 0  ? 1 : 0;
    

    if(nerrors) goto error;
    printf("Dimension scales file generation passed.\n");
    return 0;

error:
    printf("***** %d DIMENSION SCALES FILE GENERATION FAILED! *****\n",nerrors);
    return 1;
}

static hid_t open_test_file(const char *fileext)
{
    char filename[65];

    strcpy(filename, FILENAME);
    strcat(filename, fileext);
    strcat(filename, FILEEXT);
    
    return H5Fopen(filename, H5F_ACC_RDWR, H5P_DEFAULT);
}

/*-------------------------------------------------------------------------
 * create "data" dataset
 *-------------------------------------------------------------------------
 */

herr_t create_long_dataset(hid_t fid, const char *name, const char *dsidx) 
{
    int     rank = 4;
    int     rankds = 1;
    hsize_t dims[4]  = {DIM1_SIZE,DIM2_SIZE,DIM3_SIZE,DIM4_SIZE};
    long    buf[DIM_DATA*3*2] = {1,2,3,4,5,6,7,8,9,10,11,12,
            1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,
            1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,
            1,2,3,4,5,6,7,8,9,10,11,12};
    hsize_t s1_dim[1]  = {DIM1_SIZE};
    hsize_t s2_dim[1]  = {DIM2_SIZE};
    hsize_t s3_dim[1]  = {DIM3_SIZE};
    hsize_t s4_dim[1]  = {DIM4_SIZE};
    long    s1_wbuf[DIM1_SIZE] = {10,20,30};
    long    s2_wbuf[DIM2_SIZE] = {100,200,300,400};
    long    s3_wbuf[DIM3_SIZE] = {10,10,10,20,20,20,30,30,30,40,40,40};
    long    s4_wbuf[DIM4_SIZE] = {18,18};

    /* make a dataset */
    if(H5LTmake_dataset_long(fid, name, rank, dims, buf) >= 0) {
        /* make a DS dataset for the first dimension */
        char dsname[32];

        strcpy(dsname, DS_1_NAME);
        strcat(dsname, dsidx);
        /* make a DS dataset for the first dimension */
        if(H5LTmake_dataset_long(fid, dsname, rankds, s1_dim, s1_wbuf) < 0)
            return FAIL;

        strcpy(dsname, DS_2_NAME);
        strcat(dsname, dsidx);
        /* make a DS dataset for the first dimension */
        if(H5LTmake_dataset_long(fid, dsname, rankds, s2_dim, s2_wbuf) < 0)
            return FAIL;

        strcpy(dsname, DS_3_NAME);
        strcat(dsname, dsidx);
        /* make a DS dataset for the first dimension */
        if(H5LTmake_dataset_long(fid, dsname, rankds, s3_dim, s3_wbuf) < 0)
            return FAIL;

        strcpy(dsname, DS_4_NAME);
        strcat(dsname, dsidx);
        /* make a DS dataset for the first dimension */
        if(H5LTmake_dataset_long(fid, dsname, rankds, s4_dim, s4_wbuf) < 0)
            return FAIL;
     }
     else
         return FAIL;
    return SUCCEED;
}

herr_t test_attach_scale(hid_t fid, hid_t did, const char *name, unsigned int idx) 
{
    herr_t  ret_value = FAIL;
    hid_t   dsid = -1;
    
    if((dsid = H5Dopen2(fid, name, H5P_DEFAULT)) >= 0) {
        if(H5DSis_attached(did, dsid, idx) == 0) {
            if(H5DSattach_scale(did, dsid, idx) >= 0) {
                if(H5DSis_attached(did, dsid, idx) > 0) {
                    /* printf(" scale attached "); */
                    ret_value = SUCCEED;
                }
                else if(H5DSis_attached(did, dsid, idx) == 0) {
                    printf(" scale not attached ");
                }
            }
        }
        if(H5Dclose(dsid) < 0)
            ret_value = FAIL;
    }

    return ret_value;
}

herr_t test_detach_scale(hid_t fid, hid_t did, const char *name, unsigned int idx) 
{
    herr_t  ret_value = FAIL;
    hid_t   dsid = -1;

    if((dsid = H5Dopen2(fid, name, H5P_DEFAULT)) >= 0) {
        if(H5DSis_attached(did, dsid, idx) == 1) {
            if(H5DSdetach_scale(did, dsid, idx) >= 0) {
                if(H5DSis_attached(did, dsid, idx) == 0) {
                    ret_value = SUCCEED;
                }
            }
        }
        if(H5Dclose(dsid) < 0)
            ret_value = FAIL;
    }

    return ret_value;
}

herr_t test_set_scalename(hid_t fid, hid_t did, const char *name, const char *scalename, unsigned int idx) 
{
    herr_t  ret_value = FAIL;
    hid_t   dsid = -1;
    
    if((dsid = H5Dopen2(fid, name, H5P_DEFAULT)) >= 0) {
        if(H5DSis_attached(did, dsid, idx) == 1) {
            if(H5DSset_scale(dsid, scalename) >= 0) {
                if(H5DSis_attached(did, dsid, idx) == 1) {
                     ret_value = SUCCEED;
                }
            }
        }
        if(H5Dclose(dsid) < 0)
            ret_value = FAIL;
    }

    return ret_value;
}

herr_t test_cmp_scalename(hid_t fid, hid_t did, const char *name, const char *scalename, unsigned int idx) 
{
    herr_t  ret_value = FAIL;
    hid_t   dsid = -1;
    ssize_t name_len;
    char    *name_out=NULL;

    if((dsid = H5Dopen2(fid, name, H5P_DEFAULT)) >= 0) {
        if(H5DSis_attached(did, dsid, idx) == 1) {
            if((name_len=H5DSget_scale_name(dsid,NULL,(size_t)0)) > 0) {
                name_out = (char*)malloc((size_t)name_len * sizeof (char));
                if(name_out != NULL) {
                    if(H5DSget_scale_name(dsid, name_out, (size_t)name_len) >= 0) {
                        if(strcmp(scalename,name_out)==0) {
                            ret_value = SUCCEED;
                        }
                        free(name_out);
                        name_out=NULL;
                    }
                }
            }
        }
        if(H5Dclose(dsid) < 0)
            ret_value = FAIL;
    }

    return ret_value;
}

static int test_long_attachscales(const char *filename)
{
    hid_t   fid = -1;
    hid_t   did = -1;
    char    dsname[32];
    char    scalename[32];
    strcpy(dsname, DATASET_NAME);
    strcat(dsname, "al");
    
    TESTING2("test_long_attachscales");
    
    if((fid = H5Fopen(filename, H5F_ACC_RDWR, H5P_DEFAULT)) < 0)
        goto out;
    
    /* make a dataset */
    if(create_long_dataset(fid, dsname, "al") < 0)
        goto out;

    if((did = H5Dopen2(fid, dsname, H5P_DEFAULT)) >= 0) {
        strcpy(scalename, DS_1_NAME);
        strcat(scalename, "al");
        if(test_attach_scale(fid, did, scalename, DIM0) < 0)
            goto out;
       
        strcpy(scalename, DS_2_NAME);
        strcat(scalename, "al");
        if(test_attach_scale(fid, did, scalename, DIM1) < 0)
            goto out;
        
        strcpy(scalename, DS_3_NAME);
        strcat(scalename, "al");
        if(test_attach_scale(fid, did, scalename, DIM2) < 0)
            goto out;
        
        strcpy(scalename, DS_4_NAME);
        strcat(scalename, "al");
        if(test_attach_scale(fid, did, scalename, DIM3) < 0)
            goto out;
       
        if(H5Dclose(did) < 0)
            goto out;
    }
    else
        goto out;

    PASSED();
    
    H5Fclose(fid);
    return SUCCEED;
    
out:
    H5E_BEGIN_TRY  {
        H5Dclose(did);
        H5Fclose(fid);
    } H5E_END_TRY;
    
    H5_FAILED();
    
    return FAIL;
}

static int test_duplicatelong_attachscales(const char *filename)
{
    hid_t   fid = -1;
    hid_t   did = -1;
    char    dsname[32];
    char    scalename[32];
    strcpy(dsname, DATASET_NAME);
    strcat(dsname, "al2");
    
    TESTING2("test_duplicatelong_attachscales");
    
    if((fid = H5Fopen(filename, H5F_ACC_RDWR, H5P_DEFAULT)) < 0)
        goto out;
    
    /* make a dataset 2 */
    if(create_long_dataset(fid, dsname, "al2") < 0)
        goto out;

    if((did = H5Dopen2(fid, dsname, H5P_DEFAULT)) >= 0) {
        strcpy(scalename, DS_1_NAME);
        strcat(scalename, "al");
        if(test_attach_scale(fid, did, scalename, DIM0) < 0)
            goto out;
       
        strcpy(scalename, DS_2_NAME);
        strcat(scalename, "al");
        if(test_attach_scale(fid, did, scalename, DIM1) < 0)
            goto out;
        
        strcpy(scalename, DS_3_NAME);
        strcat(scalename, "al");
        if(test_attach_scale(fid, did, scalename, DIM2) < 0)
            goto out;
        
        strcpy(scalename, DS_4_NAME);
        strcat(scalename, "al");
        if(test_attach_scale(fid, did, scalename, DIM3) < 0)
            goto out;
       
        if(H5Dclose(did) < 0)
            goto out;
    }
    else
        goto out;

    PASSED();
    
    H5Fclose(fid);
    return SUCCEED;
    
out:
    H5E_BEGIN_TRY  {
        H5Dclose(did);
        H5Fclose(fid);
    } H5E_END_TRY;
    
    H5_FAILED();
    
    return FAIL;
}

static int test_long_scalenames(const char *filename) {
    hid_t   fid = -1;
    hid_t   did = -1;
    char    dsname[32];
    char    scalename[32];
    char    name[32];
    strcpy(dsname, DATASET_NAME);
    strcat(dsname, "al");
    
    if((fid = H5Fopen(filename, H5F_ACC_RDWR, H5P_DEFAULT)) < 0)
        goto out;
    
    TESTING2("set long scale/cmp scale name");
    if((did = H5Dopen2(fid, dsname, H5P_DEFAULT)) >= 0) {
        strcpy(scalename, DS_1_NAME);
        strcat(scalename, "al");
        strcpy(name, SCALE_1_NAME);
        strcat(name, "al");
        if(test_set_scalename(fid, did, scalename, name, DIM0) < 0)
            goto out;

        if(test_cmp_scalename(fid, did, scalename, name, DIM0) < 0)
            goto out;
        
        strcpy(scalename, DS_2_NAME);
        strcat(scalename, "al");
        strcpy(name, SCALE_2_NAME);
        strcat(name, "al");
        if(test_set_scalename(fid, did, scalename, name, DIM1) < 0)
            goto out;

        if(test_cmp_scalename(fid, did, scalename, name, DIM1) < 0)
            goto out;
        
        strcpy(scalename, DS_3_NAME);
        strcat(scalename, "al");
        strcpy(name, SCALE_3_NAME);
        strcat(name, "al");
        if(test_set_scalename(fid, did, scalename, name, DIM2) < 0)
            goto out;

        if(test_cmp_scalename(fid, did, scalename, name, DIM2) < 0)
            goto out;
        
        strcpy(scalename, DS_4_NAME);
        strcat(scalename, "al");
        strcpy(name, SCALE_4_NAME);
        strcat(name, "al");
        if(test_set_scalename(fid, did, scalename, name, DIM3) < 0)
            goto out;

        if(test_cmp_scalename(fid, did, scalename, name, DIM3) < 0)
            goto out;
    
        if(H5Dclose(did) < 0)
            goto out;
    }
    else
        goto out;

    PASSED();
    
    H5Fclose(fid);
    return SUCCEED;
    
out:
    H5E_BEGIN_TRY  {
        H5Dclose(did);
        H5Fclose(fid);
    } H5E_END_TRY;
    
    H5_FAILED();
    
    return FAIL;
}

static int test_samelong_scalenames(const char *filename) {
    hid_t   fid = -1;
    hid_t   did = -1;
    char    dsname[32];
    char    scalename[32];
    char    name[32];

    strcpy(dsname, DATASET_NAME);
    strcat(dsname, "al2");
    
    if((fid = open_test_file(filename)) < 0)
        goto out;
    
    TESTING2("set same long scale/cmp scale name");
    if((did = H5Dopen2(fid, dsname, H5P_DEFAULT)) >= 0) {
        strcpy(scalename, DS_1_NAME);
        strcat(scalename, "al");
        strcpy(name, DS_1_NAME);
        strcat(name, "al");
        if(test_set_scalename(fid, did, scalename, name, DIM0) < 0)
            goto out;

        if(test_cmp_scalename(fid, did, scalename, name, DIM0) < 0)
            goto out;
        
        strcpy(scalename, DS_2_NAME);
        strcat(scalename, "al");
        strcpy(name, DS_2_NAME);
        strcat(name, "al");
        if(test_set_scalename(fid, did, scalename, name, DIM1) < 0)
            goto out;

        if(test_cmp_scalename(fid, did, scalename, name, DIM1) < 0)
            goto out;
        
        strcpy(scalename, DS_3_NAME);
        strcat(scalename, "al");
        strcpy(name, DS_3_NAME);
        strcat(name, "al");
        if(test_set_scalename(fid, did, scalename, name, DIM2) < 0)
            goto out;

        if(test_cmp_scalename(fid, did, scalename, name, DIM2) < 0)
            goto out;
        
        strcpy(scalename, DS_4_NAME);
        strcat(scalename, "al");
        strcpy(name, DS_4_NAME);
        strcat(name, "al");
        if(test_set_scalename(fid, did, scalename, name, DIM3) < 0)
            goto out;

        if(test_cmp_scalename(fid, did, scalename, name, DIM3) < 0)
            goto out;
    
        if(H5Dclose(did) < 0)
            goto out;
    }
    else
        goto out;

    PASSED();
    
    H5Fclose(fid);
    return SUCCEED;
    
out:
    H5E_BEGIN_TRY  {
        H5Dclose(did);
        H5Fclose(fid);
    } H5E_END_TRY;
    
    H5_FAILED();
    
    return FAIL;
}

static int test_foreign_scaleattached(const char *filename)
{
    herr_t  ret_value = FAIL;
    hid_t   fid = -1;
    hid_t   did = -1;
    hid_t   dsid = -1;
    
    TESTING2("test_foreign_scaleattached");

    if((fid = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT)) < 0)
        goto out;

    if((did = H5Dopen2(fid, "/dset_al", H5P_DEFAULT)) >= 0) {
        if((dsid = H5Dopen2(fid, "/ds_4_al", H5P_DEFAULT)) >= 0) {
            if(H5DSis_attached(did, dsid, 3) == 1) {
                ret_value = SUCCEED;
            }  
            if(H5Dclose(dsid) < 0)
                goto out;
        }
        if(H5Dclose(did) < 0)
            goto out;
    }
    else
        goto out;
    
    if(ret_value == FAIL)
        goto out;
    
    PASSED();
    
    H5Fclose(fid);
    return 0;
    
out:
    H5E_BEGIN_TRY  {
        H5Dclose(did);
        H5Fclose(fid);
    } H5E_END_TRY;
    
    H5_FAILED();
    
    return FAIL;
}