@@ -129,27 +129,32 @@ func TestValidateRange(t *testing.T) {
129129 h3 := LineHash(lines[2])
130130
131131 // Valid range
132- if err := ValidateRange(Anchor{1, h1}, Anchor{3, h3}, lines); err != nil {
132+ s, e := Anchor{1, h1}, Anchor{3, h3}
133+ if err := ValidateRange(lines, &s, &e); err != nil {
133134 t.Errorf("valid range failed: %v", err)
134135 }
135136
136137 // Single line range
137- if err := ValidateRange(Anchor{2, h2}, Anchor{2, h2}, lines); err != nil {
138+ s2, e2 := Anchor{2, h2}, Anchor{2, h2}
139+ if err := ValidateRange(lines, &s2, &e2); err != nil {
138140 t.Errorf("single line range failed: %v", err)
139141 }
140142
141143 // Inverted range
142- if err := ValidateRange(Anchor{3, h3}, Anchor{1, h1}, lines); err == nil {
144+ s3, e3 := Anchor{3, h3}, Anchor{1, h1}
145+ if err := ValidateRange(lines, &s3, &e3); err == nil {
143146 t.Error("inverted range should fail")
144147 }
145148
146149 // Bad start hash
147- if err := ValidateRange(Anchor{1, "ff"}, Anchor{3, h3}, lines); err == nil {
150+ s4, e4 := Anchor{1, "ff"}, Anchor{3, h3}
151+ if err := ValidateRange(lines, &s4, &e4); err == nil {
148152 t.Error("bad start hash should fail")
149153 }
150154
151155 // Bad end hash
152- if err := ValidateRange(Anchor{1, h1}, Anchor{3, "ff"}, lines); err == nil {
156+ s5, e5 := Anchor{1, h1}, Anchor{3, "ff"}
157+ if err := ValidateRange(lines, &s5, &e5); err == nil {
153158 t.Error("bad end hash should fail")
154159 }
155160 }
@@ -189,3 +194,57 @@ func TestParseAnchor(t *testing.T) {
189194 }
190195 }
191196 }
197+
198+func TestAnchorRelocate(t *testing.T) {
199+ lines := []string{"alpha", "beta", "gamma", "delta"}
200+ betaHash := LineHash("beta")
201+
202+ // Wrong line number, unique hash → relocate succeeds.
203+ a := Anchor{Num: 4, Hash: betaHash}
204+ if err := a.Validate(lines); err != nil {
205+ t.Fatalf("expected relocation, got error: %v", err)
206+ }
207+ if a.Num != 2 {
208+ t.Errorf("expected relocated to line 2, got %d", a.Num)
209+ }
210+
211+ // Duplicate hash → relocation fails.
212+ dupes := []string{"same", "other", "same"}
213+ sameHash := LineHash("same")
214+ a2 := Anchor{Num: 2, Hash: sameHash}
215+ if err := a2.Validate(dupes); err == nil {
216+ t.Error("expected error for ambiguous hash")
217+ }
218+
219+ // Out of range but relocatable.
220+ a3 := Anchor{Num: 99, Hash: betaHash}
221+ if err := a3.Validate(lines); err != nil {
222+ t.Fatalf("expected relocation for out-of-range, got: %v", err)
223+ }
224+ if a3.Num != 2 {
225+ t.Errorf("expected relocated to line 2, got %d", a3.Num)
226+ }
227+}
228+
229+func TestValidateRangeRelocate(t *testing.T) {
230+ lines := []string{"alpha", "beta", "gamma", "delta"}
231+ betaHash := LineHash("beta")
232+ gammaHash := LineHash("gamma")
233+
234+ // Both anchors have wrong line numbers but unique hashes.
235+ start := Anchor{Num: 10, Hash: betaHash}
236+ end := Anchor{Num: 11, Hash: gammaHash}
237+ if err := ValidateRange(lines, &start, &end); err != nil {
238+ t.Fatalf("expected range relocation, got: %v", err)
239+ }
240+ if start.Num != 2 || end.Num != 3 {
241+ t.Errorf("expected 2..3, got %d..%d", start.Num, end.Num)
242+ }
243+
244+ // Relocated range inverted → error.
245+ s2 := Anchor{Num: 10, Hash: gammaHash}
246+ e2 := Anchor{Num: 11, Hash: betaHash}
247+ if err := ValidateRange(lines, &s2, &e2); err == nil {
248+ t.Error("expected error for inverted relocated range")
249+ }
250+}