github.com/johnathanhowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/renter/erasure_test.go (about) 1 package renter 2 3 import ( 4 "bytes" 5 "crypto/rand" 6 "io/ioutil" 7 "testing" 8 ) 9 10 // TestRSEncode tests the rsCode type. 11 func TestRSEncode(t *testing.T) { 12 badParams := []struct { 13 data, parity int 14 }{ 15 {-1, -1}, 16 {-1, 0}, 17 {0, -1}, 18 {0, 0}, 19 {0, 1}, 20 {1, 0}, 21 } 22 for _, ps := range badParams { 23 if _, err := NewRSCode(ps.data, ps.parity); err == nil { 24 t.Error("expected bad parameter error, got nil") 25 } 26 } 27 28 rsc, err := NewRSCode(10, 3) 29 if err != nil { 30 t.Fatal(err) 31 } 32 33 data := make([]byte, 777) 34 rand.Read(data) 35 36 pieces, err := rsc.Encode(data) 37 if err != nil { 38 t.Fatal(err) 39 } 40 _, err = rsc.Encode(nil) 41 if err == nil { 42 t.Fatal("expected nil data error, got nil") 43 } 44 45 buf := new(bytes.Buffer) 46 err = rsc.Recover(pieces, 777, buf) 47 if err != nil { 48 t.Fatal(err) 49 } 50 err = rsc.Recover(nil, 777, buf) 51 if err == nil { 52 t.Fatal("expected nil pieces error, got nil") 53 } 54 55 if !bytes.Equal(data, buf.Bytes()) { 56 t.Fatal("recovered data does not match original") 57 } 58 } 59 60 func BenchmarkRSEncode(b *testing.B) { 61 rsc, err := NewRSCode(80, 20) 62 if err != nil { 63 b.Fatal(err) 64 } 65 data := make([]byte, 1<<20) 66 rand.Read(data) 67 68 b.SetBytes(1 << 20) 69 b.ResetTimer() 70 for i := 0; i < b.N; i++ { 71 rsc.Encode(data) 72 } 73 } 74 75 func BenchmarkRSRecover(b *testing.B) { 76 rsc, err := NewRSCode(50, 200) 77 if err != nil { 78 b.Fatal(err) 79 } 80 data := make([]byte, 1<<20) 81 rand.Read(data) 82 pieces, err := rsc.Encode(data) 83 if err != nil { 84 b.Fatal(err) 85 } 86 87 b.SetBytes(1 << 20) 88 b.ResetTimer() 89 for i := 0; i < b.N; i++ { 90 pieces[0] = nil 91 rsc.Recover(pieces, 1<<20, ioutil.Discard) 92 } 93 }