diff options
author | Magnus Lundborg <lundborg.magnus@gmail.com> | 2014-07-17 09:48:22 (GMT) |
---|---|---|
committer | Magnus Lundborg <lundborg.magnus@gmail.com> | 2014-07-21 13:56:40 (GMT) |
commit | 3e2c4306bba827d5b1d3444d5fb9a81e4b2a88bb (patch) | |
tree | fbd19526e0fa2b8f32a4229bb8d1db6d0e0dcee4 | |
parent | 1da40c3a2b2d3809871bcdbafe9c13f337ee1e88 (diff) |
Improved testing of TNG functions.
The coverage of the testing suite is improved. Better
checks to verify that the data is what is expected.
Change-Id: I294779456fee2da2e3617de985d23d2f42f09f56
-rw-r--r-- | src/tests/tng_io_testing.c | 824 |
1 files changed, 673 insertions, 151 deletions
diff --git a/src/tests/tng_io_testing.c b/src/tests/tng_io_testing.c index 9f51bb1..e4ffee5 100644 --- a/src/tests/tng_io_testing.c +++ b/src/tests/tng_io_testing.c @@ -17,14 +17,29 @@ #include <stdlib.h> #include <string.h> +#include <math.h> #include "tng/version.h" +#define BOX_SHAPE_X 150.0 +#define BOX_SHAPE_Y 145.5 +#define BOX_SHAPE_Z 155.5 +#define N_FRAME_SETS 100 +#define MEDIUM_STRIDE_LEN 5 +#define LONG_STRIDE_LEN 25 +#define TIME_PER_FRAME 2e-15 +#define COMPRESSION_PRECISION 1000 +#define USER_NAME "USER 1" +#define PROGRAM_NAME "tng_testing" +#define COMPUTER_NAME "Unknown computer" +#define FORCEFIELD_NAME "No forcefield" + static tng_function_status tng_test_setup_molecules(tng_trajectory_t traj) { tng_molecule_t molecule; tng_chain_t chain; tng_residue_t residue; tng_atom_t atom; + tng_bond_t bond; int64_t cnt; tng_molecule_add(traj, "water", &molecule); @@ -42,8 +57,15 @@ static tng_function_status tng_test_setup_molecules(tng_trajectory_t traj) { return(TNG_CRITICAL); } + tng_molecule_bond_add(traj, molecule, 0, 1, &bond); + tng_molecule_bond_add(traj, molecule, 0, 2, &bond); tng_molecule_cnt_set(traj, molecule, 200); tng_molecule_cnt_get(traj, molecule, &cnt); + + if(cnt != 200) + { + return(TNG_CRITICAL); + } /* printf("Created %"PRId64" %s molecules.\n", cnt, molecule->name); */ /* traj->molecule_cnt_list[traj->n_molecules-1] = 5; @@ -107,29 +129,316 @@ static tng_function_status tng_test_setup_molecules(tng_trajectory_t traj) return(TNG_SUCCESS); } +static tng_function_status tng_test_molecules(tng_trajectory_t traj) +{ + tng_molecule_t molecule, molecule_new; + tng_chain_t chain; + tng_residue_t residue; + tng_atom_t atom; + int64_t cnt, *bonds_to, *bonds_from; + char var_atoms, str[TNG_MAX_STR_LEN]; + tng_function_status stat; + + stat = tng_num_molecule_types_get(traj, &cnt); + if(stat != TNG_SUCCESS || cnt != 1) + { + printf("Molecule reading error. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } + + stat = tng_num_molecules_get(traj, &cnt); + if(stat != TNG_SUCCESS || cnt != 200) + { + printf("Molecule reading error. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } + + stat = tng_num_particles_variable_get(traj, &var_atoms); + if(stat != TNG_SUCCESS || var_atoms) + { + printf("Molecule reading error. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } + + stat = tng_molecule_of_index_get(traj, 0, &molecule); + if(stat != TNG_SUCCESS) + { + printf("Cannot find molecule. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + stat = tng_molecule_find(traj, "water", -1, &molecule); + if(stat != TNG_SUCCESS) + { + printf("Cannot find molecule. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + stat =tng_molecule_name_get(traj, molecule, str, TNG_MAX_STR_LEN); + if(stat != TNG_SUCCESS) + { + printf("Cannot get molecule name. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + stat = tng_molecule_num_chains_get(traj, molecule, &cnt); + if(stat != TNG_SUCCESS || cnt != 1) + { + printf("Cannot get number of chains in molecule. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } + stat = tng_molecule_chain_of_index_get(traj, molecule, 0, &chain); + if(stat != TNG_SUCCESS) + { + printf("Cannot get chain in molecule. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + stat = tng_molecule_chain_find(traj, molecule, "W", -1, &chain); + if(stat != TNG_SUCCESS) + { + printf("Cannot get chain in molecule. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + stat = tng_molecule_num_residues_get(traj, molecule, &cnt); + if(stat != TNG_SUCCESS || cnt != 1) + { + printf("Cannot get number of residues in molecule. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } + stat = tng_molecule_residue_of_index_get(traj, molecule, 0, &residue); + if(stat != TNG_SUCCESS) + { + printf("Cannot get residue in molecule. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + stat = tng_molecule_num_atoms_get(traj, molecule, &cnt); + if(stat != TNG_SUCCESS || cnt != 3) + { + printf("Cannot get number of atoms in molecule. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } + stat = tng_molecule_atom_of_index_get(traj, molecule, 0, &atom); + if(stat != TNG_SUCCESS) + { + printf("Cannot get atom in molecule. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + stat = tng_molecule_atom_find(traj, molecule, "O", -1, &atom); + if(stat != TNG_SUCCESS) + { + printf("Cannot get atom in molecule. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + + stat =tng_chain_name_get(traj, chain, str, TNG_MAX_STR_LEN); + if(stat != TNG_SUCCESS) + { + printf("Cannot get name of chain. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + stat = tng_chain_num_residues_get(traj, chain, &cnt); + if(stat != TNG_SUCCESS || cnt != 1) + { + printf("Cannot get number of residues in chain. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } + stat = tng_chain_residue_of_index_get(traj, chain, 0, &residue); + if(stat != TNG_SUCCESS) + { + printf("Cannot get residue in chain. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + stat = tng_chain_residue_find(traj, chain, "WAT", -1, &residue); + if(stat != TNG_SUCCESS) + { + printf("Cannot get residue in chain. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + + stat = tng_residue_name_get(traj, residue, str, TNG_MAX_STR_LEN); + if(stat != TNG_SUCCESS) + { + printf("Cannot get name of residue. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + stat = tng_residue_num_atoms_get(traj, residue, &cnt); + if(stat != TNG_SUCCESS || cnt != 3) + { + printf("Cannot get number of atoms in residue. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } + stat = tng_residue_atom_of_index_get(traj, residue, 0, &atom); + if(stat != TNG_SUCCESS) + { + printf("Cannot get residue of atom. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + + stat = tng_atom_name_get(traj, atom, str, TNG_MAX_STR_LEN); + if(stat != TNG_SUCCESS) + { + printf("Cannot get name of atom. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + stat = tng_atom_type_get(traj, atom, str, TNG_MAX_STR_LEN); + if(stat != TNG_SUCCESS) + { + printf("Cannot get atom type of atom. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + + stat = tng_molecule_id_of_particle_nr_get(traj, 0, &cnt); + if(stat != TNG_SUCCESS || cnt != 1) + { + printf("Cannot get molecule id of atom. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } + stat = tng_residue_id_of_particle_nr_get(traj, 0, &cnt); + if(stat != TNG_SUCCESS || cnt != 0) + { + printf("Cannot get residue id of atom. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } + stat = tng_global_residue_id_of_particle_nr_get(traj, 599, &cnt); + if(stat != TNG_SUCCESS || cnt != 199) + { + printf("Cannot get global residue id of atom. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } + stat = tng_molecule_name_of_particle_nr_get(traj, 0, str, TNG_MAX_STR_LEN); + if(stat != TNG_SUCCESS) + { + printf("Cannot get molecule name of atom. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + stat = tng_chain_name_of_particle_nr_get(traj, 0, str, TNG_MAX_STR_LEN); + if(stat != TNG_SUCCESS) + { + printf("Cannot get chain name of atom. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + stat = tng_residue_name_of_particle_nr_get(traj, 0, str, TNG_MAX_STR_LEN); + if(stat != TNG_SUCCESS) + { + printf("Cannot get residue name of atom. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + stat = tng_atom_name_of_particle_nr_get(traj, 0, str, TNG_MAX_STR_LEN); + if(stat != TNG_SUCCESS) + { + printf("Cannot get atom name of atom. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + + stat = tng_molecule_alloc(traj, &molecule_new); + if(stat != TNG_SUCCESS) + { + printf("Cannot setup new molecule. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + stat = tng_molecule_name_set(traj, molecule_new, "TEST"); + if(stat != TNG_SUCCESS) + { + printf("Cannot set name of new molecule. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + stat = tng_molecule_existing_add(traj, &molecule_new); + if(stat != TNG_SUCCESS) + { + printf("Cannot add new molecule to molecule system. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + + stat = tng_molsystem_bonds_get(traj, &cnt, &bonds_from, &bonds_to); + if(stat != TNG_SUCCESS || cnt != 400) + { + printf("Cannot get bonds in molecule system. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + + return(TNG_SUCCESS); +} + static tng_function_status tng_test_read_and_write_file (tng_trajectory_t traj, const char hash_mode) { + char file_name[TNG_MAX_STR_LEN]; tng_function_status stat; + stat = tng_input_file_get(traj, file_name, TNG_MAX_STR_LEN); + if(stat != TNG_SUCCESS) + { + printf("Could not get name of input file. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + stat = tng_output_file_get(traj, file_name, TNG_MAX_STR_LEN); + if(stat != TNG_SUCCESS) + { + printf("Could not get name of output file. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + stat = tng_file_headers_read(traj, hash_mode); - if(stat == TNG_CRITICAL) + if(stat != TNG_SUCCESS) { + printf("Could not read headers. %s: %d\n", + __FILE__, __LINE__); return(stat); } stat = tng_file_headers_write(traj, hash_mode); - if(stat == TNG_CRITICAL) + if(stat != TNG_SUCCESS) { + printf("Could not write headers. %s: %d\n", + __FILE__, __LINE__); return(stat); } while(stat == TNG_SUCCESS) { stat = tng_frame_set_read_next(traj, hash_mode); - if(stat != TNG_SUCCESS) + if(stat == TNG_CRITICAL) { + printf("Could not read frame set. %s: %d\n", + __FILE__, __LINE__); return(stat); } + if(stat == TNG_FAILURE) + { + return(TNG_SUCCESS); + } stat = tng_frame_set_write(traj, hash_mode); } @@ -139,20 +448,31 @@ static tng_function_status tng_test_read_and_write_file static tng_function_status tng_test_write_and_read_traj(tng_trajectory_t *traj, const char hash_mode) { - int i, j, k, nr, cnt; + int i, j, k, nr, cnt, dependency; float *data, *molpos, *charges; int64_t mapping[300], n_particles, n_frames_per_frame_set, tot_n_mols; int64_t codec_id; + int64_t dist_exp = -9, temp_int, temp_int2; // int64_t frame_nr; - double box_shape[9]; + double box_shape[9], temp_double; char atom_type[16], annotation[128]; + char temp_str[TNG_MAX_STR_LEN]; + tng_trajectory_frame_set_t frame_set; tng_function_status stat = TNG_SUCCESS; - tng_medium_stride_length_set(*traj, 10); - tng_long_stride_length_set(*traj, 100); + tng_medium_stride_length_set(*traj, MEDIUM_STRIDE_LEN); + tng_long_stride_length_set(*traj, LONG_STRIDE_LEN); + + tng_first_user_name_set(*traj, USER_NAME); + tng_first_program_name_set(*traj, PROGRAM_NAME); + tng_first_computer_name_set(*traj, COMPUTER_NAME); + tng_forcefield_name_set(*traj, FORCEFIELD_NAME); + + tng_compression_precision_set(*traj, COMPRESSION_PRECISION); - tng_first_user_name_set(*traj, "User1"); - tng_first_program_name_set(*traj, "tng_testing"); + tng_distance_unit_exponential_set(*traj, dist_exp); + + tng_time_per_frame_set(*traj, TIME_PER_FRAME); /* Create molecules */ if(tng_test_setup_molecules(*traj) == TNG_CRITICAL) @@ -163,9 +483,9 @@ static tng_function_status tng_test_write_and_read_traj(tng_trajectory_t *traj, /* Set the box shape */ box_shape[1] = box_shape[2] = box_shape[3] = box_shape[5] = box_shape[6] = box_shape[7] = 0; - box_shape[0] = 150.0; - box_shape[4] = 145.5; - box_shape[8] = 155.5; + box_shape[0] = BOX_SHAPE_X; + box_shape[4] = BOX_SHAPE_Y; + box_shape[8] = BOX_SHAPE_Z; if(tng_data_block_add(*traj, TNG_TRAJ_BOX_SHAPE, "BOX SHAPE", TNG_DOUBLE_DATA, TNG_NON_TRAJECTORY_BLOCK, 1, 9, 1, TNG_UNCOMPRESSED, box_shape) == TNG_CRITICAL) @@ -198,7 +518,8 @@ static tng_function_status tng_test_write_and_read_traj(tng_trajectory_t *traj, if(stat == TNG_CRITICAL) { free(charges); - printf("Failed setting partial charges.\n"); + printf("Failed setting partial charges. %s: %d\n", + __FILE__, __LINE__); return(TNG_CRITICAL); } @@ -209,7 +530,8 @@ static tng_function_status tng_test_write_and_read_traj(tng_trajectory_t *traj, free(charges); if(stat != TNG_SUCCESS) { - printf("Failed adding partial charges\n"); + printf("Failed adding partial charges. %s: %d\n", + __FILE__, __LINE__); return(TNG_CRITICAL); } @@ -221,14 +543,16 @@ static tng_function_status tng_test_write_and_read_traj(tng_trajectory_t *traj, TNG_NON_TRAJECTORY_BLOCK, 1, 1, 1, TNG_UNCOMPRESSED, annotation) != TNG_SUCCESS) { - printf("Failed adding details annotation data block.\n"); + printf("Failed adding details annotation data block. %s: %d\n", + __FILE__, __LINE__); return(TNG_CRITICAL); } /* Write file headers (includes non trajectory data blocks */ if(tng_file_headers_write(*traj, hash_mode) == TNG_CRITICAL) { - printf("Cannot write file headers.\n"); + printf("Cannot write file headers. %s: %d\n", + __FILE__, __LINE__); } @@ -256,11 +580,11 @@ static tng_function_status tng_test_write_and_read_traj(tng_trajectory_t *traj, molpos[nr+2] = 100.0 * rand() / (RAND_MAX + 1.0); } - /* Generate 200 frame sets - each with 100 frames (by default) */ - for(i = 0; i < 200; i++) + /* Generate frame sets - each with 100 frames (by default) */ + for(i = 0; i < N_FRAME_SETS; i++) { cnt = 0; - if(i < 100) + if(i < N_FRAME_SETS/2) { codec_id = TNG_GZIP_COMPRESSION; } @@ -289,8 +613,8 @@ static tng_function_status tng_test_write_and_read_traj(tng_trajectory_t *traj, data[cnt++] = molpos[nr + 2] - 1; } } - if(tng_frame_set_new(*traj, i * n_frames_per_frame_set, - n_frames_per_frame_set) != TNG_SUCCESS) + if(tng_frame_set_with_time_new(*traj, i * n_frames_per_frame_set, + n_frames_per_frame_set, 2e-15 * (i*n_frames_per_frame_set)) != TNG_SUCCESS) { printf("Error creating frame set %d. %s: %d\n", i, __FILE__, __LINE__); @@ -378,125 +702,213 @@ static tng_function_status tng_test_write_and_read_traj(tng_trajectory_t *traj, } } - /* Write two more frame sets one frame at a time */ + free(molpos); + free(data); - /* Make a new frame set - if always using the same mapping blocks - * it is not necessary to explicitly add a new frame set - it will - * be added automatically when adding data for a frame */ -/* if(tng_frame_set_new(*traj, i * n_frames_per_frame_set, - n_frames_per_frame_set) != TNG_SUCCESS) + tng_trajectory_destroy(traj); + tng_trajectory_init(traj); + tng_input_file_set(*traj, TNG_EXAMPLE_FILES_DIR "tng_test.tng"); + + stat = tng_file_headers_read(*traj, hash_mode); + + tng_first_user_name_get(*traj, temp_str, TNG_MAX_STR_LEN); + if(strcmp(USER_NAME, temp_str) != 0) { - printf("Error creating frame set %d. %s: %d\n", - i, __FILE__, __LINE__); - free(molpos); - free(data); - return(TNG_CRITICAL); + printf("User name does not match when reading written file. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); } - frame_nr = i * n_frames_per_frame_set; + tng_first_program_name_get(*traj, temp_str, TNG_MAX_STR_LEN); + if(strcmp(PROGRAM_NAME, temp_str) != 0) + { + printf("Program name does not match when reading written file. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } - for(k=0; k<300; k++) + tng_first_computer_name_get(*traj, temp_str, TNG_MAX_STR_LEN); + if(strcmp(COMPUTER_NAME, temp_str) != 0) { - mapping[k]=k; + printf("Computer name does not match when reading written file. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); } - *//* Just use two particle mapping blocks in this frame set *//* - if(tng_particle_mapping_add(*traj, 0, 300, mapping) != TNG_SUCCESS) + + tng_forcefield_name_get(*traj, temp_str, TNG_MAX_STR_LEN); + if(strcmp(FORCEFIELD_NAME, temp_str) != 0) { - printf("Error creating particle mapping. %s: %d\n", - __FILE__, __LINE__); - free(molpos); - free(data); - return(TNG_CRITICAL); + printf("Forcefield name does not match when reading written file. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); } - for(k=0; k<300; k++) + + tng_medium_stride_length_get(*traj, &temp_int); + if(temp_int != MEDIUM_STRIDE_LEN) { - mapping[k]=599-k; + printf("Stride length does not match when reading written file. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); } - if(tng_particle_mapping_add(*traj, 300, 300, mapping) != TNG_SUCCESS) + + tng_long_stride_length_get(*traj, &temp_int); + if(temp_int != LONG_STRIDE_LEN) { - printf("Error creating particle mapping. %s: %d\n", - __FILE__, __LINE__); - free(molpos); - free(data); - return(TNG_CRITICAL); + printf("Stride length does not match when reading written file. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); } - *//* Add the data block to the current frame set *//* - if(tng_particle_data_block_add(*traj, TNG_TRAJ_POSITIONS, - "POSITIONS", - TNG_FLOAT_DATA, - TNG_TRAJECTORY_BLOCK, - n_frames_per_frame_set, 3, - 1, 0, n_particles, - TNG_UNCOMPRESSED, - 0) != TNG_SUCCESS) + tng_compression_precision_get(*traj, &temp_double); + if(temp_double != COMPRESSION_PRECISION) { - printf("Error adding data. %s: %d\n", __FILE__, __LINE__); - free(molpos); - free(data); - return(TNG_CRITICAL); + printf("Compression precision does not match when reading written file. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); } - *//* Write the frame set to disk *//* - if(tng_frame_set_write(*traj, hash_mode) != TNG_SUCCESS) + tng_distance_unit_exponential_get(*traj, &temp_int); + if(temp_int != dist_exp) { - printf("Error writing frame set. %s: %d\n", __FILE__, __LINE__); - free(molpos); - free(data); - return(TNG_CRITICAL); + printf("Distance unit exponential does not match when reading written file. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); } - *//* Write particle data to disk - one frame at a time *//* - for(i = 0; i < n_frames_per_frame_set * 2; i++) + stat = tng_test_molecules(*traj); + if(stat != TNG_SUCCESS) + { + return(stat); + } + + i = 0; + while(stat == TNG_SUCCESS) { - for(j = 0; j < 2; j++) + stat = tng_frame_set_read_next(*traj, hash_mode); + tng_current_frame_set_get(*traj, &frame_set); + tng_frame_set_prev_frame_set_file_pos_get(*traj, frame_set, &temp_int); + tng_frame_set_next_frame_set_file_pos_get(*traj, frame_set, &temp_int2); + if(i > 0) { - cnt = 0; - for(k = 0; k < tot_n_mols/2; k++) + if(temp_int == -1) { - nr = k * 3; - *//* Move -1 to 1 *//* - molpos[nr] += 2 * (rand() / (RAND_MAX + 1.0)) - 1; - molpos[nr+1] += 2 * (rand() / (RAND_MAX + 1.0)) - 1; - molpos[nr+2] += 2 * (rand() / (RAND_MAX + 1.0)) - 1; - - data[cnt++] = molpos[nr]; - data[cnt++] = molpos[nr + 1]; - data[cnt++] = molpos[nr + 2]; - data[cnt++] = molpos[nr] + 1; - data[cnt++] = molpos[nr + 1] + 1; - data[cnt++] = molpos[nr + 2] + 1; - data[cnt++] = molpos[nr] - 1; - data[cnt++] = molpos[nr + 1] - 1; - data[cnt++] = molpos[nr + 2] - 1; + printf("File position of previous frame set not correct. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); } - if(tng_frame_particle_data_write(*traj, frame_nr + i, - TNG_TRAJ_POSITIONS, j * 300, 300, - data, hash_mode) != TNG_SUCCESS) + } + else if(temp_int != -1) + { + printf("File position of previous frame set not correct. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } + if(i < N_FRAME_SETS -1) + { + if(temp_int2 == -1) { - printf("Error adding data. %s: %d\n", __FILE__, __LINE__); - free(molpos); - free(data); - return(TNG_CRITICAL); + printf("File position of next frame set not correct. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); } } + else if(temp_int2 != -1) + { + printf("File position of previous next set not correct. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } + i++; + } + if(stat == TNG_CRITICAL) + { + return(stat); } -*/ - free(molpos); - free(data); - tng_trajectory_destroy(traj); - tng_trajectory_init(traj); - tng_input_file_set(*traj, TNG_EXAMPLE_FILES_DIR "tng_test.tng"); + tng_time_per_frame_get(*traj, &temp_double); + if(fabs(TIME_PER_FRAME - temp_double) > 0.000001) + { + printf("Time per frame does not match when reading written file. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } - stat = tng_file_headers_read(*traj, hash_mode); + stat = tng_frame_set_nr_find(*traj, (int64_t)(0.30*N_FRAME_SETS)); + if(stat != TNG_SUCCESS) + { + printf("Could not find frame set %"PRId64". %s: %d\n", (int64_t)0.30*N_FRAME_SETS, + __FILE__, __LINE__); + return(stat); + } - while(stat == TNG_SUCCESS) + stat = tng_frame_set_nr_find(*traj, (int64_t)(0.75*N_FRAME_SETS)); + if(stat != TNG_SUCCESS) { - stat = tng_frame_set_read_next(*traj, hash_mode); + printf("Could not find frame set %"PRId64". %s: %d\n", (int64_t)0.75*N_FRAME_SETS, + __FILE__, __LINE__); + return(stat); } - return(stat); + tng_current_frame_set_get(*traj, &frame_set); + tng_frame_set_frame_range_get(*traj, frame_set, &temp_int, &temp_int2); + if(temp_int != 75 * n_frames_per_frame_set) + { + printf("Unexpected first frame in frame set. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } + + stat = tng_frame_set_read_current_only_data_from_block_id(*traj, hash_mode, TNG_TRAJ_POSITIONS); + if(stat != TNG_SUCCESS) + { + printf("Cannot read positions in current frame set. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } + stat = tng_frame_set_read_next_only_data_from_block_id(*traj, hash_mode, TNG_TRAJ_POSITIONS); + if(stat != TNG_SUCCESS) + { + printf("Cannot read positions in next frame set. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } + stat = tng_data_block_name_get(*traj, TNG_TRAJ_POSITIONS, temp_str, TNG_MAX_STR_LEN); + if(stat != TNG_SUCCESS || strcmp("POSITIONS", temp_str) != 0) + { + printf("Cannot get name of data block or unexpected name. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } + stat = tng_data_block_name_get(*traj, TNG_TRAJ_FORCES, temp_str, TNG_MAX_STR_LEN); + if(stat != TNG_FAILURE) + { + printf("Trying to retrieve name of non-existent data block did not return failure. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } + stat = tng_data_block_dependency_get(*traj, TNG_TRAJ_POSITIONS, &dependency); + if(stat != TNG_SUCCESS || dependency != TNG_FRAME_DEPENDENT + TNG_PARTICLE_DEPENDENT) + { + printf("Cannot get dependency of data block or unexpected dependency. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } + stat = tng_data_block_num_values_per_frame_get(*traj, TNG_TRAJ_POSITIONS, &temp_int); + if(stat != TNG_SUCCESS || temp_int != 3) + { + printf("Cannot get number of values per frame of data block or unexpected value. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } + stat = tng_data_get_stride_length(*traj, TNG_TRAJ_POSITIONS, 100, &temp_int); + if(stat != TNG_SUCCESS || temp_int != 1) + { + printf("Cannot get stride length of data block or unexpected value. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } + + return(TNG_SUCCESS); } /* This test relies on knowing that the box shape is stored as double */ @@ -525,6 +937,13 @@ tng_function_status tng_test_get_box_data(tng_trajectory_t traj) // printf("\n"); // } */ + /* The X dimension in the example file is 50 */ + if(fabs(values[0][0].d - 50) > 0.000001) + { + printf("Unexpected value in box shape. %s: %d\n", __FILE__, __LINE__); + return(TNG_FAILURE); + } + tng_data_values_free(traj, values, n_frames, n_values_per_frame, type); return(TNG_SUCCESS); @@ -548,24 +967,6 @@ tng_function_status tng_test_get_positions_data(tng_trajectory_t traj, return(TNG_CRITICAL); } -/* -// int64_t i, j, k; -// struct tng_trajectory_frame_set *frame_set = -// &traj->current_trajectory_frame_set; -// for(i = 0; i<n_frames; i++) -// { -// printf("Frame %"PRId64"\n", frame_set->first_frame + i); -// for(j = 0; j<n_particles; j++) -// { -// printf("Particle %"PRId64":", j); -// for(k=0; k<n_values_per_frame; k++) -// { -// printf("\t%f", (values[i][j][k]).f); -// } -// printf("\n"); -// } -// } -*/ tng_particle_data_values_free(traj, values, n_frames, n_particles, n_values_per_frame, type); @@ -583,20 +984,157 @@ tng_function_status tng_test_get_positions_data(tng_trajectory_t traj, return(TNG_SUCCESS); } +tng_function_status tng_test_utility_functions(tng_trajectory_t traj, const char hash_mode) +{ + tng_function_status stat; + int64_t n_particles, i, j, k, codec_id; + int64_t n_frames_to_read=10, stride_len, next_frame, n_blocks, *block_ids = 0; + double time, multiplier; + float *positions = 0; + + stat = tng_util_trajectory_open(TNG_EXAMPLE_FILES_DIR "tng_test.tng", 'r', &traj); + if(stat != TNG_SUCCESS) + { + return(stat); + } + + stat = tng_util_time_of_frame_get(traj, 50, &time); + if(stat != TNG_SUCCESS || fabs(time - 100e-13) > 0.000001) + { + return(stat); + } + stat = tng_util_time_of_frame_get(traj, 100, &time); + if(stat != TNG_SUCCESS || fabs(time - 200e-13) > 0.000001) + { + return(stat); + } + + tng_num_particles_get(traj, &n_particles); + + stat = tng_util_pos_read_range(traj, 1, n_frames_to_read, &positions, &stride_len); + if(stat != TNG_SUCCESS) + { + if(positions) + { + free(positions); + } + return(stat); + } + + for(i = 0; i < n_frames_to_read / stride_len; i++) + { + for(j = 0; j < n_particles; j++) + { + for(k = 0; k < 3; k++) + { + if(positions[i*n_particles + j*3 + k] < -500 || positions[i*n_particles + j*3 + k] > 500) + { + printf("Coordinates not in range. %s: %d\n", + __FILE__, __LINE__); + free(positions); + return(TNG_FAILURE); + } + } + } + } + + free(positions); + + stat=tng_util_trajectory_next_frame_present_data_blocks_find(traj, n_frames_to_read, + 0, 0, &next_frame, + &n_blocks, &block_ids); + if(block_ids) + { + free(block_ids); + } + if(stat != TNG_SUCCESS || n_blocks != 1 || next_frame != n_frames_to_read + stride_len) + { + printf("Unexpected data blocks in next frame. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } + + stat = tng_util_frame_current_compression_get(traj, TNG_TRAJ_POSITIONS, &codec_id, &multiplier); + if(stat != TNG_SUCCESS || codec_id != TNG_GZIP_COMPRESSION) + { + printf("Could not get compression. %s: %d\n", + __FILE__, __LINE__); + return(TNG_FAILURE); + } + + stat = tng_util_trajectory_close(&traj); + if(stat != TNG_SUCCESS) + { + return(stat); + } + + return(TNG_SUCCESS); +} + tng_function_status tng_test_append(tng_trajectory_t traj, const char hash_mode) { + char str[TNG_MAX_STR_LEN]; tng_function_status stat; stat = tng_util_trajectory_open(TNG_EXAMPLE_FILES_DIR "tng_test.tng", 'a', &traj); if(stat != TNG_SUCCESS) { + printf("Cannot open trajectory. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + + stat = tng_last_user_name_set(traj, USER_NAME); + if(stat != TNG_SUCCESS) + { + printf("Cannot set last user name. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + stat = tng_last_user_name_get(traj, str, TNG_MAX_STR_LEN); + if(stat != TNG_SUCCESS) + { + printf("Cannot get last user name. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + stat = tng_last_program_name_set(traj, PROGRAM_NAME); + if(stat != TNG_SUCCESS) + { + printf("Cannot set last program name. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + stat = tng_last_program_name_get(traj, str, TNG_MAX_STR_LEN); + if(stat != TNG_SUCCESS) + { + printf("Cannot get last program name. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + stat = tng_last_computer_name_set(traj, "Still " COMPUTER_NAME); + if(stat != TNG_SUCCESS) + { + printf("Cannot set last computer name. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } + stat = tng_last_computer_name_get(traj, str, TNG_MAX_STR_LEN); + if(stat != TNG_SUCCESS) + { + printf("Cannot get last computer name. %s: %d\n", + __FILE__, __LINE__); return(stat); } - tng_last_user_name_set(traj, "User2"); - tng_last_program_name_set(traj, "tng_testing"); - tng_file_headers_write(traj, hash_mode); + stat = tng_file_headers_write(traj, hash_mode); + if(stat != TNG_SUCCESS) + { + printf("Cannot write file headers. %s: %d\n", + __FILE__, __LINE__); + return(stat); + } stat = tng_util_trajectory_close(&traj); @@ -606,7 +1144,6 @@ tng_function_status tng_test_append(tng_trajectory_t traj, const char hash_mode) int main() { tng_trajectory_t traj; - tng_function_status stat; char time_str[TNG_MAX_DATE_STR_LEN]; char version_str[TNG_MAX_STR_LEN]; char hash_mode = TNG_USE_HASH; @@ -638,7 +1175,7 @@ int main() tng_output_file_set(traj, TNG_EXAMPLE_FILES_DIR "tng_example_out.tng"); - if(tng_test_read_and_write_file(traj, hash_mode) == TNG_CRITICAL) + if(tng_test_read_and_write_file(traj, hash_mode) != TNG_SUCCESS) { printf("Test Read and write file:\t\t\tFailed. %s: %d\n", __FILE__, __LINE__); @@ -658,8 +1195,8 @@ int main() printf("Test Get data:\t\t\t\t\tSucceeded.\n"); } - if(tng_trajectory_destroy(&traj) == TNG_CRITICAL || - tng_trajectory_init(&traj) == TNG_CRITICAL) + if(tng_trajectory_destroy(&traj) != TNG_SUCCESS || + tng_trajectory_init(&traj) != TNG_SUCCESS) { printf("Test Destroy and init trajectory:\t\tFailed. %s: %d\n", __FILE__, __LINE__); @@ -672,7 +1209,7 @@ int main() tng_output_file_set(traj, TNG_EXAMPLE_FILES_DIR "tng_test.tng"); - if(tng_test_write_and_read_traj(&traj, hash_mode) == TNG_CRITICAL) + if(tng_test_write_and_read_traj(&traj, hash_mode) != TNG_SUCCESS) { printf("Test Write and read file:\t\t\tFailed. %s: %d\n", __FILE__, __LINE__); @@ -692,7 +1229,7 @@ int main() printf("Test Get particle data:\t\t\t\tSucceeded.\n"); } - if(tng_trajectory_destroy(&traj) == TNG_CRITICAL) + if(tng_trajectory_destroy(&traj) != TNG_SUCCESS) { printf("Test Destroy trajectory:\t\t\tFailed. %s: %d.\n", __FILE__, __LINE__); @@ -703,30 +1240,15 @@ int main() printf("Test Destroy trajectory:\t\t\tSucceeded.\n"); } - - stat = tng_util_trajectory_open(TNG_EXAMPLE_FILES_DIR "tng_test.tng", 'r', &traj); - - if(stat != TNG_SUCCESS) - { - printf("Test Utility function open:\t\t\tFailed. %s: %d.\n", - __FILE__, __LINE__); - exit(1); - } - else - { - printf("Test Utility function open:\t\t\tSucceeded.\n"); - } - - stat = tng_util_trajectory_close(&traj); - if(stat != TNG_SUCCESS) + if(tng_test_utility_functions(traj, hash_mode) != TNG_SUCCESS) { - printf("Test Utility function close:\t\t\tFailed. %s: %d.\n", + printf("Test Utility functions:\t\t\t\tFailed. %s: %d.\n", __FILE__, __LINE__); exit(1); } else { - printf("Test Utility function close:\t\t\tSucceeded.\n"); + printf("Test Utility functions:\t\t\t\tSucceeded.\n"); } if(tng_test_append(traj, hash_mode) != TNG_SUCCESS) |