-
Notifications
You must be signed in to change notification settings - Fork 0
/
shard_test.go
79 lines (72 loc) · 1.96 KB
/
shard_test.go
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
package kidwords
import (
"bytes"
"compress/gzip"
"io"
"os"
"strings"
"testing"
)
func TestSplit(t *testing.T) {
shards, err := Split("somethingElse", 12, 8)
if err != nil {
t.Fatal(err)
}
if _, err = shards.Grid(6, 18).Write(os.Stdout); err != nil {
t.Fatal(err)
}
if err = shards.WriteHTML(os.Stdout, 3); err != nil {
t.Fatal(err)
}
// t.Fatal("show")
}
func compress(r io.Reader) ([]byte, error) {
b := &bytes.Buffer{}
zr, err := gzip.NewWriterLevel(b, gzip.BestCompression)
if err != nil {
return nil, err
}
if _, err = io.Copy(zr, r); err != nil {
return nil, err
}
if err = zr.Flush(); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func compressionRatio(originalLen, compressedLen int) float32 {
return float32(compressedLen) / float32(originalLen)
}
func TestProveCompressionIsPointless(t *testing.T) {
random := "jaksdASIUY3298sakhj*@&SkjASDjkhndsj"
compressed, err := compress(strings.NewReader(random))
if err != nil {
t.Fatal(err)
}
if r := compressionRatio(len(random), len(compressed)); r < .9 {
t.Fatalf("compression ratio turned out to be favorable: %.2f", r)
} else {
t.Logf("unfavorable compression ratio: %.2f", r)
}
bip39key := "goddess return math panther sustain black fatigue tortoise vast steel fiction scare"
compressed, err = compress(strings.NewReader(bip39key))
if err != nil {
t.Fatal(err)
}
if r := compressionRatio(len(bip39key), len(compressed)); r < .9 {
t.Fatalf("compression ratio turned out to be favorable: %.2f", r)
} else {
t.Logf("unfavorable compression ratio: %.2f", r)
}
bip39key = "swear expand tourist debris drink found word adjust skin input hawk extend"
compressed, err = compress(strings.NewReader(bip39key))
if err != nil {
t.Fatal(err)
}
if r := compressionRatio(len(bip39key), len(compressed)); r < .9 {
t.Fatalf("compression ratio turned out to be favorable: %.2f", r)
} else {
t.Logf("unfavorable compression ratio: %.2f", r)
}
// t.Fatal("compressed poorly")
}