GCC Code Coverage Report


Directory: ./
File: tests/unit/00_support/gtree_00.c
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 31 31 100.0%
Functions: 3 3 100.0%
Branches: 16 22 72.7%

Line Branch Exec Source
1 /*********************************************************************************/
2 /* Copyright 2009-2021 Barcelona Supercomputing Center */
3 /* */
4 /* This file is part of the DLB library. */
5 /* */
6 /* DLB is free software: you can redistribute it and/or modify */
7 /* it under the terms of the GNU Lesser General Public License as published by */
8 /* the Free Software Foundation, either version 3 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* DLB is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU Lesser General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU Lesser General Public License */
17 /* along with DLB. If not, see <https://www.gnu.org/licenses/>. */
18 /*********************************************************************************/
19
20 #include "support/gtree.h"
21
22 #include <stddef.h>
23 #include <stdbool.h>
24 #include <assert.h>
25
26 705 static gint key_compare_func(gconstpointer a, gconstpointer b) {
27 705 return *(const char*)a - *(const char*)b;
28 }
29
30 11 static gint find_A(gpointer key, gpointer value, gpointer data) {
31 11 char *ch = key;
32 11 return *ch == 'A';
33 }
34
35
36 1 int main(int argc, char **argv) {
37 GTree *tree;
38 char chars[62];
39 guint i, j;
40
41 1 tree = g_tree_new(key_compare_func);
42
43 // Insert [0-9][A-Z][a-z]
44 1 i = 0;
45
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 times.
11 for (j=0; j<10; ++j, ++i)
46 {
47 10 chars[i] = '0' + j;
48 10 g_tree_insert(tree, &chars[i], &chars[i]);
49 }
50
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 1 times.
27 for (j=0; j<26; ++j, ++i)
51 {
52 26 chars[i] = 'a' + j;
53 26 g_tree_insert(tree, &chars[i], &chars[i]);
54 }
55
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 1 times.
27 for (j=0; j<26; ++j, ++i)
56 {
57 26 chars[i] = 'A' + j;
58 26 g_tree_insert(tree, &chars[i], &chars[i]);
59 }
60
61 // Lookup each element
62
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 1 times.
63 for (i=0; i<10+26+26; ++i)
63 {
64
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
62 assert( g_tree_lookup(tree, &chars[i]) != NULL );
65 }
66
67 // Check sizes
68
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( g_tree_nnodes(tree) == 10 + 26 + 26 );
69
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( g_tree_height(tree) == 7 );
70
71 1 g_tree_foreach(tree, find_A, NULL);
72
73 // Remove [0-9]
74
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 times.
11 for (i=0; i<10; ++i) {
75 10 g_tree_remove(tree, &chars[i]);
76 }
77
78 // Check sizes
79
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( g_tree_nnodes(tree) == 26 + 26 );
80
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( g_tree_height(tree) == 7 );
81
82 1 g_tree_ref(tree);
83 1 g_tree_unref(tree);
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tree != NULL );
85 1 g_tree_destroy(tree);
86
87 1 return 0;
88 }
89