File: io_test.go

package info (click to toggle)
golang-github-dcso-bloom 0.2.4-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 200 kB
  • sloc: makefile: 36
file content (118 lines) | stat: -rw-r--r-- 2,332 bytes parent folder | download | duplicates (4)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// DCSO go bloom filter
// Copyright (c) 2017, DCSO GmbH

package bloom

import (
	"io/ioutil"
	"os"
	"regexp"
	"testing"
)

func checkResults(t *testing.T, bf *BloomFilter) {
	for _, v := range []string{"foo", "bar", "baz"} {
		if !bf.Check([]byte(v)) {
			t.Fatalf("value %s expected in filter but wasn't found", v)
		}
	}
	if bf.Check([]byte("")) {
		t.Fatal("empty value not expected in filter but was found")
	}
	if bf.Check([]byte("12345")) {
		t.Fatal("missing value not expected in filter but was found")
	}
}

func TestFromReaderFile(t *testing.T) {
	f, err := os.Open("testdata/test.bloom")
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()
	bf, err := LoadFromReader(f, false)
	if err != nil {
		t.Fatal(err)
	}
	checkResults(t, bf)
}

func TestFromReaderCorruptFile(t *testing.T) {
	f, err := os.Open("testdata/broken.bloom")
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()
	_, err = LoadFromReader(f, false)
	if err == nil {
		t.Fatal("error expected")
	}
	r, _ := regexp.Compile("is too high")
	if !r.MatchString(err.Error()) {
		t.Fatalf("wrong error message: %s", err.Error())
	}
}

func testFromSerialized(t *testing.T, gzip bool) {
	bf := Initialize(100, 0.0001)
	for _, v := range []string{"foo", "bar", "baz"} {
		bf.Add([]byte(v))
	}
	tmpfile, err := ioutil.TempFile("", "test")
	if err != nil {
		t.Fatal(err)
	}
	defer os.Remove(tmpfile.Name())

	err = WriteFilter(&bf, tmpfile.Name(), gzip)
	if err != nil {
		t.Fatal(err)
	}

	loadedBf, err := LoadFilter(tmpfile.Name(), gzip)
	if err != nil {
		t.Fatal(err)
	}
	checkResults(t, loadedBf)
}

func TestFromSerialized(t *testing.T) {
	testFromSerialized(t, false)
}

func TestFromSerializedZip(t *testing.T) {
	testFromSerialized(t, true)
}

func TestFromReaderFileZip(t *testing.T) {
	f, err := os.Open("testdata/test.bloom.gz")
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()
	bf, err := LoadFromReader(f, true)
	if err != nil {
		t.Fatal(err)
	}
	checkResults(t, bf)
}

func TestFromBytes(t *testing.T) {
	testBytes, err := ioutil.ReadFile("testdata/test.bloom")
	if err != nil {
		t.Fatal(err)
	}
	bf, err := LoadFromBytes(testBytes, false)
	if err != nil {
		t.Fatal(err)
	}
	checkResults(t, bf)
}

func TestFromFile(t *testing.T) {
	bf, err := LoadFilter("testdata/test.bloom", false)
	if err != nil {
		t.Fatal(err)
	}
	checkResults(t, bf)
}