Skip to content
quarry Artifact [aa4e56bba1]
Login

Artifact [aa4e56bba1]

Artifact aa4e56bba176ecbc8f1309fce026a7b1590b7e899b3f1cdde41e0c4a6fab7526:


package diff

import "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)
	}
}