File: getter_test.go

package info (click to toggle)
golang-github-vbatts-tar-split 0.11.6-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 744 kB
  • sloc: makefile: 2
file content (84 lines) | stat: -rw-r--r-- 1,970 bytes parent folder | download
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
package storage

import (
	"bytes"
	"fmt"
	"io"
	"strings"
	"testing"
)

func TestGetter(t *testing.T) {
	fgp := NewBufferFileGetPutter()
	files := map[string]map[string][]byte{
		"file1.txt": {"foo": []byte{60, 60, 48, 48, 0, 0, 0, 0}},
		"file2.txt": {"bar": []byte{45, 196, 22, 240, 0, 0, 0, 0}},
	}
	for n, b := range files {
		for body, sum := range b {
			_, csum, err := fgp.Put(n, bytes.NewBufferString(body))
			if err != nil {
				t.Error(err)
			}
			if !bytes.Equal(csum, sum) {
				t.Errorf("checksum: expected 0x%x; got 0x%x", sum, csum)
			}
		}
	}
	for n, b := range files {
		for body := range b {
			r, err := fgp.Get(n)
			if err != nil {
				t.Error(err)
			}
			buf, err := io.ReadAll(r)
			if err != nil {
				t.Error(err)
			}
			if body != string(buf) {
				t.Errorf("expected %q, got %q", body, string(buf))
			}
		}
	}
}

func TestPutter(t *testing.T) {
	fp := NewDiscardFilePutter()
	// map[filename]map[body]crc64sum
	files := map[string]map[string][]byte{
		"file1.txt": {"foo": []byte{60, 60, 48, 48, 0, 0, 0, 0}},
		"file2.txt": {"bar": []byte{45, 196, 22, 240, 0, 0, 0, 0}},
		"file3.txt": {"baz": []byte{32, 68, 22, 240, 0, 0, 0, 0}},
		"file4.txt": {"bif": []byte{48, 9, 150, 240, 0, 0, 0, 0}},
	}
	for n, b := range files {
		for body, sum := range b {
			_, csum, err := fp.Put(n, bytes.NewBufferString(body))
			if err != nil {
				t.Error(err)
			}
			if !bytes.Equal(csum, sum) {
				t.Errorf("checksum on %q: expected %v; got %v", n, sum, csum)
			}
		}
	}
}

func BenchmarkPutter(b *testing.B) {
	files := []string{
		strings.Repeat("foo", 1000),
		strings.Repeat("bar", 1000),
		strings.Repeat("baz", 1000),
		strings.Repeat("fooz", 1000),
		strings.Repeat("vbatts", 1000),
		strings.Repeat("systemd", 1000),
	}
	for i := 0; i < b.N; i++ {
		fgp := NewBufferFileGetPutter()
		for n, body := range files {
			if _, _, err := fgp.Put(fmt.Sprintf("%d", n), bytes.NewBufferString(body)); err != nil {
				b.Fatal(err)
			}
		}
	}
}