github.com/scottcagno/storage@v1.8.0/pkg/mmap/segment/segment_test.go (about)

     1  package segment
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"testing"
     7  )
     8  
     9  // Maximal values of the unsigned integer types.
    10  const (
    11  	maxUint8  = uint8(math.MaxUint8)
    12  	maxUint16 = uint16(math.MaxUint16)
    13  	maxUint32 = uint32(math.MaxUint32)
    14  	maxUint64 = uint64(math.MaxUint64)
    15  )
    16  
    17  // TestOffset tests the segment offset.
    18  // CASE: First byte MUST NOT be modified.
    19  func TestOffset(t *testing.T) {
    20  	data := make([]byte, 9)
    21  	seg := New(1, data[1:])
    22  	*seg.Uint64(1) = math.MaxUint64
    23  	if data[0] != 0 {
    24  		t.Fatalf("first byte must be zero, %d found", data[0])
    25  	}
    26  }
    27  
    28  // TestScanUint tests the unsigned integers scanning.
    29  // CASE: The read values MUST be exactly the same as the previously written.
    30  func TestScanUint(t *testing.T) {
    31  	seg := New(0, make([]byte, 16))
    32  	off := int64(1)
    33  	in8, in16, in32, in64 := maxUint8-1, maxUint16-200, maxUint32-3_000, maxUint64-40_000
    34  	*seg.Uint8(off) = in8
    35  	*seg.Uint16(off + Uint8Size) = in16
    36  	*seg.Uint32(off + Uint8Size + Uint16Size) = in32
    37  	*seg.Uint64(off + Uint8Size + Uint16Size + Uint32Size) = in64
    38  	out8, out16, out32, out64 := uint8(1), uint16(1), uint32(1), uint64(1)
    39  	if err := seg.ScanUint(off, &out8, &out16, &out32, &out64); err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	if in8 != out8 {
    43  		t.Fatalf("uint8 value must be %d, %d found", in8, out8)
    44  	}
    45  	if in16 != out16 {
    46  		t.Fatalf("uint16 value must be %d, %d found", in16, out16)
    47  	}
    48  	if in32 != out32 {
    49  		t.Fatalf("uint32 value must be %d, %d found", in32, out32)
    50  	}
    51  	if in64 != out64 {
    52  		t.Fatalf("uint64 value must be %d, %d found", in64, out64)
    53  	}
    54  	fmt.Printf("segment.offset: %d\n", seg.offset)
    55  }