Skip to content

Commit

Permalink
Simple tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lalinsky committed Feb 11, 2024
1 parent 83b9282 commit 96bb3e3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
4 changes: 2 additions & 2 deletions fpstore/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func ExtractLegacyQuery(fp *pb.Fingerprint) []uint32 {
const QuerySize = 120
const QueryStart = 80

const NumQueryBits = 26
const NumQueryBits = 28
const QueryBitMask = ((1 << NumQueryBits) - 1) << (32 - NumQueryBits)

const SilenceHash = 627964279
Expand Down Expand Up @@ -65,7 +65,7 @@ func ExtractLegacyQuery(fp *pb.Fingerprint) []uint32 {

func filterIndexSearchResults(results []*index_pb.Result, limit int) []*index_pb.Result {
sort.Slice(results, func(i, j int) bool {
return results[i].Hits >= results[j].Hits && results[i].Id < results[j].Id
return results[i].Hits > results[j].Hits || (results[i].Hits == results[j].Hits && results[i].Id < results[j].Id)
})
if limit == 0 || len(results) > limit {
threshold := (results[0].Hits*10 + 50) / 100
Expand Down
65 changes: 65 additions & 0 deletions fpstore/index_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package fpstore

import (
"testing"

pb "github.com/acoustid/go-acoustid/proto/fpstore"
index_pb "github.com/acoustid/go-acoustid/proto/index"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFilterIndexSearchResults_1(t *testing.T) {
results := []*index_pb.Result{
{Hits: 1, Id: 1},
{Hits: 2, Id: 2},
{Hits: 3, Id: 3},
{Hits: 4, Id: 4},
}

filtered := filterIndexSearchResults(results, 2)
require.Equal(t, 2, len(filtered))
assert.Equal(t, uint32(4), filtered[0].Id)
assert.Equal(t, uint32(3), filtered[1].Id)
}

func TestFilterIndexSearchResults_2(t *testing.T) {
results := []*index_pb.Result{
{Hits: 1, Id: 1},
{Hits: 50, Id: 2},
{Hits: 3, Id: 3},
{Hits: 100, Id: 4},
}

filtered := filterIndexSearchResults(results, 2)
require.Equal(t, 2, len(filtered))
assert.Equal(t, uint32(4), filtered[0].Id)
assert.Equal(t, uint32(2), filtered[1].Id)
}

func TestFilterIndexSearchResults_3(t *testing.T) {
results := []*index_pb.Result{
{Hits: 1, Id: 1},
{Hits: 2, Id: 2},
{Hits: 3, Id: 3},
{Hits: 100, Id: 4},
}

filtered := filterIndexSearchResults(results, 2)
require.Equal(t, 1, len(filtered))
assert.Equal(t, uint32(4), filtered[0].Id)
}

func signedToUnsignedHashes(signedHashes []int32) []uint32 {
hashes := make([]uint32, len(signedHashes))
for i, hash := range signedHashes {
hashes[i] = uint32(hash)
}
return hashes
}

func TestExtractLegacyQuery(t *testing.T) {
fp := &pb.Fingerprint{Hashes: signedToUnsignedHashes([]int32{506444819, 1064287265, 1055763488, 2142086176, 2137886720, 2100269056, -63994816, -64039456})}
query := ExtractLegacyQuery(fp)
assert.Equal(t, signedToUnsignedHashes([]int32{506444816, 1064287264, 1055763488, 2142086176, 2137886720, 2100269056, -63994816, -64039456}), query)
}

0 comments on commit 96bb3e3

Please sign in to comment.