File: sparse_test.go

package info (click to toggle)
golang-github-retailnext-hllpp 1.0.0+git20170901.6e8b6d3-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 196 kB
  • sloc: makefile: 2
file content (71 lines) | stat: -rw-r--r-- 1,251 bytes parent folder | download | duplicates (2)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) 2015, RetailNext, Inc.
// All rights reserved.

package hllpp

import (
	"testing"
)

func TestSparseReaderWriter(t *testing.T) {
	writer := newSparseWriter()

	if writer.Len() != 0 {
		t.Errorf("got %d", writer.Len())
	}

	if len(writer.Bytes()) != 0 {
		t.Errorf("got %+v", writer.Bytes())
	}

	reader := newSparseReader(writer.Bytes())

	if !reader.Done() {
		t.Errorf("should be done")
	}

	writer.Append(127, 0, 1)
	// same idx, but bigger rho than previous, use this one
	writer.Append(126, 0, 2)

	if writer.Len() != 1 || len(writer.Bytes()) != 1 {
		t.Errorf("got %d", writer.Len())
	}

	// show we are storing deltas since 128 takes two bytes as
	// a varint
	writer.Append(128, 1, 0)
	if writer.Len() != 2 || len(writer.Bytes()) != 2 {
		t.Errorf("got %d", writer.Len())
	}

	reader = newSparseReader(writer.Bytes())

	if reader.Done() {
		t.Errorf("shouldn't be done")
	}

	if reader.Peek() != 126 {
		t.Errorf("got %d", reader.Peek())
	}

	if reader.Peek() != 126 {
		t.Errorf("got %d", reader.Peek())
	}

	reader.Advance()

	if reader.Done() {
		t.Errorf("shouldn't be done")
	}

	if reader.Peek() != 128 {
		t.Errorf("got %d", reader.Peek())
	}

	reader.Advance()

	if !reader.Done() {
		t.Errorf("should be done")
	}
}