Skip to content

Commit

Permalink
chore: add more test cases for the compact share splitter (#1295)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmwaters authored Jan 26, 2023
1 parent a760323 commit 2afaa65
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions pkg/shares/split_compact_shares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ func TestCount(t *testing.T) {
testCases := []testCase{
{transactions: []coretypes.Tx{}, wantShareCount: 0},
{transactions: []coretypes.Tx{[]byte{0}}, wantShareCount: 1},
{transactions: []coretypes.Tx{bytes.Repeat([]byte{0}, 100)}, wantShareCount: 1},
{transactions: []coretypes.Tx{bytes.Repeat([]byte{0}, appconsts.ContinuationCompactShareContentSize+1)}, wantShareCount: 2},
{transactions: []coretypes.Tx{bytes.Repeat([]byte{0}, appconsts.ContinuationCompactShareContentSize*2+1)}, wantShareCount: 3},
{transactions: []coretypes.Tx{bytes.Repeat([]byte{1}, 100)}, wantShareCount: 1},
// Test with 1 byte over 1 share
{transactions: []coretypes.Tx{bytes.Repeat([]byte{1}, rawTxSize(appconsts.FirstCompactShareContentSize+1))}, wantShareCount: 2},
{transactions: []coretypes.Tx{generateTx(1)}, wantShareCount: 1},
{transactions: []coretypes.Tx{generateTx(2)}, wantShareCount: 2},
{transactions: []coretypes.Tx{generateTx(20)}, wantShareCount: 20},
}
for _, tc := range testCases {
css := NewCompactShareSplitter(appconsts.TxNamespaceID, appconsts.ShareVersionZero)
Expand All @@ -33,6 +36,25 @@ func TestCount(t *testing.T) {
}
}

// generateTx generates a transaction that occupies exactly numShares number of
// shares.
func generateTx(numShares int) coretypes.Tx {
if numShares == 0 {
return coretypes.Tx{}
}
if numShares == 1 {
return bytes.Repeat([]byte{1}, rawTxSize(appconsts.FirstCompactShareContentSize))
}
return bytes.Repeat([]byte{1}, rawTxSize(appconsts.FirstCompactShareContentSize+(numShares-1)*appconsts.ContinuationCompactShareContentSize))
}

// rawTxSize returns the raw tx size that can be used to construct a
// tx of desiredSize bytes. This function is useful in tests to account for
// the length delimiter that is prefixed to a tx.
func rawTxSize(desiredSize int) int {
return desiredSize - DelimLen(uint64(desiredSize))
}

func TestExport(t *testing.T) {
type testCase struct {
name string
Expand Down

0 comments on commit 2afaa65

Please sign in to comment.