github.com/matrixorigin/matrixone@v1.2.0/pkg/objectio/id_test.go (about)

     1  // Copyright 2021 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package objectio
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"testing"
    21  	"unsafe"
    22  
    23  	"github.com/matrixorigin/matrixone/pkg/container/types"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestIDAllocate(t *testing.T) {
    28  	sid := NewSegmentid()
    29  	bid := NewBlockid(sid, 1, 2)
    30  	rid := NewRowid(bid, 42)
    31  
    32  	// ensure not copy
    33  	sidSlice := sid[:]
    34  	require.Equal(t, unsafe.Pointer(&sidSlice[0]), unsafe.Pointer(&sid[0]))
    35  
    36  	// get block id from rowid without copy
    37  	bidUnsafe := rid.BorrowBlockID()
    38  	require.Equal(t, unsafe.Pointer(&bidUnsafe[0]), unsafe.Pointer(&rid[0]))
    39  	require.Zero(t, bid.Compare(*bidUnsafe))
    40  
    41  	require.Equal(t, rid.GetObjectString(), fmt.Sprintf("%s-%d", sid.ToString(), 1))
    42  }
    43  
    44  func TestWriteID(t *testing.T) {
    45  	sid := NewSegmentid()
    46  	// bid := NewBlockid(&sid, 1, 2)
    47  	// rid := NewRowid(&bid, 42)
    48  
    49  	var b bytes.Buffer
    50  
    51  	b.Write(sid[:])
    52  
    53  	var desid types.Uuid
    54  	b.Read(desid[:])
    55  
    56  	require.True(t, sid.Eq(desid))
    57  	require.Equal(t, sid.ToString(), desid.ToString())
    58  }