remove "current" from receiver, add very simple serialization test
This commit is contained in:
parent
d4a31d4127
commit
2301af5615
@ -104,9 +104,13 @@ func NewElkremReceiver(th uint8) ElkremReceiver {
|
|||||||
|
|
||||||
// Next() increments the index to the next hash and outputs it
|
// Next() increments the index to the next hash and outputs it
|
||||||
func (e *ElkremSender) Next() (*wire.ShaHash, error) {
|
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++
|
e.current++
|
||||||
return e.AtIndex(e.current)
|
return sha, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// w is the wanted index, i is the root index
|
// w is the wanted index, i is the root index
|
||||||
|
@ -8,10 +8,11 @@ import (
|
|||||||
|
|
||||||
// TestElkremBig makes a height 63 (max size possible) tree and tries 10K hashes
|
// TestElkremBig makes a height 63 (max size possible) tree and tries 10K hashes
|
||||||
func TestElkremBig(t *testing.T) {
|
func TestElkremBig(t *testing.T) {
|
||||||
ex := NewElkremSender(63, wire.DoubleSha256SH([]byte("elktest")))
|
sndr := NewElkremSender(63, wire.DoubleSha256SH([]byte("elktest")))
|
||||||
rcv := NewElkremReceiver(63)
|
rcv := NewElkremReceiver(63)
|
||||||
|
SenderSerdesTest(t, sndr)
|
||||||
for n := uint64(0); n < 10000; n++ {
|
for n := uint64(0); n < 10000; n++ {
|
||||||
sha, err := ex.AtIndex(n)
|
sha, err := sndr.Next()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
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 {
|
for n := uint64(0); n < 10000; n += 500 {
|
||||||
sha, err := rcv.AtIndex(n)
|
sha, err := rcv.AtIndex(n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
t.Logf("Retreived index %d %s\n",
|
t.Logf("Retreived index %d %s\n", n, sha.String())
|
||||||
n, sha.String())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestElkremSmall makes a height 15 (65534 size) tree and tries 10K hashes
|
// TestElkremSmall makes a height 15 (65534 size) tree and tries 10K hashes
|
||||||
func TestElkremSmall(t *testing.T) {
|
func TestElkremSmall(t *testing.T) {
|
||||||
ex := NewElkremSender(15, wire.DoubleSha256SH([]byte("elktest")))
|
sndr := NewElkremSender(15, wire.DoubleSha256SH([]byte("elktest")))
|
||||||
rcv := NewElkremReceiver(15)
|
rcv := NewElkremReceiver(15)
|
||||||
for n := uint64(0); n < 5000; n++ {
|
for n := uint64(0); n < 5000; n++ {
|
||||||
sha, err := ex.AtIndex(n)
|
sha, err := sndr.Next()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ func (e *ElkremSender) ToBytes() ([]byte, error) {
|
|||||||
return nil, fmt.Errorf("%d byte hash, expect 32", n)
|
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
|
// ElkremSenderFromBytes turns a 41 byte slice into a sender, picking up at
|
||||||
|
19
elkrem/serdes_test.go
Normal file
19
elkrem/serdes_test.go
Normal 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)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user