commit 743df20e734aa65bbde9ec1e80b7b0421396f45c
parent f4377e10eb55475673778da0d76441a961741a85
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Mon, 9 Mar 2026 21:25:58 +0100
Rename the function that calculates the metadata hash.
Name it "hash" instead of "compute_signature" which is too long and does
not provide any additional information about what is being done.
Update its tests accordingly and improve them slightly to check more
situations.
Diffstat:
3 files changed, 52 insertions(+), 41 deletions(-)
diff --git a/src/shtr.h b/src/shtr.h
@@ -331,9 +331,9 @@ shtr_isotope_metadata_write
FILE* stream);
SHTR_API res_T
-shtr_isotope_metadata_compute_signature
+shtr_isotope_metadata_hash
(const struct shtr_isotope_metadata* metadata,
- hash256_T signature);
+ hash256_T hash);
/*******************************************************************************
* Lines API
diff --git a/src/shtr_isotope_metadata.c b/src/shtr_isotope_metadata.c
@@ -971,21 +971,21 @@ error:
}
res_T
-shtr_isotope_metadata_compute_signature
+shtr_isotope_metadata_hash
(const struct shtr_isotope_metadata* metadata,
- hash256_T signature)
+ hash256_T hash)
{
struct sha256_ctx ctx;
res_T res = RES_OK;
- if(!metadata || !signature) {
+ if(!metadata || !hash) {
res = RES_BAD_ARG;
goto error;
}
sha256_ctx_init(&ctx);
hash_molecule_list(metadata, &ctx);
- sha256_ctx_finalize(&ctx, signature);
+ sha256_ctx_finalize(&ctx, hash);
exit:
return res;
diff --git a/src/test_shtr_isotope_metadata.c b/src/test_shtr_isotope_metadata.c
@@ -81,18 +81,6 @@ molecule_print(FILE* fp, const struct shtr_molecule* molecule)
}
}
-static void
-molecule_print_reverse(FILE* fp, const struct shtr_molecule* molecule)
-{
- size_t i;
- CHK(fp && molecule);
-
- fprintf(fp, " %s (%d)\n", molecule->name, molecule->id);
- FOR_EACH_REVERSE(i, molecule->nisotopes, 0) {
- isotope_print(fp, molecule->isotopes+i-1);
- }
-}
-
static int
isotope_eq(const struct shtr_isotope* i0, const struct shtr_isotope* i1)
{
@@ -322,19 +310,25 @@ check_equality
}
static void
-test_signature(struct shtr* shtr)
+test_hash(struct shtr* shtr)
{
+ struct shtr_molecule CO2_custom = SHTR_MOLECULE_NULL;
+ struct shtr_isotope isotopes_custom[SHTR_MAX_ISOTOPES_COUNT];
+
struct shtr_isotope_metadata* mdata1 = NULL;
struct shtr_isotope_metadata* mdata2 = NULL;
struct shtr_isotope_metadata* mdata3 = NULL;
struct shtr_isotope_metadata* mdata4 = NULL;
struct shtr_isotope_metadata* mdata5 = NULL;
- hash256_T signature1;
- hash256_T signature2;
- hash256_T signature3;
- hash256_T signature4;
- hash256_T signature5;
+ struct shtr_isotope_metadata* mdata6 = NULL;
+ hash256_T hash1;
+ hash256_T hash2;
+ hash256_T hash3;
+ hash256_T hash4;
+ hash256_T hash5;
+ hash256_T hash6;
FILE* fp = NULL;
+ size_t i = 0;
/* Setup the metadata of the H2O and the CO2 molecules */
CHK(fp = tmpfile());
@@ -345,10 +339,10 @@ test_signature(struct shtr* shtr)
CHK(shtr_isotope_metadata_load_stream(shtr, fp, NULL, &mdata1) == RES_OK);
CHK(fclose(fp) == 0);
- /* Check the signature API */
- CHK(shtr_isotope_metadata_compute_signature(NULL, signature1) == RES_BAD_ARG);
- CHK(shtr_isotope_metadata_compute_signature(mdata1, NULL) == RES_BAD_ARG);
- CHK(shtr_isotope_metadata_compute_signature(mdata1, signature1) == RES_OK);
+ /* Check the hash API */
+ CHK(shtr_isotope_metadata_hash(NULL, hash1) == RES_BAD_ARG);
+ CHK(shtr_isotope_metadata_hash(mdata1, NULL) == RES_BAD_ARG);
+ CHK(shtr_isotope_metadata_hash(mdata1, hash1) == RES_OK);
/* Setup the same metadata of the first one, in the same order */
CHK(fp = tmpfile());
@@ -359,11 +353,11 @@ test_signature(struct shtr* shtr)
CHK(shtr_isotope_metadata_load_stream(shtr, fp, NULL, &mdata2) == RES_OK);
CHK(fclose(fp) == 0);
- CHK(shtr_isotope_metadata_compute_signature(mdata2, signature2) == RES_OK);
- CHK(hash256_eq(signature2, signature1) != 0);
+ CHK(shtr_isotope_metadata_hash(mdata2, hash2) == RES_OK);
+ CHK(hash256_eq(hash2, hash1) != 0);
/* Configure metadata for H2O and CO2 molecules but in a different order.
- * This may not change the signature */
+ * This may not change the hash */
CHK(fp = tmpfile());
fprintf(fp, "Molecule # Iso Abundance Q(296K) gj Molar Mass(g)\n");
molecule_print(fp, &CO2);
@@ -372,8 +366,8 @@ test_signature(struct shtr* shtr)
CHK(shtr_isotope_metadata_load_stream(shtr, fp, NULL, &mdata3) == RES_OK);
CHK(fclose(fp) == 0);
- CHK(shtr_isotope_metadata_compute_signature(mdata3, signature3) == RES_OK);
- CHK(hash256_eq(signature3, signature1) != 0);
+ CHK(shtr_isotope_metadata_hash(mdata3, hash3) == RES_OK);
+ CHK(hash256_eq(hash3, hash1) != 0);
/* Configure metadata for CO2 */
CHK(fp = tmpfile());
@@ -383,27 +377,44 @@ test_signature(struct shtr* shtr)
CHK(shtr_isotope_metadata_load_stream(shtr, fp, NULL, &mdata4) == RES_OK);
CHK(fclose(fp) == 0);
- CHK(shtr_isotope_metadata_compute_signature(mdata4, signature4) == RES_OK);
- CHK(hash256_eq(signature4, signature1) == 0);
+ CHK(shtr_isotope_metadata_hash(mdata4, hash4) == RES_OK);
+ CHK(hash256_eq(hash4, hash1) == 0);
/* Configure metadata for CO2 but print its isotope in different order.
- * This may not change the signature */
+ * This may not change the hash */
+ CO2_custom = CO2;
+ CO2_custom.isotopes = isotopes_custom;
+ FOR_EACH(i, 0, CO2.nisotopes) {
+ isotopes_custom[i] = CO2_isotopes[CO2.nisotopes-i-1];
+ }
CHK(fp = tmpfile());
fprintf(fp, "Molecule # Iso Abundance Q(296K) gj Molar Mass(g)\n");
- molecule_print_reverse(fp, &CO2);
+ molecule_print(fp, &CO2_custom);
rewind(fp);
CHK(shtr_isotope_metadata_load_stream(shtr, fp, NULL, &mdata5) == RES_OK);
CHK(fclose(fp) == 0);
- CHK(shtr_isotope_metadata_compute_signature(mdata4, signature5) == RES_OK);
- CHK(hash256_eq(signature5, signature4) != 0);
+ CHK(shtr_isotope_metadata_hash(mdata5, hash5) == RES_OK);
+ CHK(hash256_eq(hash5, hash4) != 0);
+
+ /* Configure metadata for CO2 with only few isotopes */
+ CO2_custom.nisotopes = CO2_custom.nisotopes/4;
+ CHK(fp = tmpfile());
+ fprintf(fp, "Molecule # Iso Abundance Q(296K) gj Molar Mass(g)\n");
+ molecule_print(fp, &CO2_custom);
+ rewind(fp);
+ CHK(shtr_isotope_metadata_load_stream(shtr, fp, NULL, &mdata6) == RES_OK);
+ CHK(fclose(fp) == 0);
+
+ CHK(shtr_isotope_metadata_hash(mdata6, hash6) == RES_OK);
+ CHK(hash256_eq(hash6, hash5) == 0);
- /* Compute the signatures */
CHK(shtr_isotope_metadata_ref_put(mdata1) == RES_OK);
CHK(shtr_isotope_metadata_ref_put(mdata2) == RES_OK);
CHK(shtr_isotope_metadata_ref_put(mdata3) == RES_OK);
CHK(shtr_isotope_metadata_ref_put(mdata4) == RES_OK);
CHK(shtr_isotope_metadata_ref_put(mdata5) == RES_OK);
+ CHK(shtr_isotope_metadata_ref_put(mdata6) == RES_OK);
}
static void
@@ -557,7 +568,7 @@ main(int argc, char** argv)
test_load(shtr);
test_load_failures(shtr);
- test_signature(shtr);
+ test_hash(shtr);
test_serialization(shtr);
FOR_EACH(i, 1, argc) {
test_load_file(shtr, argv[i]);