1 /* This is part of the netCDF package. Copyright 2005 University
2    Corporation for Atmospheric Research/Unidata See COPYRIGHT file for
3    conditions of use. See www.unidata.ucar.edu for more info.
4 
5    Test netcdf-4 string types.
6 
7    Ed Hartnett
8 */
9 
10 #include <config.h>
11 #include <nc_tests.h>
12 #include "err_macros.h"
13 
14 #define TEST_NAME "tst_strings"
15 #define FILE_NAME "tst_strings.nc"
16 #define DIM_LEN 9
17 #define ATT_NAME "measure_for_measure_att"
18 #define DIM_NAME "line"
19 #define VAR_NAME "measure_for_measure_var"
20 #define NDIMS 1
21 
22 int
main(int argc,char ** argv)23 main(int argc, char **argv)
24 {
25    printf("\n*** Testing netcdf-4 string type.\n");
26    printf("*** testing string attribute...");
27    {
28 #define ATT_LEN_1 1
29 #define MOUNTAIN_RANGE "mountain_range"
30       size_t att_len;
31       int ndims, nvars, natts, unlimdimid;
32       nc_type att_type;
33       int ncid, i;
34       char *data_in[ATT_LEN_1] = {NULL};
35       char *data[ATT_LEN_1] = {"R"};
36 
37       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
38       if (nc_put_att(ncid, NC_GLOBAL, MOUNTAIN_RANGE, NC_STRING, ATT_LEN_1, data)) ERR;
39       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
40       if (ndims != 0 || nvars != 0 || natts != 1 || unlimdimid != -1) ERR;
41       if (nc_inq_att(ncid, NC_GLOBAL, MOUNTAIN_RANGE, &att_type, &att_len)) ERR;
42       if (att_type != NC_STRING || att_len != ATT_LEN_1) ERR;
43       if (nc_close(ncid)) ERR;
44 
45       /* Check it out. */
46       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
47       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
48       if (ndims != 0 || nvars != 0 || natts != 1 || unlimdimid != -1) ERR;
49       if (nc_inq_att(ncid, NC_GLOBAL, MOUNTAIN_RANGE, &att_type, &att_len)) ERR;
50       if (att_type != NC_STRING || att_len != ATT_LEN_1) ERR;
51       if (nc_get_att(ncid, NC_GLOBAL, MOUNTAIN_RANGE, data_in)) ERR;
52       for (i = 0; i < att_len; i++)
53       	 if (strcmp(data_in[i], data[i])) ERR;
54       if (nc_free_string(ATT_LEN_1, (char **)data_in)) ERR;
55       if (nc_close(ncid)) ERR;
56    }
57    SUMMARIZE_ERR;
58    printf("*** testing string attribute...");
59    {
60 #define ATT_LEN 9
61       size_t att_len;
62       int ndims, nvars, natts, unlimdimid;
63       nc_type att_type;
64       int ncid, i;
65       char *data_in[ATT_LEN];
66       char *data[ATT_LEN] = {"Let but your honour know",
67 			     "Whom I believe to be most strait in virtue",
68 			     "That, in the working of your own affections",
69 			     "Had time cohered with place or place with wishing",
70 			     "Or that the resolute acting of your blood",
71 			     "Could have attain'd the effect of your own purpose",
72 			     "Whether you had not sometime in your life",
73 			     "Err'd in this point which now you censure him",
74 			     "And pull'd the law upon you."};
75 
76       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
77       if (nc_put_att(ncid, NC_GLOBAL, ATT_NAME, NC_STRING, ATT_LEN, data)) ERR;
78       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
79       if (ndims != 0 || nvars != 0 || natts != 1 || unlimdimid != -1) ERR;
80       if (nc_inq_att(ncid, NC_GLOBAL, ATT_NAME, &att_type, &att_len)) ERR;
81       if (att_type != NC_STRING || att_len != ATT_LEN) ERR;
82       if (nc_close(ncid)) ERR;
83 
84       /* Check it out. */
85       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
86       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
87       if (ndims != 0 || nvars != 0 || natts != 1 || unlimdimid != -1) ERR;
88       if (nc_inq_att(ncid, NC_GLOBAL, ATT_NAME, &att_type, &att_len)) ERR;
89       if (att_type != NC_STRING || att_len != ATT_LEN) ERR;
90       if (nc_get_att(ncid, NC_GLOBAL, ATT_NAME, data_in)) ERR;
91       for (i = 0; i < att_len; i++)
92       	 if (strcmp(data_in[i], data[i])) ERR;
93       if (nc_free_string(att_len, (char **)data_in)) ERR;
94       if (nc_close(ncid)) ERR;
95    }
96    SUMMARIZE_ERR;
97    printf("*** testing string var functions...");
98    {
99 #define MOBY_LEN 16
100       int ncid, varid, dimids[NDIMS];
101       char *data[] = {"Perhaps a very little thought will now enable you to account for ",
102 		      "those repeated whaling disasters--some few of which are casually ",
103 		      "chronicled--of this man or that man being taken out of the boat by ",
104 		      "the line, and lost.",
105 		      "For, when the line is darting out, to be seated then in the boat, ",
106 		      "is like being seated in the midst of the manifold whizzings of a ",
107 		      "steam-engine in full play, when every flying beam, and shaft, and wheel, ",
108 		      "is grazing you.",
109 		      "It is worse; for you cannot sit motionless in the heart of these perils, ",
110 		      "because the boat is rocking like a cradle, and you are pitched one way and ",
111 		      "the other, without the slightest warning;",
112 		      "But why say more?",
113 		      "All men live enveloped in whale-lines.",
114 		      "All are born with halters round their necks; but it is only when caught ",
115 		      "in the swift, sudden turn of death, that mortals realize the silent, subtle, ",
116 		      "ever-present perils of life."};
117       char *data_in[MOBY_LEN];
118       int i;
119 
120       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
121       if (nc_def_dim(ncid, DIM_NAME, MOBY_LEN, dimids)) ERR;
122       if (nc_def_var(ncid, VAR_NAME, NC_STRING, NDIMS, dimids, &varid)) ERR;
123       if (nc_put_var_string(ncid, varid, (const char **)data)) ERR;
124       if (nc_close(ncid)) ERR;
125 
126       /* Check it out. */
127      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
128      if (nc_get_var_string(ncid, varid, data_in)) ERR;
129      for (i=0; i<MOBY_LEN; i++)
130 	if (strcmp(data_in[i], data[i])) ERR;
131      if (nc_free_string(MOBY_LEN, (char **)data_in)) ERR;
132      if (nc_close(ncid)) ERR;
133    }
134    SUMMARIZE_ERR;
135    printf("*** testing string attributes...");
136    {
137 #define SOME_PRES 16
138 #define NDIMS_PRES 1
139 #define ATT2_NAME "presidents"
140 
141       int ncid, i;
142       char *data[SOME_PRES] = {"Washington", "Adams", "Jefferson", "Madison",
143 			       "Monroe", "Adams", "Jackson", "Van Buren",
144 			       "Harrison", "Tyler", "Polk", "Taylor",
145 			       "Fillmore", "Pierce", "Buchanan", "Lincoln"};
146       char *data_in[SOME_PRES];
147 
148       /* Create a file with string attribute. */
149       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
150       if (nc_put_att_string(ncid, NC_GLOBAL, ATT2_NAME, SOME_PRES, (const char **)data)) ERR;
151       if (nc_close(ncid)) ERR;
152 
153       /* Check it out. */
154       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
155       if (nc_get_att_string(ncid, NC_GLOBAL, ATT2_NAME, (char **)data_in)) ERR;
156       for (i=0; i < SOME_PRES; i++)
157 	 if (strcmp(data_in[i], data[i])) ERR;
158 
159       /* Must free your data! */
160       if (nc_free_string(SOME_PRES, (char **)data_in)) ERR;
161 
162       if (nc_close(ncid)) ERR;
163    }
164    SUMMARIZE_ERR;
165    printf("*** testing string fill value...");
166    {
167 #define NUM_PRES 43
168 #define SOME_PRES 16
169 #define NDIMS_PRES 1
170 #define VAR_NAME_P "presidents"
171       int ncid, varid, i, dimids[NDIMS_PRES];
172       size_t start[NDIMS_PRES], count[NDIMS_PRES];
173       char *data[SOME_PRES] = {"Washington", "Adams", "Jefferson", "Madison",
174 			       "Monroe", "Adams", "Jackson", "Van Buren",
175 			       "Harrison", "Tyler", "Polk", "Taylor",
176 			       "Fillmore", "Pierce", "Buchanan", "Lincoln"};
177       char *data_in[NUM_PRES];
178 
179       /* Create a file with NUM_PRES strings, and write SOME_PRES of
180        * them. */
181       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
182       if (nc_def_dim(ncid, DIM_NAME, NUM_PRES, dimids)) ERR;
183       if (nc_def_var(ncid, VAR_NAME_P, NC_STRING, NDIMS_PRES, dimids, &varid)) ERR;
184       start[0] = 0;
185       count[0] = SOME_PRES;
186       if (nc_put_vara_string(ncid, varid, start, count, (const char **)data)) ERR;
187       if (nc_close(ncid)) ERR;
188 
189       /* Check it out. */
190       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
191       if (nc_get_var_string(ncid, varid, data_in)) ERR;
192       for (i = 0; i < NUM_PRES; i++)
193       {
194 	 if (i < SOME_PRES && (data_in[i] == NULL || strcmp(data_in[i], data[i]))) ERR;
195 	 if (i >= SOME_PRES && (data_in[i] == NULL || strcmp(data_in[i], ""))) ERR;
196       }
197 
198       /* Must free your data! */
199       if (nc_free_string(NUM_PRES, (char **)data_in)) ERR;
200 
201       if (nc_close(ncid)) ERR;
202    }
203    SUMMARIZE_ERR;
204    printf("*** Testing netcdf-4 strided string access...");
205    {
206 #define NUM_PRES 43
207 #define SOME_PRES 16
208 #define NDIMS_PRES 1
209 #define VAR_NAME_P "presidents"
210       int ncid, varid, i, dimids[NDIMS_PRES];
211       size_t start[NDIMS_PRES], count[NDIMS_PRES];
212       ptrdiff_t stride[NDIMS_PRES];
213       char *data[SOME_PRES] = {"Washington", "Adams", "Jefferson", "Madison",
214 			       "Monroe", "Adams", "Jackson", "Van Buren",
215 			       "Harrison", "Tyler", "Polk", "Taylor",
216 			       "Fillmore", "Pierce", "Buchanan", "Lincoln"};
217       char *data_in[NUM_PRES];
218       int status;
219 
220       /* Create a file with NUM_PRES strings, and write SOME_PRES of
221        * them. */
222       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
223       if (nc_def_dim(ncid, DIM_NAME, NUM_PRES, dimids)) ERR;
224       if (nc_def_var(ncid, VAR_NAME_P, NC_STRING, NDIMS_PRES, dimids, &varid)) ERR;
225       start[0] = 0;
226       count[0] = SOME_PRES;
227       stride[0] = 2;
228       status = nc_put_vars_string(ncid, varid, start, count, stride, (const char **)data);
229       if(status != NC_NOERR)
230 	  fprintf(stderr,"%s\n",nc_strerror(status));
231       if (nc_close(ncid)) ERR;
232 
233       /* Check it out. */
234       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
235       if (nc_get_vars_string(ncid, varid, start, count, stride, data_in)) ERR;
236       for (i = 0; i < NUM_PRES; i++)
237       {
238 	 if (i < SOME_PRES && strcmp(data_in[i], data[i])) ERR;
239       }
240 
241       /* Must free your data! */
242       if (nc_free_string(SOME_PRES, (char **)data_in)) ERR;
243 
244       if (nc_close(ncid)) ERR;
245    }
246    SUMMARIZE_ERR;
247    printf("*** testing string variables with fill values...");
248    {
249 #define VAR_NAME2 "empty"
250 #define ATT_NAME2 "empty"
251 #define DHR_LEN 30
252 #define DIM_NAME1 "article"
253 #define VAR_NAME1 "universal_declaration_of_human_rights"
254       int var_dimids[NDIMS];
255       int ndims, nvars, natts, unlimdimid;
256       nc_type var_type;
257       char var_name[NC_MAX_NAME + 1];
258       int var_natts, var_ndims;
259       int ncid, varid, i, dimids[NDIMS], varid2;
260       char *data[DHR_LEN] = {
261 	 "All human beings are born free and equal in dignity and rights. "
262 	 "They are endowed with reason and "
263 	 "conscience and should act towards one another in a "
264 	 "spirit of brotherhood.",
265 	 "Everyone is entitled to all the rights and freedoms set "
266 	 "forth in this Declaration, without distinction of "
267 	 "any kind, such as race, colour, sex, language, religion, "
268 	 "political or other opinion, national or social "
269 	 "origin, property, birth or other status. Furthermore, no "
270 	 "distinction shall be made on the basis of the "
271 	 "political, jurisdictional or international status of the "
272 	 "country or territory to which a person belongs, "
273 	 "whether it be independent, trust, non-self-governing or "
274 	 "under any other limitation of sovereignty.",
275 	 "Everyone has the right to life, liberty and security of person.",
276 	 "No one shall be held in slavery or servitude; slavery and the "
277 	 "slave trade shall be prohibited in all their forms.",
278 	 "No one shall be subjected to torture or to cruel, "
279 	 "inhuman or degrading treatment or punishment.",
280 	 "Everyone has the right to recognition everywhere as "
281 	 "a person before the law.",
282 	 "All are equal before the law and are entitled without "
283 	 "any discrimination to equal protection of the law. All are "
284 	 "entitled to equal protection against any discrimination in "
285 	 "violation of this Declaration and against any incitement "
286 	 "to such discrimination.",
287 	 "Everyone has the right to an effective remedy by the "
288 	 "competent national tribunals for acts violating the "
289 	 "fundamental rights granted "
290 	 "him by the constitution or by law.",
291 	 "No one shall be subjected to arbitrary arrest, detention or exile.",
292 	 "Everyone is entitled in full equality to a fair and public "
293 	 "hearing by an independent and impartial tribunal, in the "
294 	 "determination of his rights and obligations and of any "
295 	 "criminal charge against him.",
296 	 "(1) Everyone charged with a penal offence has the right "
297 	 "to be presumed innocent until proved guilty according to law in a "
298 	 "public trial at which he has had all the guarantees "
299 	 "necessary for his defence."
300 	 "(2) No one shall be held guilty of any penal offence "
301 	 "on account of any act or omission which did not constitute a penal "
302 	 "offence, under national or international law, at the time "
303 	 "when it was committed. Nor shall a heavier penalty be imposed "
304 	 "than the one that was applicable at the time the penal "
305 	 "offence was committed.",
306 	 "No one shall be subjected to arbitrary interference with "
307 	 "his privacy, family, home or correspondence, nor to attacks upon "
308 	 "his honour and reputation. Everyone has the right to the "
309 	 "protection of the law against such interference or attacks.",
310 	 "(1) Everyone has the right to freedom of movement and "
311 	 "residence within the borders of each state."
312 	 "(2) Everyone has the right to leave any country, "
313 	 "including his own, and to return to his country.",
314 	 "(1) Everyone has the right to seek and to enjoy in "
315 	 "other countries asylum from persecution. "
316 	 "(2) This right may not be invoked in the case of prosecutions "
317 	 "genuinely arising from non-political crimes or from acts "
318 	 "contrary to the purposes and principles of the United Nations.",
319 	 "(1) Everyone has the right to a nationality. (2) No one shall "
320 	 "be arbitrarily deprived of his nationality nor denied the "
321 	 "right to change his nationality.",
322 	 "(1) Men and women of full age, without any limitation "
323 	 "due to race, nationality or religion, have the right "
324 	 "to marry and to found a family. "
325 	 "They are entitled to equal rights as to marriage, "
326 	 "during marriage and at its dissolution. (2) Marriage "
327 	 "shall be entered into only with the free and full "
328 	 "consent of the intending spouses. (3) The family is "
329 	 "the natural and fundamental group unit of society and "
330 	 "is entitled to protection by society and the State.",
331 	 "(1) Everyone has the right to own property alone as "
332 	 "well as in association with others. (2) No one shall "
333 	 "be arbitrarily deprived of his property.",
334 	 "Everyone has the right to freedom of thought, conscience "
335 	 "and religion; this right includes freedom to change "
336 	 "his religion or belief, and freedom, "
337 	 "either alone or in community with others and in "
338 	 "public or private, to manifest his religion or "
339 	 "belief in teaching, practice, worship and observance.",
340 	 "Everyone has the right to freedom of opinion and "
341 	 "expression; this right includes freedom to hold "
342 	 "opinions without interference and to seek, receive "
343 	 "and impart information and ideas through any media "
344 	 "and regardless of frontiers.",
345 	 "(1) Everyone has the right to freedom of peaceful assembly "
346 	 "and association. (2) No one may be compelled to belong to "
347 	 "an association.",
348 	 "(1) Everyone has the right to take part in the government "
349 	 "of his country, directly or through freely chosen representatives. "
350 	 "(2) Everyone has the right of equal access to public "
351 	 "service in his country. (3) The will of the people "
352 	 "shall be the basis of the authority of government; "
353 	 "this will shall be expressed in periodic and genuine "
354 	 "elections which shall be by universal and equal suffrage "
355 	 "and shall be held by secret vote or by "
356 	 "equivalent free voting procedures.",
357 	 "Everyone, as a member of society, has the right to "
358 	 "social security and is entitled to realization, "
359 	 "through national effort and international co-operation "
360 	 "and in accordance with the organization and resources of "
361 	 "each State, of the economic, social and cultural rights "
362 	 "indispensable for his dignity and the free "
363 	 "development of his personality.",
364 	 "(1) Everyone has the right to work, to free choice "
365 	 "of employment, to just and favourable conditions of "
366 	 "work and to protection against unemployment. "
367 	 "(2) Everyone, without any discrimination, has the right "
368 	 "to equal pay for equal work. (3) Everyone who works "
369 	 "has the right to just and favourable "
370 	 "remuneration ensuring for himself and his family an "
371 	 "existence worthy of human dignity, and supplemented, "
372 	 "if necessary, by other means of social protection."
373 	 "(4) Everyone has the right to form and to join trade "
374 	 "unions for the protection of his interests.",
375 	 "Everyone has the right to rest and leisure, including "
376 	 "reasonable limitation of working hours and periodic "
377 	 "holidays with pay.",
378 	 "(1) Everyone has the right to a standard of living "
379 	 "adequate for the health and well-being of himself "
380 	 "and of his family, including food, clothing, housing "
381 	 "and medical care and necessary social services, and "
382 	 "the right to security in the event of unemployment, "
383 	 "sickness, disability, widowhood, old age or other lack "
384 	 "of livelihood in circumstances beyond his control. "
385 	 "(2) Motherhood and childhood are entitled to special "
386 	 "care and assistance. All children, whether "
387 	 "born in or out of wedlock, shall enjoy the same "
388 	 "social protection.",
389 	 "(1) Everyone has the right to education. Education "
390 	 "shall be free, at least in the elementary and "
391 	 "fundamental stages. Elementary education shall be compulsory. "
392 	 "Technical and professional education shall be made generally "
393 	 "available and higher education "
394 	 "shall be equally accessible to all on the basis of merit. "
395 	 "(2) Education shall be directed to the full development of "
396 	 "the human personality and to the strengthening of respect for "
397 	 "human rights and fundamental freedoms. "
398 	 "It shall promote understanding, tolerance and friendship among "
399 	 "all nations, racial or religious groups, and shall further the "
400 	 "activities of the United Nations "
401 	 "for the maintenance of peace. (3) Parents have a prior right to "
402 	 "choose the kind of education that shall be given to their children.",
403 	 "(1) Everyone has the right freely to participate in the cultural "
404 	 "life of the community, to enjoy the arts and to share in scientific "
405 	 "advancement and its benefits. "
406 	 "(2) Everyone has the right to the protection of the moral "
407 	 "and material interests resulting from any scientific, "
408 	 "literary or artistic production of which he is the author.",
409 	 "Everyone is entitled to a social and international order in "
410 	 "which the rights and freedoms set forth in this Declaration "
411 	 "can be fully realized.",
412 	 "(1) Everyone has duties to the community in which alone "
413 	 "the free and full development of his personality is possible. "
414 	 "(2) In the exercise of his rights and "
415 	 "freedoms, everyone shall be subject only to such limitations "
416 	 "as are determined by law solely for the purpose "
417 	 "of securing due recognition and respect for the rights "
418 	 "and freedoms of others and of meeting the just requirements "
419 	 "of morality, public order and the general welfare in a "
420 	 "democratic society. (3) These rights and freedoms may in no "
421 	 "case be exercised contrary to the purposes and "
422 	 "principles of the United Nations.",
423 	 "Nothing in this Declaration may be interpreted as implying "
424 	 "for any State, group or person any right to engage in any "
425 	 "activity or to perform any act aimed at the destruction of "
426 	 "any of the rights and freedoms set forth herein."
427       };
428       char *empty_string[] = {""};
429       char *my_string_fill[] = {"fill_string"};
430 
431 #define NUM_DIM_COMBOS 4
432       int dim_combo;
433 
434       for (dim_combo = 0; dim_combo < NUM_DIM_COMBOS; dim_combo++)
435       {
436          char filename[NC_MAX_NAME + 1];
437          int dim_len = dim_combo ? NC_UNLIMITED : DHR_LEN;
438          int expected_unlimdimid = dim_combo ? 0 : -1;
439          char *default_fill = ((char *)"");
440          char **string_fillp = dim_combo == 3 ? my_string_fill : &default_fill;
441          char *data_in;
442 
443          sprintf(filename, "%s_dim_combo_%d.nc", TEST_NAME, dim_combo);
444          if (nc_create(filename, NC_NETCDF4, &ncid)) ERR;
445 
446          /* Create an array of strings for the Universal Declaraion of Human Rights. */
447          if (nc_def_dim(ncid, DIM_NAME1, dim_len, dimids)) ERR;
448          if (nc_def_var(ncid, VAR_NAME1, NC_STRING, NDIMS, dimids, &varid)) ERR;
449 
450          /* Create a scalar variable for the empty string. */
451          if (nc_def_var(ncid, VAR_NAME2, NC_STRING, 0, NULL, &varid2)) ERR;
452          if (dim_combo == 3)
453             if (nc_put_att(ncid, varid, _FillValue, NC_STRING, 1, my_string_fill)) ERR;
454 
455          /* Check some stuff. */
456          if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
457          if (ndims != NDIMS || nvars != 2 || natts != 0 || unlimdimid != expected_unlimdimid) ERR;
458          if (nc_inq_var(ncid, varid, var_name, &var_type, &var_ndims,
459                         var_dimids, &var_natts)) ERR;
460          if (var_type != NC_STRING || strcmp(var_name, VAR_NAME1) || var_ndims != NDIMS ||
461              var_dimids[0] != dimids[0]) ERR;
462 
463          /* Write the universal declaraion of human rights. */
464          if (dim_combo)
465          {
466             size_t start[NDIMS], count[NDIMS] = {1};
467             int counter = 1;
468 
469             /* Write one record at a time. */
470             for (start[0] = 0; start[0] < DHR_LEN; start[0]++)
471             {
472                size_t new_start[NDIMS];
473                size_t *my_startp = start;
474 
475                /* For dim_combo 2 or 3 skip every other record. */
476                new_start[0] = start[0] + counter++;
477                if (dim_combo >= 2)
478                   my_startp = new_start;
479 
480                /* Write a record. */
481                nc_put_vara_string(ncid, varid, my_startp, count, (const char **)&data[start[0]]);
482             }
483          }
484          else
485          {
486             /* Write all records at once. */
487             if (nc_put_var(ncid, varid, data)) ERR;
488          }
489 
490          /* Write an empty string with an empty attribute. */
491          if (nc_put_var(ncid, varid2, empty_string)) ERR;
492          if (nc_put_att(ncid, varid2, ATT_NAME2, NC_STRING, 0, empty_string)) ERR;
493 
494          /* Close the file. */
495          if (nc_close(ncid)) ERR;
496 
497          /* Check it out. */
498          if (nc_open(filename, NC_NOWRITE, &ncid)) ERR;
499          if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
500          if (ndims != NDIMS || nvars != 2 || natts != 0 || unlimdimid != expected_unlimdimid) ERR;
501 
502          /* Check declaration. */
503          if (nc_inq_varid(ncid, VAR_NAME1, &varid)) ERR;
504          if (nc_inq_var(ncid, varid, var_name, &var_type, &var_ndims, var_dimids,
505                         &var_natts)) ERR;
506          if (var_type != NC_STRING || strcmp(var_name, VAR_NAME1) || var_ndims != NDIMS ||
507              var_dimids[0] != dimids[0]) ERR;
508 
509          /* Check fill value stuff. */
510          {
511             int no_fill;
512             char *fill_value_in[1];
513 
514             if (nc_inq_var_fill(ncid, varid, &no_fill, (char **)fill_value_in)) ERR;
515             if (no_fill) ERR;
516             if (strcmp(fill_value_in[0], *string_fillp)) ERR;
517             if (nc_free_string(1, (char **)fill_value_in)) ERR;
518          }
519 
520          if (dim_combo < 2)
521          {
522             char *data_in[DHR_LEN];
523 
524             /* Get the data in one read of the entire var. */
525             if (nc_get_var(ncid, varid, data_in)) ERR;
526             for (i = 0; i < DHR_LEN; i++)
527                if (strcmp(data_in[i], data[i])) ERR;
528             if (nc_free_string(DHR_LEN, data_in)) ERR;
529          }
530          else
531          {
532             char *data_in;
533             size_t start[NDIMS], count[NDIMS] = {1};
534             int my_count = 0;
535 
536             /* Get the data one record at a time. Every other record,
537              * starting with the first, is a fill value. */
538             for (start[0] = 0; start[0] < DHR_LEN; start[0]++)
539             {
540                if (nc_get_vara(ncid, varid, start, count, &data_in)) ERR;
541                if (start[0] % 2)
542                {
543                   if (strcmp(data_in, data[my_count++])) ERR;
544                }
545                else
546                {
547                   if (strcmp(data_in, *string_fillp)) ERR;
548                }
549                if (nc_free_string(1, &data_in)) ERR;
550             }
551          }
552 
553          /* Check the empty var and att. */
554          if (nc_inq_varid(ncid, VAR_NAME2, &varid)) ERR;
555          if (nc_get_var(ncid, varid, &data_in)) ERR;
556          if (strcmp(data_in, empty_string[0])) ERR;
557          if (nc_free_string(1, &data_in)) ERR;
558          if (nc_get_att(ncid, varid, ATT_NAME2, NULL)) ERR;
559          if (nc_close(ncid)) ERR;
560       } /* next dim_combo */
561    }
562    SUMMARIZE_ERR;
563 
564    printf("*** Testing a file that causes ncdump problems...");
565    {
566 #define NUM_DIMS 2
567 #define DIM_0_NAME "dim_0"
568 #define DIM_1_NAME "dim_1"
569 #define DIM_1_LEN 2
570 #define FILE_NAME_NCDUMP "tst_strings_ncdump_problem.nc"
571 #define VAR_NAME_NCDUMP "var_string"
572       int ncid, varid, dimid[NUM_DIMS];
573       char *string_data[] = {"x"};
574       int t;
575 
576       /* Create a file. */
577       if (nc_create(FILE_NAME_NCDUMP, NC_NETCDF4, &ncid)) ERR;
578 
579       /* Create dims. */
580       if (nc_def_dim(ncid, DIM_0_NAME, NC_UNLIMITED, &dimid[0])) ERR;
581       if (nc_def_dim(ncid, DIM_1_NAME, DIM_1_LEN, &dimid[1])) ERR;
582 
583       /* Create a var. */
584       if (nc_def_var(ncid, VAR_NAME_NCDUMP, NC_STRING, NUM_DIMS, dimid, &varid)) ERR;
585 
586       /* Check that you can't turn off fill mode for NC_STRING variables. */
587       if (nc_def_var_fill(ncid, varid, NC_NOFILL, NULL) != NC_EINVAL) ERR;
588 
589       /* End define mode. */
590       if (nc_enddef(ncid)) ERR;
591 
592       /* Write to each var. */
593       for (t = 0; t < 1; t++)
594       {
595          size_t start[NUM_DIMS] = {1, 0};
596          size_t count[NUM_DIMS] = {1, 1};
597 
598          if (nc_put_vara_string(ncid, varid, start, count, (const char **)string_data)) ERR;
599       }
600       if (nc_close(ncid)) ERR;
601    }
602    SUMMARIZE_ERR;
603    FINAL_RESULTS;
604 }
605