MCPcopy Index your code
hub / github.com/databus23/helm-diff / TestDiffLinesSemanticValidity

Function TestDiffLinesSemanticValidity

diff/lcs_test.go:111–151  ·  view source on GitHub ↗

TestDiffLinesSemanticValidity verifies that diffLines produces semantically valid diffs on random inputs. When multiple valid LCS paths exist, diffLines may choose a different path than difflib.Diff, so we verify correctness properties rather than exact output match.

(t *testing.T)

Source from the content-addressed store, hash-verified

109// diffLines may choose a different path than difflib.Diff, so we verify
110// correctness properties rather than exact output match.
111func TestDiffLinesSemanticValidity(t *testing.T) {
112 rnd := rand.New(rand.NewSource(42))
113
114 for iter := 0; iter < 1000; iter++ {
115 seq1 := generateRandomSequence(rnd, 1+rnd.Intn(30), 1+rnd.Intn(8))
116 seq2 := generateRandomSequence(rnd, 1+rnd.Intn(30), 1+rnd.Intn(8))
117
118 records := diffLines(seq1, seq2)
119
120 i1, i2 := 0, 0
121 commonCount := 0
122 for _, r := range records {
123 switch r.Delta {
124 case difflib.Common:
125 require.Less(t, i1, len(seq1), "iter %d: Common references seq1[%d] out of range", iter, i1)
126 require.Less(t, i2, len(seq2), "iter %d: Common references seq2[%d] out of range", iter, i2)
127 require.Equal(t, seq1[i1], r.Payload, "iter %d: Common payload mismatch at seq1[%d]", iter, i1)
128 require.Equal(t, seq2[i2], r.Payload, "iter %d: Common payload mismatch at seq2[%d]", iter, i2)
129 i1++
130 i2++
131 commonCount++
132 case difflib.LeftOnly:
133 require.Less(t, i1, len(seq1), "iter %d: LeftOnly references seq1[%d] out of range", iter, i1)
134 require.Equal(t, seq1[i1], r.Payload, "iter %d: LeftOnly payload mismatch at seq1[%d]", iter, i1)
135 i1++
136 case difflib.RightOnly:
137 require.Less(t, i2, len(seq2), "iter %d: RightOnly references seq2[%d] out of range", iter, i2)
138 require.Equal(t, seq2[i2], r.Payload, "iter %d: RightOnly payload mismatch at seq2[%d]", iter, i2)
139 i2++
140 }
141 }
142
143 require.Equal(t, len(seq1), i1, "iter %d: not all seq1 elements consumed", iter)
144 require.Equal(t, len(seq2), i2, "iter %d: not all seq2 elements consumed", iter)
145
146 expectedLCS := lcsLength(seq1, seq2)
147 require.Equal(t, expectedLCS, commonCount,
148 "iter %d: LCS length mismatch (expected %d, got %d)\nseq1=%v\nseq2=%v",
149 iter, expectedLCS, commonCount, seq1, seq2)
150 }
151}
152
153// TestDiffLinesLargeInput ensures the implementation handles large inputs
154// without excessive memory usage.

Callers

nothing calls this directly

Calls 3

generateRandomSequenceFunction · 0.85
diffLinesFunction · 0.85
lcsLengthFunction · 0.85

Tested by

no test coverage detected