forked from Unidata/netcdf-c
/
test_write.m4
2517 lines (2291 loc) · 85.5 KB
/
test_write.m4
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
dnl This is m4 source.
dnl Process using m4 to produce 'C' language file.
dnl
dnl This file is supposed to be the same as PnetCDF's test_write.m4
dnl
dnl If you see this line, you can ignore the next one.
/* Do not edit this file. It is produced from the corresponding .m4 source */
dnl
/*
* Copyright (C) 2003, Northwestern University and Argonne National Laboratory
* See COPYRIGHT notice in top-level directory.
*/
/* $Id: test_write.m4 2687 2016-12-08 18:32:13Z wkliao $ */
dnl
dnl The command-line m4 macro "PNETCDF" is to differentiate PnetCDF and netCDF
dnl in terms of function prefix names (ncmpi_ vs. nc_), integer data types
dnl (MPI_Offset vs. size_t), and function name substrings for external data
dnl types.
dnl
#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> /* read() */
#endif
#include "tests.h"
#include "math.h"
define(`EXPECT_ERR',`error("expecting $1 but got %s",nc_err_code_name($2));')dnl
define(`IntType', `ifdef(`PNETCDF',`MPI_Offset',`size_t')')dnl
define(`PTRDType',`ifdef(`PNETCDF',`MPI_Offset',`ptrdiff_t')')dnl
define(`TestFunc',`ifdef(`PNETCDF',`test_ncmpi_$1',`test_nc_$1')')dnl
define(`APIFunc', `ifdef(`PNETCDF',`ncmpi_$1',`nc_$1')')dnl
define(`FileOpen', `ifdef(`PNETCDF',`ncmpi_open(comm, $1, $2, info, $3)', `file_open($1, $2, $3)')')dnl
define(`FileCreate',`ifdef(`PNETCDF',`ncmpi_create(comm, $1, $2, info, $3)', `file_create($1, $2, $3)')')dnl
define(`FileDelete',`ifdef(`PNETCDF',`ncmpi_delete($1,$2)',`nc_delete($1)')')dnl
define(`Def_Vars', `ifdef(`PNETCDF',`def_vars($1,$2)',`def_vars($1)')')dnl
define(`Put_Atts', `ifdef(`PNETCDF',`put_atts($1,$2,$3)',`put_atts($1)')')dnl
define(`Put_Vars', `ifdef(`PNETCDF',`put_vars($1,$2)',`put_vars($1)')')dnl
define(`Check_File',`ifdef(`PNETCDF',`check_file($1,$2,$3)',`check_file($1)')')dnl
define(`Check_Atts',`ifdef(`PNETCDF',`check_atts($1,$2,$3)',`check_atts($1)')')dnl
define(`Check_Vars',`ifdef(`PNETCDF',`check_vars($1,$2)',`check_vars($1)')')dnl
define(`VarArgs', `ifdef(`PNETCDF',`int numVars',`void')')dnl
define(`AttVarArgs',`ifdef(`PNETCDF',`int numGatts, int numVars',`void')')dnl
define(`GetVar1TYPE',`ifdef(`PNETCDF',`ncmpi_get_var1_$1_all',`nc_get_var1_$1')')dnl
define(`PutVar1TYPE',`ifdef(`PNETCDF',`ncmpi_put_var1_$1_all',`nc_put_var1_$1')')dnl
define(`PutVar1', `ifdef(`PNETCDF',`ncmpi_put_var1_all($1,$2,$3,$4,$5,$6)', `nc_put_var1($1,$2,$3,$4)')')dnl
define(`PutVar', `ifdef(`PNETCDF',`ncmpi_put_var_all( $1,$2,$3,$4,$5)', `nc_put_var( $1,$2,$3)')')dnl
define(`PutVara', `ifdef(`PNETCDF',`ncmpi_put_vara_all($1,$2,$3,$4,$5,$6,$7)', `nc_put_vara($1,$2,$3,$4,$5)')')dnl
define(`PutVars', `ifdef(`PNETCDF',`ncmpi_put_vars_all($1,$2,$3,$4,$5,$6,$7,$8)', `nc_put_vars($1,$2,$3,$4,$5,$6)')')dnl
define(`PutVarm', `ifdef(`PNETCDF',`ncmpi_put_varm_all($1,$2,$3,$4,$5,$6,$7,$8,$9)', `nc_put_varm($1,$2,$3,$4,$5,$6,$7)')')dnl
/*
* Test APIFunc(create)
* For mode in NC_NOCLOBBER, NC_CLOBBER do:
* create netcdf file 'scratch.nc' with no data, close it
* test that it can be opened, do APIFunc(inq) to check nvars = 0, etc.
* Try again in NC_NOCLOBBER mode, check error return
* On exit, delete this file
*/
int
TestFunc(create)(void)
{
int clobber; /* 0 for NC_NOCLOBBER, 1 for NC_CLOBBER */
int err;
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 nok=0;
for (clobber = 0; clobber < 2; clobber++) {
err = FileCreate(scratch, clobber ? NC_CLOBBER : NC_NOCLOBBER, &ncid);
IF (err != NC_NOERR)
error("create: %s", APIFunc(strerror)(err));
ELSE_NOK
err = APIFunc(close)(ncid);
IF (err != NC_NOERR)
error("close: %s", APIFunc(strerror)(err));
err = FileOpen(scratch, NC_NOWRITE, &ncid);
IF (err != NC_NOERR)
error("open: %s", APIFunc(strerror)(err));
err = APIFunc(inq)(ncid, &ndims, &nvars, &ngatts, &recdim);
IF (err != NC_NOERR)
error("inq: %s", APIFunc(strerror)(err));
else IF (ndims != 0)
error("inq: wrong number of dimensions returned, %d", ndims);
else IF (nvars != 0)
error("inq: wrong number of variables returned, %d", nvars);
else IF (ngatts != 0)
error("inq: wrong number of global atts returned, %d", ngatts);
else IF (recdim != -1)
error("inq: wrong record dimension ID returned, %d", recdim);
ELSE_NOK
err = APIFunc(close)(ncid);
IF (err != NC_NOERR)
error("close: %s", APIFunc(strerror)(err));
}
err = FileCreate(scratch, NC_NOCLOBBER, &ncid);
IF (err != NC_EEXIST)
error("expecting NC_EEXIST but got %s", nc_err_code_name(err));
ELSE_NOK
err = FileDelete(scratch, info);
IF (err != NC_NOERR)
error("remove of %s failed", scratch);
return nok;
}
/*
* Test APIFunc(redef)
* (In fact also tests APIFunc(enddef) - called from TestFunc(enddef))
* BAD_ID
* attempt redef (error) & enddef on read-only file
* create file, define dims & vars.
* attempt put var (error)
* attempt redef (error) & enddef.
* put vars
* attempt def new dims (error)
* redef
* def new dims, vars.
* put atts
* enddef
* put vars
* close
* check file: vars & atts
* check reopening with NC_WRITE and adding new dims, atts, vars
*/
int
TestFunc(redef)(AttVarArgs)
{
int ncid; /* netcdf id */
/* used to force effective test of ncio->move() in redef */
IntType sizehint = 8192;
int dimid; /* dimension id */
int varid; /* variable id */
int varid1; /* variable id */
int nok=0, err;
const char * title = "Not funny";
double var;
char name[NC_MAX_NAME];
IntType length;
int fmt_variant1, fmt_variant2;
/* BAD_ID tests */
err = APIFunc(redef)(BAD_ID);
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
err = APIFunc(enddef)(BAD_ID);
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
/* read-only tests */
err = FileOpen(testfile, NC_NOWRITE, &ncid);
IF (err != NC_NOERR)
error("open: %s", APIFunc(strerror)(err));
err = APIFunc(redef)(ncid);
IF (err != NC_EPERM)
error("expecting NC_EPERM but got %s", nc_err_code_name(err));
ELSE_NOK
err = APIFunc(enddef)(ncid);
IF (err != NC_ENOTINDEFINE)
error("expecting NC_ENOTINDEFINE but got %s", nc_err_code_name(err));
ELSE_NOK
err = APIFunc(close)(ncid);
IF (err != NC_NOERR)
error("close: %s", APIFunc(strerror)(err));
/* tests using scratch file */
ifdef(`PNETCDF',`dnl
err = FileCreate(scratch, NC_NOCLOBBER, &ncid);',`dnl
err = file__create(scratch, NC_NOCLOBBER, 0, &sizehint, &ncid);')
IF (err != NC_NOERR) {
error("create: %s", APIFunc(strerror)(err));
return nok;
}
ifdef(`PNETCDF',,`dnl
/* limit for ncio implementations which have infinite chunksize */
if(sizehint > 32768) sizehint = 16384;')
def_dims(ncid);
Def_Vars(ncid, numVars);
Put_Atts(ncid, numGatts, numVars);
err = APIFunc(inq_varid)(ncid, "d", &varid);
IF (err != NC_NOERR)
error("inq_varid: %s", APIFunc(strerror)(err));
var = 1.0;
ifdef(`PNETCDF', `
err = ncmpi_begin_indep_data(ncid);
IF (err != NC_EINDEFINE)
error("expecting NC_EINDEFINE but got %s", nc_err_code_name(err));')dnl
err = PutVar1TYPE(double)(ncid, varid, NULL, &var);
IF (err != NC_EINDEFINE)
error("expecting NC_EINDEFINE but got %s", nc_err_code_name(err));
ifdef(`PNETCDF', `
err = ncmpi_end_indep_data(ncid);
IF (err != NC_ENOTINDEP)
error("expecting NC_ENOTINDEP but got %s", nc_err_code_name(err));')dnl
err = APIFunc(redef)(ncid);
IF (err != NC_EINDEFINE)
error("expecting NC_EINDEFINE but got %s", nc_err_code_name(err));
ELSE_NOK
err = APIFunc(enddef)(ncid);
IF (err != NC_NOERR)
error("enddef: %s", APIFunc(strerror)(err));
ELSE_NOK
Put_Vars(ncid, numVars);
err = APIFunc(def_dim)(ncid, "abc", sizehint, &dimid);
IF (err != NC_ENOTINDEFINE)
error("expecting NC_ENOTINDEFINE but got %s", nc_err_code_name(err));
err = APIFunc(redef)(ncid);
IF (err != NC_NOERR)
error("redef: %s", APIFunc(strerror)(err));
ELSE_NOK
err = APIFunc(set_fill)(ncid, NC_NOFILL, NULL);
IF (err != NC_NOERR)
error("set_fill: %s", APIFunc(strerror)(err));
err = APIFunc(def_dim)(ncid, "abc", sizehint, &dimid);
IF (err != NC_NOERR)
error("def_dim: %s", APIFunc(strerror)(err));
err = APIFunc(def_var)(ncid, "abcScalar", NC_INT, 0, NULL, &varid);
IF (err != NC_NOERR)
error("def_var: %s", APIFunc(strerror)(err));
err = APIFunc(def_var)(ncid, "abc", NC_INT, 1, &dimid, &varid1);
IF (err != NC_NOERR)
error("def_var: %s", APIFunc(strerror)(err));
{
int dimids[NDIMS +1];
int ii = 0;
for(ii = 0; ii < NDIMS; ii++) dimids[ii] = ii;
dimids[NDIMS] = dimid;
err = APIFunc(def_var)(ncid, "abcRec", NC_INT, NDIMS, dimids, &varid1);
IF (err != NC_NOERR)
error("def_var: %s", APIFunc(strerror)(err));
}
err = APIFunc(put_att_text)(ncid, NC_GLOBAL, "title", 1+strlen(title), title);
IF (err != NC_NOERR)
error("put_att_text: %s", APIFunc(strerror)(err));
err = APIFunc(enddef)(ncid);
IF (err != NC_NOERR)
error("enddef: %s", APIFunc(strerror)(err));
ELSE_NOK
var = 1.0;
ifdef(`PNETCDF', `
err = ncmpi_end_indep_data(ncid);
IF (err != NC_ENOTINDEP)
error("expecting NC_ENOTINDEP but got %s", nc_err_code_name(err));')dnl
err = PutVar1TYPE(double)(ncid, varid, NULL, &var);
IF (err != NC_NOERR)
error("put_var1_double: %s", APIFunc(strerror)(err));
err = APIFunc(inq_format)(ncid, &fmt_variant1);
IF (err)
error("inq_format: %s", APIFunc(strerror)(err));
err = APIFunc(close)(ncid);
IF (err != NC_NOERR)
error("close: %s", APIFunc(strerror)(err));
/* check scratch file written as expected */
Check_File(scratch, numGatts, numVars); /* checks all except "abc" stuff added above */
IF ((err = FileOpen(scratch, NC_NOWRITE, &ncid)))
error("open: %s", APIFunc(strerror)(err));
IF ((err = APIFunc(inq_dim)(ncid, dimid, name, &length)))
error("inq_dim: %s", APIFunc(strerror)(err));
IF (strcmp(name, "abc") != 0)
error("Unexpected dim name");
IF (length != sizehint)
error("Unexpected dim length");
IF ((err = GetVar1TYPE(double)(ncid, varid, NULL, &var)))
error("get_var1_double: %s", APIFunc(strerror)(err));
IF (var != 1.0)
error("get_var1_double: unexpected value");
IF ((err = APIFunc(close)(ncid)))
error("close: %s", APIFunc(strerror)(err));
/* open scratch file for writing, add another dim, var, att, then check */
IF ((err = FileOpen(scratch, NC_WRITE, &ncid)))
error("open: %s", APIFunc(strerror)(err));
IF ((err = APIFunc(redef)(ncid)))
error("redef: %s", APIFunc(strerror)(err));
IF ((err = APIFunc(def_dim)(ncid, "def", sizehint, &dimid)))
error("def_dim: %s", APIFunc(strerror)(err));
IF ((err = APIFunc(def_var)(ncid, "defScalar", NC_INT, 0, NULL, &varid)))
error("def_var: %s", APIFunc(strerror)(err));
IF ((err = APIFunc(def_var)(ncid, "def", NC_INT, 1, &dimid, &varid1)))
error("def_var: %s", APIFunc(strerror)(err));
IF ((err = APIFunc(put_att_text)(ncid, NC_GLOBAL, "Credits", 1+strlen("Thanks!"), "Thanks!")))
error("put_att_text: %s", APIFunc(strerror)(err));
IF ((err = APIFunc(enddef)(ncid)))
error("enddef: %s", APIFunc(strerror)(err));
var = 2.0;
IF ((err = PutVar1TYPE(double)(ncid, varid, NULL, &var)))
error("put_var1_double: %s", APIFunc(strerror)(err));
IF ((err = APIFunc(close)(ncid)))
error("close: %s", APIFunc(strerror)(err));
/* check scratch file written as expected */
Check_File(scratch, numGatts, numVars);
err = FileOpen(scratch, NC_NOWRITE, &ncid);
IF (err)
error("open: %s", APIFunc(strerror)(err));
err = APIFunc(inq_dim)(ncid, dimid, name, &length);
IF (err)
error("inq_dim: %s", APIFunc(strerror)(err));
IF (strcmp(name, "def") != 0)
error("Unexpected dim name");
IF (length != sizehint)
error("Unexpected dim length");
err = GetVar1TYPE(double)(ncid, varid, NULL, &var);
IF (err)
error("get_var1_double: %s", APIFunc(strerror)(err));
IF (var != 2.0)
error("get_var1_double: unexpected value");
/* make sure format variant hasn't changed from when created */
err = APIFunc(inq_format)(ncid, &fmt_variant2);
IF (err)
error("inq_format: %s", APIFunc(strerror)(err));
IF (fmt_variant1 != fmt_variant2)
error("enddef changed format variant");
err = APIFunc(close)(ncid);
IF (err)
error("close: %s", APIFunc(strerror)(err));
err = FileDelete(scratch, info);
IF (err != NC_NOERR)
error("remove of %s failed", scratch);
return nok;
}
/*
* Test APIFunc(enddef)
* Simply calls TestFunc(redef) which tests both APIFunc(redef) & APIFunc(enddef)
*/
int
TestFunc(enddef)(AttVarArgs)
{
ifdef(`PNETCDF',
`return test_ncmpi_redef(numGatts, numVars);',
`return test_nc_redef();')
}
/*
* Test APIFunc(sync)
* try with bad handle, check error
* try in define mode, check error
* try writing with one handle, reading with another on same netCDF
*/
int
TestFunc(sync)(AttVarArgs)
{
int ncidw; /* netcdf id for writing */
int ncidr; /* netcdf id for reading */
int nok=0, err;
/* BAD_ID test */
err = APIFunc(sync)(BAD_ID);
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
/* create scratch file & try APIFunc(sync) in define mode */
err = FileCreate(scratch, NC_NOCLOBBER, &ncidw);
IF (err != NC_NOERR) {
error("create: %s", APIFunc(strerror)(err));
return nok;
}
err = APIFunc(sync)(ncidw);
IF (err != NC_EINDEFINE)
error("expecting NC_EINDEFINE but got %s", nc_err_code_name(err));
ELSE_NOK
/* write using same handle */
def_dims(ncidw);
Def_Vars(ncidw, numVars);
Put_Atts(ncidw, numGatts, numVars);
err = APIFunc(enddef)(ncidw);
IF (err != NC_NOERR)
error("enddef: %s", APIFunc(strerror)(err));
Put_Vars(ncidw, numVars);
err = APIFunc(sync)(ncidw);
IF (err != NC_NOERR)
error("sync of ncidw failed: %s", APIFunc(strerror)(err));
ELSE_NOK
/* open another handle, APIFunc(sync), read (check) */
err = FileOpen(scratch, NC_NOWRITE, &ncidr);
IF (err != NC_NOERR)
error("open: %s", APIFunc(strerror)(err));
err = APIFunc(sync)(ncidr);
IF (err != NC_NOERR)
error("sync of ncidr failed: %s", APIFunc(strerror)(err));
ELSE_NOK
check_dims(ncidr);
Check_Atts(ncidr, numGatts, numVars);
Check_Vars(ncidr, numVars);
/* close both handles */
err = APIFunc(close)(ncidr);
IF (err != NC_NOERR)
error("close: %s", APIFunc(strerror)(err));
err = APIFunc(close)(ncidw);
IF (err != NC_NOERR)
error("close: %s", APIFunc(strerror)(err));
err = FileDelete(scratch, info);
IF (err != NC_NOERR)
error("remove of %s failed", scratch);
return nok;
}
/*
* Test APIFunc(abort)
* try with bad handle, check error
* try in define mode before anything written, check that file was deleted
* try after APIFunc(enddef), APIFunc(redef), define new dims, vars, atts
* try after writing variable
*/
int
TestFunc(abort)(AttVarArgs)
{
int ncid; /* netcdf id */
int err;
int ndims;
int nvars;
int ngatts;
int nok=0;
/* BAD_ID test */
err = APIFunc(abort)(BAD_ID);
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
/* create scratch file & try APIFunc(abort) in define mode */
err = FileCreate(scratch, NC_NOCLOBBER, &ncid);
IF (err != NC_NOERR) {
error("create: %s", APIFunc(strerror)(err));
return nok;
}
def_dims(ncid);
Def_Vars(ncid, numVars);
Put_Atts(ncid, numGatts, numVars);
err = APIFunc(abort)(ncid);
IF (err != NC_NOERR)
error("abort of ncid failed: %s", APIFunc(strerror)(err));
ELSE_NOK
err = APIFunc(close)(ncid); /* should already be closed */
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
err = FileDelete(scratch, info); /* should already be deleted */
ifdef(`PNETCDF',
`IF (err != NC_ENOENT && err != NC_EFILE)
error("expecting NC_ENOENT or NC_EFILE but got %s", nc_err_code_name(err));',
`IF (err != ENOENT && err != NC_EIO)
error("expecting ENOENT or NC_EIO but got %s", nc_err_code_name(err));')dnl
/*
* create scratch file
* do APIFunc(enddef) & APIFunc(redef)
* define new dims, vars, atts
* try APIFunc(abort): should restore previous state (no dims, vars, atts)
*/
err = FileCreate(scratch, NC_NOCLOBBER, &ncid);
IF (err != NC_NOERR) {
error("create: %s", APIFunc(strerror)(err));
return nok;
}
err = APIFunc(enddef)(ncid);
IF (err != NC_NOERR)
error("enddef: %s", APIFunc(strerror)(err));
err = APIFunc(redef)(ncid);
IF (err != NC_NOERR)
error("redef: %s", APIFunc(strerror)(err));
def_dims(ncid);
Def_Vars(ncid, numVars);
Put_Atts(ncid, numGatts, numVars);
err = APIFunc(abort)(ncid);
IF (err != NC_NOERR)
error("abort of ncid failed: %s", APIFunc(strerror)(err));
ELSE_NOK
err = APIFunc(close)(ncid); /* should already be closed */
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
err = FileOpen(scratch, NC_NOWRITE, &ncid);
IF (err != NC_NOERR)
error("open: %s", APIFunc(strerror)(err));
err = APIFunc(inq)(ncid, &ndims, &nvars, &ngatts, NULL);
IF (err != NC_NOERR)
error("inq: %s", APIFunc(strerror)(err));
IF (ndims != 0)
error("ndims should be 0");
IF (nvars != 0)
error("nvars should be 0");
IF (ngatts != 0)
error("ngatts should be 0");
err = APIFunc(close)(ncid);
IF (err != NC_NOERR)
error("close: %s", APIFunc(strerror)(err));
/* try APIFunc(abort) in data mode - should just close */
err = FileCreate(scratch, NC_CLOBBER, &ncid);
IF (err != NC_NOERR) {
error("create: %s", APIFunc(strerror)(err));
return nok;
}
def_dims(ncid);
Def_Vars(ncid, numVars);
Put_Atts(ncid, numGatts, numVars);
err = APIFunc(enddef)(ncid);
IF (err != NC_NOERR)
error("enddef: %s", APIFunc(strerror)(err));
Put_Vars(ncid, numVars);
err = APIFunc(abort)(ncid);
IF (err != NC_NOERR)
error("abort of ncid failed: %s", APIFunc(strerror)(err));
ELSE_NOK
err = APIFunc(close)(ncid); /* should already be closed */
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
Check_File(scratch, numGatts, numVars);
err = FileDelete(scratch, info);
IF (err != NC_NOERR)
error("remove of %s failed", scratch);
return nok;
}
/*
* Test APIFunc(def_dim)
* try with bad netCDF handle, check error
* try in data mode, check error
* check that returned id is one more than previous id
* try adding same dimension twice, check error
* try with illegal sizes, check error
* make sure unlimited size works, shows up in APIFunc(inq_unlimdim)
* try to define a second unlimited dimension, check error
*/
int
TestFunc(def_dim)(VarArgs)
{
int ncid;
int err; /* status */
int i, nok=0;
int dimid; /* dimension id */
IntType length;
/* BAD_ID test */
err = APIFunc(def_dim)(BAD_ID, "abc", 8, &dimid);
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
/* data mode test */
err = FileCreate(scratch, NC_CLOBBER, &ncid);
IF (err != NC_NOERR) {
error("create: %s", APIFunc(strerror)(err));
return nok;
}
err = APIFunc(enddef)(ncid);
IF (err != NC_NOERR)
error("enddef: %s", APIFunc(strerror)(err));
err = APIFunc(def_dim)(ncid, "abc", 8, &dimid);
IF (err != NC_ENOTINDEFINE)
error("expecting NC_ENOTINDEFINE but got %s", nc_err_code_name(err));
ELSE_NOK
/* define-mode tests: unlimited dim */
err = APIFunc(redef)(ncid);
IF (err != NC_NOERR)
error("redef: %s", APIFunc(strerror)(err));
err = APIFunc(def_dim)(ncid, dim_name[0], NC_UNLIMITED, &dimid);
IF (err != NC_NOERR)
error("def_dim: %s", APIFunc(strerror)(err));
ELSE_NOK
IF (dimid != 0)
error("Unexpected dimid");
ELSE_NOK
err = APIFunc(inq_unlimdim)(ncid, &dimid);
IF (err != NC_NOERR)
error("inq_unlimdim: %s", APIFunc(strerror)(err));
IF (dimid != 0)
error("Unexpected recdim");
err = APIFunc(inq_dimlen)(ncid, dimid, &length);
IF (length != 0)
error("Unexpected length");
err = APIFunc(def_dim)(ncid, "abc", NC_UNLIMITED, &dimid);
IF (err != NC_EUNLIMIT)
error("expecting NC_EUNLIMIT but got %s", nc_err_code_name(err));
ELSE_NOK
/* define-mode tests: remaining dims */
for (i = 1; i < NDIMS; i++) {
err = APIFunc(def_dim)(ncid, dim_name[i-1], dim_len[i], &dimid);
IF (err != NC_ENAMEINUSE)
error("expecting NC_ENAMEINUSE but got %s", nc_err_code_name(err));
ELSE_NOK
err = APIFunc(def_dim)(ncid, BAD_NAME, dim_len[i], &dimid);
IF (err != NC_EBADNAME)
error("expecting NC_EBADNAME but got %s", nc_err_code_name(err));
ELSE_NOK
ifdef(`PNETCDF', ,`if(sizeof(long) > 4) /* Fix: dmh 11/4/2011: works only if sizeof(long) > 4 */')
{
err = APIFunc(def_dim)(ncid, dim_name[i], (IntType)(NC_UNLIMITED-1), &dimid);
IF (err != NC_EDIMSIZE)
error("expecting NC_EDIMSIZE but got %s", nc_err_code_name(err));
ELSE_NOK
}
err = APIFunc(def_dim)(ncid, dim_name[i], dim_len[i], &dimid);
IF (err != NC_NOERR)
error("def_dim: %s", APIFunc(strerror)(err));
ELSE_NOK
IF (dimid != i)
error("Unexpected dimid");
}
/* Following just to expand unlimited dim */
Def_Vars(ncid, numVars);
err = APIFunc(enddef)(ncid);
IF (err != NC_NOERR)
error("enddef: %s", APIFunc(strerror)(err));
Put_Vars(ncid, numVars);
/* Check all dims */
check_dims(ncid);
err = APIFunc(close)(ncid);
IF (err != NC_NOERR)
error("close: %s", APIFunc(strerror)(err));
err = FileDelete(scratch, info);
IF (err != NC_NOERR)
error("remove of %s failed", scratch);
return nok;
}
/*
* Test APIFunc(rename_dim)
* try with bad netCDF handle, check error
* check that proper rename worked with APIFunc(inq_dim)
* try renaming to existing dimension name, check error
* try with bad dimension handle, check error
*/
int
TestFunc(rename_dim)(void)
{
int ncid;
int err, nok=0; /* status */
char name[NC_MAX_NAME];
/* BAD_ID test */
err = APIFunc(rename_dim)(BAD_ID, 0, "abc");
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
/* main tests */
err = FileCreate(scratch, NC_NOCLOBBER, &ncid);
IF (err != NC_NOERR) {
error("create: %s", APIFunc(strerror)(err));
return nok;
}
def_dims(ncid);
err = APIFunc(rename_dim)(ncid, BAD_DIMID, "abc");
IF (err != NC_EBADDIM)
error("expecting NC_EBADDIM but got %s", nc_err_code_name(err));
ELSE_NOK
err = APIFunc(rename_dim)(ncid, 2, "abc");
IF (err != NC_NOERR)
error("rename_dim: %s", APIFunc(strerror)(err));
ELSE_NOK
err = APIFunc(inq_dimname)(ncid, 2, name);
IF (strcmp(name, "abc") != 0)
error("Unexpected name: %s", name);
err = APIFunc(rename_dim)(ncid, 0, "abc");
IF (err != NC_ENAMEINUSE)
error("expecting NC_ENAMEINUSE but got %s", nc_err_code_name(err));
ELSE_NOK
err = APIFunc(close)(ncid);
IF (err != NC_NOERR)
error("close: %s", APIFunc(strerror)(err));
err = FileDelete(scratch, info);
IF (err != NC_NOERR)
error("remove of %s failed", scratch);
return nok;
}
/*
* Test APIFunc(def_var)
* try with bad netCDF handle, check error
* try with bad name, check error
* scalar tests:
* check that proper define worked with APIFunc(inq_var)
* try redefining an existing variable, check error
* try with bad datatype, check error
* try with bad number of dimensions, check error
* try in data mode, check error
* check that returned id is one more than previous id
* try with bad dimension ids, check error
*/
int
TestFunc(def_var)(VarArgs)
{
int ncid;
int varid;
int err, nok=0; /* status */
int i;
int ndims;
int natts;
char name[NC_MAX_NAME];
int dimids[MAX_RANK];
nc_type datatype;
/* BAD_ID test */
err = APIFunc(def_var)(BAD_ID, "abc", NC_SHORT, 0, NULL, &varid);
IF (err != NC_EBADID)
error("expecting NC_EBADID but got %s", nc_err_code_name(err));
ELSE_NOK
/* scalar tests */
err = FileCreate(scratch, NC_NOCLOBBER, &ncid);
IF (err != NC_NOERR) {
error("create: %s", APIFunc(strerror)(err));
return nok;
}
err = APIFunc(def_var)(ncid, "abc", NC_SHORT, 0, NULL, &varid);
IF (err != NC_NOERR)
error("def_var: %s", APIFunc(strerror)(err));
ELSE_NOK
err = APIFunc(inq_var)(ncid, varid, name, &datatype, &ndims, dimids, &natts);
IF (err != NC_NOERR)
error("inq_var: %s", APIFunc(strerror)(err));
IF (strcmp(name, "abc") != 0)
error("Unexpected name: %s", name);
IF (datatype != NC_SHORT)
error("Unexpected datatype");
IF (ndims != 0)
error("Unexpected rank");
err = APIFunc(def_var)(ncid, BAD_NAME, NC_SHORT, 0, NULL, &varid);
IF (err != NC_EBADNAME)
error("expecting NC_EBADNAME but got %s", nc_err_code_name(err));
ELSE_NOK
err = APIFunc(def_var)(ncid, "abc", NC_SHORT, 0, NULL, &varid);
IF (err != NC_ENAMEINUSE)
error("expecting NC_ENAMEINUSE but got %s", nc_err_code_name(err));
ELSE_NOK
err = APIFunc(def_var)(ncid, "ABC", BAD_TYPE, -1, dimids, &varid);
IF (err != NC_EBADTYPE)
error("expecting NC_EBADTYPE but got %s", nc_err_code_name(err));
ELSE_NOK
err = APIFunc(def_var)(ncid, "ABC", NC_SHORT, -1, dimids, &varid);
IF (err != NC_EINVAL)
error("expecting NC_EINVAL but got %s", nc_err_code_name(err));
ELSE_NOK
err = APIFunc(enddef)(ncid);
IF (err != NC_NOERR)
error("enddef: %s", APIFunc(strerror)(err));
err = APIFunc(def_var)(ncid, "ABC", NC_SHORT, 0, dimids, &varid);
IF (err != NC_ENOTINDEFINE)
error("expecting NC_ENOTINDEFINE but got %s", nc_err_code_name(err));
ELSE_NOK
err = APIFunc(close)(ncid);
IF (err != NC_NOERR)
error("close: %s", APIFunc(strerror)(err));
err = FileDelete(scratch, info);
IF (err != NC_NOERR)
error("remove of %s failed", scratch);
/* general tests using global vars */
err = FileCreate(scratch, NC_CLOBBER, &ncid);
IF (err != NC_NOERR) {
error("create: %s", APIFunc(strerror)(err));
return nok;
}
def_dims(ncid);
for (i = 0; i < numVars; i++) {
err = APIFunc(def_var)(ncid, var_name[i], var_type[i], var_rank[i],
var_dimid[i], &varid);
IF (err != NC_NOERR)
error("def_var: %s", APIFunc(strerror)(err));
ELSE_NOK
IF (varid != i)
error("Unexpected varid");
ELSE_NOK
}
/* try bad dim ids */
dimids[0] = BAD_DIMID;
err = APIFunc(def_var)(ncid, "abc", NC_SHORT, 1, dimids, &varid);
IF (err != NC_EBADDIM)
error("expecting NC_EBADDIM but got %s", nc_err_code_name(err));
ELSE_NOK
err = APIFunc(close)(ncid);
IF (err != NC_NOERR)
error("close: %s", APIFunc(strerror)(err));
err = FileDelete(scratch, info);
IF (err != NC_NOERR)
error("remove of %s failed", scratch);
return nok;
}
/*
* Test PutVar1
*/
int
TestFunc(put_var1)(VarArgs)
{
int i, err, ncid, nok=0;
IntType j, index[MAX_RANK];
double value[1];
double buf[1]; /* (void *) buffer */
ifdef(`PNETCDF', `MPI_Datatype datatype;')
err = FileCreate(scratch, NC_NOCLOBBER, &ncid);
IF (err != NC_NOERR) {
error("create: %s", APIFunc(strerror)(err));
return nok;
}
def_dims(ncid);
Def_Vars(ncid, numVars);
err = APIFunc(enddef)(ncid);
IF (err != NC_NOERR)
error("enddef: %s", APIFunc(strerror)(err));
/* check if can detect a bad file ID */
err = PutVar1(BAD_ID, 0, NULL, NULL, 1, MPI_DATATYPE_NULL);
IF (err != NC_EBADID)
EXPECT_ERR(NC_EBADID, err)
ELSE_NOK
/* check if can detect a bad variable ID */
err = PutVar1(ncid, BAD_VARID, NULL, NULL, 1, MPI_DATATYPE_NULL);
IF (err != NC_ENOTVAR)
EXPECT_ERR(NC_ENOTVAR, err)
ELSE_NOK
for (i = 0; i < numVars; i++) {
assert(var_rank[i] <= MAX_RANK);
assert(var_nels[i] <= MAX_NELS);
value[0] = 5; /* reset to a value within bounds */
ifdef(`PNETCDF', `datatype = nc_mpi_type(var_type[i]);')
ifdef(`PNETCDF',`dnl
/* for non-scalar variables, argument start cannot be NULL */
err = PutVar1(ncid, i, NULL, value, 1, datatype);
if (var_rank[i] == 0) { /* scalar variable */
IF (err != NC_NOERR) EXPECT_ERR(NC_NOERR, err)
}
else IF (err != NC_EINVALCOORDS) {
EXPECT_ERR(NC_EINVALCOORDS, err)
}
ELSE_NOK
')dnl
/* test NC_EINVALCOORDS */
for (j = 0; j < var_rank[i]; j++) index[j] = 0;
for (j = 0; j < var_rank[i]; j++) {
if (var_dimid[i][j] == RECDIM) continue; /* skip record dim */
index[j] = var_shape[i][j]; /* out of boundary check */
err = PutVar1(ncid, i, index, value, 1, datatype);
IF (err != NC_EINVALCOORDS)
EXPECT_ERR(NC_EINVALCOORDS, err)
ELSE_NOK
index[j] = 0;
}
for (j = 0; j < var_nels[i]; j++) {
err = toMixedBase(j, var_rank[i], var_shape[i], index);
IF (err != 0) error("error in toMixedBase");
value[0] = hash(var_type[i], var_rank[i], index);
if (inRange(value[0], var_type[i])) {
err = dbl2nc(value[0], var_type[i], buf);
IF (err != NC_NOERR)
error("error in dbl2nc var:%s type:%s",
var_name[i],s_nc_type(var_type[i]));
err = PutVar1(ncid, i, index, buf, 1, datatype);
IF (err != NC_NOERR)
EXPECT_ERR(NC_NOERR, err)
ELSE_NOK
}
}
}
Check_Vars(ncid, numVars);
err = APIFunc(close)(ncid);
IF (err != NC_NOERR)
error("close: %s", APIFunc(strerror)(err));
err = FileDelete(scratch, info);
IF (err != NC_NOERR)
error("remove of %s failed", scratch);
return nok;
}
/*
* Test PutVara
* Choose a random point dividing each dim into 2 parts
* Put 2^rank (nslabs) slabs so defined
* Redefine buffer for each put.
* At end check all variables using Check_Vars
*/
int
TestFunc(put_vara)(VarArgs)
{
int d, i, k, err, nslabs, ncid, nok=0;
IntType j, nels;
IntType start[MAX_RANK];
IntType edge[MAX_RANK];
IntType index[MAX_RANK];
IntType mid[MAX_RANK];
double buf[MAX_NELS]; /* (void *) buffer */
char *p; /* (void *) pointer */
double value;
ifdef(`PNETCDF', `MPI_Datatype datatype;')
err = FileCreate(scratch, NC_NOCLOBBER, &ncid);
IF (err != NC_NOERR) {
error("create: %s", APIFunc(strerror)(err));
return nok;
}
def_dims(ncid);
Def_Vars(ncid, numVars);
err = APIFunc(enddef)(ncid);
IF (err != NC_NOERR)
error("enddef: %s", APIFunc(strerror)(err));
/* check if can detect a bad file ID */
err = PutVara(BAD_ID, 0, start, edge, buf, 1, MPI_DATATYPE_NULL);
IF (err != NC_EBADID)
EXPECT_ERR(NC_EBADID, err)
ELSE_NOK
/* check if can detect a bad variable ID */
err = PutVara(ncid, BAD_VARID, start, edge, buf, 1, MPI_DATATYPE_NULL);
IF (err != NC_ENOTVAR)
EXPECT_ERR(NC_ENOTVAR, err)
ELSE_NOK
for (i = 0; i < numVars; i++) {
assert(var_rank[i] <= MAX_RANK);
assert(var_nels[i] <= MAX_NELS);
buf[0] = 5; /* reset to a value within bounds */
/* check if can detect a bad file ID */
err = PutVara(BAD_ID, i, start, edge, buf, 1, MPI_DATATYPE_NULL);
IF (err != NC_EBADID)
EXPECT_ERR(NC_EBADID, err)
ELSE_NOK
ifdef(`PNETCDF', `datatype = nc_mpi_type(var_type[i]);')
for (j = 0; j < var_rank[i]; j++) {
start[j] = 0;
edge[j] = 1;
}
ifdef(`PNETCDF',`dnl