Files
openchamber-android-native/third_party/mygo/internal/diff/diff_test.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

195 lines
5.2 KiB
Go

package diff
import (
"math/rand/v2"
"reflect"
"slices"
"strings"
"testing"
)
// apply turns a into b with the hunks, checking that what they leave
// alone is the same on both sides.
func apply(t *testing.T, a, b []string, hunks []Hunk) {
t.Helper()
var out []string
i, j := 0, 0
for _, h := range hunks {
if h.A0 < i || h.B0 < j || h.A0-i != h.B0-j || h.A1 < h.A0 || h.B1 < h.B0 {
t.Fatalf("hunk %+v out of order after %d, %d", h, i, j)
}
for ; i < h.A0; i, j = i+1, j+1 {
if a[i] != b[j] {
t.Fatalf("unchanged line %d of a %q is %q in b", i, a[i], b[j])
}
out = append(out, a[i])
}
out = append(out, b[h.B0:h.B1]...)
i, j = h.A1, h.B1
}
if len(a)-i != len(b)-j {
t.Fatalf("tails differ: %d of a, %d of b", len(a)-i, len(b)-j)
}
out = append(out, a[i:]...)
if !reflect.DeepEqual(out, b) && !(len(out) == 0 && len(b) == 0) {
t.Fatalf("applying %v gives %q, want %q", hunks, out, b)
}
}
func lines(s string) []string {
if s == "" {
return nil
}
return strings.Split(s, "\n")
}
func TestLines(t *testing.T) {
for _, c := range []struct {
a, b string
want []Hunk
}{
{"", "", nil},
{"a", "a", nil},
{"", "a\nb", []Hunk{{0, 0, 0, 2}}},
{"a\nb", "", []Hunk{{0, 2, 0, 0}}},
{"a\nb\nc", "a\nx\nc", []Hunk{{1, 2, 1, 2}}},
{"a\nb\nc", "a\nc", []Hunk{{1, 2, 1, 1}}},
{"a\nc", "a\nb\nc", []Hunk{{1, 1, 1, 2}}},
{"a\nb\nc\nd", "b\nc\nd\ne", []Hunk{{0, 1, 0, 0}, {4, 4, 3, 4}}},
} {
got := Lines(lines(c.a), lines(c.b))
if !reflect.DeepEqual(got, c.want) {
t.Errorf("Lines(%q, %q) = %v, want %v", c.a, c.b, got, c.want)
}
apply(t, lines(c.a), lines(c.b), got)
}
}
// A function added between two others shows as the function and the
// blank line after it, not as the end of one function and the start of
// the next, as git's indent heuristic places it.
func TestLinesSlide(t *testing.T) {
a := lines("func a() {\n\tx()\n}\n\nfunc c() {\n\tx()\n}")
b := lines("func a() {\n\tx()\n}\n\nfunc b() {\n\tx()\n}\n\nfunc c() {\n\tx()\n}")
got := Lines(a, b)
want := []Hunk{{4, 4, 4, 8}}
if !reflect.DeepEqual(got, want) {
t.Fatalf("Lines = %v, want %v: %q", got, want, b[got[0].B0:got[0].B1])
}
apply(t, a, b, got)
}
// Histogram anchors on rare lines: moving a block keeps the unique lines
// matched rather than the braces that repeat.
func TestLinesHistogram(t *testing.T) {
a := lines("one\n{\n}\ntwo\n{\n}\nthree")
b := lines("two\n{\n}\none\n{\n}\nthree")
got := Lines(a, b)
apply(t, a, b, got)
changed := 0
for _, h := range got {
changed += h.A1 - h.A0 + h.B1 - h.B0
}
if changed > 6 {
t.Fatalf("Lines = %v, %d lines changed", got, changed)
}
}
func TestLinesRandom(t *testing.T) {
r := rand.New(rand.NewPCG(1, 2))
words := []string{"a", "b", "c", "{", "}", "", "x := 1", "return"}
gen := func(n int) []string {
out := make([]string, n)
for i := range out {
out[i] = words[r.IntN(len(words))]
}
return out
}
for range 2000 {
a := gen(r.IntN(40))
b := append([]string(nil), a...)
// Edit some.
for range r.IntN(6) {
if len(b) == 0 || r.IntN(2) == 0 {
at := r.IntN(len(b) + 1)
b = append(b[:at], append(gen(r.IntN(4)+1), b[at:]...)...)
} else {
at := r.IntN(len(b))
b = append(b[:at], b[min(len(b), at+r.IntN(3)+1):]...)
}
}
if r.IntN(5) == 0 {
b = gen(r.IntN(40))
}
apply(t, a, b, Lines(a, b))
ia, ib := Intern(a, b)
d := differ{a: ia, b: ib}
d.myers(0, len(ia), 0, len(ib))
apply(t, a, b, d.hunks())
}
}
// Myers's cost limit keeps unrelated texts quick and the result valid.
func TestMyersCostLimit(t *testing.T) {
r := rand.New(rand.NewPCG(3, 4))
gen := func(n int) []string {
out := make([]string, n)
for i := range out {
out[i] = string(rune('a' + r.IntN(3)))
}
return out
}
a, b := gen(5000), gen(5000)
apply(t, a, b, Lines(a, b))
}
func TestWords(t *testing.T) {
a := "return foo(bar, baz)"
b := "return foo(qux, baz)"
ra, rb, same := Words(a, b)
if !reflect.DeepEqual(ra, []Range{{11, 14}}) || !reflect.DeepEqual(rb, []Range{{11, 14}}) {
t.Fatalf("Words = %v, %v", ra, rb)
}
if same < 0.8 {
t.Fatalf("same = %v", same)
}
// Changed words across whitespace join.
ra, rb, _ = Words("a b c d", "a x y d")
if !reflect.DeepEqual(ra, []Range{{2, 5}}) || !reflect.DeepEqual(rb, []Range{{2, 5}}) {
t.Fatalf("Words joined = %v, %v", ra, rb)
}
ra, rb, _ = Chars("colour", "color")
if !reflect.DeepEqual(ra, []Range{{4, 5}}) || rb != nil {
t.Fatalf("Chars = %v, %v", ra, rb)
}
}
func BenchmarkLines(b *testing.B) {
var sb strings.Builder
for i := range 20000 {
sb.WriteString("line ")
sb.WriteString(strings.Repeat("x", i%40))
sb.WriteString("\n")
}
a := lines(sb.String())
c := append([]string(nil), a...)
for i := 100; i < len(c); i += 997 {
c[i] = "changed"
}
b.ResetTimer()
for range b.N {
Lines(a, c)
}
}
// The rarity of a line is its record's count in the region, as git's,
// so that the last of repeated lines is not taken for a unique anchor.
func TestHistogramRarity(t *testing.T) {
a := []string{"", "", "", "\tv14 := f13()", "\tv22 := f25()", "}", ""}
b := []string{"\t\treturn err", "\treturn nil", "", "", "\tv22 := f25()", "}", ""}
got, want := Lines(a, b), []Hunk{{0, 0, 0, 2}, {2, 4, 4, 4}} // git diff --histogram's
if !slices.Equal(got, want) {
t.Errorf("hunks %v, want git's %v", got, want)
}
}