github.com/grailbio/base@v0.0.11/mapio/buf_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  	"math/rand"
    10  	"testing"
    11  )
    12  
    13  func TestBuf(t *testing.T) {
    14  	const N = 1000
    15  	entries := makeEntries(N)
    16  	// Shuffle to make sure the buffer sorts properly.
    17  	rand.Shuffle(len(entries), func(i, j int) {
    18  		entries[i], entries[j] = entries[j], entries[i]
    19  	})
    20  	var buf Buf
    21  	for _, e := range entries {
    22  		buf.Append(e.Key, e.Value)
    23  	}
    24  	var b bytes.Buffer
    25  	w := NewWriter(&b, BlockSize(1<<10), RestartInterval(10))
    26  	if err := buf.WriteTo(w); err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	if err := w.Close(); err != nil {
    30  		t.Fatal(err)
    31  	}
    32  	m, err := New(bytes.NewReader(b.Bytes()))
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  
    37  	testSeeker(t, entries, mapSeeker{m})
    38  }