]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/et-forest.c
Allow automatics in equivalences
[thirdparty/gcc.git] / gcc / et-forest.c
CommitLineData
917bbcab 1/* ET-trees data structure implementation.
5dd29ee7 2 Contributed by Pavel Nejedly
fbd26352 3 Copyright (C) 2002-2019 Free Software Foundation, Inc.
5dd29ee7 4
5This file is part of the libiberty library.
6Libiberty is free software; you can redistribute it and/or
7modify it under the terms of the GNU Library General Public
8License as published by the Free Software Foundation; either
8c4c00c1 9version 3 of the License, or (at your option) any later version.
5dd29ee7 10
11Libiberty is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14Library General Public License for more details.
15
16You should have received a copy of the GNU Library General Public
8c4c00c1 17License along with libiberty; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>.
5dd29ee7 19
20 The ET-forest structure is described in:
21 D. D. Sleator and R. E. Tarjan. A data structure for dynamic trees.
22 J. G'omput. System Sci., 26(3):362 381, 1983.
23*/
24
25#include "config.h"
26#include "system.h"
805e22b2 27#include "coretypes.h"
b694b261 28#include "alloc-pool.h"
63c440eb 29#include "et-forest.h"
99b4f3a2 30#include "selftest.h"
5dd29ee7 31
382ecba7 32/* We do not enable this with CHECKING_P, since it is awfully slow. */
0051c76a 33#undef DEBUG_ET
5dd29ee7 34
0051c76a 35#ifdef DEBUG_ET
9ef16211 36#include "backend.h"
94ea8568 37#include "hard-reg-set.h"
0051c76a 38#endif
5dd29ee7 39
4885b286 40/* The occurrence of a node in the et tree. */
0051c76a 41struct et_occ
5dd29ee7 42{
0051c76a 43 struct et_node *of; /* The node. */
44
45 struct et_occ *parent; /* Parent in the splay-tree. */
46 struct et_occ *prev; /* Left son in the splay-tree. */
47 struct et_occ *next; /* Right son in the splay-tree. */
48
49 int depth; /* The depth of the node is the sum of depth
50 fields on the path to the root. */
51 int min; /* The minimum value of the depth in the subtree
52 is obtained by adding sum of depth fields
53 on the path to the root. */
4885b286 54 struct et_occ *min_occ; /* The occurrence in the subtree with the minimal
0051c76a 55 depth. */
56};
5dd29ee7 57
1dc6c44d 58static object_allocator<et_node> et_nodes ("et_nodes pool");
59static object_allocator<et_occ> et_occurrences ("et_occ pool");
5dd29ee7 60
0051c76a 61/* Changes depth of OCC to D. */
5dd29ee7 62
0051c76a 63static inline void
64set_depth (struct et_occ *occ, int d)
65{
66 if (!occ)
67 return;
5dd29ee7 68
0051c76a 69 occ->min += d - occ->depth;
70 occ->depth = d;
71}
5dd29ee7 72
0051c76a 73/* Adds D to the depth of OCC. */
5dd29ee7 74
0051c76a 75static inline void
76set_depth_add (struct et_occ *occ, int d)
5dd29ee7 77{
0051c76a 78 if (!occ)
79 return;
5dd29ee7 80
0051c76a 81 occ->min += d;
82 occ->depth += d;
83}
5dd29ee7 84
0051c76a 85/* Sets prev field of OCC to P. */
5dd29ee7 86
0051c76a 87static inline void
88set_prev (struct et_occ *occ, struct et_occ *t)
5dd29ee7 89{
0051c76a 90#ifdef DEBUG_ET
611234b4 91 gcc_assert (occ != t);
0051c76a 92#endif
5dd29ee7 93
0051c76a 94 occ->prev = t;
95 if (t)
96 t->parent = occ;
5dd29ee7 97}
98
0051c76a 99/* Sets next field of OCC to P. */
100
101static inline void
102set_next (struct et_occ *occ, struct et_occ *t)
5dd29ee7 103{
0051c76a 104#ifdef DEBUG_ET
611234b4 105 gcc_assert (occ != t);
0051c76a 106#endif
107
108 occ->next = t;
109 if (t)
110 t->parent = occ;
5dd29ee7 111}
112
4885b286 113/* Recompute minimum for occurrence OCC. */
5dd29ee7 114
0051c76a 115static inline void
116et_recomp_min (struct et_occ *occ)
5dd29ee7 117{
0051c76a 118 struct et_occ *mson = occ->prev;
5dd29ee7 119
0051c76a 120 if (!mson
121 || (occ->next
122 && mson->min > occ->next->min))
123 mson = occ->next;
5dd29ee7 124
0051c76a 125 if (mson && mson->min < 0)
126 {
127 occ->min = mson->min + occ->depth;
128 occ->min_occ = mson->min_occ;
129 }
130 else
131 {
132 occ->min = occ->depth;
133 occ->min_occ = occ;
134 }
135}
5dd29ee7 136
0051c76a 137#ifdef DEBUG_ET
91275768 138/* Checks whether neighborhood of OCC seems sane. */
5dd29ee7 139
0051c76a 140static void
141et_check_occ_sanity (struct et_occ *occ)
142{
143 if (!occ)
144 return;
5dd29ee7 145
611234b4 146 gcc_assert (occ->parent != occ);
147 gcc_assert (occ->prev != occ);
148 gcc_assert (occ->next != occ);
149 gcc_assert (!occ->next || occ->next != occ->prev);
5dd29ee7 150
0051c76a 151 if (occ->next)
5dd29ee7 152 {
611234b4 153 gcc_assert (occ->next != occ->parent);
154 gcc_assert (occ->next->parent == occ);
35cb5232 155 }
0051c76a 156
157 if (occ->prev)
5dd29ee7 158 {
611234b4 159 gcc_assert (occ->prev != occ->parent);
160 gcc_assert (occ->prev->parent == occ);
5dd29ee7 161 }
162
611234b4 163 gcc_assert (!occ->parent
164 || occ->parent->prev == occ
165 || occ->parent->next == occ);
5dd29ee7 166}
167
0051c76a 168/* Checks whether tree rooted at OCC is sane. */
169
5dd29ee7 170static void
0051c76a 171et_check_sanity (struct et_occ *occ)
5dd29ee7 172{
0051c76a 173 et_check_occ_sanity (occ);
174 if (occ->prev)
175 et_check_sanity (occ->prev);
176 if (occ->next)
177 et_check_sanity (occ->next);
178}
5dd29ee7 179
0051c76a 180/* Checks whether tree containing OCC is sane. */
5dd29ee7 181
0051c76a 182static void
183et_check_tree_sanity (struct et_occ *occ)
184{
185 while (occ->parent)
186 occ = occ->parent;
5dd29ee7 187
0051c76a 188 et_check_sanity (occ);
189}
5dd29ee7 190
0051c76a 191/* For recording the paths. */
5dd29ee7 192
4ee9c684 193/* An ad-hoc constant; if the function has more blocks, this won't work,
194 but since it is used for debugging only, it does not matter. */
195#define MAX_NODES 100000
196
0051c76a 197static int len;
4ee9c684 198static void *datas[MAX_NODES];
199static int depths[MAX_NODES];
5dd29ee7 200
0051c76a 201/* Records the path represented by OCC, with depth incremented by DEPTH. */
5dd29ee7 202
0051c76a 203static int
204record_path_before_1 (struct et_occ *occ, int depth)
205{
206 int mn, m;
5dd29ee7 207
0051c76a 208 depth += occ->depth;
209 mn = depth;
210
211 if (occ->prev)
212 {
48e1416a 213 m = record_path_before_1 (occ->prev, depth);
0051c76a 214 if (m < mn)
215 mn = m;
5dd29ee7 216 }
217
0051c76a 218 fprintf (stderr, "%d (%d); ", ((basic_block) occ->of->data)->index, depth);
4ee9c684 219
611234b4 220 gcc_assert (len < MAX_NODES);
4ee9c684 221
0051c76a 222 depths[len] = depth;
223 datas[len] = occ->of;
224 len++;
225
226 if (occ->next)
5dd29ee7 227 {
0051c76a 228 m = record_path_before_1 (occ->next, depth);
229 if (m < mn)
230 mn = m;
231 }
5dd29ee7 232
611234b4 233 gcc_assert (mn == occ->min + depth - occ->depth);
5dd29ee7 234
0051c76a 235 return mn;
236}
5dd29ee7 237
0051c76a 238/* Records the path represented by a tree containing OCC. */
5dd29ee7 239
0051c76a 240static void
241record_path_before (struct et_occ *occ)
242{
243 while (occ->parent)
244 occ = occ->parent;
5dd29ee7 245
0051c76a 246 len = 0;
247 record_path_before_1 (occ, 0);
248 fprintf (stderr, "\n");
5dd29ee7 249}
250
0051c76a 251/* Checks whether the path represented by OCC, with depth incremented by DEPTH,
252 was not changed since the last recording. */
253
254static int
255check_path_after_1 (struct et_occ *occ, int depth)
5dd29ee7 256{
0051c76a 257 int mn, m;
258
259 depth += occ->depth;
260 mn = depth;
5dd29ee7 261
0051c76a 262 if (occ->next)
5dd29ee7 263 {
48e1416a 264 m = check_path_after_1 (occ->next, depth);
0051c76a 265 if (m < mn)
266 mn = m;
267 }
5dd29ee7 268
0051c76a 269 len--;
611234b4 270 gcc_assert (depths[len] == depth && datas[len] == occ->of);
0051c76a 271
272 if (occ->prev)
273 {
274 m = check_path_after_1 (occ->prev, depth);
275 if (m < mn)
276 mn = m;
5dd29ee7 277 }
278
611234b4 279 gcc_assert (mn == occ->min + depth - occ->depth);
0051c76a 280
281 return mn;
5dd29ee7 282}
283
0051c76a 284/* Checks whether the path represented by a tree containing OCC was
285 not changed since the last recording. */
286
287static void
288check_path_after (struct et_occ *occ)
289{
290 while (occ->parent)
291 occ = occ->parent;
292
293 check_path_after_1 (occ, 0);
611234b4 294 gcc_assert (!len);
0051c76a 295}
5dd29ee7 296
0051c76a 297#endif
5dd29ee7 298
4885b286 299/* Splay the occurrence OCC to the root of the tree. */
5dd29ee7 300
89df180d 301static void
0051c76a 302et_splay (struct et_occ *occ)
5dd29ee7 303{
0051c76a 304 struct et_occ *f, *gf, *ggf;
305 int occ_depth, f_depth, gf_depth;
306
307#ifdef DEBUG_ET
308 record_path_before (occ);
309 et_check_tree_sanity (occ);
310#endif
48e1416a 311
0051c76a 312 while (occ->parent)
313 {
314 occ_depth = occ->depth;
5dd29ee7 315
0051c76a 316 f = occ->parent;
317 f_depth = f->depth;
5dd29ee7 318
0051c76a 319 gf = f->parent;
5dd29ee7 320
0051c76a 321 if (!gf)
322 {
323 set_depth_add (occ, f_depth);
324 occ->min_occ = f->min_occ;
325 occ->min = f->min;
5dd29ee7 326
0051c76a 327 if (f->prev == occ)
328 {
329 /* zig */
330 set_prev (f, occ->next);
331 set_next (occ, f);
332 set_depth_add (f->prev, occ_depth);
333 }
334 else
335 {
336 /* zag */
337 set_next (f, occ->prev);
338 set_prev (occ, f);
339 set_depth_add (f->next, occ_depth);
340 }
341 set_depth (f, -occ_depth);
342 occ->parent = NULL;
343
344 et_recomp_min (f);
345#ifdef DEBUG_ET
346 et_check_tree_sanity (occ);
347 check_path_after (occ);
348#endif
349 return;
350 }
351
352 gf_depth = gf->depth;
353
354 set_depth_add (occ, f_depth + gf_depth);
355 occ->min_occ = gf->min_occ;
356 occ->min = gf->min;
357
358 ggf = gf->parent;
359
360 if (gf->prev == f)
361 {
362 if (f->prev == occ)
363 {
364 /* zig zig */
365 set_prev (gf, f->next);
366 set_prev (f, occ->next);
367 set_next (occ, f);
368 set_next (f, gf);
369
370 set_depth (f, -occ_depth);
371 set_depth_add (f->prev, occ_depth);
372 set_depth (gf, -f_depth);
373 set_depth_add (gf->prev, f_depth);
374 }
375 else
376 {
377 /* zag zig */
378 set_prev (gf, occ->next);
379 set_next (f, occ->prev);
380 set_prev (occ, f);
381 set_next (occ, gf);
382
383 set_depth (f, -occ_depth);
384 set_depth_add (f->next, occ_depth);
385 set_depth (gf, -occ_depth - f_depth);
386 set_depth_add (gf->prev, occ_depth + f_depth);
387 }
388 }
389 else
390 {
391 if (f->prev == occ)
392 {
393 /* zig zag */
394 set_next (gf, occ->prev);
395 set_prev (f, occ->next);
396 set_prev (occ, gf);
397 set_next (occ, f);
398
399 set_depth (f, -occ_depth);
400 set_depth_add (f->prev, occ_depth);
401 set_depth (gf, -occ_depth - f_depth);
402 set_depth_add (gf->next, occ_depth + f_depth);
403 }
404 else
405 {
406 /* zag zag */
407 set_next (gf, f->prev);
408 set_next (f, occ->prev);
409 set_prev (occ, f);
410 set_prev (f, gf);
411
412 set_depth (f, -occ_depth);
413 set_depth_add (f->next, occ_depth);
414 set_depth (gf, -f_depth);
415 set_depth_add (gf->next, f_depth);
416 }
417 }
418
419 occ->parent = ggf;
420 if (ggf)
421 {
422 if (ggf->prev == gf)
423 ggf->prev = occ;
424 else
425 ggf->next = occ;
426 }
427
428 et_recomp_min (gf);
429 et_recomp_min (f);
430#ifdef DEBUG_ET
431 et_check_tree_sanity (occ);
432#endif
433 }
434
435#ifdef DEBUG_ET
436 et_check_sanity (occ);
437 check_path_after (occ);
438#endif
439}
440
4885b286 441/* Create a new et tree occurrence of NODE. */
0051c76a 442
443static struct et_occ *
444et_new_occ (struct et_node *node)
5dd29ee7 445{
e16712b1 446 et_occ *nw = et_occurrences.allocate ();
0051c76a 447
448 nw->of = node;
449 nw->parent = NULL;
450 nw->prev = NULL;
451 nw->next = NULL;
452
453 nw->depth = 0;
454 nw->min_occ = nw;
455 nw->min = 0;
456
457 return nw;
5dd29ee7 458}
459
0051c76a 460/* Create a new et tree containing DATA. */
461
462struct et_node *
463et_new_tree (void *data)
5dd29ee7 464{
e16712b1 465 et_node *nw = et_nodes.allocate ();
0051c76a 466
467 nw->data = data;
468 nw->father = NULL;
469 nw->left = NULL;
470 nw->right = NULL;
471 nw->son = NULL;
472
473 nw->rightmost_occ = et_new_occ (nw);
474 nw->parent_occ = NULL;
475
476 return nw;
5dd29ee7 477}
478
0051c76a 479/* Releases et tree T. */
480
481void
482et_free_tree (struct et_node *t)
5dd29ee7 483{
0051c76a 484 while (t->son)
485 et_split (t->son);
5dd29ee7 486
0051c76a 487 if (t->father)
488 et_split (t);
5dd29ee7 489
e16712b1 490 et_occurrences.remove (t->rightmost_occ);
491 et_nodes.remove (t);
5dd29ee7 492}
493
108beec7 494/* Releases et tree T without maintaining other nodes. */
495
496void
497et_free_tree_force (struct et_node *t)
498{
e16712b1 499 et_occurrences.remove (t->rightmost_occ);
0a06d4f0 500 if (t->parent_occ)
e16712b1 501 et_occurrences.remove (t->parent_occ);
502 et_nodes.remove (t);
108beec7 503}
504
0a06d4f0 505/* Release the alloc pools, if they are empty. */
506
507void
508et_free_pools (void)
509{
e16712b1 510 et_occurrences.release_if_empty ();
511 et_nodes.release_if_empty ();
0a06d4f0 512}
513
0051c76a 514/* Sets father of et tree T to FATHER. */
515
5dd29ee7 516void
0051c76a 517et_set_father (struct et_node *t, struct et_node *father)
5dd29ee7 518{
0051c76a 519 struct et_node *left, *right;
520 struct et_occ *rmost, *left_part, *new_f_occ, *p;
5dd29ee7 521
0051c76a 522 /* Update the path represented in the splay tree. */
523 new_f_occ = et_new_occ (father);
5dd29ee7 524
0051c76a 525 rmost = father->rightmost_occ;
526 et_splay (rmost);
5dd29ee7 527
0051c76a 528 left_part = rmost->prev;
5dd29ee7 529
0051c76a 530 p = t->rightmost_occ;
531 et_splay (p);
5dd29ee7 532
0051c76a 533 set_prev (new_f_occ, left_part);
534 set_next (new_f_occ, p);
535
536 p->depth++;
537 p->min++;
538 et_recomp_min (new_f_occ);
5dd29ee7 539
0051c76a 540 set_prev (rmost, new_f_occ);
35cb5232 541
0051c76a 542 if (new_f_occ->min + rmost->depth < rmost->min)
543 {
544 rmost->min = new_f_occ->min + rmost->depth;
545 rmost->min_occ = new_f_occ->min_occ;
546 }
5dd29ee7 547
0051c76a 548 t->parent_occ = new_f_occ;
5dd29ee7 549
0051c76a 550 /* Update the tree. */
551 t->father = father;
552 right = father->son;
553 if (right)
554 left = right->left;
555 else
556 left = right = t;
5dd29ee7 557
0051c76a 558 left->right = t;
559 right->left = t;
560 t->left = left;
561 t->right = right;
5dd29ee7 562
0051c76a 563 father->son = t;
5dd29ee7 564
0051c76a 565#ifdef DEBUG_ET
566 et_check_tree_sanity (rmost);
567 record_path_before (rmost);
568#endif
5dd29ee7 569}
570
0051c76a 571/* Splits the edge from T to its father. */
572
573void
574et_split (struct et_node *t)
5dd29ee7 575{
0051c76a 576 struct et_node *father = t->father;
577 struct et_occ *r, *l, *rmost, *p_occ;
5dd29ee7 578
0051c76a 579 /* Update the path represented by the splay tree. */
580 rmost = t->rightmost_occ;
581 et_splay (rmost);
5dd29ee7 582
0051c76a 583 for (r = rmost->next; r->prev; r = r->prev)
584 continue;
48e1416a 585 et_splay (r);
5dd29ee7 586
0051c76a 587 r->prev->parent = NULL;
588 p_occ = t->parent_occ;
589 et_splay (p_occ);
590 t->parent_occ = NULL;
5dd29ee7 591
0051c76a 592 l = p_occ->prev;
593 p_occ->next->parent = NULL;
35cb5232 594
0051c76a 595 set_prev (r, l);
5dd29ee7 596
0051c76a 597 et_recomp_min (r);
5dd29ee7 598
0051c76a 599 et_splay (rmost);
600 rmost->depth = 0;
601 rmost->min = 0;
5dd29ee7 602
e16712b1 603 et_occurrences.remove (p_occ);
5dd29ee7 604
0051c76a 605 /* Update the tree. */
606 if (father->son == t)
607 father->son = t->right;
608 if (father->son == t)
609 father->son = NULL;
610 else
5dd29ee7 611 {
0051c76a 612 t->left->right = t->right;
613 t->right->left = t->left;
614 }
615 t->left = t->right = NULL;
616 t->father = NULL;
617
618#ifdef DEBUG_ET
619 et_check_tree_sanity (rmost);
620 record_path_before (rmost);
621
622 et_check_tree_sanity (r);
623 record_path_before (r);
624#endif
625}
626
627/* Finds the nearest common ancestor of the nodes N1 and N2. */
628
629struct et_node *
630et_nca (struct et_node *n1, struct et_node *n2)
631{
632 struct et_occ *o1 = n1->rightmost_occ, *o2 = n2->rightmost_occ, *om;
633 struct et_occ *l, *r, *ret;
634 int mn;
635
636 if (n1 == n2)
637 return n1;
638
639 et_splay (o1);
640 l = o1->prev;
641 r = o1->next;
642 if (l)
643 l->parent = NULL;
644 if (r)
645 r->parent = NULL;
646 et_splay (o2);
647
648 if (l == o2 || (l && l->parent != NULL))
649 {
650 ret = o2->next;
651
652 set_prev (o1, o2);
653 if (r)
654 r->parent = o1;
5dd29ee7 655 }
8cb78e28 656 else if (r == o2 || (r && r->parent != NULL))
5dd29ee7 657 {
0051c76a 658 ret = o2->prev;
659
660 set_next (o1, o2);
661 if (l)
662 l->parent = o1;
5dd29ee7 663 }
8cb78e28 664 else
665 {
666 /* O1 and O2 are in different components of the forest. */
667 if (l)
668 l->parent = o1;
669 if (r)
670 r->parent = o1;
671 return NULL;
672 }
35cb5232 673
c9281ef8 674 if (o2->depth > 0)
5dd29ee7 675 {
0051c76a 676 om = o1;
677 mn = o1->depth;
678 }
679 else
680 {
681 om = o2;
682 mn = o2->depth + o1->depth;
5dd29ee7 683 }
684
0051c76a 685#ifdef DEBUG_ET
686 et_check_tree_sanity (o2);
687#endif
5dd29ee7 688
0051c76a 689 if (ret && ret->min + o1->depth + o2->depth < mn)
690 return ret->min_occ->of;
691 else
692 return om->of;
5dd29ee7 693}
694
0051c76a 695/* Checks whether the node UP is an ancestor of the node DOWN. */
696
697bool
698et_below (struct et_node *down, struct et_node *up)
5dd29ee7 699{
0051c76a 700 struct et_occ *u = up->rightmost_occ, *d = down->rightmost_occ;
701 struct et_occ *l, *r;
702
703 if (up == down)
704 return true;
705
706 et_splay (u);
707 l = u->prev;
708 r = u->next;
709
710 if (!l)
711 return false;
712
713 l->parent = NULL;
714
715 if (r)
716 r->parent = NULL;
5dd29ee7 717
0051c76a 718 et_splay (d);
719
720 if (l == d || l->parent != NULL)
5dd29ee7 721 {
0051c76a 722 if (r)
723 r->parent = u;
724 set_prev (u, d);
725#ifdef DEBUG_ET
726 et_check_tree_sanity (u);
727#endif
5dd29ee7 728 }
0051c76a 729 else
730 {
731 l->parent = u;
732
733 /* In case O1 and O2 are in two different trees, we must just restore the
734 original state. */
735 if (r && r->parent != NULL)
736 set_next (u, d);
737 else
738 set_next (u, r);
739
740#ifdef DEBUG_ET
741 et_check_tree_sanity (u);
742#endif
743 return false;
744 }
745
c9281ef8 746 if (d->depth <= 0)
0051c76a 747 return false;
748
749 return !d->next || d->next->min + d->depth >= 0;
5dd29ee7 750}
3f9439d7 751
752/* Returns the root of the tree that contains NODE. */
753
754struct et_node *
755et_root (struct et_node *node)
756{
757 struct et_occ *occ = node->rightmost_occ, *r;
758
f0b5f617 759 /* The root of the tree corresponds to the rightmost occurrence in the
3f9439d7 760 represented path. */
761 et_splay (occ);
762 for (r = occ; r->next; r = r->next)
763 continue;
764 et_splay (r);
765
766 return r->of;
767}
99b4f3a2 768
769#if CHECKING_P
770
771namespace selftest {
772
773/* Selftests for et-forest.c. */
774
775/* Perform sanity checks for a tree consisting of a single node. */
776
777static void
778test_single_node ()
779{
780 void *test_data = (void *)0xcafebabe;
781
782 et_node *n = et_new_tree (test_data);
783 ASSERT_EQ (n->data, test_data);
784 ASSERT_EQ (n, et_root (n));
785 et_free_tree (n);
786}
787
788/* Test of this tree:
789 a
790 / \
791 / \
792 b c
793 / \ |
794 d e f. */
795
796static void
797test_simple_tree ()
798{
799 et_node *a = et_new_tree (NULL);
800 et_node *b = et_new_tree (NULL);
801 et_node *c = et_new_tree (NULL);
802 et_node *d = et_new_tree (NULL);
803 et_node *e = et_new_tree (NULL);
804 et_node *f = et_new_tree (NULL);
805
806 et_set_father (b, a);
807 et_set_father (c, a);
808 et_set_father (d, b);
809 et_set_father (e, b);
810 et_set_father (f, c);
811
812 ASSERT_TRUE (et_below (a, a));
813 ASSERT_TRUE (et_below (b, a));
814 ASSERT_TRUE (et_below (c, a));
815 ASSERT_TRUE (et_below (d, a));
816 ASSERT_TRUE (et_below (e, a));
817 ASSERT_TRUE (et_below (f, a));
818
819 ASSERT_FALSE (et_below (a, b));
820 ASSERT_TRUE (et_below (b, b));
821 ASSERT_FALSE (et_below (c, b));
822 ASSERT_TRUE (et_below (d, b));
823 ASSERT_TRUE (et_below (e, b));
824 ASSERT_FALSE (et_below (f, b));
825
826 ASSERT_FALSE (et_below (a, c));
827 ASSERT_FALSE (et_below (b, c));
828 ASSERT_TRUE (et_below (c, c));
829 ASSERT_FALSE (et_below (d, c));
830 ASSERT_FALSE (et_below (e, c));
831 ASSERT_TRUE (et_below (f, c));
832
833 ASSERT_FALSE (et_below (a, d));
834 ASSERT_FALSE (et_below (b, d));
835 ASSERT_FALSE (et_below (c, d));
836 ASSERT_TRUE (et_below (d, d));
837 ASSERT_FALSE (et_below (e, d));
838 ASSERT_FALSE (et_below (f, d));
839
840 ASSERT_FALSE (et_below (a, e));
841 ASSERT_FALSE (et_below (b, e));
842 ASSERT_FALSE (et_below (c, e));
843 ASSERT_FALSE (et_below (d, e));
844 ASSERT_TRUE (et_below (e, e));
845 ASSERT_FALSE (et_below (f, e));
846
847 ASSERT_FALSE (et_below (a, f));
848 ASSERT_FALSE (et_below (b, f));
849 ASSERT_FALSE (et_below (c, f));
850 ASSERT_FALSE (et_below (d, f));
851 ASSERT_FALSE (et_below (e, f));
852 ASSERT_TRUE (et_below (f, f));
853
854 et_free_tree_force (a);
855}
856
857/* Verify that two disconnected nodes are unrelated. */
858
859static void
860test_disconnected_nodes ()
861{
862 et_node *a = et_new_tree (NULL);
863 et_node *b = et_new_tree (NULL);
864
865 ASSERT_FALSE (et_below (a, b));
866 ASSERT_FALSE (et_below (b, a));
867
868 et_free_tree (a);
869 et_free_tree (b);
870}
871
872/* Run all of the selftests within this file. */
873
874void
875et_forest_c_tests ()
876{
877 test_single_node ();
878 test_simple_tree ();
879 test_disconnected_nodes ();
880}
881
882} // namespace selftest
883
884#endif /* CHECKING_P */