fix negentropy tests.

This commit is contained in:
fiatjaf
2025-04-20 11:19:57 -03:00
parent 15c6093c9b
commit ee2d618285
2 changed files with 17 additions and 15 deletions

View File

@@ -1,6 +1,7 @@
package negentropy_test
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"fmt"
@@ -29,10 +30,10 @@ func FuzzWhatever(f *testing.F) {
// prepare the two sides
s1 := vector.New()
l1 := make([]string, 0, 500)
l1 := make([]nostr.ID, 0, 500)
neg1 := negentropy.New(s1, int(frameSizeLimit))
s2 := vector.New()
l2 := make([]string, 0, 500)
l2 := make([]nostr.ID, 0, 500)
neg2 := negentropy.New(s2, int(frameSizeLimit))
start := 0
@@ -47,14 +48,14 @@ func FuzzWhatever(f *testing.F) {
item := start + i
rnd := sha256.Sum256(binary.BigEndian.AppendUint64(nil, uint64(item)))
id := fmt.Sprintf("%x%056d", rnd[0:4], item)
id := nostr.MustIDFromHex(fmt.Sprintf("%x%056d", rnd[0:4], item))
if rand.IntN(100) < int(pctChance) {
s1.Insert(nostr.Timestamp(item), id)
l1 = append(l1, id)
}
if rand.IntN(100) < int(pctChance) {
id := fmt.Sprintf("%064d", item)
id := nostr.MustIDFromHex(fmt.Sprintf("%064d", item))
s2.Insert(nostr.Timestamp(item), id)
l2 = append(l2, id)
}
@@ -106,9 +107,9 @@ func FuzzWhatever(f *testing.F) {
}
wg.Wait()
slices.Sort(l1)
slices.SortFunc(l1, func(a, b nostr.ID) int { return bytes.Compare(a[:], b[:]) })
l1 = slices.Compact(l1)
slices.Sort(l2)
slices.SortFunc(l2, func(a, b nostr.ID) int { return bytes.Compare(a[:], b[:]) })
l2 = slices.Compact(l2)
require.ElementsMatch(t, l1, l2)
})

View File

@@ -1,6 +1,7 @@
package negentropy_test
import (
"bytes"
"fmt"
"slices"
"sync"
@@ -64,14 +65,14 @@ func runTestWith(t *testing.T,
var n1 *negentropy.Negentropy
var n2 *negentropy.Negentropy
events := make([]*nostr.Event, totalEvents)
events := make([]nostr.Event, totalEvents)
for i := range events {
evt := nostr.Event{}
evt.Content = fmt.Sprintf("event %d", i)
evt.Kind = 1
evt.CreatedAt = nostr.Timestamp(i)
evt.ID = fmt.Sprintf("%064d", i)
events[i] = &evt
evt.ID = nostr.MustIDFromHex(fmt.Sprintf("%064d", i))
events[i] = evt
}
{
@@ -115,39 +116,39 @@ func runTestWith(t *testing.T,
go func() {
defer wg.Done()
expectedHave := make([]string, 0, 100)
expectedHave := make([]nostr.ID, 0, 100)
for _, r := range expectedN1HaveRanges {
for i := r[0]; i < r[1]; i++ {
expectedHave = append(expectedHave, events[i].ID)
}
}
haves := make([]string, 0, 100)
haves := make([]nostr.ID, 0, 100)
for item := range n1.Haves {
if slices.Contains(haves, item) {
continue
}
haves = append(haves, item)
}
slices.Sort(haves)
slices.SortFunc(haves, func(a, b nostr.ID) int { return bytes.Compare(a[:], b[:]) })
require.Equal(t, expectedHave, haves, "wrong have")
}()
go func() {
defer wg.Done()
expectedNeed := make([]string, 0, 100)
expectedNeed := make([]nostr.ID, 0, 100)
for _, r := range expectedN1NeedRanges {
for i := r[0]; i < r[1]; i++ {
expectedNeed = append(expectedNeed, events[i].ID)
}
}
havenots := make([]string, 0, 100)
havenots := make([]nostr.ID, 0, 100)
for item := range n1.HaveNots {
if slices.Contains(havenots, item) {
continue
}
havenots = append(havenots, item)
}
slices.Sort(havenots)
slices.SortFunc(havenots, func(a, b nostr.ID) int { return bytes.Compare(a[:], b[:]) })
require.Equal(t, expectedNeed, havenots, "wrong need")
}()