forked from Unidata/netcdf-c
/
test_read.c
1950 lines (1759 loc) · 63.9 KB
/
test_read.c
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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Do not edit this file. It is produced from the corresponding .m4 source */
/*
* Copyright (C) 2003, Northwestern University and Argonne National Laboratory
* See COPYRIGHT notice in top-level directory.
*/
/* $Id: test_read.m4 2861 2017-02-09 19:38:02Z wkliao $ */
#if defined (_WIN32) || defined (_WIN64)
#include <io.h>
#endif
#include <sys/types.h> /* open() */
#include <sys/stat.h> /* open() */
#include <fcntl.h> /* open() */
#ifdef _MSC_VER
#include <io.h>
#else
#include <unistd.h> /* unlink(), write() */
#endif
#include <errno.h> /* errno, strerror() */
#include "tests.h"
/*
* Test nc_strerror.
* Try on a bad error status.
* Test for each defined error status.
*/
int
test_nc_strerror(void)
{
int i;
const char *message, *expected_msg;
int nok=0;
static const struct {
int status;
const char *msg;
} ncerrs[] = {
{NC_NOERR, "No error"},
{NC_EBADID, "NetCDF: Not a valid ID"},
{NC_ENFILE, "NetCDF: Too many files open"},
{NC_EEXIST, "NetCDF: File exists && NC_NOCLOBBER"},
{NC_EINVAL, "NetCDF: Invalid argument"},
{NC_EPERM, "NetCDF: Write to read only"},
{NC_ENOTINDEFINE, "NetCDF: Operation not allowed in data mode"},
{NC_EINDEFINE, "NetCDF: Operation not allowed in define mode"},
{NC_EINVALCOORDS, "NetCDF: Index exceeds dimension bound"},
{NC_EMAXDIMS, "NetCDF: NC_MAX_DIMS exceeded"},
{NC_ENAMEINUSE, "NetCDF: String match to name in use"},
{NC_ENOTATT, "NetCDF: Attribute not found"},
{NC_EMAXATTS, "NetCDF: NC_MAX_ATTRS exceeded"},
{NC_EBADTYPE, "NetCDF: Not a valid data type or _FillValue type mismatch"},
{NC_EBADDIM, "NetCDF: Invalid dimension ID or name"},
{NC_EUNLIMPOS, "NetCDF: NC_UNLIMITED in the wrong index"},
{NC_EMAXVARS, "NetCDF: NC_MAX_VARS exceeded"},
{NC_ENOTVAR, "NetCDF: Variable not found"},
{NC_EGLOBAL, "NetCDF: Action prohibited on NC_GLOBAL varid"},
{NC_ENOTNC, "NetCDF: Unknown file format"},
{NC_ESTS, "NetCDF: In Fortran, string too short"},
{NC_EMAXNAME, "NetCDF: NC_MAX_NAME exceeded"},
{NC_EUNLIMIT, "NetCDF: NC_UNLIMITED size already in use"},
{NC_ENORECVARS, "NetCDF: nc_rec op when there are no record vars"},
{NC_ECHAR, "NetCDF: Attempt to convert between text & numbers"},
{NC_EEDGE, "NetCDF: Start+count exceeds dimension bound"},
{NC_ESTRIDE, "NetCDF: Illegal stride"},
{NC_EBADNAME, "NetCDF: Name contains illegal characters"},
{NC_ERANGE, "NetCDF: Numeric conversion not representable"},
{NC_ENOMEM, "NetCDF: Memory allocation (malloc) failure"},
{NC_EVARSIZE, "NetCDF: One or more variable sizes violate format constraints"},
{NC_EDIMSIZE, "NetCDF: Invalid dimension size"}
};
/* Try on a bad error status */
message = nc_strerror(-666);/* should fail */
expected_msg = "Unknown Error";
IF (strncmp(message, expected_msg, strlen(expected_msg)) != 0)
error(" nc_strerror on bad error status returned: %s", message);
ELSE_NOK
/* Try on each legitimate error status */
for (i=0; i<LEN_OF(ncerrs); i++) {
const char *message = nc_strerror(ncerrs[i].status);
IF (strcmp(message, ncerrs[i].msg) != 0)
error(" nc_strerror(%d) should return %s, not %s",
ncerrs[i].status, ncerrs[i].msg, message);
ELSE_NOK
}
return nok;
}
/*
* Test nc_open.
* If in read-only section of tests,
* Try to open a non-existent netCDF file, check error return.
* Open a file that is not a netCDF file, check error return.
* Open a netCDF file with a bad mode argument, check error return.
* Open a netCDF file with NC_NOWRITE, read-only mode, try to write, check error.
* Try to open a netcdf twice, check whether returned netcdf ids different.
* If in writable section of tests,
* Open a netCDF file with NC_WRITE mode, write something, close it.
* On exit, any open netCDF files are closed.
*/
#define NOT_NC_FILE "dummy_not_nc_file"
int
test_nc_open(void)
{
int err, ncid, ncid2, nok=0;
#if 0
int fd;
ssize_t w_len;
#endif
/* Try to open a nonexistent file */
err = file_open("tooth-fairy.nc", NC_NOWRITE, &ncid); /* should fail */
/* on some systems (such as Lustre), opening an nonexisting file will
* actually create the file. In this case, we print the error messages on
* screen and move on to the next test, instead of aborting the entire test.
* The created file will be of zero-length and PnetCDF should complain it
* is not an NC file, i.e. NC_ENOTNC.
*/
IF (err == NC_NOERR) {
error("opening a nonexistent file expects to fail, but got NC_NOERR\n");
}
#ifndef USE_PARALLEL
IF (! NC_ISSYSERR(err))
error("nc_open of nonexistent file should have returned system error");
#endif
/* Open a file that is not a netCDF file. But need a portable
* test that also works for cross-compiles ... */
/* err = nc_open("nc_test.o", NC_NOWRITE, &ncid);/\* should fail *\/ */
/* IF (err != NC_ENOTNC) */
/* error("nc_open of non-netCDF file: status = %d", err); */
#if 0
/* create a not-nc file */
fd = open(NOT_NC_FILE, O_CREAT|O_WRONLY, 0600);
IF (fd == -1) {
error("Error: creating a non-CDF file (%s)", strerror(errno));
}
else {
w_len = write(fd, "0123456789abcdefghijklmnopqrstuvwxyz", 36);
assert(w_len >= 0);
close(fd);
}
/* Open a file that is not a netCDF file. */
err = file_open(NOT_NC_FILE, NC_NOWRITE, &ncid); /* should fail */
IF (err != NC_ENOTNC)
error("expecting NC_ENOTNC or NC_EFILE but got %s", nc_err_code_name(err));
ELSE_NOK
/* delete the not-nc file */
unlink(NOT_NC_FILE);
#endif
/* Open a netCDF file in read-only mode, check that write fails */
err = file_open(testfile, NC_NOWRITE, &ncid);
IF (err != NC_NOERR)
error("open: %s", nc_strerror(err));
ELSE_NOK
err = nc_redef(ncid); /* should fail */
IF (err != NC_EPERM)
error("expecting NC_EPERM but got %s", nc_err_code_name(err));
/* Opened OK, see if can open again and get a different netCDF ID */
err = file_open(testfile, NC_NOWRITE, &ncid2);
IF (err != NC_NOERR)
error("open: %s", nc_strerror(err));
else {
nc_close(ncid2);
nok++;
}
IF (ncid2 == ncid)
error("netCDF IDs for first and second open calls should differ");
{ /* tests using netCDF scratch file */
err = file_create(scratch, NC_NOCLOBBER, &ncid2);
IF (err != NC_NOERR)
error("create: %s", nc_strerror(err));
else
nc_close(ncid2);
err = file_open(scratch, NC_WRITE, &ncid2);
IF (err != NC_NOERR)
error("open: %s", nc_strerror(err));
else {
nc_close(ncid2);
nok++;
}
err = nc_delete(scratch);
IF (err != NC_NOERR)
error("remove of %s failed", scratch);
}
err = nc_close(ncid);
IF (err != NC_NOERR)
error("close: %s", nc_strerror(err));
return nok;
}
/*
* Test nc_close.
* Try to close a netCDF file twice, check whether second close fails.
* Try on bad handle, check error return.
* Try in define mode and data mode.
*/
int
test_nc_close(void)
{
int ncid, nok=0;
int err = file_open(testfile, NC_NOWRITE, &ncid);
IF (err != NC_NOERR)
error("open: %s", nc_strerror(err));
/* Close a netCDF file twice, second time should fail */
err = nc_close(ncid);
IF (err != NC_NOERR)
error("close failed: %s", nc_strerror(err));
ELSE_NOK
err = nc_close(ncid);
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
/* Try with a bad netCDF ID */
err = nc_close(BAD_ID);/* should fail */
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
/* Close in data mode */
err = file_open(testfile, NC_NOWRITE, &ncid);
IF (err != NC_NOERR)
error("open: %s", nc_strerror(err));
err = nc_close(ncid);
IF (err != NC_NOERR)
error("close in data mode failed: %s", nc_strerror(err));
ELSE_NOK
{ /* tests using netCDF scratch file */
err = file_create(scratch, NC_NOCLOBBER, &ncid);
IF (err != NC_NOERR)
error("create: %s", nc_strerror(err));
err = nc_close(ncid);
IF (err != NC_NOERR)
error("close in define mode: %s", nc_strerror(err));
ELSE_NOK
err = nc_delete(scratch);
IF (err != NC_NOERR)
error("remove of %s failed", scratch);
}
return nok;
}
/*
* Test nc_inq
* Try on bad handle, check error return.
* Try in data mode, check returned values.
* Try asking for subsets of info.
* If in writable section of tests,
* Try in define mode, after adding an unlimited dimension, variable.
* On exit, any open netCDF files are closed.
*/
int
test_nc_inq(void)
{
int ncid;
int ndims; /* number of dimensions */
int nvars; /* number of variables */
int ngatts; /* number of global attributes */
int recdim; /* id of unlimited dimension */
int err;
int nok=0;
err = file_open(testfile, NC_NOWRITE, &ncid);
IF (err != NC_NOERR)
error("open: %s", nc_strerror(err));
/* Try on bad handle */
err = nc_inq(BAD_ID, 0, 0, 0, 0);
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
err = nc_inq(ncid, &ndims, &nvars, &ngatts, &recdim);
IF (err != NC_NOERR)
error("inq: %s", nc_strerror(err));
else IF (ndims != NDIMS)
error("inq: wrong number of dimensions returned, %d", ndims);
else IF (nvars != numVars)
error("inq: wrong number of variables returned, %d", nvars);
else IF (ngatts != numGatts)
error("inq: wrong number of global atts returned, %d", ngatts);
else IF (recdim != RECDIM)
error("inq: wrong record dimension ID returned, %d", recdim);
ELSE_NOK
/* Inguire for no info (useless, but should still work) */
err = nc_inq(ncid, 0, 0, 0, 0);
IF (err != NC_NOERR)
error("inq for no info failed: %s", nc_strerror(err));
ELSE_NOK
/* Inguire for subsets of info */
ngatts = numGatts - 1; /* wipe out previous correct value */
err = nc_inq(ncid, 0, 0, &ngatts, 0);
IF (err != NC_NOERR)
error("inq for one item failed: %s", nc_strerror(err));
else IF (ngatts != numGatts)
error("inq subset: wrong number of global atts returned, %d", ngatts);
ELSE_NOK
ndims = NDIMS - 1;
nvars = numVars - 1;
err = nc_inq(ncid, &ndims, &nvars, 0, 0);
IF (err != NC_NOERR)
error("inq for two items failed: %s", nc_strerror(err));
else IF (ndims != NDIMS)
error("inq subset: wrong number of dimensions returned, %d", ndims);
else IF (nvars != numVars)
error("inq subset: wrong number of variables returned, %d", nvars);
ELSE_NOK
{ /* tests using netCDF scratch file */
int ncid2; /* for scratch netCDF dataset */
err = file_create(scratch, NC_NOCLOBBER, &ncid2);
IF (err != NC_NOERR) {
error("create: %s", nc_strerror(err));
} else { /* add dim, var, gatt, check inq */
int ndims0;
int nvars0;
int ngatts0;
int recdim0;
err = nc_enddef(ncid2); /* enter data mode */
err = nc_inq(ncid2, &ndims0, &nvars0, &ngatts0, &recdim0);
IF (err != NC_NOERR)
error("inq: %s", nc_strerror(err));
ELSE_NOK
err = nc_redef(ncid2); /* enter define mode */
/* Check that inquire still works in define mode */
err = nc_inq(ncid2, &ndims, &nvars, &ngatts, &recdim);
IF (err != NC_NOERR)
error("inq in define mode: %s", nc_strerror(err));
else IF (ndims != ndims0)
error("inq in define mode: ndims wrong, %d", ndims);
else IF (nvars != nvars0)
error("inq in define mode: nvars wrong, %d", nvars);
else IF (ngatts != ngatts0)
error("inq in define mode: ngatts wrong, %d", ngatts);
else IF (recdim != recdim0)
error("inq in define mode: recdim wrong, %d", recdim);
ELSE_NOK
{
int did, vid;
/* Add dim, var, global att */
err = nc_def_dim(ncid2, "inqd", 1L, &did);
IF (err != NC_NOERR)
error("def_dim: %s", nc_strerror(err));
err = nc_def_var(ncid2, "inqv", NC_FLOAT, 0, 0, &vid);
IF (err != NC_NOERR)
error("def_var: %s", nc_strerror(err));
}
err = nc_put_att_text(ncid2, NC_GLOBAL, "inqa", 1+strlen("stuff"),
"stuff");
IF (err != NC_NOERR)
error("put_att_text: %s", nc_strerror(err));
/* Make sure nc_inq sees the additions while in define mode */
err = nc_inq(ncid2, &ndims, &nvars, &ngatts, &recdim);
IF (err != NC_NOERR)
error("inq in define mode: %s", nc_strerror(err));
else IF (ndims != ndims0 + 1)
error("inq in define mode: ndims wrong, %d", ndims);
else IF (nvars != nvars0 + 1)
error("inq in define mode: nvars wrong, %d", nvars);
else IF (ngatts != ngatts0 + 1)
error("inq in define mode: ngatts wrong, %d", ngatts);
ELSE_NOK
err = nc_enddef(ncid2);
IF (err != NC_NOERR)
error("enddef: %s", nc_strerror(err));
/* Make sure nc_inq stills sees additions in data mode */
err = nc_inq(ncid2, &ndims, &nvars, &ngatts, &recdim);
IF (err != NC_NOERR)
error("inq failed in data mode: %s", nc_strerror(err));
else IF (ndims != ndims0 + 1)
error("inq in define mode: ndims wrong, %d", ndims);
else IF (nvars != nvars0 + 1)
error("inq in define mode: nvars wrong, %d", nvars);
else IF (ngatts != ngatts0 + 1)
error("inq in define mode: ngatts wrong, %d", ngatts);
ELSE_NOK
nc_close(ncid2);
err = nc_delete(scratch);
IF (err != NC_NOERR)
error("remove of %s failed", scratch);
}
}
err = nc_close(ncid);
IF (err != NC_NOERR)
error("close: %s", nc_strerror(err));
return nok;
}
int
test_nc_inq_natts(void)
{
int ncid;
int ngatts; /* number of global attributes */
int err, nok=0;
err = nc_inq_natts(BAD_ID, &ngatts);
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
err = file_open(testfile, NC_NOWRITE, &ncid);
IF (err != NC_NOERR)
error("open: %s", nc_strerror(err));
err = nc_inq_natts(ncid, &ngatts);
IF (err != NC_NOERR)
error("inq_natts: %s", nc_strerror(err));
else IF (ngatts != numGatts)
error("inq_natts: wrong number of global atts returned, %d", ngatts);
ELSE_NOK
err = nc_close(ncid);
IF (err != NC_NOERR)
error("close: %s", nc_strerror(err));
return nok;
}
int
test_nc_inq_ndims(void)
{
int ncid;
int ndims;
int err;
int nok=0;
err = nc_inq_ndims(BAD_ID, &ndims);
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
err = file_open(testfile, NC_NOWRITE, &ncid);
IF (err != NC_NOERR)
error("open: %s", nc_strerror(err));
err = nc_inq_ndims(ncid, &ndims);
IF (err != NC_NOERR)
error("inq_ndims: %s", nc_strerror(err));
else IF (ndims != NDIMS)
error("inq_ndims: wrong number returned, %d", ndims);
ELSE_NOK
err = nc_close(ncid);
IF (err != NC_NOERR)
error("close: %s", nc_strerror(err));
return nok;
}
int
test_nc_inq_nvars(void)
{
int ncid;
int nvars;
int err;
int nok=0;
err = nc_inq_nvars(BAD_ID, &nvars);
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
err = file_open(testfile, NC_NOWRITE, &ncid);
IF (err != NC_NOERR)
error("open: %s", nc_strerror(err));
err = nc_inq_nvars(ncid, &nvars);
IF (err != NC_NOERR)
error("inq_nvars: %s", nc_strerror(err));
else IF (nvars != numVars)
error("inq_nvars: wrong number returned, %d", nvars);
ELSE_NOK
err = nc_close(ncid);
IF (err != NC_NOERR)
error("close: %s", nc_strerror(err));
return nok;
}
int
test_nc_inq_unlimdim(void)
{
int ncid;
int unlimdim;
int err;
int nok=0;
err = nc_inq_unlimdim(BAD_ID, &unlimdim);
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
err = file_open(testfile, NC_NOWRITE, &ncid);
IF (err != NC_NOERR)
error("open: %s", nc_strerror(err));
err = nc_inq_unlimdim(ncid, &unlimdim);
IF (err != NC_NOERR)
error("inq_unlimdim: %s", nc_strerror(err));
else IF (unlimdim != RECDIM)
error("inq_unlimdim: wrong number returned, %d", unlimdim);
ELSE_NOK
err = nc_close(ncid);
IF (err != NC_NOERR)
error("close: %s", nc_strerror(err));
return nok;
}
int
test_nc_inq_dimid(void)
{
int ncid;
int dimid;
int i;
int err;
int nok=0;
err = file_open(testfile, NC_NOWRITE, &ncid);
IF (err != NC_NOERR)
error("open: %s", nc_strerror(err));
err = nc_inq_dimid(ncid, "noSuch", &dimid);
IF (err != NC_EBADDIM)
error("expecting NC_EBADDIM but got %s", nc_err_code_name(err));
ELSE_NOK
for (i = 0; i < NDIMS; i++) {
err = nc_inq_dimid(BAD_ID, dim_name[i], &dimid);
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
err = nc_inq_dimid(ncid, dim_name[i], &dimid);
IF (err != NC_NOERR)
error("inq_dimid: %s", nc_strerror(err));
else IF (dimid != i)
error("expected %d, got %d", i, dimid);
ELSE_NOK
}
err = nc_close(ncid);
IF (err != NC_NOERR)
error("close: %s", nc_strerror(err));
return nok;
}
int
test_nc_inq_dim(void)
{
int ncid;
int i;
int err;
char name[NC_MAX_NAME];
size_t length;
int nok=0;
err = file_open(testfile, NC_NOWRITE, &ncid);
IF (err != NC_NOERR)
error("open: %s", nc_strerror(err));
for (i = 0; i < NDIMS; i++) {
err = nc_inq_dim(BAD_ID, i, name, &length);
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
err = nc_inq_dim(ncid, BAD_DIMID, name, &length);
IF (err != NC_EBADDIM)
error("expecting NC_EBADDIM but got %s", nc_err_code_name(err));
ELSE_NOK
err = nc_inq_dim(ncid, i, 0, 0);
IF (err != NC_NOERR)
error("inq_dim: %s", nc_strerror(err));
ELSE_NOK
err = nc_inq_dim(ncid, i, name, &length);
IF (err != NC_NOERR)
error("inq_dim: %s", nc_strerror(err));
else IF (strcmp(dim_name[i],name))
error("name expected: %s, got: %s",dim_name[i],name);
else IF (dim_len[i] != length)
error("size expected: %d, got: %d",dim_len[i],length);
ELSE_NOK
err = nc_inq_dim(ncid, i, name, 0);
IF (err != NC_NOERR)
error("inq_dim: %s", nc_strerror(err));
else IF (strcmp(dim_name[i],name))
error("name expected: %s, got: %s",dim_name[i],name);
ELSE_NOK
err = nc_inq_dim(ncid, i, 0, &length);
IF (err != NC_NOERR)
error("inq_dim: %s", nc_strerror(err));
else IF (dim_len[i] != length)
error("size expected: %d, got: %d",dim_len[i],length);
ELSE_NOK
}
err = nc_close(ncid);
IF (err != NC_NOERR)
error("close: %s", nc_strerror(err));
return nok;
}
int
test_nc_inq_dimlen(void)
{
int ncid;
int i;
int err;
size_t length;
int nok=0;
err = file_open(testfile, NC_NOWRITE, &ncid);
IF (err != NC_NOERR)
error("open: %s", nc_strerror(err));
for (i = 0; i < NDIMS; i++) {
err = nc_inq_dimlen(BAD_ID, i, &length);
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
err = nc_inq_dimlen(ncid, BAD_DIMID, &length);
IF (err != NC_EBADDIM)
error("expecting NC_EBADDIM but got %s", nc_err_code_name(err));
ELSE_NOK
err = nc_inq_dimlen(ncid, i, &length);
IF (err != NC_NOERR)
error("inq_dimlen: %s", nc_strerror(err));
else IF (dim_len[i] != length)
error("size expected: %d, got: %d",dim_len[i],length);
ELSE_NOK
}
err = nc_close(ncid);
IF (err != NC_NOERR)
error("close: %s", nc_strerror(err));
return nok;
}
int
test_nc_inq_dimname(void)
{
int ncid;
int i;
int err;
char name[NC_MAX_NAME];
int nok=0;
err = file_open(testfile, NC_NOWRITE, &ncid);
IF (err != NC_NOERR)
error("open: %s", nc_strerror(err));
for (i = 0; i < NDIMS; i++) {
err = nc_inq_dimname(BAD_ID, i, name);
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
err = nc_inq_dimname(ncid, BAD_DIMID, name);
IF (err != NC_EBADDIM)
error("expecting NC_EBADDIM but got %s", nc_err_code_name(err));
ELSE_NOK
err = nc_inq_dimname(ncid, i, name);
IF (err != NC_NOERR)
error("inq_dimname: %s", nc_strerror(err));
else IF (strcmp(dim_name[i],name))
error("name expected: %s, got: %s",dim_name[i],name);
ELSE_NOK
}
err = nc_close(ncid);
IF (err != NC_NOERR)
error("close: %s", nc_strerror(err));
return nok;
}
int
test_nc_inq_varid(void)
{
int ncid;
int varid;
int i;
int err;
int nok=0;
err = file_open(testfile, NC_NOWRITE, &ncid);
IF (err != NC_NOERR)
error("open: %s", nc_strerror(err));
err = nc_inq_varid(ncid, "noSuch", &varid);
IF (err != NC_ENOTVAR)
error("expecting NC_ENOTVAR but got %s", nc_err_code_name(err));
ELSE_NOK
for (i = 0; i < numVars; i++) {
err = nc_inq_varid(BAD_ID, var_name[i], &varid);
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
err = nc_inq_varid(ncid, var_name[i], &varid);
IF (err != NC_NOERR)
error("inq_varid: %s", nc_strerror(err));
else IF (varid != i)
error("expected %d, got %d", i, varid);
ELSE_NOK
}
err = nc_close(ncid);
IF (err != NC_NOERR)
error("close: %s", nc_strerror(err));
return nok;
}
int
test_nc_inq_var(void)
{
int ncid;
int i;
int err;
char name[NC_MAX_NAME];
nc_type datatype;
int ndims;
int dimids[MAX_RANK];
int natts;
int nok=0;
err = file_open(testfile, NC_NOWRITE, &ncid);
IF (err != NC_NOERR)
error("open: %s", nc_strerror(err));
for (i = 0; i < numVars; i++) {
err = nc_inq_var(BAD_ID, i, name, &datatype, &ndims, dimids, &natts);
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
err = nc_inq_var(ncid,BAD_VARID,name,&datatype,&ndims,dimids,&natts);
IF (err != NC_ENOTVAR)
error("expecting NC_ENOTVAR but got %s", nc_err_code_name(err));
ELSE_NOK
err = nc_inq_var(ncid, i, 0, 0, 0, 0, 0);
IF (err != NC_NOERR)
error("inq_var: %s", nc_strerror(err));
ELSE_NOK
err = nc_inq_var(ncid, i, name, &datatype, &ndims, dimids, &natts);
IF (err != NC_NOERR)
error("inq_var: %s", nc_strerror(err));
else IF (strcmp(var_name[i],name))
error("name expected: %s, got: %s",var_name[i],name);
else IF (var_type[i] != datatype)
error("type expected: %d, got: %d",var_type[i],datatype);
else IF (var_rank[i] != ndims)
error("ndims expected: %d, got: %d",var_rank[i],ndims);
else IF (!int_vec_eq(var_dimid[i],dimids,ndims))
error("unexpected dimid");
else IF (var_natts[i] != natts)
error("natts expected: %d, got: %d",var_natts[i],natts);
ELSE_NOK
err = nc_inq_var(ncid, i, name, 0, 0, 0, 0);
IF (err != NC_NOERR)
error("inq_var: %s", nc_strerror(err));
else IF (strcmp(var_name[i],name))
error("name expected: %s, got: %s",var_name[i],name);
ELSE_NOK
err = nc_inq_var(ncid, i, 0, &datatype, 0, 0, 0);
IF (err != NC_NOERR)
error("inq_var: %s", nc_strerror(err));
else IF (var_type[i] != datatype)
error("type expected: %d, got: %d",var_type[i],datatype);
ELSE_NOK
err = nc_inq_var(ncid, i, 0, 0, &ndims, 0, 0);
IF (err != NC_NOERR)
error("inq_var: %s", nc_strerror(err));
else IF (var_rank[i] != ndims)
error("ndims expected: %d, got: %d",var_rank[i],ndims);
ELSE_NOK
err = nc_inq_var(ncid, i, 0, 0, 0, dimids, 0);
IF (err != NC_NOERR)
error("inq_var: %s", nc_strerror(err));
else IF (!int_vec_eq(var_dimid[i],dimids,ndims))
error("unexpected dimid");
ELSE_NOK
err = nc_inq_var(ncid, i, 0, 0, 0, 0, &natts);
IF (err != NC_NOERR)
error("inq_var: %s", nc_strerror(err));
else IF (var_natts[i] != natts)
error("natts expected: %d, got: %d",var_natts[i],natts);
ELSE_NOK
}
err = nc_close(ncid);
IF (err != NC_NOERR)
error("close: %s", nc_strerror(err));
return nok;
}
int
test_nc_inq_vardimid(void)
{
int ncid;
int i;
int err;
int dimids[MAX_RANK];
int nok=0;
err = file_open(testfile, NC_NOWRITE, &ncid);
IF (err != NC_NOERR)
error("open: %s", nc_strerror(err));
for (i = 0; i < numVars; i++) {
err = nc_inq_vardimid(BAD_ID, i, dimids);
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
err = nc_inq_vardimid(ncid, BAD_VARID, dimids);
IF (err != NC_ENOTVAR)
error("expecting NC_ENOTVAR but got %s", nc_err_code_name(err));
ELSE_NOK
err = nc_inq_vardimid(ncid, i, dimids);
IF (err != NC_NOERR)
error("inq_vardimid: %s", nc_strerror(err));
else IF (!int_vec_eq(var_dimid[i], dimids, var_rank[i]))
error("unexpected dimid");
ELSE_NOK
}
err = nc_close(ncid);
IF (err != NC_NOERR)
error("close: %s", nc_strerror(err));
return nok;
}
int
test_nc_inq_varname(void)
{
int ncid;
int i;
int err;
char name[NC_MAX_NAME];
int nok=0;
err = file_open(testfile, NC_NOWRITE, &ncid);
IF (err != NC_NOERR)
error("open: %s", nc_strerror(err));
for (i = 0; i < numVars; i++) {
err = nc_inq_varname(BAD_ID, i, name);
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
nok++;
err = nc_inq_varname(ncid, BAD_VARID, name);
IF (err != NC_ENOTVAR)
error("expecting NC_ENOTVAR but got %s", nc_err_code_name(err));
ELSE_NOK
err = nc_inq_varname(ncid, i, name);
IF (err != NC_NOERR)
error("inq_varname: %s", nc_strerror(err));
else IF (strcmp(var_name[i],name))
error("name expected: %s, got: %s",var_name[i],name);
ELSE_NOK
}
err = nc_close(ncid);
IF (err != NC_NOERR)
error("close: %s", nc_strerror(err));
return nok;
}
int
test_nc_inq_varnatts(void)
{
int ncid;
int i;
int err;
int natts;
int nok=0;
err = file_open(testfile, NC_NOWRITE, &ncid);
IF (err != NC_NOERR)
error("open: %s", nc_strerror(err));
for (i = -1; i < numVars; i++) {
err = nc_inq_varnatts(BAD_ID, i, &natts);
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
err = nc_inq_varnatts(ncid, BAD_VARID, &natts);
IF (err != NC_ENOTVAR)
error("expecting NC_ENOTVAR but got %s", nc_err_code_name(err));
ELSE_NOK
err = nc_inq_varnatts(ncid, VARID(i), &natts);
IF (err != NC_NOERR)
error("inq_varnatts: %s", nc_strerror(err));
else IF (NATTS(i) != natts)
error("natts expected: %d, got: %d",NATTS(i),natts);
ELSE_NOK
}
err = nc_close(ncid);
IF (err != NC_NOERR)
error("close: %s", nc_strerror(err));
return nok;
}
int
test_nc_inq_varndims(void)
{
int ncid;
int i;
int err;
int ndims;
int nok=0;
err = file_open(testfile, NC_NOWRITE, &ncid);
IF (err != NC_NOERR)
error("open: %s", nc_strerror(err));
for (i = 0; i < numVars; i++) {
err = nc_inq_varndims(BAD_ID, i, &ndims);
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
err = nc_inq_varndims(ncid, BAD_VARID, &ndims);
IF (err != NC_ENOTVAR)
error("expecting NC_ENOTVAR but got %s", nc_err_code_name(err));
ELSE_NOK
err = nc_inq_varndims(ncid, i, &ndims);
IF (err != NC_NOERR)
error("inq_varndims: %s", nc_strerror(err));
else IF (var_rank[i] != ndims)
error("ndims expected: %d, got: %d",var_rank[i],ndims);
ELSE_NOK
}
err = nc_close(ncid);
IF (err != NC_NOERR)
error("close: %s", nc_strerror(err));
return nok;
}
int
test_nc_inq_vartype(void)
{
int ncid;
int i;
int err;
nc_type datatype;
int nok=0;
err = file_open(testfile, NC_NOWRITE, &ncid);
IF (err != NC_NOERR)
error("open: %s", nc_strerror(err));
for (i = 0; i < numVars; i++) {
err = nc_inq_vartype(BAD_ID, i, &datatype);
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
err = nc_inq_vartype(ncid, BAD_VARID, &datatype);
IF (err != NC_ENOTVAR)
error("expecting NC_ENOTVAR but got %s", nc_err_code_name(err));
ELSE_NOK
err = nc_inq_vartype(ncid, i, &datatype);
IF (err != NC_NOERR)
error("inq_vartype: %s", nc_strerror(err));
else IF (var_type[i] != datatype)
error("type expected: %d, got: %d", var_type[i], datatype);
ELSE_NOK
}
err = nc_close(ncid);
IF (err != NC_NOERR)
error("close: %s", nc_strerror(err));
return nok;
}
/*
* Test nc_get_var1(,,,)
*/
int
test_nc_get_var1(void)
{
int ncid;
int i;
int err;
double expect;
int nok = 0; /* count of valid comparisons */
double buf[1]; /* (void *) buffer */
double value[1];