Path: blob/master/dep/rapidyaml/include/c4/yml/detail/checks.hpp
4270 views
#ifndef C4_YML_DETAIL_CHECKS_HPP_1#define C4_YML_DETAIL_CHECKS_HPP_23#include "c4/yml/tree.hpp"45#ifdef __clang__6# pragma clang diagnostic push7#elif defined(__GNUC__)8# pragma GCC diagnostic push9# pragma GCC diagnostic ignored "-Wtype-limits" // error: comparison of unsigned expression >= 0 is always true10#elif defined(_MSC_VER)11# pragma warning(push)12# pragma warning(disable: 4296/*expression is always 'boolean_value'*/)13#endif1415namespace c4 {16namespace yml {171819void check_invariants(Tree const& t, size_t node=NONE);20void check_free_list(Tree const& t);21void check_arena(Tree const& t);222324//-----------------------------------------------------------------------------25//-----------------------------------------------------------------------------26//-----------------------------------------------------------------------------2728inline void check_invariants(Tree const& t, size_t node)29{30if(node == NONE)31{32if(t.size() == 0) return;33node = t.root_id();34}3536auto const& n = *t._p(node);37#ifdef RYML_DBG38if(n.m_first_child != NONE || n.m_last_child != NONE)39{40printf("check(%zu): fc=%zu lc=%zu\n", node, n.m_first_child, n.m_last_child);41}42else43{44printf("check(%zu)\n", node);45}46#endif4748C4_CHECK(n.m_parent != node);49if(n.m_parent == NONE)50{51C4_CHECK(t.is_root(node));52}53else //if(n.m_parent != NONE)54{55C4_CHECK(t.has_child(n.m_parent, node));5657auto const& p = *t._p(n.m_parent);58if(n.m_prev_sibling == NONE)59{60C4_CHECK(p.m_first_child == node);61C4_CHECK(t.first_sibling(node) == node);62}63else64{65C4_CHECK(p.m_first_child != node);66C4_CHECK(t.first_sibling(node) != node);67}6869if(n.m_next_sibling == NONE)70{71C4_CHECK(p.m_last_child == node);72C4_CHECK(t.last_sibling(node) == node);73}74else75{76C4_CHECK(p.m_last_child != node);77C4_CHECK(t.last_sibling(node) != node);78}79}8081C4_CHECK(n.m_first_child != node);82C4_CHECK(n.m_last_child != node);83if(n.m_first_child != NONE || n.m_last_child != NONE)84{85C4_CHECK(n.m_first_child != NONE);86C4_CHECK(n.m_last_child != NONE);87}8889C4_CHECK(n.m_prev_sibling != node);90C4_CHECK(n.m_next_sibling != node);91if(n.m_prev_sibling != NONE)92{93C4_CHECK(t._p(n.m_prev_sibling)->m_next_sibling == node);94C4_CHECK(t._p(n.m_prev_sibling)->m_prev_sibling != node);95}96if(n.m_next_sibling != NONE)97{98C4_CHECK(t._p(n.m_next_sibling)->m_prev_sibling == node);99C4_CHECK(t._p(n.m_next_sibling)->m_next_sibling != node);100}101102size_t count = 0;103for(size_t i = n.m_first_child; i != NONE; i = t.next_sibling(i))104{105#ifdef RYML_DBG106printf("check(%zu): descend to child[%zu]=%zu\n", node, count, i);107#endif108auto const& ch = *t._p(i);109C4_CHECK(ch.m_parent == node);110C4_CHECK(ch.m_next_sibling != i);111++count;112}113C4_CHECK(count == t.num_children(node));114115if(n.m_prev_sibling == NONE && n.m_next_sibling == NONE)116{117if(n.m_parent != NONE)118{119C4_CHECK(t.num_children(n.m_parent) == 1);120C4_CHECK(t.num_siblings(node) == 1);121}122}123124if(node == t.root_id())125{126C4_CHECK(t.size() == t.m_size);127C4_CHECK(t.capacity() == t.m_cap);128C4_CHECK(t.m_cap == t.m_size + t.slack());129check_free_list(t);130check_arena(t);131}132133for(size_t i = t.first_child(node); i != NONE; i = t.next_sibling(i))134{135check_invariants(t, i);136}137}138139140//-----------------------------------------------------------------------------141//-----------------------------------------------------------------------------142//-----------------------------------------------------------------------------143144inline void check_free_list(Tree const& t)145{146if(t.m_free_head == NONE)147{148C4_CHECK(t.m_free_tail == t.m_free_head);149return;150}151152C4_CHECK(t.m_free_head >= 0 && t.m_free_head < t.m_cap);153C4_CHECK(t.m_free_tail >= 0 && t.m_free_tail < t.m_cap);154155auto const& head = *t._p(t.m_free_head);156//auto const& tail = *t._p(t.m_free_tail);157158//C4_CHECK(head.m_prev_sibling == NONE);159//C4_CHECK(tail.m_next_sibling == NONE);160161size_t count = 0;162for(size_t i = t.m_free_head, prev = NONE; i != NONE; i = t._p(i)->m_next_sibling)163{164auto const& elm = *t._p(i);165if(&elm != &head)166{167C4_CHECK(elm.m_prev_sibling == prev);168}169prev = i;170++count;171}172C4_CHECK(count == t.slack());173}174175176//-----------------------------------------------------------------------------177//-----------------------------------------------------------------------------178//-----------------------------------------------------------------------------179180inline void check_arena(Tree const& t)181{182C4_CHECK(t.m_arena.len == 0 || (t.m_arena_pos >= 0 && t.m_arena_pos <= t.m_arena.len));183C4_CHECK(t.arena_size() == t.m_arena_pos);184C4_CHECK(t.arena_slack() + t.m_arena_pos == t.m_arena.len);185}186187188} /* namespace yml */189} /* namespace c4 */190191#ifdef __clang__192# pragma clang diagnostic pop193#elif defined(__GNUC__)194# pragma GCC diagnostic pop195#elif defined(_MSC_VER)196# pragma warning(pop)197#endif198199#endif /* C4_YML_DETAIL_CHECKS_HPP_ */200201202