commit 7d57dc6c29978eb6458b775d7ee23fd707e8f94c
parent 2ea5b1880382e72eb995ad13ef53295caf65ed93
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Mon, 23 May 2022 10:52:13 +0200
Test the isotope metadata [de]serialization
Diffstat:
1 file changed, 77 insertions(+), 0 deletions(-)
diff --git a/src/test_shtr_isotope_metadata.c b/src/test_shtr_isotope_metadata.c
@@ -269,6 +269,82 @@ test_load(struct shtr* shtr)
}
static void
+check_equality
+ (const struct shtr_isotope_metadata* mdata1,
+ const struct shtr_isotope_metadata* mdata2)
+{
+ size_t n1, n2;
+ size_t imol, nmol;
+ CHK(mdata1 && mdata2);
+
+ CHK(shtr_isotope_metadata_get_molecules_count(mdata1, &n1) == RES_OK);
+ CHK(shtr_isotope_metadata_get_molecules_count(mdata2, &n2) == RES_OK);
+ CHK(n1 == n2);
+ nmol = n1;
+
+ CHK(shtr_isotope_metadata_get_isotopes_count(mdata1, &n1) == RES_OK);
+ CHK(shtr_isotope_metadata_get_isotopes_count(mdata2, &n2) == RES_OK);
+ CHK(n1 == n2);
+
+ FOR_EACH(imol, 0, nmol) {
+ struct shtr_molecule mol1 = SHTR_MOLECULE_NULL;
+ struct shtr_molecule mol2 = SHTR_MOLECULE_NULL;
+ size_t iiso, niso;
+
+ CHK(shtr_isotope_metadata_get_molecule(mdata1, imol, &mol1) == RES_OK);
+ CHK(shtr_isotope_metadata_get_molecule(mdata2, imol, &mol2) == RES_OK);
+ CHK(!strcmp(mol1.name, mol2.name));
+ CHK(mol1.id == mol2.id);
+ CHK(mol1.nisotopes == mol2.nisotopes);
+ niso = mol1.nisotopes;
+
+ FOR_EACH(iiso, 0, niso) {
+ CHK(mol1.isotopes[iiso].abundance == mol2.isotopes[iiso].abundance);
+ CHK(mol1.isotopes[iiso].Q296K == mol2.isotopes[iiso].Q296K);
+ CHK(mol1.isotopes[iiso].molar_mass == mol2.isotopes[iiso].molar_mass);
+ CHK(mol1.isotopes[iiso].molecule_id_local == mol2.isotopes[iiso].molecule_id_local);
+ CHK(mol1.isotopes[iiso].gj == mol2.isotopes[iiso].gj);
+ CHK(mol1.isotopes[iiso].id == mol2.isotopes[iiso].id);
+ }
+ }
+}
+
+static void
+test_serialization(struct shtr* shtr)
+{
+ struct shtr_isotope_metadata* mdata1 = NULL;
+ struct shtr_isotope_metadata* mdata2 = NULL;
+ FILE* fp = NULL;
+
+ CHK(fp = tmpfile());
+
+ fprintf(fp, "Molecule # Iso Abundance Q(296K) gj Molar Mass(g)\n");
+ molecule_print(fp, &H2O);
+ molecule_print(fp, &CO2);
+ rewind(fp);
+
+ CHK(shtr_isotope_metadata_load_stream(shtr, fp, NULL, &mdata1) == RES_OK);
+ fclose(fp);
+
+ CHK(fp = tmpfile());
+ CHK(shtr_isotope_metadata_write(NULL, fp) == RES_BAD_ARG);
+ CHK(shtr_isotope_metadata_write(mdata1, NULL) == RES_BAD_ARG);
+ CHK(shtr_isotope_metadata_write(mdata1, fp) == RES_OK);
+ rewind(fp);
+
+ CHK(shtr_isotope_metadata_create_from_stream(NULL, fp, &mdata2) == RES_BAD_ARG);
+ CHK(shtr_isotope_metadata_create_from_stream(shtr, NULL, &mdata2) == RES_BAD_ARG);
+ CHK(shtr_isotope_metadata_create_from_stream(shtr, fp, NULL) == RES_BAD_ARG);
+ CHK(shtr_isotope_metadata_create_from_stream(shtr, fp, &mdata2) == RES_OK);
+ fclose(fp);
+
+ check_equality(mdata1, mdata2);
+
+ CHK(shtr_isotope_metadata_ref_put(mdata1) == RES_OK);
+ CHK(shtr_isotope_metadata_ref_put(mdata2) == RES_OK);
+}
+
+static void
test_load_failures(struct shtr* shtr)
{
struct shtr_isotope isotope = SHTR_ISOTOPE_NULL;
@@ -384,6 +460,7 @@ main(int argc, char** argv)
test_load(shtr);
test_load_failures(shtr);
+ test_serialization(shtr);
FOR_EACH(i, 1, argc) {
test_load_file(shtr, argv[i]);
}