github.com/bhojpur/cache@v0.0.4/pkg/memory/page_test.go (about)

     1  package memory
     2  
     3  // Copyright (c) 2018 Bhojpur Consulting Private Limited, India. All rights reserved.
     4  
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  
    12  // The above copyright notice and this permission notice shall be included in
    13  // all copies or substantial portions of the Software.
    14  
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    21  // THE SOFTWARE.
    22  
    23  import (
    24  	"reflect"
    25  	"sort"
    26  	"testing"
    27  	"testing/quick"
    28  )
    29  
    30  // Ensure that the page type can be returned in human readable format.
    31  func TestPage_typ(t *testing.T) {
    32  	if typ := (&page{flags: branchPageFlag}).typ(); typ != "branch" {
    33  		t.Fatalf("exp=branch; got=%v", typ)
    34  	}
    35  	if typ := (&page{flags: leafPageFlag}).typ(); typ != "leaf" {
    36  		t.Fatalf("exp=leaf; got=%v", typ)
    37  	}
    38  	if typ := (&page{flags: metaPageFlag}).typ(); typ != "meta" {
    39  		t.Fatalf("exp=meta; got=%v", typ)
    40  	}
    41  	if typ := (&page{flags: freelistPageFlag}).typ(); typ != "freelist" {
    42  		t.Fatalf("exp=freelist; got=%v", typ)
    43  	}
    44  	if typ := (&page{flags: 20000}).typ(); typ != "unknown<4e20>" {
    45  		t.Fatalf("exp=unknown<4e20>; got=%v", typ)
    46  	}
    47  }
    48  
    49  // Ensure that the hexdump debugging function doesn't blow up.
    50  func TestPage_dump(t *testing.T) {
    51  	(&page{id: 256}).hexdump(16)
    52  }
    53  
    54  func TestPgids_merge(t *testing.T) {
    55  	a := pgids{4, 5, 6, 10, 11, 12, 13, 27}
    56  	b := pgids{1, 3, 8, 9, 25, 30}
    57  	c := a.merge(b)
    58  	if !reflect.DeepEqual(c, pgids{1, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 25, 27, 30}) {
    59  		t.Errorf("mismatch: %v", c)
    60  	}
    61  
    62  	a = pgids{4, 5, 6, 10, 11, 12, 13, 27, 35, 36}
    63  	b = pgids{8, 9, 25, 30}
    64  	c = a.merge(b)
    65  	if !reflect.DeepEqual(c, pgids{4, 5, 6, 8, 9, 10, 11, 12, 13, 25, 27, 30, 35, 36}) {
    66  		t.Errorf("mismatch: %v", c)
    67  	}
    68  }
    69  
    70  func TestPgids_merge_quick(t *testing.T) {
    71  	if err := quick.Check(func(a, b pgids) bool {
    72  		// Sort incoming lists.
    73  		sort.Sort(a)
    74  		sort.Sort(b)
    75  
    76  		// Merge the two lists together.
    77  		got := a.merge(b)
    78  
    79  		// The expected value should be the two lists combined and sorted.
    80  		exp := append(a, b...)
    81  		sort.Sort(exp)
    82  
    83  		if !reflect.DeepEqual(exp, got) {
    84  			t.Errorf("\nexp=%+v\ngot=%+v\n", exp, got)
    85  			return false
    86  		}
    87  
    88  		return true
    89  	}, nil); err != nil {
    90  		t.Fatal(err)
    91  	}
    92  }