commit 96fb7eb12a74281edbaec2aa2e0fad1d157d7e68
parent e0fa5c7137a12e4f5ef973010e97a8b7d6efbf0b
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Tue, 10 Mar 2026 10:19:53 +0100
Testing the hash function of a list of lines
Diffstat:
| M | src/test_shtr_lines.c | | | 93 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 93 insertions(+), 0 deletions(-)
diff --git a/src/test_shtr_lines.c b/src/test_shtr_lines.c
@@ -308,6 +308,8 @@ check_line_list_equality
{
size_t n1, n2;
size_t iline, nlines;
+ hash256_T hash1;
+ hash256_T hash2;
CHK(list1 && list2);
CHK(shtr_line_list_get_size(list1, &n1) == RES_OK);
@@ -324,6 +326,83 @@ check_line_list_equality
CHK(shtr_line_eq(&lines1, &lines2));
}
+
+ CHK(shtr_line_list_hash(list1, hash1) == RES_OK);
+ CHK(shtr_line_list_hash(list2, hash2) == RES_OK);
+ CHK(hash256_eq(hash1, hash2) != 0);
+}
+
+static void
+test_hash(struct shtr* shtr)
+{
+ struct shtr_line lines[] = {
+ {0.000134, 2.672E-38, 0.0533, 0.410, 608.4727, 0.79, 0.000060, 1, 4},
+ {0.000379, 1.055E-39, 0.0418, 0.329,1747.9686, 0.79, 0.000110, 1, 5},
+ {0.000448, 5.560E-38, 0.0490, 0.364,1093.0269, 0.79, 0.000060, 1, 4},
+ {0.000686, 1.633E-36, 0.0578, 0.394, 701.1162, 0.79, 0.000180, 1, 4},
+ {0.000726, 6.613E-33, 0.0695, 0.428, 402.3295, 0.79, 0.000240, 1, 3}
+ };
+ const size_t nlines = sizeof(lines) / sizeof(struct shtr_line);
+
+ FILE* fp = NULL;
+
+ struct shtr_line_list_load_args load_args = SHTR_LINE_LIST_LOAD_ARGS_NULL;
+ struct shtr_line_list* list1 = NULL;
+ struct shtr_line_list* list2 = NULL;
+ struct shtr_line_list* list3 = NULL;
+ struct shtr_line_list* list4 = NULL;
+ hash256_T hash1;
+ hash256_T hash2;
+ hash256_T hash3;
+ hash256_T hash4;
+
+ CHK(fp = tmpfile());
+ print_lines(fp, lines, nlines);
+ rewind(fp);
+ load_args.file = fp;
+ CHK(shtr_line_list_load(shtr, &load_args, &list1) == RES_OK);
+ CHK(fclose(fp) == 0);
+
+ /* Check hash API */
+ CHK(shtr_line_list_hash(NULL, hash1) == RES_BAD_ARG);
+ CHK(shtr_line_list_hash(list1, NULL) == RES_BAD_ARG);
+ CHK(shtr_line_list_hash(list1, hash1) == RES_OK);
+
+ /* Load fewer lines so that the hash is different from that of the 1st list */
+ CHK(fp = tmpfile());
+ print_lines(fp, lines, nlines-1);
+ rewind(fp);
+ load_args.file = fp;
+ CHK(shtr_line_list_load(shtr, &load_args, &list2) == RES_OK);
+ CHK(fclose(fp) == 0);
+
+ CHK(shtr_line_list_hash(list2, hash2) == RES_OK);
+ CHK(hash256_eq(hash1, hash2) == 0);
+
+ /* Change a value in the 1st list and verify that its hash is different */
+ lines[0].isotope_id_local = 3;
+ CHK(fp = tmpfile());
+ print_lines(fp, lines, nlines);
+ rewind(fp);
+ load_args.file = fp;
+ CHK(shtr_line_list_load(shtr, &load_args, &list3) == RES_OK);
+
+ CHK(shtr_line_list_hash(list3, hash3) == RES_OK);
+ CHK(hash256_eq(hash3, hash1) == 0);
+ CHK(hash256_eq(hash3, hash2) == 0);
+
+ /* Create the same list and ensure that the hash is identical */
+ rewind(fp);
+ CHK(shtr_line_list_load(shtr, &load_args, &list4) == RES_OK);
+ CHK(fclose(fp) == 0);
+
+ CHK(shtr_line_list_hash(list4, hash4) == RES_OK);
+ CHK(hash256_eq(hash4, hash3) != 0);
+
+ CHK(shtr_line_list_ref_put(list1) == RES_OK);
+ CHK(shtr_line_list_ref_put(list2) == RES_OK);
+ CHK(shtr_line_list_ref_put(list3) == RES_OK);
+ CHK(shtr_line_list_ref_put(list4) == RES_OK);
}
static void
@@ -407,6 +486,9 @@ test_deserialization_of_a_subset(struct shtr* shtr)
struct shtr_line_list* list1 = NULL;
struct shtr_line_list* list2 = NULL;
+ hash256_T hash1;
+ hash256_T hash2;
+
FILE* fp = NULL;
size_t i = 0;
size_t n = 0;
@@ -423,6 +505,7 @@ test_deserialization_of_a_subset(struct shtr* shtr)
write_args.file = fp;
CHK(shtr_line_list_write(list1, &write_args) == RES_OK);
+ /* Read an empty subset */
rewind(fp);
read_args.file = fp;
read_args.range[0] = nlines;
@@ -432,6 +515,7 @@ test_deserialization_of_a_subset(struct shtr* shtr)
CHK(n == 0);
CHK(shtr_line_list_ref_put(list2) == RES_OK);
+ /* Read all lines */
rewind(fp);
read_args.range[0] = 0;
read_args.range[1] = nlines-1; /*inclusive bound*/
@@ -439,6 +523,7 @@ test_deserialization_of_a_subset(struct shtr* shtr)
check_line_list_equality(list1, list2);
CHK(shtr_line_list_ref_put(list2) == RES_OK);
+ /* Read only the first three lines */
rewind(fp);
read_args.range[0] = 0;
read_args.range[1] = 2;
@@ -451,8 +536,12 @@ test_deserialization_of_a_subset(struct shtr* shtr)
CHK(shtr_line_list_at(list2, i, &line2) == RES_OK);
CHK(shtr_line_eq(&line1, &line2));
}
+ CHK(shtr_line_list_hash(list1, hash1) == RES_OK);
+ CHK(shtr_line_list_hash(list2, hash2) == RES_OK);
+ CHK(hash256_eq(hash1, hash2) == 0);
CHK(shtr_line_list_ref_put(list2) == RES_OK);
+ /* Read the last three lines */
rewind(fp);
read_args.range[0] = nlines-3;
read_args.range[1] = SIZE_MAX; /* Upper bound will be fit */
@@ -465,6 +554,9 @@ test_deserialization_of_a_subset(struct shtr* shtr)
CHK(shtr_line_list_at(list2, i, &line2) == RES_OK);
CHK(shtr_line_eq(&line1, &line2));
}
+ CHK(shtr_line_list_hash(list1, hash1) == RES_OK);
+ CHK(shtr_line_list_hash(list2, hash2) == RES_OK);
+ CHK(hash256_eq(hash1, hash2) == 0);
CHK(shtr_line_list_ref_put(list2) == RES_OK);
CHK(fclose(fp) == 0);
@@ -484,6 +576,7 @@ main(int argc, char** argv)
test_line_eq();
test_load(shtr);
test_load_failures(shtr);
+ test_hash(shtr);
test_serialization(shtr);
test_deserialization_of_a_subset(shtr);