1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-07-22 18:57:39 +02:00

unit testing for sparse data in C

This commit is contained in:
q-posev 2021-12-03 11:06:31 +01:00
parent 7e83e6698d
commit 1bb8ae5b67

View File

@ -1,25 +1,37 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include "trexio.h" #include "trexio.h"
#include <assert.h> #include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define FILENAME "test_sparse.dir" #define TEST_BACKEND TREXIO_TEXT
#define TREXIO_FILE "test_dset_sparse.dir"
#define RM_COMMAND "rm -rf " TREXIO_FILE
#define SIZE 100
#define N_CHUNKS 5
static int test_write_dset_sparse (const char* file_name, const back_end_t backend) {
int main(){ /* Try to write an array of sparse data into the TREXIO file */
uint64_t offset_f = 0; trexio_t* file = NULL;
trexio_exit_code rc;
// create the data to write /*================= START OF TEST ==================*/
uint64_t size = 100;
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
assert (rc == TREXIO_SUCCESS);
// parameters to be written
int32_t* index; int32_t* index;
double* value; double* value;
index = calloc(4L*size, sizeof(int32_t)); index = calloc(4L*SIZE, sizeof(int32_t));
value = calloc(size, sizeof(double)); value = calloc(SIZE, sizeof(double));
for(int i=0; i<size; i++){ for(int i=0; i<SIZE; i++){
index[4*i] = 4*i; index[4*i] = 4*i;
index[4*i+1] = 4*i+1; index[4*i+1] = 4*i+1;
index[4*i+2] = 4*i+2; index[4*i+2] = 4*i+2;
@ -27,86 +39,163 @@ int main(){
value[i] = 3.14 + (double) i; value[i] = 3.14 + (double) i;
} }
int rc_open; // write dataset chunks of sparse data in the file (including FAKE statements)
trexio_t* trexio_file = trexio_open(FILENAME, 'w', TREXIO_TEXT, &rc_open); uint64_t chunk_size = (uint64_t) SIZE/N_CHUNKS;
assert(rc_open == TREXIO_SUCCESS); uint64_t offset_f = 0;
assert(trexio_file != NULL);
// write n_chunks times using write_sparse
// check if the file exists for(int i=0; i<N_CHUNKS; ++i){
int rc; rc = trexio_write_mo_2e_int_eri(file, offset_f, chunk_size, &index[4*offset_f], &value[offset_f]);
rc = trexio_has_mo_2e_int_eri(trexio_file); assert(rc == TREXIO_SUCCESS);
assert(rc==TREXIO_SUCCESS || rc==TREXIO_HAS_NOT);
if(rc==TREXIO_HAS_NOT){
printf("FILE DID NOT EXIST AT THE START.\n");
} else {
printf("FILE EXIST AT THE START.\n");
}
// check that previous call to has_sparse did not create a file
if (rc==TREXIO_HAS_NOT) {
rc = trexio_has_mo_2e_int_eri(trexio_file);
assert(rc==TREXIO_HAS_NOT);
}
// define chunks: here they contain 20 elements, i.e. 100/5
int n_chunks = 5;
uint64_t chunk_size = size/n_chunks;
// write 5 chunks using write_sparse
for(int i=0; i<n_chunks; ++i){
rc = trexio_write_mo_2e_int_eri(trexio_file, offset_f, chunk_size, &index[4*offset_f], &value[offset_f]);
assert(rc==TREXIO_SUCCESS);
offset_f += chunk_size; offset_f += chunk_size;
} }
rc = trexio_close(trexio_file); // close current session
assert(rc == TREXIO_SUCCESS); rc = trexio_close(file);
// TODO: SHOULD NOT TREXIO_CLOSE SET TO NULL THE TREXIO_T POINTER ??? assert (rc == TREXIO_SUCCESS);
//assert(trexio_file == NULL);
trexio_t* trexio_file_2 = trexio_open(FILENAME, 'r', TREXIO_TEXT, &rc_open); // free the allocated memeory
assert(rc_open == TREXIO_SUCCESS);
assert(trexio_file_2 != NULL);
// define arrays to read into
int32_t* index_read;
double* value_read;
index_read = (int32_t*) calloc(4L*chunk_size,sizeof(int32_t));
value_read = (double*) calloc(chunk_size,sizeof(double));
// read 1 chunk of 20 elements using offset of 40( i.e. lines No. 40--59)
uint64_t offset_f_read = 40L;
int offset_data_read = 5;
rc = trexio_read_mo_2e_int_eri(trexio_file_2, offset_f_read, 10L, &index_read[4*offset_data_read], &value_read[offset_data_read]);
assert(rc==TREXIO_SUCCESS);
assert(index_read[0]==0 && index_read[4*offset_data_read]==offset_f_read*4);
// print the data being read
for(int i=0; i<chunk_size; ++i){
printf("%d %d %d %d %lf\n",
index_read[4*i],
index_read[4*i+1],
index_read[4*i+2],
index_read[4*i+3],
value_read[i]
);
}
// check that the file has been created
rc = trexio_has_mo_2e_int_eri(trexio_file_2);
assert(rc==TREXIO_SUCCESS);
// close the TREXIO file
rc = trexio_close(trexio_file_2);
assert(rc == TREXIO_SUCCESS);
// free the memory
free(index); free(index);
free(value); free(value);
/*================= END OF TEST ==================*/
return 0;
}
static int test_has_dset_sparse (const char* file_name, const back_end_t backend) {
/* Try to check the existence of a dataset of sparse data in the TREXIO file */
trexio_t* file = NULL;
trexio_exit_code rc;
/*================= START OF TEST ==================*/
// open file
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
assert (rc == TREXIO_SUCCESS);
// first check that mo_2e_int_eri_lr (we only write non-lr component in this unit test)
rc = trexio_has_mo_2e_int_eri_lr(file);
assert(rc==TREXIO_HAS_NOT);
// check that previous call to has_sparse did not create a file/dset
rc = trexio_has_mo_2e_int_eri_lr(file);
assert(rc==TREXIO_HAS_NOT);
// now check that previously written mo_2e_int_eri exists
rc = trexio_has_mo_2e_int_eri(file);
assert(rc==TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
static int test_read_dset_sparse (const char* file_name, const back_end_t backend) {
/* Try to read one chunk of dataset of sparse data in the TREXIO file */
trexio_t* file = NULL;
trexio_exit_code rc;
/*================= START OF TEST ==================*/
// open file
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
assert (rc == TREXIO_SUCCESS);
// define arrays to read into
int32_t* index_read;
double* value_read;
uint64_t size = 20L;
index_read = (int32_t*) calloc(4L*size,sizeof(int32_t));
value_read = (double*) calloc(size,sizeof(double));
// specify the read parameters, here:
// 1 chunk of 10 elements using offset of 40 (i.e. lines No. 40--59) into elements of the array starting from 5
uint64_t chunk_read = 10L;
uint64_t offset_file_read = 40L;
int offset_data_read = 5;
// read one chunk using the aforementioned parameters
rc = trexio_read_mo_2e_int_eri(file, offset_file_read, chunk_read, &index_read[4*offset_data_read], &value_read[offset_data_read]);
assert(rc == TREXIO_SUCCESS);
assert(index_read[0] == 0);
assert(index_read[4*offset_data_read] == offset_file_read*4);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
// free the memory
free(index_read); free(index_read);
free(value_read); free(value_read);
/*================= END OF TEST ==================*/
return 0;
}
static int test_read_dset_sparse_size (const char* file_name, const back_end_t backend, const int64_t size_check) {
/* Try to read a size of the dataset of sparse data in the TREXIO file */
trexio_t* file = NULL;
trexio_exit_code rc;
/*================= START OF TEST ==================*/
// open file
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
assert (rc == TREXIO_SUCCESS);
// define the variable to read into
int64_t size_written;
// read one chunk using the aforementioned parameters
rc = trexio_read_mo_2e_int_eri_size(file, &size_written);
assert(rc == TREXIO_SUCCESS);
assert(size_written == size_check);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
int main(){
/*============== Test launcher ================*/
int rc;
rc = system(RM_COMMAND);
assert (rc == 0);
// check the first write attempt (SIZE elements written in N_CHUNKS chunks)
test_write_dset_sparse (TREXIO_FILE, TEST_BACKEND);
test_has_dset_sparse (TREXIO_FILE, TEST_BACKEND);
test_read_dset_sparse (TREXIO_FILE, TEST_BACKEND);
test_read_dset_sparse_size(TREXIO_FILE, TEST_BACKEND, (int64_t) SIZE);
// check the second write attempt (SIZE elements written in N_CHUNKS chunks)
test_write_dset_sparse (TREXIO_FILE, TEST_BACKEND);
test_read_dset_sparse_size(TREXIO_FILE, TEST_BACKEND, (int64_t) SIZE*2);
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
} }