<commit_before>/* This is part of the netCDF package. Copyright 2020 University
   Corporation for Atmospheric Research/Unidata See COPYRIGHT file for
   conditions of use.

   Test internal netcdf-4 file code for memory leaks. This test was
   suggest by <NAME>. See
   https://github.com/Unidata/netcdf-c/issues/1575.

   <NAME> 2/9/20
*/

#include <config.h>
#include <nc_tests.h>
#include <err_macros.h>
#include <sys/resource.h>

int main()
{
    int dataset_id, time_id, dummyvar_id, ret, idx;
    size_t start[1] = {0};
    size_t count[1] = {100};
    double data[100];
    struct rusage r_usage;

    for (idx = 0; idx < 100; idx++)
    {
        data[idx]=-99;
    };

    printf("\n*** Testing netcdf-4 memory use with unlimited dimensions.\n");
    printf("*** testing with user-contributed code...");
    {
        if (nc_create("test.nc", NC_CLOBBER | NC_NETCDF4, &dataset_id)) ERR;
        /*ret=nc_create("test.nc", NC_CLOBBER | NC_64BIT_OFFSET, &dataset_id);*/
        if(ret!=NC_NOERR) {printf("%s\n",nc_strerror(ret)); return 1;}
        ret=nc_def_dim(dataset_id, "time", NC_UNLIMITED, &time_id);
        if(ret!=NC_NOERR) {printf("%s\n",nc_strerror(ret)); return 1;}
        ret=nc_def_var(dataset_id, "dummy", NC_DOUBLE, 1, &time_id, &dummyvar_id);
        if(ret!=NC_NOERR) {printf("%s\n",nc_strerror(ret)); return 1;}
        /*ret=nc_enddef(dataset_id);*/
        ret=nc_put_vara(dataset_id, dummyvar_id, start, count, data);
        if(ret!=NC_NOERR) {printf("%s\n",nc_strerror(ret)); return 1;}
        ret=nc_close(dataset_id);
        if(ret!=NC_NOERR) {printf("%s\n",nc_strerror(ret)); return 1;}

        for (idx = 0; idx < 100000; idx++) {
            ret=nc_open("test.nc", NC_NOWRITE, &dataset_id);
            if(ret!=NC_NOERR) {printf("%s\n",nc_strerror(ret)); return 1;}
            ret=nc_close(dataset_id);
            if(ret!=NC_NOERR) {printf("%s\n",nc_strerror(ret)); return 1;}
            getrusage(RUSAGE_SELF,&r_usage);
            /* printf("Memory usage: %ld kilobytes\n",r_usage.ru_maxrss); */
        };
    }
    SUMMARIZE_ERR;
    FINAL_RESULTS;
}
<commit_msg>now detect memory leak open/closing files with tst_mem1.c<commit_after>/* This is part of the netCDF package. Copyright 2020 University
   Corporation for Atmospheric Research/Unidata See COPYRIGHT file for
   conditions of use.

   Test internal netcdf-4 file code for memory leaks. This test was
   suggest by <NAME>. See
   https://github.com/Unidata/netcdf-c/issues/1575.

   <NAME> 2/9/20
*/

#include <config.h>
#include <nc_tests.h>
#include <err_macros.h>
#include <sys/resource.h>

#define FILE_NAME "tst_mem1.nc"
#define NUM_FILE_OPENS 100

int main()
{
    int dataset_id, time_id, dummyvar_id, idx;
    size_t start[1] = {0};
    size_t count[1] = {100};
    double data[100];
    struct rusage r_usage;

    for (idx = 0; idx < 100; idx++)
    {
        data[idx]=-99;
    };

    printf("\n*** Testing netcdf-4 memory use.\n");
    printf("*** testing mem use opening/closing file...");
    {
        long my_rss = 0;

        if (nc_create(FILE_NAME, NC_CLOBBER | NC_NETCDF4, &dataset_id)) ERR;
        if (nc_def_dim(dataset_id, "time", NC_UNLIMITED, &time_id)) ERR;
        if (nc_def_var(dataset_id, "dummy", NC_DOUBLE, 1, &time_id, &dummyvar_id)) ERR;
        if (nc_put_vara(dataset_id, dummyvar_id, start, count, data)) ERR;
        if (nc_close(dataset_id)) ERR;

        for (idx = 0; idx < NUM_FILE_OPENS; idx++)
        {
            if (nc_open(FILE_NAME, NC_NOWRITE, &dataset_id)) ERR;
            if (nc_close(dataset_id)) ERR;
            getrusage(RUSAGE_SELF, &r_usage);
            printf("Memory usage: %ld kilobytes\n",r_usage.ru_maxrss);
            if (!my_rss)
                my_rss = r_usage.ru_maxrss;
            else
                if (my_rss != r_usage.ru_maxrss) ERR;
        };
    }
    SUMMARIZE_ERR;
    FINAL_RESULTS;
}

{'fraction_non_alphanumeric': 0.14679371619881534, 'fraction_numerical': 0.021890291012104043, 'mean_word_length': 2.923232323232323, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1732083', 'n_tokens_mistral': 1499, 'n_tokens_neox': 1415, 'n_words': 320}