netcdf-c/unit_test/tst_nclist.c

118 lines
3.3 KiB
C
Raw Normal View History

2019-08-09 23:31:24 +08:00
/* This is part of the netCDF package. Copyright 2005-2019 University
Corporation for Atmospheric Research/Unidata. See COPYRIGHT file
for conditions of use.
Test list functions in nclistmgr.c.
Ed Hartnett, 8/10/19
*/
#include "config.h"
#include <nc_tests.h>
2019-08-09 23:38:40 +08:00
#include "nc.h"
2019-08-10 03:37:54 +08:00
#include "ncdispatch.h"
2019-08-09 23:31:24 +08:00
#include "err_macros.h"
2019-08-10 03:49:52 +08:00
/* An integer value to use in testing. */
#define TEST_VAL_42 42
#define FILE_NAME "tst_nclist.nc"
2019-08-09 23:31:24 +08:00
int
main(int argc, char **argv)
{
2019-08-10 01:13:55 +08:00
printf("\n*** Testing netcdf internal NC list functions.\n");
printf("Testing NC list functions with no open files...");
{
/* These are cases where there are no files, or NULL
* params. */
if (count_NCList()) ERR;
free_NCList();
if (find_in_NCList_by_name("nope")) ERR;
if (iterate_NCList(0, NULL)) ERR;
}
SUMMARIZE_ERR;
printf("Testing adding to NC list...");
{
int ncid;
2019-08-10 03:49:52 +08:00
NC *ncp, *ncp2;
2019-08-10 03:37:54 +08:00
int mode = 0;
NCmodel model;
int ret;
2019-08-09 23:31:24 +08:00
2019-08-10 03:37:54 +08:00
/* Create the NC* instance and insert its dispatcher and model. */
if ((ret = new_NC(NULL, FILE_NAME, mode, &model, &ncp))) ERR;
2019-08-10 01:13:55 +08:00
/* Nothing to find yet. */
if (find_in_NCList(TEST_VAL_42)) ERR;
2019-08-21 18:52:33 +08:00
/* Add to list of known open files and define ext_ncid. To get
* the ncid, we find the first open index > 1 in the
* nc_filelist array, which has a size of 65536. Then we
* left-shift that index 16 bits to put it in the first
* 2-bytes of the 4-byte ncid. (The other two bytes are
* reserved for grpid of netCDF-4 groups.) */
2019-08-10 03:37:54 +08:00
add_to_NCList(ncp);
2019-08-10 03:49:52 +08:00
/* These won't work! */
if (find_in_NCList(TEST_VAL_42)) ERR;
if (find_in_NCList_by_name("nope")) ERR;
if ((ret = iterate_NCList(2, &ncp2))) ERR;
/* Find it in the list. */
if (!(ncp2 = find_in_NCList(ncp->ext_ncid))) ERR;
if (!(ncp2 = find_in_NCList_by_name(FILE_NAME))) ERR;
2019-08-10 03:49:52 +08:00
if ((ret = iterate_NCList(1, &ncp2))) ERR;
if (count_NCList() != 1) ERR;
/* Won't do anything because list contains an entry. */
free_NCList();
2019-08-10 03:49:52 +08:00
/* Delete it. */
ncid = ncp->ext_ncid;
del_from_NCList(ncp); /* Will free empty list. */
free_NC(ncp);
/* Ensure it is no longer in list. */
if (find_in_NCList(ncid)) ERR;
2019-08-10 01:13:55 +08:00
}
SUMMARIZE_ERR;
#ifdef LARGE_FILE_TESTS
/* This test is slow, only run it on large file test builds. */
printf("Testing maxing out NC list...");
{
NC *ncp;
int mode = 0;
NCmodel model;
int max_num_nc = 65535;
int i;
int ret;
/* Fill the NC list. */
for (i = 0; i < max_num_nc; i++)
{
if ((ret = new_NC(NULL, FILE_NAME, mode, &model, &ncp))) ERR;
if (add_to_NCList(ncp)) ERR;
}
/* Check the count. */
if (count_NCList() != max_num_nc) ERR;
/* Try and add another. It will fail. */
if (add_to_NCList(ncp) != NC_ENOMEM) ERR;
/* Delete them all. */
for (i = 0; i < max_num_nc; i++)
{
if (iterate_NCList(i + 1, &ncp)) ERR;
if (!ncp) ERR;
del_from_NCList(ncp);
free_NC(ncp);
}
}
SUMMARIZE_ERR;
#endif /* LARGE_FILE_TESTS */
2019-08-10 01:13:55 +08:00
FINAL_RESULTS;
2019-08-09 23:31:24 +08:00
}