Skip to content

Commit

Permalink
Unit test ticket quality computation
Browse files Browse the repository at this point in the history
Test ticket quality
* is weighed by power
* is as expected for edge case power value
* handles unexpected ticket length gracefully

Part of #9
  • Loading branch information
masih committed Sep 5, 2024
1 parent 2dd4f5d commit b3eb360
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion gpbft/ticket_quality_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package gpbft

import (
"bytes"
"math"
"math/big"
"runtime"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/exp/rand"
)

func TestTQ_BigLog2_Table(t *testing.T) {
Expand All @@ -21,6 +23,7 @@ func TestTQ_BigLog2_Table(t *testing.T) {
{"0.(9)8", "fffffffffffff8000000000000000000", -1, 0.9999999999999999},
{"0.(9)7", "fffffffffffff7000000000000000000", -1, 0.9999999999999997},
{"0.5", "80000000000000000000000000000000", -1, 0.0},
{"2^-129", "0", -129, 0.0},
{"2^-128", "1", -128, 0.0},
{"2^-127", "2", -127, 0.0},
{"2^-127 + eps", "3", -127, 0.5849625007211563},
Expand Down Expand Up @@ -49,7 +52,7 @@ func TestTQ_BigLog2_Table(t *testing.T) {
}
}

func FuzzTQ_linearToExp(f *testing.F) {
func FuzzTQ_LinearToExp(f *testing.F) {
f.Add(make([]byte, 16))
f.Add(bytes.Repeat([]byte{0xff}, 16))
f.Add(bytes.Repeat([]byte{0xa0}, 16))
Expand All @@ -61,3 +64,56 @@ func FuzzTQ_linearToExp(f *testing.F) {
runtime.KeepAlive(q)
})
}

func TestComputeTicketQuality(t *testing.T) {
t.Run("Non-zero for non-zero power", func(t *testing.T) {
ticket := generateTicket(t)
power := int64(10)
quality := ComputeTicketQuality(ticket, power)
require.Greater(t, quality, 0.0, "Expected positive quality value, got %f", quality)
})

t.Run("Weighed by power", func(t *testing.T) {
ticket := generateTicket(t)
quality1 := ComputeTicketQuality(ticket, 10)
quality2 := ComputeTicketQuality(ticket, 11)
require.Less(t, quality2, quality1, "Expected quality2 to be less than quality1 due to weight by power, got quality1=%f, quality2=%f", quality1, quality2)
})

t.Run("Zero power is handled gracefully", func(t *testing.T) {
ticket := generateTicket(t)
quality := ComputeTicketQuality(ticket, 0)
require.True(t, math.IsInf(quality, 1), "Expected quality to be infinity with power 0, got %f", quality)
})

t.Run("Negative power is handled gracefully", func(t *testing.T) {
ticket := generateTicket(t)
quality := ComputeTicketQuality(ticket, -5)
require.Negative(t, quality, "Expected negative quality for negative power, got %f", quality)
})

t.Run("Different tickets should have different qualities", func(t *testing.T) {
quality1 := ComputeTicketQuality(generateTicket(t), 1413)
quality2 := ComputeTicketQuality(generateTicket(t), 1413)
require.NotEqual(t, quality1, quality2, "Expected different qualities for different tickets, got quality1=%f, quality2=%f", quality1, quality2)
})

t.Run("Tickets with same 16 byte prefix should different quality", func(t *testing.T) {
prefix := generateTicket(t)
ticket1 := append(prefix, 14)
ticket2 := append(prefix, 13)
require.NotEqual(t, ticket1, ticket2)

quality1 := ComputeTicketQuality(ticket1, 1413)
quality2 := ComputeTicketQuality(ticket2, 1413)
require.NotEqual(t, quality1, quality2, "Expected different qualities for different tickets with the same 16 byte prefix, got quality1=%f, quality2=%f", quality1, quality2)
})
}

func generateTicket(t *testing.T) []byte {
var ticket [16]byte
n, err := rand.Read(ticket[:])
require.NoError(t, err)
require.Equal(t, 16, n)
return ticket[:]
}

0 comments on commit b3eb360

Please sign in to comment.