Source file src/runtime/bitcursor_test.go

     1  // Copyright 2024 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package runtime_test
     6  
     7  import (
     8  	. "runtime"
     9  	"testing"
    10  )
    11  
    12  func TestBitCursor(t *testing.T) {
    13  	ones := [5]byte{0xff, 0xff, 0xff, 0xff, 0xff}
    14  	zeros := [5]byte{0, 0, 0, 0, 0}
    15  
    16  	for start := uintptr(0); start < 16; start++ {
    17  		for end := start + 1; end < 32; end++ {
    18  			buf := zeros
    19  			NewBitCursor(&buf[0]).Offset(start).Write(&ones[0], end-start)
    20  
    21  			for i := uintptr(0); i < uintptr(len(buf)*8); i++ {
    22  				bit := buf[i/8] >> (i % 8) & 1
    23  				if bit == 0 && i >= start && i < end {
    24  					t.Errorf("bit %d not set in [%d:%d]", i, start, end)
    25  				}
    26  				if bit == 1 && (i < start || i >= end) {
    27  					t.Errorf("bit %d is set outside [%d:%d]", i, start, end)
    28  				}
    29  			}
    30  		}
    31  	}
    32  
    33  	for start := uintptr(0); start < 16; start++ {
    34  		for end := start + 1; end < 32; end++ {
    35  			buf := ones
    36  			NewBitCursor(&buf[0]).Offset(start).Write(&zeros[0], end-start)
    37  
    38  			for i := uintptr(0); i < uintptr(len(buf)*8); i++ {
    39  				bit := buf[i/8] >> (i % 8) & 1
    40  				if bit == 1 && i >= start && i < end {
    41  					t.Errorf("bit %d not cleared in [%d:%d]", i, start, end)
    42  				}
    43  				if bit == 0 && (i < start || i >= end) {
    44  					t.Errorf("bit %d cleared outside [%d:%d]", i, start, end)
    45  				}
    46  			}
    47  		}
    48  	}
    49  }
    50  

View as plain text