Skip to content
quarry Artifact [7e23c14f2f]
Login

Artifact [7e23c14f2f]

Artifact 7e23c14f2fc80cb51f29cb2a791c8d0ca2b449868a64d91f33594f8fae22be1b:


package diff

import (
	"strings"
	"testing"
)

// Windows are contiguous and chain: following next from 0 visits every
// hunk once, in order, and LastWindow names the final link.
func TestWindowChainIsContiguous(t *testing.T) {
	mk := func(sizes ...int) []FileDiff {
		var f FileDiff
		for _, n := range sizes {
			f.Hunks = append(f.Hunks, Hunk{Lines: make([]Line, n)})
		}
		return []FileDiff{f}
	}
	sizes := []int{10, 50, 5, 5, 70, 5}
	const max = 40
	last := LastWindow(mk(sizes...), max)
	total := 0
	for _, n := range sizes {
		total += n
	}
	off, seen, windows := 0, 0, 0
	for {
		files := mk(sizes...)
		start, kept, after := Window(files, off, max)
		if kept == 0 || start != off {
			t.Fatalf("window at offset %d: start=%d kept=%d", off, start, kept)
		}
		if off+kept+after != total {
			t.Fatalf("offset %d: %d+%d+%d != %d", off, off, kept, after, total)
		}
		seen += kept
		windows++
		if after == 0 {
			break
		}
		off += kept
	}
	if seen != total {
		t.Fatalf("chain saw %d of %d lines", seen, total)
	}
	if off != last {
		t.Fatalf("LastWindow=%d, chain ended at %d", last, off)
	}
	if windows != 5 { // [10] [50] [5 5] [70] [5]
		t.Fatalf("got %d windows", windows)
	}
	// A mid-hunk offset snaps back to that hunk's boundary and the
	// counts still sum to the whole.
	start, kept, after := Window(mk(sizes...), 12, max)
	if start != 10 || start+kept+after != total {
		t.Fatalf("mid-hunk offset 12: start=%d kept=%d after=%d", start, kept, after)
	}
}

// No window is bigger than its budget, however the change is shaped:
// one hunk of thousands of lines, or lines of generated data far longer than
// any reader reads. Following next from 0 still visits every line once,
// LastWindow still names the final link, and the line numbers carry on
// across the cuts.
func TestAWindowIsBoundedInLinesAndBytes(t *testing.T) {
	long := strings.Repeat("x", 3*maxLineBytes)
	mk := func() []FileDiff {
		h := Hunk{OldStart: 1, NewStart: 1, Header: "h"}
		for i := 0; i < 7000; i++ {
			text := "row"
			if i%2 == 0 {
				text = long
			}
			h.Lines = append(h.Lines, Line{Kind: "addition", NewNum: i + 1, Tokens: []Token{{Text: text}}})
		}
		return []FileDiff{{Path: "compare.txt", Hunks: []Hunk{h}}}
	}
	const max = 3000
	last := LastWindow(mk(), max)
	off, seen, next := 0, 0, 1
	for {
		files := mk()
		start, kept, after := Window(files, off, max)
		if start != off || kept == 0 || kept > max {
			t.Fatalf("FAIL: window at %d: start=%d kept=%d", off, start, kept)
		}
		text := 0
		for _, h := range files[0].Hunks {
			for _, l := range h.Lines {
				if l.NewNum != next {
					t.Fatalf("FAIL: window at %d: line numbered %d, want %d", off, l.NewNum, next)
				}
				next++
				text += len(l.Tokens[0].Text)
				if n := len(l.Tokens[0].Text); n > maxLineBytes || (l.Clipped > 0) != (n+l.Clipped == len(long)) {
					t.Fatalf("FAIL: line %d kept %d bytes, clipped %d", l.NewNum, len(l.Tokens[0].Text), l.Clipped)
				}
			}
		}
		if text > maxWindowBytes {
			t.Fatalf("FAIL: window at %d carries %d bytes of text", off, text)
		}
		seen += kept
		if after == 0 {
			break
		}
		off += kept
	}
	if seen != 7000 || off != last {
		t.Errorf("FAIL: the chain saw %d lines and ended at %d, LastWindow says %d", seen, off, last)
	}
	// Unbounded, nothing is cut.
	files := mk()
	if _, kept, _ := Window(files, 0, 0); kept != 7000 || len(files[0].Hunks) != 1 || files[0].Hunks[0].Lines[0].Clipped != 0 {
		t.Errorf("FAIL: maxLines 0 kept %d lines in %d hunks", kept, len(files[0].Hunks))
	}
	if !t.Failed() {
		t.Log("ok")
	}
}