commit abdbd1f6aa716905a4eba1d26dadb8ea2e5707b7
parent 9ee065a9ed1e75649e31eb4f6421c832825be8b4
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Wed, 25 May 2022 11:09:22 +0200
Test the tree [de]serialization
Diffstat:
2 files changed, 118 insertions(+), 1 deletion(-)
diff --git a/src/sln_tree_build.c b/src/sln_tree_build.c
@@ -230,6 +230,7 @@ build_polylines
} else {
ASSERT(NODE(ichild1)->nvertices == 0);
+ ASSERT(istack < STACK_SIZE - 2);
stack[istack++] = inode; /* Push the current node */
stack[istack++] = ichild1; /* Push child1 */
@@ -314,7 +315,7 @@ partition_lines
inode = ichildren + 0; /* Make the left child current node */
- ASSERT(istack < STACK_SIZE);
+ ASSERT(istack < STACK_SIZE - 1);
stack[istack++] = ichildren + 1; /* Push the right child */
}
}
diff --git a/src/test_sln_tree.c b/src/test_sln_tree.c
@@ -224,6 +224,121 @@ test_tree(struct sln_mixture* mixture, const struct shtr_line_list* line_list)
CHK(sln_tree_ref_put(tree) == RES_OK);
}
+static void
+check_mesh_equality(const struct sln_mesh* mesh1, const struct sln_mesh* mesh2)
+{
+ size_t i;
+ CHK(mesh1 && mesh2);
+ CHK(mesh1->nvertices == mesh2->nvertices);
+
+ FOR_EACH(i, 0, mesh1->nvertices) {
+ CHK(mesh1->vertices[i].wavenumber == mesh2->vertices[i].wavenumber);
+ CHK(mesh1->vertices[i].ka == mesh2->vertices[i].ka);
+ }
+}
+
+static void
+check_node_equality
+ (const struct sln_tree* tree1,
+ const struct sln_tree* tree2,
+ const struct sln_node* node1,
+ const struct sln_node* node2)
+{
+ struct sln_mesh mesh1 = SLN_MESH_NULL;
+ struct sln_mesh mesh2 = SLN_MESH_NULL;
+ size_t iline, nlines;
+
+ CHK(node1 && node2);
+ CHK(sln_node_is_leaf(node1) == sln_node_is_leaf(node2));
+ CHK(sln_node_get_lines_count(node1) == sln_node_get_lines_count(node2));
+ nlines = sln_node_get_lines_count(node1);
+
+ FOR_EACH(iline, 0, nlines) {
+ const struct shtr_line* line1 = NULL;
+ const struct shtr_line* line2 = NULL;
+ CHK(sln_node_get_line(tree1, node1, iline, &line1) == RES_OK);
+ CHK(sln_node_get_line(tree2, node2, iline, &line2) == RES_OK);
+ CHK(shtr_line_eq(line1, line2));
+ }
+
+ CHK(sln_node_get_mesh(tree1, node1, &mesh1) == RES_OK);
+ CHK(sln_node_get_mesh(tree2, node2, &mesh2) == RES_OK);
+ check_mesh_equality(&mesh1, &mesh2);
+}
+
+static void
+check_tree_equality
+ (const struct sln_tree* tree1,
+ const struct sln_tree* tree2)
+{
+ const struct sln_node* stack[128] = {NULL};
+ int istack = 0;
+
+ struct sln_tree_desc desc1 = SLN_TREE_DESC_NULL;
+ struct sln_tree_desc desc2 = SLN_TREE_DESC_NULL;
+ const struct sln_node* node1 = NULL;
+ const struct sln_node* node2 = NULL;
+
+ CHK(sln_tree_get_desc(tree1, &desc1) == RES_OK);
+ CHK(sln_tree_get_desc(tree2, &desc2) == RES_OK);
+ CHK(desc1.max_nlines_per_leaf == desc2.max_nlines_per_leaf);
+ CHK(desc1.mesh_decimation_err == desc2.mesh_decimation_err);
+ CHK(desc1.line_profile == desc2.line_profile);
+
+ stack[istack++] = NULL;
+ stack[istack++] = NULL;
+
+ node1 = sln_tree_get_root(tree1);
+ node2 = sln_tree_get_root(tree2);
+
+ while(node1 || node2) {
+ CHK((!node1 && !node2) || (node1 && node2));
+ check_node_equality(tree1, tree2, node1, node2);
+
+ if(sln_node_is_leaf(node1)) {
+ node2 = stack[--istack];
+ node1 = stack[--istack];
+ } else {
+ stack[istack++] = sln_node_get_child(node1, 1);
+ stack[istack++] = sln_node_get_child(node2, 1);
+ node1 = sln_node_get_child(node1, 0);
+ node2 = sln_node_get_child(node2, 0);
+ }
+ }
+}
+
+static void
+test_tree_serialization
+ (struct sln_device* sln,
+ struct shtr* shtr,
+ struct sln_mixture* mixture)
+{
+ struct sln_tree_create_args tree_args = SLN_TREE_CREATE_ARGS_DEFAULT;
+ struct sln_tree* tree1 = NULL;
+ struct sln_tree* tree2 = NULL;
+ FILE* fp = NULL;
+
+ CHK(sln_tree_create(mixture, &tree_args, &tree1) == RES_OK);
+
+ CHK(fp = tmpfile());
+ CHK(sln_tree_write(NULL, fp) == RES_BAD_ARG);
+ CHK(sln_tree_write(tree1, NULL) == RES_BAD_ARG);
+ CHK(sln_tree_write(tree1, fp) == RES_OK);
+ rewind(fp);
+
+ CHK(sln_tree_create_from_stream(NULL, shtr, fp, &tree2) == RES_BAD_ARG);
+ CHK(sln_tree_create_from_stream(sln, NULL, fp, &tree2) == RES_BAD_ARG);
+ CHK(sln_tree_create_from_stream(sln, shtr, NULL, &tree2) == RES_BAD_ARG);
+ CHK(sln_tree_create_from_stream(sln, shtr, fp, NULL) == RES_BAD_ARG);
+ CHK(sln_tree_create_from_stream(sln, shtr, fp, &tree2) == RES_OK);
+ fclose(fp);
+
+ check_tree_equality(tree1, tree2);
+
+ CHK(sln_tree_ref_put(tree1) == RES_OK);
+ CHK(sln_tree_ref_put(tree2) == RES_OK);
+}
+
/*******************************************************************************
* Test function
******************************************************************************/
@@ -292,6 +407,7 @@ main(int argc, char** argv)
CHK(sln_mixture_create(sln, &mixture_args, &mixture) == RES_OK);
test_tree(mixture, line_list);
+ test_tree_serialization(sln, shtr, mixture);
CHK(sln_device_ref_put(sln) == RES_OK);
CHK(sln_mixture_ref_put(mixture) == RES_OK);