github.com/grailbio/base@v0.0.11/mapio/map_test.go (about)

     1  // Copyright 2018 GRAIL, Inc. All rights reserved.
     2  // Use of this source code is governed by the Apache 2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  package mapio
     6  
     7  import (
     8  	"bytes"
     9  	"testing"
    10  )
    11  
    12  type mapSeeker struct{ *Map }
    13  
    14  func (m mapSeeker) Seek(key []byte) Scanner { return m.Map.Seek(key) }
    15  
    16  func TestMap(t *testing.T) {
    17  	const N = 15000
    18  	entries := makeEntries(N)
    19  
    20  	var b bytes.Buffer
    21  	w := NewWriter(&b, BlockSize(1024))
    22  	for i := range entries {
    23  		if err := w.Append(entries[i].Key, entries[i].Value); err != nil {
    24  			t.Fatal(err)
    25  		}
    26  	}
    27  	if err := w.Close(); err != nil {
    28  		t.Fatal(err)
    29  	}
    30  
    31  	m, err := New(bytes.NewReader(b.Bytes()))
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	testSeeker(t, entries, mapSeeker{m})
    37  }
    38  
    39  func TestEmptyMap(t *testing.T) {
    40  	var b bytes.Buffer
    41  	w := NewWriter(&b, BlockSize(1024))
    42  	// Flush to get an extra (empty) block.
    43  	if err := w.Flush(); err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	if err := w.Close(); err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	m, err := New(bytes.NewReader(b.Bytes()))
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  
    55  	scan := m.Seek(nil)
    56  	if scan.Scan() {
    57  		t.Error("expected EOF")
    58  	}
    59  	if err := scan.Err(); err != nil {
    60  		t.Error(err)
    61  	}
    62  }