remove "current" from receiver, add very simple serialization test

This commit is contained in:
Tadge Dryja 2016-01-18 21:04:53 -08:00
parent d4a31d4127
commit 2301af5615
4 changed files with 34 additions and 9 deletions

View File

@ -104,9 +104,13 @@ func NewElkremReceiver(th uint8) ElkremReceiver {
// Next() increments the index to the next hash and outputs it
func (e *ElkremSender) Next() (*wire.ShaHash, error) {
// increment index
sha, err := e.AtIndex(e.current)
if err != nil {
return nil, err
}
// increment index for next time
e.current++
return e.AtIndex(e.current)
return sha, nil
}
// w is the wanted index, i is the root index

View File

@ -8,10 +8,11 @@ import (
// TestElkremBig makes a height 63 (max size possible) tree and tries 10K hashes
func TestElkremBig(t *testing.T) {
ex := NewElkremSender(63, wire.DoubleSha256SH([]byte("elktest")))
sndr := NewElkremSender(63, wire.DoubleSha256SH([]byte("elktest")))
rcv := NewElkremReceiver(63)
SenderSerdesTest(t, sndr)
for n := uint64(0); n < 10000; n++ {
sha, err := ex.AtIndex(n)
sha, err := sndr.Next()
if err != nil {
t.Fatal(err)
}
@ -27,22 +28,23 @@ func TestElkremBig(t *testing.T) {
}
}
}
SenderSerdesTest(t, sndr)
ReceiverSerdesTest(t, rcv)
for n := uint64(0); n < 10000; n += 500 {
sha, err := rcv.AtIndex(n)
if err != nil {
t.Fatal(err)
}
t.Logf("Retreived index %d %s\n",
n, sha.String())
t.Logf("Retreived index %d %s\n", n, sha.String())
}
}
// TestElkremSmall makes a height 15 (65534 size) tree and tries 10K hashes
func TestElkremSmall(t *testing.T) {
ex := NewElkremSender(15, wire.DoubleSha256SH([]byte("elktest")))
sndr := NewElkremSender(15, wire.DoubleSha256SH([]byte("elktest")))
rcv := NewElkremReceiver(15)
for n := uint64(0); n < 5000; n++ {
sha, err := ex.AtIndex(n)
sha, err := sndr.Next()
if err != nil {
t.Fatal(err)
}

View File

@ -38,7 +38,7 @@ func (e *ElkremSender) ToBytes() ([]byte, error) {
return nil, fmt.Errorf("%d byte hash, expect 32", n)
}
return nil, nil
return buf.Bytes(), nil
}
// ElkremSenderFromBytes turns a 41 byte slice into a sender, picking up at

19
elkrem/serdes_test.go Normal file
View File

@ -0,0 +1,19 @@
package elkrem
import "testing"
func ReceiverSerdesTest(t *testing.T, er ElkremReceiver) {
b, err := er.ToBytes()
if err != nil {
t.Fatal(err)
}
t.Logf("Serialized receiver; %d bytes, hex:\n%x\n", len(b), b)
}
func SenderSerdesTest(t *testing.T, es ElkremSender) {
b, err := es.ToBytes()
if err != nil {
t.Fatal(err)
}
t.Logf("Serialized sender; %d bytes, hex:\n%x\n", len(b), b)
}