github.com/tunabay/go-bitarray@v1.3.1/buffer_internal_test.go (about) 1 // Copyright (c) 2021 Hirotsuna Mizuno. All rights reserved. 2 // Use of this source code is governed by the MIT license that can be found in 3 // the LICENSE file. 4 5 package bitarray 6 7 import ( 8 "fmt" 9 ) 10 11 // D returns the string representing of its internal state. 12 func (buf *Buffer) D() string { 13 if buf == nil { 14 return "<nil>" 15 } 16 return fmt.Sprintf("BUF{nbit=%d, off=%d, b=%08b}", buf.nBits, buf.off, buf.b) 17 } 18 19 // V validate the internal data representation. It panics on failure. 20 func (buf *Buffer) V() { 21 switch { 22 case buf == nil: 23 return 24 25 case buf.nBits < 0: 26 panicf("V: negative nBits %d", buf.nBits) 27 28 case buf.b != nil && len(buf.b) == 0: 29 panicf("V: buf.b is an empty slice, must be nil: %08b", buf.b) 30 31 case buf.b == nil && buf.nBits != 0: 32 panicf("V: buf.b is nil, must be non nil for nbits=%d", buf.nBits) 33 34 case buf.b == nil: 35 return 36 37 case buf.off < 0: 38 panicf("V: negative off %d", buf.off) 39 40 case len(buf.b) < (buf.off+buf.nBits+7)>>3: 41 panicf( 42 "V: short buf: off=%d, nBits=%d, reqB=%d, lenB=%d: %08b", 43 buf.off, buf.nBits, (buf.off+buf.nBits+7)>>3, len(buf.b), 44 buf.b, 45 ) 46 } 47 }