1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
package bigcache
import (
"testing"
"time"
)
func TestEncodeDecode(t *testing.T) {
// given
now := uint64(time.Now().Unix())
hash := uint64(42)
key := "key"
data := []byte("data")
buffer := make([]byte, 100)
// when
wrapped := wrapEntry(now, hash, key, data, &buffer)
// then
assertEqual(t, key, readKeyFromEntry(wrapped))
assertEqual(t, hash, readHashFromEntry(wrapped))
assertEqual(t, now, readTimestampFromEntry(wrapped))
assertEqual(t, data, readEntry(wrapped))
assertEqual(t, 100, len(buffer))
}
func TestAllocateBiggerBuffer(t *testing.T) {
//given
now := uint64(time.Now().Unix())
hash := uint64(42)
key := "1"
data := []byte("2")
buffer := make([]byte, 1)
// when
wrapped := wrapEntry(now, hash, key, data, &buffer)
// then
assertEqual(t, key, readKeyFromEntry(wrapped))
assertEqual(t, hash, readHashFromEntry(wrapped))
assertEqual(t, now, readTimestampFromEntry(wrapped))
assertEqual(t, data, readEntry(wrapped))
assertEqual(t, 2+headersSizeInBytes, len(buffer))
}
|