check.go 656 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
package trie

// CheckInvariant panics of the trie does not meet its invariant.
func (trie *Trie) CheckInvariant() {
	switch {
	case trie.IsLeaf():
		return
	default:
		if trie.IsEmpty() {
			b0, b1 := trie.Branch[0], trie.Branch[1]
			b0.CheckInvariant()
			b1.CheckInvariant()
			switch {
			case b0.IsEmptyLeaf() && b1.IsEmptyLeaf():
				panic("intermediate node with two empty leaves")
			case b0.IsEmptyLeaf() && b1.IsNonEmptyLeaf():
				panic("intermediate node with one empty leaf")
			case b0.IsNonEmptyLeaf() && b1.IsEmptyLeaf():
				panic("intermediate node with one empty leaf")
			}
		} else {
			panic("intermediate node with a key")
		}
	}
}