commit 3f40c3c6158fddb969bde36b08359fecc184078b
parent 0f0ba10404e57d315238e551f00ee6ec54b49e63
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Wed, 9 Feb 2022 14:02:16 +0100
Test empty metadata or molecules wo isotope
Diffstat:
1 file changed, 91 insertions(+), 30 deletions(-)
diff --git a/src/test_shitran_isotope_metadata.c b/src/test_shitran_isotope_metadata.c
@@ -20,6 +20,7 @@
#include <rsys/mem_allocator.h>
#include <rsys/math.h>
+
#include <string.h>
static const struct shitran_isotope H2O_isotopes[] = {
@@ -57,6 +58,18 @@ static const struct shitran_molecule CO2 = {
};
static void
+isotope_print(FILE* fp, const struct shitran_isotope* isotope)
+{
+ CHK(fp && isotope);
+ fprintf(fp, " %d %.5E %.4E %d %.6f\n",
+ isotope->id,
+ isotope->abundance,
+ isotope->Q296K,
+ isotope->gj,
+ isotope->molar_mass);
+}
+
+static void
molecule_print(FILE* fp, const struct shitran_molecule* molecule)
{
size_t i;
@@ -64,12 +77,7 @@ molecule_print(FILE* fp, const struct shitran_molecule* molecule)
fprintf(fp, " %s (%d)\n", molecule->name, molecule->id);
FOR_EACH(i, 0, molecule->nisotopes) {
- fprintf(fp, " %d %.5E %.4E %d %.6f\n",
- molecule->isotopes[i].id,
- molecule->isotopes[i].abundance,
- molecule->isotopes[i].Q296K,
- molecule->isotopes[i].gj,
- molecule->isotopes[i].molar_mass);
+ isotope_print(fp, molecule->isotopes+i);
}
}
@@ -105,21 +113,21 @@ molecule_eq(const struct shitran_molecule* m0, const struct shitran_molecule* m1
static void
check_molecule_isotopes
- (struct shitran_isotope_metadata* metadata,
+ (struct shitran_isotope_metadata* mdata,
const struct shitran_molecule* molecule)
{
struct shitran_isotope isotope = SHITRAN_ISOTOPE_NULL;
struct shitran_molecule molecule2 = SHITRAN_MOLECULE_NULL;
size_t i;
- CHK(metadata && molecule);
+ CHK(mdata && molecule);
FOR_EACH(i, 0, H2O.nisotopes) {
CHK(shitran_isotope_metadata_find_isotope
- (metadata, molecule->isotopes[i].id, &isotope) == RES_OK);
+ (mdata, molecule->isotopes[i].id, &isotope) == RES_OK);
CHK(!SHITRAN_ISOTOPE_IS_NULL(&isotope));
CHK(isotope_eq(&isotope, &molecule->isotopes[i]));
CHK(shitran_isotope_metadata_get_molecule
- (metadata, isotope.imolecule, &molecule2) == RES_OK);
+ (mdata, isotope.imolecule, &molecule2) == RES_OK);
CHK(molecule_eq(molecule, &molecule2));
}
}
@@ -130,7 +138,7 @@ test_load(struct shitran* shitran)
struct shitran_isotope isotope = SHITRAN_ISOTOPE_NULL;
struct shitran_molecule molecule = SHITRAN_MOLECULE_NULL;
const char* filename = "test_isotope_metadata.txt";
- struct shitran_isotope_metadata* metadata = NULL;
+ struct shitran_isotope_metadata* mdata = NULL;
FILE* fp = NULL;
size_t nmolecules = 0;
@@ -141,52 +149,105 @@ test_load(struct shitran* shitran)
molecule_print(fp, &CO2);
rewind(fp);
- CHK(shitran_isotope_metadata_load_stream(NULL, fp, NULL, &metadata) == RES_BAD_ARG);
- CHK(shitran_isotope_metadata_load_stream(shitran, NULL, NULL, &metadata) == RES_BAD_ARG);
+ CHK(shitran_isotope_metadata_load_stream(NULL, fp, NULL, &mdata) == RES_BAD_ARG);
+ CHK(shitran_isotope_metadata_load_stream(shitran, NULL, NULL, &mdata) == RES_BAD_ARG);
CHK(shitran_isotope_metadata_load_stream(shitran, fp, NULL, NULL) == RES_BAD_ARG);
- CHK(shitran_isotope_metadata_load_stream(shitran, fp, NULL, &metadata) == RES_OK);
+ CHK(shitran_isotope_metadata_load_stream(shitran, fp, NULL, &mdata) == RES_OK);
CHK(shitran_isotope_metadata_get_molecules_count(NULL, &nmolecules) == RES_BAD_ARG);
- CHK(shitran_isotope_metadata_get_molecules_count(metadata, NULL) == RES_BAD_ARG);
- CHK(shitran_isotope_metadata_get_molecules_count(metadata, &nmolecules) == RES_OK);
+ CHK(shitran_isotope_metadata_get_molecules_count(mdata, NULL) == RES_BAD_ARG);
+ CHK(shitran_isotope_metadata_get_molecules_count(mdata, &nmolecules) == RES_OK);
CHK(nmolecules == 2);
CHK(shitran_isotope_metadata_get_molecule(NULL, 0, &molecule) == RES_BAD_ARG);
- CHK(shitran_isotope_metadata_get_molecule(metadata, 2, &molecule) == RES_BAD_ARG);
+ CHK(shitran_isotope_metadata_get_molecule(mdata, 2, &molecule) == RES_BAD_ARG);
- CHK(shitran_isotope_metadata_get_molecule(metadata, 0, &molecule) == RES_OK);
+ CHK(shitran_isotope_metadata_get_molecule(mdata, 0, &molecule) == RES_OK);
CHK(molecule_eq(&molecule, &H2O));
- CHK(shitran_isotope_metadata_get_molecule(metadata, 1, &molecule) == RES_OK);
+ CHK(shitran_isotope_metadata_get_molecule(mdata, 1, &molecule) == RES_OK);
CHK(molecule_eq(&molecule, &CO2));
CHK(shitran_isotope_metadata_find_molecule(NULL, 1, &molecule) == RES_BAD_ARG);
- CHK(shitran_isotope_metadata_find_molecule(metadata, 1, NULL) == RES_BAD_ARG);
- CHK(shitran_isotope_metadata_find_molecule(metadata, 1, &molecule) == RES_OK);
+ CHK(shitran_isotope_metadata_find_molecule(mdata, 1, NULL) == RES_BAD_ARG);
+ CHK(shitran_isotope_metadata_find_molecule(mdata, 1, &molecule) == RES_OK);
CHK(!SHITRAN_MOLECULE_IS_NULL(&molecule));
CHK(molecule_eq(&molecule, &H2O));
- CHK(shitran_isotope_metadata_find_molecule(metadata, 2, &molecule) == RES_OK);
+ CHK(shitran_isotope_metadata_find_molecule(mdata, 2, &molecule) == RES_OK);
CHK(!SHITRAN_MOLECULE_IS_NULL(&molecule));
CHK(molecule_eq(&molecule, &CO2));
- CHK(shitran_isotope_metadata_find_molecule(metadata, 0, &molecule) == RES_OK);
+ CHK(shitran_isotope_metadata_find_molecule(mdata, 0, &molecule) == RES_OK);
CHK(SHITRAN_MOLECULE_IS_NULL(&molecule));
CHK(shitran_isotope_metadata_find_isotope(NULL, 161, &isotope) == RES_BAD_ARG);
- CHK(shitran_isotope_metadata_find_isotope(metadata, 161, NULL) == RES_BAD_ARG);
- CHK(shitran_isotope_metadata_find_isotope(metadata, 0, &isotope) == RES_OK);
+ CHK(shitran_isotope_metadata_find_isotope(mdata, 161, NULL) == RES_BAD_ARG);
+ CHK(shitran_isotope_metadata_find_isotope(mdata, 0, &isotope) == RES_OK);
CHK(SHITRAN_ISOTOPE_IS_NULL(&isotope));
- check_molecule_isotopes(metadata, &H2O);
- check_molecule_isotopes(metadata, &CO2);
+ check_molecule_isotopes(mdata, &H2O);
+ check_molecule_isotopes(mdata, &CO2);
CHK(shitran_isotope_metadata_ref_get(NULL) == RES_BAD_ARG);
- CHK(shitran_isotope_metadata_ref_get(metadata) == RES_OK);
+ CHK(shitran_isotope_metadata_ref_get(mdata) == RES_OK);
CHK(shitran_isotope_metadata_ref_put(NULL) == RES_BAD_ARG);
- CHK(shitran_isotope_metadata_ref_put(metadata) == RES_OK);
- CHK(shitran_isotope_metadata_ref_put(metadata) == RES_OK);
+ CHK(shitran_isotope_metadata_ref_put(mdata) == RES_OK);
+ CHK(shitran_isotope_metadata_ref_put(mdata) == RES_OK);
+
+ CHK(fclose(fp) == 0);
+
+ CHK(shitran_isotope_metadata_load(NULL, filename, &mdata) == RES_BAD_ARG);
+ CHK(shitran_isotope_metadata_load(shitran, NULL, &mdata) == RES_BAD_ARG);
+ CHK(shitran_isotope_metadata_load(shitran, filename, NULL) == RES_BAD_ARG);
+ CHK(shitran_isotope_metadata_load(shitran, "no_file", &mdata) == RES_IO_ERR);
+
+ CHK(shitran_isotope_metadata_load(shitran, filename, &mdata) == RES_OK);
+ CHK(shitran_isotope_metadata_get_molecules_count(mdata, &nmolecules) == RES_OK);
+ CHK(nmolecules == 2);
+ CHK(shitran_isotope_metadata_get_molecule(mdata, 0, &molecule) == RES_OK);
+ CHK(molecule_eq(&molecule, &H2O));
+ CHK(shitran_isotope_metadata_get_molecule(mdata, 1, &molecule) == RES_OK);
+ CHK(molecule_eq(&molecule, &CO2));
+ CHK(shitran_isotope_metadata_ref_put(mdata) == RES_OK);
+
+ /* Empty file */
+ CHK(fp = tmpfile());
+ CHK(shitran_isotope_metadata_load_stream(shitran, fp, NULL, &mdata) == RES_OK);
+ CHK(shitran_isotope_metadata_get_molecules_count(mdata, &nmolecules) == RES_OK);
+ CHK(nmolecules == 0);
+ CHK(shitran_isotope_metadata_ref_put(mdata) == RES_OK);
+ fprintf(fp, "Molecule # Iso Abundance Q(296K) gj Molar Mass(g)\n");
+ rewind(fp);
+ CHK(shitran_isotope_metadata_load_stream(shitran, fp, NULL, &mdata) == RES_OK);
+ CHK(shitran_isotope_metadata_get_molecules_count(mdata, &nmolecules) == RES_OK);
+ CHK(nmolecules == 0);
+ CHK(shitran_isotope_metadata_ref_put(mdata) == RES_OK);
+ CHK(fclose(fp) == 0);
+
+ /* Molecule without isotope */
+ CHK(fp = tmpfile());
+ fprintf(fp, "Molecule # Iso Abundance Q(296K) gj Molar Mass(g)\n");
+ fprintf(fp, "%s (%d)\n", H2O.name, H2O.id);
+ fprintf(fp, "%s (%d)\n", CO2.name, CO2.id);
+ rewind(fp);
+ CHK(shitran_isotope_metadata_load_stream(shitran, fp, NULL, &mdata) == RES_OK);
+ CHK(shitran_isotope_metadata_get_molecules_count(mdata, &nmolecules) == RES_OK);
+ CHK(nmolecules == 2);
+
+ CHK(shitran_isotope_metadata_get_molecule(mdata, 0, &molecule) == RES_OK);
+ CHK(!strcmp(molecule.name, H2O.name));
+ CHK(molecule.id == H2O.id);
+ CHK(molecule.nisotopes == 0);
+ CHK(molecule.isotopes == NULL);
+
+ CHK(shitran_isotope_metadata_get_molecule(mdata, 1, &molecule) == RES_OK);
+ CHK(!strcmp(molecule.name, CO2.name));
+ CHK(molecule.id == CO2.id);
+ CHK(molecule.nisotopes == 0);
+ CHK(molecule.isotopes == NULL);
+ CHK(shitran_isotope_metadata_ref_put(mdata) == RES_OK);
CHK(fclose(fp) == 0);
}