github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/bitree/bdb/page_test.go (about)

     1  // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors.
     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 bdb
    16  
    17  import (
    18  	"reflect"
    19  	"sort"
    20  	"testing"
    21  	"testing/quick"
    22  )
    23  
    24  func TestPage_typ(t *testing.T) {
    25  	if typ := (&page{flags: branchPageFlag}).typ(); typ != "branch" {
    26  		t.Fatalf("exp=branch; got=%v", typ)
    27  	}
    28  	if typ := (&page{flags: leafPageFlag}).typ(); typ != "leaf" {
    29  		t.Fatalf("exp=leaf; got=%v", typ)
    30  	}
    31  	if typ := (&page{flags: metaPageFlag}).typ(); typ != "meta" {
    32  		t.Fatalf("exp=meta; got=%v", typ)
    33  	}
    34  	if typ := (&page{flags: freelistPageFlag}).typ(); typ != "freelist" {
    35  		t.Fatalf("exp=freelist; got=%v", typ)
    36  	}
    37  	if typ := (&page{flags: 20000}).typ(); typ != "unknown<4e20>" {
    38  		t.Fatalf("exp=unknown<4e20>; got=%v", typ)
    39  	}
    40  }
    41  
    42  func TestPage_dump(t *testing.T) {
    43  	(&page{id: 256}).hexdump(16)
    44  }
    45  
    46  func TestPgids_merge(t *testing.T) {
    47  	a := pgids{4, 5, 6, 10, 11, 12, 13, 27}
    48  	b := pgids{1, 3, 8, 9, 25, 30}
    49  	c := a.merge(b)
    50  	if !reflect.DeepEqual(c, pgids{1, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 25, 27, 30}) {
    51  		t.Errorf("mismatch: %v", c)
    52  	}
    53  
    54  	a = pgids{4, 5, 6, 10, 11, 12, 13, 27, 35, 36}
    55  	b = pgids{8, 9, 25, 30}
    56  	c = a.merge(b)
    57  	if !reflect.DeepEqual(c, pgids{4, 5, 6, 8, 9, 10, 11, 12, 13, 25, 27, 30, 35, 36}) {
    58  		t.Errorf("mismatch: %v", c)
    59  	}
    60  }
    61  
    62  func TestPgids_merge_quick(t *testing.T) {
    63  	if err := quick.Check(func(a, b pgids) bool {
    64  		sort.Sort(a)
    65  		sort.Sort(b)
    66  
    67  		got := a.merge(b)
    68  
    69  		exp := append(a, b...)
    70  		sort.Sort(exp)
    71  
    72  		if !reflect.DeepEqual(exp, got) {
    73  			t.Errorf("\nexp=%+v\ngot=%+v\n", exp, got)
    74  			return false
    75  		}
    76  
    77  		return true
    78  	}, nil); err != nil {
    79  		t.Fatal(err)
    80  	}
    81  }