/* Copyright 2016-2024 Michele Santullo * This file is part of "duckhandy". * * "duckhandy" is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * "duckhandy" is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with "duckhandy". If not, see . */ #include "catch2/catch_test_macros.hpp" #include "duckhandy/tree_iterator.hpp" namespace { enum Letters : unsigned int { A = 'a', B, C, D, E, F, G, H, I }; struct TestNode { TestNode (unsigned int val) : content(val) {} TestNode* left{nullptr}, *right{nullptr}; unsigned int content{0}; }; } //unnamed namespace TEST_CASE("Check TreeIterator", "[TreeIterator][containers][iterator]") { typedef dhandy::TreeIterator TestIterator; TestIterator empty; { TestNode root{0xDEADBEEFu}; TestIterator it{&root, 1}; CHECK(*it == 0xDEADBEEFu); ++it; CHECK(it == empty); } { TestNode root{A}; TestNode l{B}, ll{C}, lr{D}; TestNode r{E}, rl{F}, rr{G}, rll{H}, rrr{I}; root.left = &l; root.right = &r; l.left = ≪ l.right = &lr; r.left = &rl; r.right = &rr; rl.left = &rll; rr.right = &rrr; TestIterator it{&root, 4}; CHECK(*it == C); //ll ++it; CHECK(*it == B); //l it++; CHECK(*it == D); //lr it++; CHECK(*it == A); //root ++it; CHECK(*it == H); //rll it++; CHECK(*it == F); //rl it++; CHECK(*it == E); //r ++it; CHECK(*it == G); //rr ++it; CHECK(*it == I); //rrr CHECK(it != empty); it++; CHECK(it == empty); } { TestNode root{A}; TestNode l{B}, ll{C}, lll{D}, llll{E}, lllll{F}; root.left = &l; l.left = ≪ ll.left = &lll; lll.left = &llll; llll.left = &lllll; TestIterator it{&root, 6}; CHECK(it != empty); CHECK(*it == F); ++it; CHECK(*it == E); ++it; CHECK(*it == D); ++it; CHECK(*it == C); ++it; CHECK(*it == B); ++it; CHECK(*it == A); ++it; CHECK(it == empty); } { TestNode root{A}; TestNode r{B}, rr{C}, rrr{D}, rrrr{E}, rrrrr{F}; root.right = &r; r.right = &rr; rr.right = &rrr; rrr.right = &rrrr; rrrr.right = &rrrrr; TestIterator it{&root, 6}; CHECK(it != empty); CHECK(*it == A); ++it; CHECK(*it == B); ++it; CHECK(*it == C); ++it; CHECK(*it == D); ++it; CHECK(*it == E); ++it; CHECK(*it == F); ++it; CHECK(it == empty); } }