commit ef8fe3f37c7ca8d209fd7c3be13b92e321f2eb72
parent add72438e0eca575751d1cca1f027c9171ce36c8
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Thu, 5 Feb 2026 15:39:01 +0100
Test how loading reacts with invalid isotope definitions
Diffstat:
1 file changed, 130 insertions(+), 0 deletions(-)
diff --git a/src/test_sln_mixture.c b/src/test_sln_mixture.c
@@ -257,6 +257,135 @@ test_invalid_molecule
CHK(fclose(fp) == 0);
}
+static void
+test_invalid_isotope
+ (struct sln_device* sln,
+ struct shtr_isotope_metadata* molparam)
+{
+ struct sln_mixture_load_args args = SLN_MIXTURE_LOAD_ARGS_NULL;
+ struct sln_mixture* mixture = NULL;
+
+ char buf[1024] = {0};
+ FILE* fp = NULL;
+ size_t i = 0;
+
+ const struct sln_isotope H2O_iso[] = {
+ {1.0/7.0, 161},
+ {1.0/7.0, 181},
+ {1.0/7.0, 171},
+ {1.0/7.0, 162},
+ {1.0/7.0, 182},
+ {1.0/7.0, 172},
+ {1.0/7.0, 262}
+ };
+ const size_t H2O_niso = sizeof(H2O_iso)/sizeof(H2O_iso[0]);
+
+ /* Note that the comment char will be added as the last character written to
+ * the file in order to fill the rest of the file with a comment */
+ CHK(fp = fmemopen(buf, sizeof(buf), "w+"));
+
+ args.filename = "memstream";
+ args.molparam = molparam;
+ args.file = fp;
+
+ #define RESET { memset(buf, 0, sizeof(buf)); rewind(fp); } (void)0
+
+ /* Isotope ID is missing */
+ RESET;
+ CHK(fprintf(fp, "H2O 0.2 25\n") > 0);
+ FOR_EACH(i, 0, H2O_niso) {
+ /* Arbitrarily select an isotope whose identifier is not defined */
+ if(i == H2O_niso/2) {
+ CHK(fprintf(fp, " %E\n", H2O_iso[i].abundance) > 0);
+ } else {
+ CHK(fprintf(fp, "%d %E\n", H2O_iso[i].id, H2O_iso[i].abundance) > 0);
+ }
+ }
+ CHK(fprintf(fp, "#") > 0); /* The rest of the file is a comment */
+ rewind(fp);
+ CHK(sln_mixture_load(sln, &args, &mixture) == RES_BAD_ARG);
+
+ /* Isotope abundance is missing */
+ RESET;
+ CHK(fprintf(fp, "H2O 0.2 25\n") > 0);
+ FOR_EACH(i, 0, H2O_niso) {
+ /* Arbitrarily select an isotope whose identifier is not defined */
+ if(i == H2O_niso/2) {
+ CHK(fprintf(fp, "%d \n", H2O_iso[i].id) > 0);
+ } else {
+ CHK(fprintf(fp, "%d %E\n", H2O_iso[i].id, H2O_iso[i].abundance) > 0);
+ }
+ }
+ CHK(fprintf(fp, "#") > 0); /* The rest of the file is a comment */
+ rewind(fp);
+ CHK(sln_mixture_load(sln, &args, &mixture) == RES_BAD_ARG);
+
+ /* Missing an isotope */
+ RESET;
+ CHK(fprintf(fp, "H2O 0.2 25\n") > 0);
+ FOR_EACH(i, 0, H2O_niso-1) {
+ CHK(fprintf(fp, "%d %E\n", H2O_iso[i].id, H2O_iso[i].abundance) > 0);
+ }
+ CHK(fprintf(fp, "#") > 0);
+ rewind(fp);
+ CHK(sln_mixture_load(sln, &args, &mixture) == RES_BAD_ARG);
+
+ /* Inconsistency in the order of isotopes compared to that of metadata */
+ RESET;
+ CHK(fprintf(fp, "H2O 0.2 25\n") > 0);
+ FOR_EACH_REVERSE(i, H2O_niso, 0) {
+ CHK(fprintf(fp, "%d %E\n", H2O_iso[i-1].id, H2O_iso[i-1].abundance) > 0);
+ }
+ CHK(fprintf(fp, "#") > 0);
+ rewind(fp);
+ CHK(sln_mixture_load(sln, &args, &mixture) == RES_BAD_ARG);
+
+ /* The sum of the abundances is not 1 */
+ RESET;
+ CHK(fprintf(fp, "H2O 0.2 25\n") > 0);
+ FOR_EACH(i, 0, H2O_niso) {
+ double abundance = H2O_iso[i].abundance;
+ if(i == H2O_niso/2) abundance /= 2;
+ CHK(fprintf(fp, "%d %E\n", H2O_iso[i].id, abundance) > 0);
+ }
+ CHK(fprintf(fp, "#") > 0);
+ rewind(fp);
+ CHK(sln_mixture_load(sln, &args, &mixture) == RES_BAD_ARG);
+
+ /* Additional text is questionable, but still acceptable
+ * (a warning message should be displayed) */
+ RESET;
+ CHK(fprintf(fp, "H2O 0.2 25\n") > 0);
+ FOR_EACH(i, 0, H2O_niso) {
+ CHK(fprintf(fp, "%d %E", H2O_iso[i].id, H2O_iso[i].abundance) > 0);
+ if(i == H2O_niso*2 / 3) {
+ CHK(fprintf(fp, " dummy text 42\n") > 0);
+ } else {
+ CHK(fprintf(fp, "\n") > 0);
+ }
+ }
+ CHK(fprintf(fp, "#") > 0);
+ rewind(fp);
+ CHK(sln_mixture_load(sln, &args, &mixture) == RES_OK);
+ CHK(sln_mixture_ref_put(mixture) == RES_OK);
+
+ /* Only a subset of isotopes can be defined as long as the sum of their
+ * abundances equals 1. Also check that tabs and multiple spaces are handled
+ * correctly */
+ RESET;
+ CHK(fprintf(fp, "H2O 0.2 25\n") > 0);
+ CHK(fprintf(fp, "\t%d\t0.5\n", H2O_iso[0].id) > 0);
+ CHK(fprintf(fp, "\t%d 0.5\n", H2O_iso[1].id) > 0);
+ CHK(fprintf(fp, "#") > 0);
+ rewind(fp);
+ CHK(sln_mixture_load(sln, &args, &mixture) == RES_OK);
+ CHK(sln_mixture_ref_put(mixture) == RES_OK);
+
+ #undef RESET
+
+ CHK(fclose(fp) == 0);
+}
+
static struct shtr_isotope_metadata*
load_isotope_metadata(struct shtr* shtr)
{
@@ -301,6 +430,7 @@ main(void)
test_api(sln, molparam);
test_empty_file(sln, molparam);
test_invalid_molecule(sln, molparam);
+ test_invalid_isotope(sln, molparam);
CHK(sln_device_ref_put(sln) == RES_OK);
CHK(shtr_ref_put(shtr) == RES_OK);