Skip to content

Commit 47db8cb

Browse files
RoasbeefViktorT-11
authored andcommitted
gbn: simplify refContains to use uint8 wrapping like production code
In this commit, we update the reference implementation used in the brute-force property test to operate in the full uint8 space with natural wrapping, matching the production containsSequence exactly. The previous version used a mod-s space which happened to agree for small values but was a non-obvious equivalence that could silently break if the test ranges were widened.
1 parent ac3815b commit 47db8cb

1 file changed

Lines changed: 11 additions & 12 deletions

File tree

gbn/rapid_test.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,28 +100,27 @@ func TestRapidContainsSequenceProperties(t *testing.T) {
100100
})
101101
}
102102

103-
// TestRapidContainsSequenceBruteForce exhaustively validates containsSequence
104-
// against a simple reference implementation for all s values in range.
103+
// TestRapidContainsSequenceBruteForce validates containsSequence against a
104+
// simple reference implementation that enumerates the half-open interval.
105105
func TestRapidContainsSequenceBruteForce(t *testing.T) {
106106
t.Parallel()
107107

108108
rapid.Check(t, func(t *rapid.T) {
109-
// Use a small modular space to make brute force feasible.
110-
s := rapid.Uint8Range(2, 20).Draw(t, "s")
111-
base := rapid.Uint8Range(0, s-1).Draw(t, "base")
112-
top := rapid.Uint8Range(0, s-1).Draw(t, "top")
113-
seq := rapid.Uint8Range(0, s-1).Draw(t, "seq")
109+
base := rapid.Uint8().Draw(t, "base")
110+
top := rapid.Uint8().Draw(t, "top")
111+
seq := rapid.Uint8().Draw(t, "seq")
114112

115-
expected := refContains(base, top, seq, s)
113+
expected := refContains(base, top, seq)
116114
got := containsSequence(base, top, seq)
117115
require.Equal(t, expected, got,
118-
"base=%d top=%d seq=%d s=%d", base, top, seq, s)
116+
"base=%d top=%d seq=%d", base, top, seq)
119117
})
120118
}
121119

122120
// refContains is a reference implementation of sequence containment that
123-
// enumerates the half-open interval [base, top) modulo s.
124-
func refContains(base, top, seq, s uint8) bool {
121+
// enumerates the half-open interval [base, top) in uint8 space, wrapping
122+
// at 256 to match the production containsSequence behavior.
123+
func refContains(base, top, seq uint8) bool {
125124
if base == top {
126125
return false
127126
}
@@ -130,7 +129,7 @@ func refContains(base, top, seq, s uint8) bool {
130129
if cur == seq {
131130
return true
132131
}
133-
cur = (cur + 1) % s
132+
cur++ // uint8 wraps at 256, matching containsSequence.
134133
if cur == top {
135134
return false
136135
}

0 commit comments

Comments
 (0)