Skip to content

Commit

Permalink
add test to catch struct size changes
Browse files Browse the repository at this point in the history
  • Loading branch information
keegancsmith committed Jan 12, 2024
1 parent 9524f2f commit 9d63fa5
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package zoekt // import "github.com/sourcegraph/zoekt"
import (
"bytes"
"encoding/gob"
"reflect"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -136,3 +137,31 @@ func TestSizeBytesChunkMatches(t *testing.T) {
t.Fatalf("want %d, got %d", wantBytes, cm.sizeBytes())
}
}

func TestMatchSize(t *testing.T) {
cases := []struct {
v any
size int
}{{
v: FileMatch{},
size: 256,
}, {
v: ChunkMatch{},
size: 112,
}, {
v: candidateMatch{},
size: 72,
}, {
v: candidateChunk{},
size: 40,
}}
for _, c := range cases {
got := reflect.TypeOf(c.v).Size()
if int(got) != c.size {
t.Errorf(`sizeof struct %T has changed from %d to %d.
These are match structs that occur a lot in memory, so we optimize size.
When changing, please ensure there isn't unnecessary padding via the
tool fieldalignment then update this test.`, c.v, c.size, got)
}
}
}

0 comments on commit 9d63fa5

Please sign in to comment.