commit 3b65704cf1fbe70c12d97960e559c895832bae65
parent 0ea298a9db29a1fe241ab3df4a341d8c9e40c938
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Fri, 10 Apr 2026 12:01:18 +0200
Add tests for tree arity to the tree tests
The test procedures are now performed for two different arities: the
default arity (i.e., 2) and an arity of 13.
Some tests have therefore been updated to correctly handle variable
arity, such as the one that calculates the tree depth, which previously
assumed the tree was binary.
Updated the tests to use the constants provided by the Star-Line API to
calculate the maximum effective size of the stacks used by the tree
traversals implemented in the tests.
Finally, removal of the root node’s polyline output, as it served no
purpose. It had been added to allow the caller to redirect the output to
a file that could then be viewed. However, this functionality is now
handled by a dedicated tool, namely sln-get.
Diffstat:
2 files changed, 41 insertions(+), 27 deletions(-)
diff --git a/Makefile b/Makefile
@@ -243,10 +243,13 @@ $(TEST_OBJ): config.mk sln-local.pc
test_sln_device \
test_sln_mesh \
test_sln_mixture \
-test_sln_tree \
: config.mk sln-local.pc $(LIBNAME)
$(CC) $(CFLAGS_TEST) -o $@ src/$@.o $(LDFLAGS_TEST)
+test_sln_tree \
+: config.mk sln-local.pc $(LIBNAME)
+ $(CC) $(CFLAGS_TEST) -o $@ src/$@.o $(LDFLAGS_TEST) -lm
+
clean_test:
rm -f $(TEST_DEP) $(TEST_OBJ) $(TEST_TGT) mixture.txt
for i in $(TEST_SRC); do rm -f "$$(basename "$${i}" ".c")"; done
diff --git a/src/test_sln_tree.c b/src/test_sln_tree.c
@@ -75,11 +75,14 @@ find_line
/* This test assumes that all the lines contained into the list are
* partitionned in the tree */
static void
-check_tree
+check_tree_lines
(const struct sln_tree* tree,
struct shtr_line_list* line_list)
{
- #define STACK_SIZE 64
+ #define STACK_SIZE \
+ ( SLN_TREE_DEPTH_MAX \
+ * SLN_TREE_ARITY_MAX - 1/*1st child*/ \
+ + 1/*Dummy node*/)
const struct sln_node* stack[STACK_SIZE] = {NULL};
struct sln_tree_desc desc = SLN_TREE_DESC_NULL;
size_t istack = 0;
@@ -94,13 +97,19 @@ check_tree
CHK(found_lines = mem_calloc(line_list_sz, sizeof(char)));
+ stack[istack++] = NULL; /* Dummy node to stop the recursion */
+
node = sln_tree_get_root(tree);
while(node) {
if(!sln_node_is_leaf(node)) {
+ unsigned i = 0;
+ unsigned n = sln_node_get_child_count(tree, node);
+
CHK(sln_node_get_line_count(node) > desc.max_nlines_per_leaf);
- ASSERT(istack < STACK_SIZE);
- stack[istack++] = sln_node_get_child(tree, node, 1); /* Push the child 1 */
+ FOR_EACH(i, 1, n) {
+ stack[istack++] = sln_node_get_child(tree, node, i);
+ }
node = sln_node_get_child(tree, node, 0); /* Handle the child 0 */
} else {
@@ -121,7 +130,7 @@ check_tree
}
}
- node = istack ? stack[--istack] : NULL; /* Pop the next node */
+ node = stack[--istack]; /* Pop the next node */
}
}
@@ -133,18 +142,6 @@ check_tree
}
static void
-dump_line(FILE* stream, const struct sln_mesh* mesh)
-{
- size_t i;
- CHK(mesh);
- FOR_EACH(i, 0, mesh->nvertices) {
- fprintf(stream, "%g %g\n",
- mesh->vertices[i].wavenumber,
- mesh->vertices[i].ka);
- }
-}
-
-static void
test_tree
(struct sln_device* sln,
const struct sln_tree_create_args* tree_args_in,
@@ -180,7 +177,8 @@ test_tree
CHK(desc.nlines == nlines);
CHK(desc.nvertices >= 1);
CHK(desc.nnodes >= 1);
- CHK(desc.depth == (unsigned)log2i((int)round_up_pow2(desc.nlines)));
+ CHK(desc.depth
+ == (unsigned)ceil(log((double)desc.nlines)/log((double)desc.arity)));
CHK(desc.pressure == tree_args.pressure);
CHK(desc.temperature == tree_args.temperature);
CHK(desc.arity == tree_args.arity);
@@ -208,7 +206,6 @@ test_tree
CHK(node_desc_next.nlines >= 1);
CHK(node_desc_next.nlines < node_desc.nlines);
CHK(node_desc_next.nvertices >= 1);
- CHK(node_desc_next.nlines < node_desc.nvertices);
if(sln_node_is_leaf(node)) {
CHK(node_desc_next.nchildren == 0);
} else {
@@ -235,10 +232,7 @@ test_tree
CHK(sln_node_get_mesh(tree, node, &mesh) == RES_OK);
CHK(node = sln_tree_get_root(tree));
- CHK(sln_node_get_mesh(tree, node, &mesh) == RES_OK);
-
- dump_line(stdout, &mesh);
- check_tree(tree, line_list);
+ check_tree_lines(tree, line_list);
CHK(sln_tree_ref_get(NULL) == RES_BAD_ARG);
CHK(sln_tree_ref_get(tree) == RES_OK);
@@ -309,7 +303,12 @@ check_tree_equality
(const struct sln_tree* tree1,
const struct sln_tree* tree2)
{
- const struct sln_node* stack[128] = {NULL};
+ #define STACK_SIZE \
+ ( SLN_TREE_DEPTH_MAX \
+ * (SLN_TREE_ARITY_MAX-1/*1st child*/) * 2/*#trees*/ \
+ + 1/*Dummy node*/)
+
+ const struct sln_node* stack[STACK_SIZE] = {NULL};
int istack = 0;
struct sln_tree_desc desc1 = SLN_TREE_DESC_NULL;
@@ -345,12 +344,19 @@ check_tree_equality
node2 = stack[--istack];
node1 = stack[--istack];
} else {
- stack[istack++] = sln_node_get_child(tree1, node1, 1);
- stack[istack++] = sln_node_get_child(tree2, node2, 1);
+ unsigned i = 0;
+ unsigned n = sln_node_get_child_count(tree1, node1);
+
+ FOR_EACH(i, 1, n) {
+ stack[istack++] = sln_node_get_child(tree1, node1, i);
+ stack[istack++] = sln_node_get_child(tree2, node2, i);
+ }
node1 = sln_node_get_child(tree1, node1, 0);
node2 = sln_node_get_child(tree2, node2, 0);
}
}
+
+ #undef STACK_SIZE
}
static void
@@ -479,6 +485,11 @@ main(int argc, char** argv)
test_tree(sln, &tree_args, line_list);
test_tree_serialization(sln, &tree_args);
+ /* Test a tree with a non default arity */
+ tree_args.arity = 13;
+ test_tree(sln, &tree_args, line_list);
+ test_tree_serialization(sln, &tree_args);
+
CHK(sln_device_ref_put(sln) == RES_OK);
CHK(shtr_ref_put(shtr) == RES_OK);
CHK(shtr_line_list_ref_put(line_list) == RES_OK);