|
| 1 | +package dev.jahidhasanco.diffly.domain.util |
| 2 | + |
| 3 | +import dev.jahidhasanco.diffly.domain.model.CharDiffType |
| 4 | +import kotlin.test.Test |
| 5 | +import kotlin.test.assertEquals |
| 6 | + |
| 7 | + |
| 8 | +class CharLevelDiffTest { |
| 9 | + |
| 10 | + private val diffUtil = CharLevelDiff() |
| 11 | + |
| 12 | + @Test |
| 13 | + fun test_emptyStrings() { |
| 14 | + val result = diffUtil.diff("", "") |
| 15 | + assertEquals(emptyList(), result) |
| 16 | + } |
| 17 | + |
| 18 | + @Test |
| 19 | + fun test_identicalStrings() { |
| 20 | + val input = "hello world" |
| 21 | + val result = diffUtil.diff(input, input) |
| 22 | + assertEquals(input.length, result.size) |
| 23 | + assert(result.all { it.type == CharDiffType.UNCHANGED }) |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + fun test_insertionAtEnd() { |
| 28 | + val oldStr = "hello" |
| 29 | + val newStr = "hello!" |
| 30 | + val result = diffUtil.diff(oldStr, newStr) |
| 31 | + |
| 32 | + val expectedTypes = listOf( |
| 33 | + CharDiffType.UNCHANGED, |
| 34 | + CharDiffType.UNCHANGED, |
| 35 | + CharDiffType.UNCHANGED, |
| 36 | + CharDiffType.UNCHANGED, |
| 37 | + CharDiffType.UNCHANGED, |
| 38 | + CharDiffType.INSERTED |
| 39 | + ) |
| 40 | + assertEquals(expectedTypes, result.map { it.type }) |
| 41 | + assertEquals(newStr, result.map { it.char }.joinToString("")) |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + fun test_deletionAtEnd() { |
| 46 | + val oldStr = "hello!" |
| 47 | + val newStr = "hello" |
| 48 | + val result = diffUtil.diff(oldStr, newStr) |
| 49 | + |
| 50 | + val expectedTypes = listOf( |
| 51 | + CharDiffType.UNCHANGED, |
| 52 | + CharDiffType.UNCHANGED, |
| 53 | + CharDiffType.UNCHANGED, |
| 54 | + CharDiffType.UNCHANGED, |
| 55 | + CharDiffType.UNCHANGED, |
| 56 | + CharDiffType.DELETED |
| 57 | + ) |
| 58 | + assertEquals(expectedTypes, result.map { it.type }) |
| 59 | + assertEquals(oldStr, result.map { it.char }.joinToString("")) |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + fun test_insertionInMiddle() { |
| 64 | + val oldStr = "helo" |
| 65 | + val newStr = "hello" |
| 66 | + val result = diffUtil.diff(oldStr, newStr) |
| 67 | + |
| 68 | + val expectedTypes = listOf( |
| 69 | + CharDiffType.UNCHANGED, |
| 70 | + CharDiffType.UNCHANGED, |
| 71 | + CharDiffType.UNCHANGED, |
| 72 | + CharDiffType.INSERTED, |
| 73 | + CharDiffType.UNCHANGED |
| 74 | + ) |
| 75 | + assertEquals(expectedTypes, result.map { it.type }) |
| 76 | + assertEquals("hello", result.map { it.char }.joinToString("")) |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun test_deletionInMiddle() { |
| 81 | + val oldStr = "hello" |
| 82 | + val newStr = "helo" |
| 83 | + val result = diffUtil.diff(oldStr, newStr) |
| 84 | + |
| 85 | + val expectedTypes = listOf( |
| 86 | + CharDiffType.UNCHANGED, |
| 87 | + CharDiffType.UNCHANGED, |
| 88 | + CharDiffType.UNCHANGED, |
| 89 | + CharDiffType.DELETED, |
| 90 | + CharDiffType.UNCHANGED |
| 91 | + ) |
| 92 | + assertEquals(expectedTypes, result.map { it.type }) |
| 93 | + assertEquals("helo", |
| 94 | + result.filter { it.type != CharDiffType.DELETED }.map { it.char } |
| 95 | + .joinToString("") |
| 96 | + ) |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + fun test_substitution() { |
| 101 | + val oldStr = "hello" |
| 102 | + val newStr = "hallo" |
| 103 | + val result = diffUtil.diff(oldStr, newStr) |
| 104 | + |
| 105 | + // Deletion followed by insertion for 'e' -> 'a' |
| 106 | + val types = result.map { it.type } |
| 107 | + assert(types.contains(CharDiffType.DELETED)) |
| 108 | + assert(types.contains(CharDiffType.INSERTED)) |
| 109 | + |
| 110 | + val filteredNew = |
| 111 | + result.filter { it.type != CharDiffType.DELETED }.map { it.char } |
| 112 | + .joinToString("") |
| 113 | + assertEquals(newStr, filteredNew) |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + fun test_complexChange() { |
| 118 | + val oldStr = "kitten" |
| 119 | + val newStr = "sitting" |
| 120 | + val result = diffUtil.diff(oldStr, newStr) |
| 121 | + |
| 122 | + val filteredNew = |
| 123 | + result.filter { it.type != CharDiffType.DELETED }.map { it.char } |
| 124 | + .joinToString("") |
| 125 | + assertEquals(newStr, filteredNew) |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + fun test_completelyDifferentStrings() { |
| 130 | + val oldStr = "abc" |
| 131 | + val newStr = "xyz" |
| 132 | + val result = diffUtil.diff(oldStr, newStr) |
| 133 | + |
| 134 | + assertEquals(oldStr.length + newStr.length, result.size) |
| 135 | + assertEquals( |
| 136 | + listOf( |
| 137 | + CharDiffType.DELETED, CharDiffType.DELETED, CharDiffType.DELETED, |
| 138 | + CharDiffType.INSERTED, CharDiffType.INSERTED, CharDiffType.INSERTED |
| 139 | + ), |
| 140 | + result.map { it.type } |
| 141 | + ) |
| 142 | + val filteredNew = result.filter { it.type != CharDiffType.DELETED }.map { it.char }.joinToString("") |
| 143 | + assertEquals(newStr, filteredNew) |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + fun test_newStringEmpty() { |
| 148 | + val oldStr = "delete me" |
| 149 | + val newStr = "" |
| 150 | + val result = diffUtil.diff(oldStr, newStr) |
| 151 | + |
| 152 | + assertEquals(oldStr.length, result.size) |
| 153 | + assert(result.all { it.type == CharDiffType.DELETED }) |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + fun test_oldStringEmpty() { |
| 158 | + val oldStr = "" |
| 159 | + val newStr = "insert me" |
| 160 | + val result = diffUtil.diff(oldStr, newStr) |
| 161 | + |
| 162 | + assertEquals(newStr.length, result.size) |
| 163 | + assert(result.all { it.type == CharDiffType.INSERTED }) |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + fun test_singleCharSame() { |
| 168 | + val oldStr = "a" |
| 169 | + val newStr = "a" |
| 170 | + val result = diffUtil.diff(oldStr, newStr) |
| 171 | + |
| 172 | + assertEquals(1, result.size) |
| 173 | + assertEquals(CharDiffType.UNCHANGED, result[0].type) |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + fun test_singleCharDifferent() { |
| 178 | + val oldStr = "a" |
| 179 | + val newStr = "b" |
| 180 | + val result = diffUtil.diff(oldStr, newStr) |
| 181 | + |
| 182 | + assertEquals(2, result.size) |
| 183 | + assertEquals(listOf(CharDiffType.DELETED, CharDiffType.INSERTED), result.map { it.type }) |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + fun test_repeatingCharactersChange() { |
| 188 | + val oldStr = "aaabbb" |
| 189 | + val newStr = "aaaabbb" |
| 190 | + val result = diffUtil.diff(oldStr, newStr) |
| 191 | + |
| 192 | + val types = result.map { it.type } |
| 193 | + assert(types.contains(CharDiffType.INSERTED)) |
| 194 | + assertEquals(newStr, result.filter { it.type != CharDiffType.DELETED }.map { it.char }.joinToString("")) |
| 195 | + } |
| 196 | + |
| 197 | + @Test |
| 198 | + fun test_insertionAtStart() { |
| 199 | + val oldStr = "world" |
| 200 | + val newStr = "hello world" |
| 201 | + val result = diffUtil.diff(oldStr, newStr) |
| 202 | + |
| 203 | + val types = result.map { it.type } |
| 204 | + // Should begin with inserted 'hello ' |
| 205 | + assert(types.take(6).all { it == CharDiffType.INSERTED }) |
| 206 | + assertEquals(newStr, result.filter { it.type != CharDiffType.DELETED }.map { it.char }.joinToString("")) |
| 207 | + } |
| 208 | + |
| 209 | + @Test |
| 210 | + fun test_deletionAtStart() { |
| 211 | + val oldStr = "hello world" |
| 212 | + val newStr = "world" |
| 213 | + val result = diffUtil.diff(oldStr, newStr) |
| 214 | + |
| 215 | + val types = result.map { it.type } |
| 216 | + // Should begin with deleted 'hello ' |
| 217 | + assert(types.take(6).all { it == CharDiffType.DELETED }) |
| 218 | + assertEquals(newStr, result.filter { it.type != CharDiffType.DELETED }.map { it.char }.joinToString("")) |
| 219 | + } |
| 220 | + |
| 221 | + @Test |
| 222 | + fun test_largeInputPerformance() { |
| 223 | + val oldStr = "a".repeat(1000) |
| 224 | + val newStr = "a".repeat(999) + "b" |
| 225 | + val result = diffUtil.diff(oldStr, newStr) |
| 226 | + |
| 227 | + // Expect 1001 results: 999 unchanged + delete + insert |
| 228 | + assertEquals(1001, result.size) |
| 229 | + |
| 230 | + // Verify first 999 are unchanged |
| 231 | + assert(result.take(999).all { it.type == CharDiffType.UNCHANGED }) |
| 232 | + |
| 233 | + // Verify deletion and insertion |
| 234 | + assert(result[999].type == CharDiffType.DELETED) |
| 235 | + assert(result[1000].type == CharDiffType.INSERTED) |
| 236 | + |
| 237 | + // Verify reconstructed new string |
| 238 | + val filteredNew = result.filter { it.type != CharDiffType.DELETED }.map { it.char }.joinToString("") |
| 239 | + assertEquals(newStr, filteredNew) |
| 240 | + } |
| 241 | + |
| 242 | + |
| 243 | + @Test |
| 244 | + fun test_mixedInsertionsAndDeletions() { |
| 245 | + val oldStr = "abcdef" |
| 246 | + val newStr = "abXYef" |
| 247 | + val result = diffUtil.diff(oldStr, newStr) |
| 248 | + |
| 249 | + val filteredNew = result.filter { it.type != CharDiffType.DELETED }.map { it.char }.joinToString("") |
| 250 | + assertEquals(newStr, filteredNew) |
| 251 | + } |
| 252 | + |
| 253 | +} |
0 commit comments