From aeb2ff191c3e9c45572a10a10ed23acdbedba303 Mon Sep 17 00:00:00 2001 From: akkkihi Date: Fri, 12 May 2023 21:57:24 +0300 Subject: [PATCH 01/10] added class MapDiffUtilsTests for testing MapDiff --- .../micro_utils/common/MapDiffUtilsTests.kt | 220 ++++++++++++++++++ 1 file changed, 220 insertions(+) create mode 100644 common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt diff --git a/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt new file mode 100644 index 00000000000..b44b8563a40 --- /dev/null +++ b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt @@ -0,0 +1,220 @@ +package dev.inmo.micro_utils.common + +import kotlin.random.Random +import kotlin.test.Test +import kotlin.test.assertEquals + +class MapDiffUtilsTests { + + private fun compareFun(): (K, V, V) -> Boolean = { _, a, b -> a == b } + + @Test + fun testMapDiffRemoved() { + val map1 = mapOf("a" to 1, "b" to 2, "c" to 3) + val map2 = mapOf("a" to 1, "c" to 3, "d" to 4) + + val diff = map1.diff(map2) + + assertEquals(mapOf("b" to 2), diff.removed) + } + + @Test + fun testMapDiffAdded() { + val map1 = mapOf("a" to 1, "b" to 2, "c" to 3) + val map2 = mapOf("a" to 1, "c" to 3, "d" to 4) + + val diff = map1.diff(map2) + + assertEquals(mapOf("d" to 4), diff.added) + } + + @Test + fun testMapDiffChanged() { + val map1 = mapOf("a" to 1, "b" to 2, "c" to 3) + val map2 = mapOf("a" to 1, "c" to 5, "d" to 4) + + val diff = map1.diff(map2) + + assertEquals(mapOf("c" to (3 to 5)), diff.changed) + } + + @Test + fun testMapDiffRemovedWithCompareFun() { + val map1 = mapOf("a" to 1, "b" to 2, "c" to 3) + val map2 = mapOf("a" to 1, "c" to 3, "d" to 4) + + val diff = map1.diff(map2, compareFun()) + + assertEquals(mapOf("b" to 2), diff.removed) + } + + @Test + fun testMapDiffAddedWithCompareFun() { + val map1 = mapOf("a" to 1, "b" to 2, "c" to 3) + val map2 = mapOf("a" to 1, "c" to 3, "d" to 4) + + val diff = map1.diff(map2, compareFun()) + + assertEquals(mapOf("d" to 4), diff.added) + } + + @Test + fun testMapDiffChangedWithCompareFun() { + val map1 = mapOf("a" to 1, "b" to 2, "c" to 3) + val map2 = mapOf("a" to 1, "c" to 5, "d" to 4) + + val diff = map1.diff(map2) { _, v1, v2 -> v1 == v2 } + + assertEquals(mapOf("c" to (3 to 5)), diff.changed) + } + + @Test + fun testMapDiffRemovedStrictComparison() { + val map1 = mapOf("a" to 1, "b" to 2, "c" to 3) + val map2 = mapOf("a" to 1, "c" to 3, "d" to 4) + + val diff = map1.diff(map2, true) + + assertEquals(mapOf("b" to 2), diff.removed) + } + + @Test + fun testMapDiffAddedStrictComparison() { + val map1 = mapOf("a" to 1, "b" to 2, "c" to 3) + val map2 = mapOf("a" to 1, "c" to 3, "d" to 4) + + val diff = map1.diff(map2, true) + + assertEquals(mapOf("d" to 4), diff.added) + } + + @Test + fun testMapDiffChangedStrictComparison() { + val map1 = mapOf("a" to 1, "b" to 2, "c" to 3) + val map2 = mapOf("a" to 1, "c" to 5, "d" to 4) + + val diff = map1.diff(map2, true) + + assertEquals(mapOf("c" to (3 to 5)), diff.changed) + } + + @Test + @OptIn(Warning::class) + fun testApplyMapDiffRemoved() { + val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) + val diff = MapDiff(mapOf("b" to 2), emptyMap(), emptyMap()) + originalMap.applyDiff(diff) + assertEquals(mapOf("a" to 1, "c" to 3), originalMap) + } + + @Test + @OptIn(Warning::class) + fun testApplyMapDiffChanged() { + val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) + val diff = MapDiff(emptyMap(), mapOf("b" to (2 to 4)), emptyMap()) + originalMap.applyDiff(diff) + assertEquals(mapOf("a" to 1, "b" to 4, "c" to 3), originalMap) + } + + @Test + @OptIn(Warning::class) + fun testApplyMapDiffAdded() { + val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) + val diff = MapDiff(emptyMap(), emptyMap(), mapOf("d" to 4)) + originalMap.applyDiff(diff) + assertEquals(mapOf("a" to 1, "b" to 2, "c" to 3, "d" to 4), originalMap) + } + + @Test + @OptIn(Warning::class) + fun testApplyMapDiffMixed() { + val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) + val diff = MapDiff(mapOf("b" to 2), mapOf("c" to (3 to 5)), mapOf("d" to 4)) + originalMap.applyDiff(diff) + assertEquals(mapOf("a" to 1, "c" to 5, "d" to 4), originalMap) + } + + @Test + @OptIn(Warning::class) + fun testMapDiffWorksStrictComparison() { + val oldMap = mutableMapOf() + val newMap = mutableMapOf() + + for (i in 0 until 10) { + oldMap[i] = "old$i" + newMap[i] = "new$i" + } + + oldMap[0] = "old0" + oldMap[2] = "old2" + oldMap[4] = "old4" + + newMap[0] = "new0" + newMap[2] = "new2" + newMap[4] = "new4" + newMap[10] = "new10" + newMap[11] = "new11" + + newMap.remove(1) + newMap.remove(3) + + val mapDiff = oldMap.diff(newMap, true) + + val removed = mapOf( + 1 to "old1", + 3 to "old3" + ) + val added = mapOf( + 10 to "new10", + 11 to "new11" + ) + val changed = oldMap.keys.intersect(newMap.keys).associateWith { + oldMap[it] to newMap[it] + } + val equalMapDiff = MapDiff(removed, changed, added) + + assertEquals( + added, + mapDiff.added + ) + assertEquals( + equalMapDiff.removed, + mapDiff.removed + ) + assertEquals( + equalMapDiff.changed, + mapDiff.changed + ) + } + + @Test + fun testMapDiffWorksWithEqualValuesStrictComparison() { + val oldMap = mutableMapOf() + val newMap = mutableMapOf() + + for (i in 0 until 10) { + val value = Random.nextInt() + oldMap[i.toString()] = value + newMap[i.toString()] = value + } + + val mapDiff = oldMap.diff(newMap, true) + + val mergedValues = oldMap.keys.intersect(newMap.keys).associateWith { + oldMap[it] to newMap[it] + } + + assertEquals( + mergedValues, + mapDiff.changed, + ) + assertEquals( + emptyMap(), + mapDiff.removed + ) + assertEquals( + emptyMap(), + mapDiff.added + ) + } +} From 34d8c18a46a7fecf7379942b82fcee991a7d9b25 Mon Sep 17 00:00:00 2001 From: akkkihi Date: Fri, 12 May 2023 22:36:59 +0300 Subject: [PATCH 02/10] added testApplyMapDiff(Removed, Added, Changed) and refactor --- .../micro_utils/common/MapDiffUtilsTests.kt | 179 +++++++++++++----- 1 file changed, 135 insertions(+), 44 deletions(-) diff --git a/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt index b44b8563a40..06643e7bd5a 100644 --- a/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt +++ b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt @@ -10,92 +10,119 @@ class MapDiffUtilsTests { @Test fun testMapDiffRemoved() { - val map1 = mapOf("a" to 1, "b" to 2, "c" to 3) - val map2 = mapOf("a" to 1, "c" to 3, "d" to 4) + val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) + val newMap = mapOf("a" to 1, "c" to 3, "d" to 4) - val diff = map1.diff(map2) + val diff = oldMap.diff(newMap) - assertEquals(mapOf("b" to 2), diff.removed) + assertEquals( + mapOf("b" to 2), + diff.removed + ) } @Test fun testMapDiffAdded() { - val map1 = mapOf("a" to 1, "b" to 2, "c" to 3) - val map2 = mapOf("a" to 1, "c" to 3, "d" to 4) + val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) + val newMap = mapOf("a" to 1, "c" to 3, "d" to 4) - val diff = map1.diff(map2) + val diff = oldMap.diff(newMap) - assertEquals(mapOf("d" to 4), diff.added) + assertEquals( + mapOf("d" to 4), + diff.added + ) } @Test fun testMapDiffChanged() { - val map1 = mapOf("a" to 1, "b" to 2, "c" to 3) - val map2 = mapOf("a" to 1, "c" to 5, "d" to 4) + val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) + val newMap = mapOf("a" to 1, "c" to 5, "d" to 4) - val diff = map1.diff(map2) + val diff = oldMap.diff(newMap) - assertEquals(mapOf("c" to (3 to 5)), diff.changed) + assertEquals( + mapOf("c" to (3 to 5)), + diff.changed + ) } @Test fun testMapDiffRemovedWithCompareFun() { - val map1 = mapOf("a" to 1, "b" to 2, "c" to 3) - val map2 = mapOf("a" to 1, "c" to 3, "d" to 4) + val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) + val newMap = mapOf("a" to 1, "c" to 3, "d" to 4) - val diff = map1.diff(map2, compareFun()) + val diff = oldMap.diff(newMap, compareFun()) - assertEquals(mapOf("b" to 2), diff.removed) + assertEquals( + mapOf("b" to 2), + diff.removed + ) } @Test fun testMapDiffAddedWithCompareFun() { - val map1 = mapOf("a" to 1, "b" to 2, "c" to 3) - val map2 = mapOf("a" to 1, "c" to 3, "d" to 4) + val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) + val newMap = mapOf("a" to 1, "c" to 3, "d" to 4) - val diff = map1.diff(map2, compareFun()) + val diff = oldMap.diff(newMap, compareFun()) - assertEquals(mapOf("d" to 4), diff.added) + assertEquals( + mapOf("d" to 4), + diff.added + ) } @Test fun testMapDiffChangedWithCompareFun() { - val map1 = mapOf("a" to 1, "b" to 2, "c" to 3) - val map2 = mapOf("a" to 1, "c" to 5, "d" to 4) + val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) + val newMap = mapOf("a" to 1, "c" to 5, "d" to 4) - val diff = map1.diff(map2) { _, v1, v2 -> v1 == v2 } + val diff = oldMap.diff(newMap) { _, v1, v2 -> v1 == v2 } - assertEquals(mapOf("c" to (3 to 5)), diff.changed) + assertEquals( + mapOf("c" to (3 to 5)), + diff.changed + ) } @Test fun testMapDiffRemovedStrictComparison() { - val map1 = mapOf("a" to 1, "b" to 2, "c" to 3) - val map2 = mapOf("a" to 1, "c" to 3, "d" to 4) + val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) + val newMap = mapOf("a" to 1, "c" to 3, "d" to 4) - val diff = map1.diff(map2, true) + val diff = oldMap.diff(newMap, true) - assertEquals(mapOf("b" to 2), diff.removed) + assertEquals( + mapOf("b" to 2), + diff.removed + ) } @Test fun testMapDiffAddedStrictComparison() { - val map1 = mapOf("a" to 1, "b" to 2, "c" to 3) - val map2 = mapOf("a" to 1, "c" to 3, "d" to 4) + val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) + val newMap = mapOf("a" to 1, "c" to 3, "d" to 4) - val diff = map1.diff(map2, true) + val diff = oldMap.diff(newMap, true) - assertEquals(mapOf("d" to 4), diff.added) + assertEquals( + mapOf("d" to 4), + diff.added + ) } @Test fun testMapDiffChangedStrictComparison() { - val map1 = mapOf("a" to 1, "b" to 2, "c" to 3) - val map2 = mapOf("a" to 1, "c" to 5, "d" to 4) + val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) + val newMap = mapOf("a" to 1, "c" to 5, "d" to 4) - val diff = map1.diff(map2, true) + val diff = oldMap.diff(newMap, true) - assertEquals(mapOf("c" to (3 to 5)), diff.changed) + assertEquals( + mapOf("c" to (3 to 5)), + diff.changed + ) } @Test @@ -103,8 +130,26 @@ class MapDiffUtilsTests { fun testApplyMapDiffRemoved() { val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) val diff = MapDiff(mapOf("b" to 2), emptyMap(), emptyMap()) + originalMap.applyDiff(diff) - assertEquals(mapOf("a" to 1, "c" to 3), originalMap) + + assertEquals( + mapOf("a" to 1, "c" to 3), + originalMap + ) + } + @Test + @OptIn(Warning::class) + fun testApplyMapDiffAdded() { + val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) + val diff = MapDiff(emptyMap(), emptyMap(), mapOf("d" to 4)) + + originalMap.applyDiff(diff) + + assertEquals( + mapOf("a" to 1, "b" to 2, "c" to 3, "d" to 4), + originalMap + ) } @Test @@ -112,17 +157,58 @@ class MapDiffUtilsTests { fun testApplyMapDiffChanged() { val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) val diff = MapDiff(emptyMap(), mapOf("b" to (2 to 4)), emptyMap()) + originalMap.applyDiff(diff) - assertEquals(mapOf("a" to 1, "b" to 4, "c" to 3), originalMap) + + assertEquals( + mapOf("a" to 1, "b" to 4, "c" to 3), + originalMap + ) } @Test - @OptIn(Warning::class) - fun testApplyMapDiffAdded() { + fun testApplyMapDiffRemovedWithCompareFun() { val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) - val diff = MapDiff(emptyMap(), emptyMap(), mapOf("d" to 4)) - originalMap.applyDiff(diff) - assertEquals(mapOf("a" to 1, "b" to 2, "c" to 3, "d" to 4), originalMap) + val oldMap = mutableMapOf() + val newMap = mutableMapOf() + + oldMap.putAll(originalMap) + newMap.putAll(originalMap) + + val remove = mapOf("b" to 2) + newMap.remove(remove.keys.first()) + + val diff = oldMap.applyDiff(newMap, compareFun()) + + assertEquals( + remove, + diff.removed + ) + } + @Test + fun testApplyMapDiffAddedWithCompareFun() { + val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) + val apply = mapOf("d" to 4) + val diff = originalMap.applyDiff(apply, compareFun()) + + assertEquals( + apply, + diff.added + ) + } + + @Test + fun testApplyMapDiffChangedWithCompareFun() { + val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) + + val applyDiff = originalMap.applyDiff(mapOf("b" to 4), compareFun()) + + val changed = mapOf("b" to (2 to 4)) + + assertEquals( + changed, + applyDiff.changed + ) } @Test @@ -130,8 +216,13 @@ class MapDiffUtilsTests { fun testApplyMapDiffMixed() { val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) val diff = MapDiff(mapOf("b" to 2), mapOf("c" to (3 to 5)), mapOf("d" to 4)) + originalMap.applyDiff(diff) - assertEquals(mapOf("a" to 1, "c" to 5, "d" to 4), originalMap) + + assertEquals( + mapOf("a" to 1, "c" to 5, "d" to 4), + originalMap + ) } @Test From 61ea85ac1b77f81229006254142132d39656b6f4 Mon Sep 17 00:00:00 2001 From: akkkihi Date: Fri, 12 May 2023 22:42:24 +0300 Subject: [PATCH 03/10] refactor and add mixed --- .../micro_utils/common/MapDiffUtilsTests.kt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt index 06643e7bd5a..6e111b5bb84 100644 --- a/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt +++ b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt @@ -211,6 +211,24 @@ class MapDiffUtilsTests { ) } + @Test + @OptIn(Warning::class) + fun testMapDiffMixed() { + val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) + val diff = MapDiff(mapOf("b" to 2), mapOf("c" to (3 to 5)), mapOf("d" to 4)) + + originalMap.remove("b") + // change value + originalMap["c"] = 5 + // add value + originalMap["d"] = 4 + + assertEquals( + mapOf("a" to 1, "c" to 5, "d" to 4), + originalMap + ) + } + @Test @OptIn(Warning::class) fun testApplyMapDiffMixed() { From ac34050285fe517908dccad4eec250709a6394e8 Mon Sep 17 00:00:00 2001 From: akkkihi Date: Fri, 12 May 2023 23:04:06 +0300 Subject: [PATCH 04/10] refactor --- .../micro_utils/common/MapDiffUtilsTests.kt | 84 +++++++++++++++---- 1 file changed, 68 insertions(+), 16 deletions(-) diff --git a/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt index 6e111b5bb84..e203e86ee17 100644 --- a/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt +++ b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt @@ -166,6 +166,45 @@ class MapDiffUtilsTests { ) } + @Test + fun testApplyMapDiffRemovedStrictComparison() { + val oldMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) + val newMap = mutableMapOf("a" to 1, "c" to 3, "d" to 4) + + val diff = oldMap.applyDiff(newMap, true) + + assertEquals( + mapOf("b" to 2), + diff.removed + ) + } + + @Test + fun testApplyMapDiffAddedStrictComparison() { + val oldMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) + val newMap = mutableMapOf("a" to 1, "c" to 3, "d" to 4) + + val diff = oldMap.applyDiff(newMap, true) + + assertEquals( + mapOf("d" to 4), + diff.added + ) + } + + @Test + fun testApplyMapDiffChangedStrictComparison() { + val oldMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) + val newMap = mutableMapOf("a" to 1, "c" to 5, "d" to 4) + + val diff = oldMap.applyDiff(newMap, true) + + assertEquals( + mapOf("c" to (3 to 5)), + diff.changed + ) + } + @Test fun testApplyMapDiffRemovedWithCompareFun() { val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) @@ -212,34 +251,47 @@ class MapDiffUtilsTests { } @Test - @OptIn(Warning::class) fun testMapDiffMixed() { - val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) - val diff = MapDiff(mapOf("b" to 2), mapOf("c" to (3 to 5)), mapOf("d" to 4)) + val oldMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) + val newMap = mutableMapOf("a" to 1, "c" to 5, "d" to 4) + val mapDiff = MapDiff(mapOf("b" to 2), mapOf("c" to (3 to 5)), mapOf("d" to 4)) - originalMap.remove("b") - // change value - originalMap["c"] = 5 - // add value - originalMap["d"] = 4 + val diff = oldMap.diff(newMap) assertEquals( - mapOf("a" to 1, "c" to 5, "d" to 4), - originalMap + mapDiff.removed, + diff.removed + ) + assertEquals( + mapDiff.changed, + diff.changed + ) + assertEquals( + mapDiff.added, + diff.added ) } @Test - @OptIn(Warning::class) fun testApplyMapDiffMixed() { - val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) - val diff = MapDiff(mapOf("b" to 2), mapOf("c" to (3 to 5)), mapOf("d" to 4)) + val oldMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) + val newMap = mutableMapOf("a" to 1, "c" to 5, "d" to 4) - originalMap.applyDiff(diff) + val mapDiff = MapDiff(mapOf("b" to 2), mapOf("c" to (3 to 5)), mapOf("d" to 4)) + + val diff = oldMap.applyDiff(newMap) assertEquals( - mapOf("a" to 1, "c" to 5, "d" to 4), - originalMap + mapDiff.removed, + diff.removed + ) + assertEquals( + mapDiff.changed, + diff.changed + ) + assertEquals( + mapDiff.added, + diff.added ) } From 3573b2c55805e26f06781f23b509020f4f530954 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 13 May 2023 15:17:08 +0600 Subject: [PATCH 05/10] Update MapDiffUtilsTests.kt --- .../kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt index e203e86ee17..a0fe54d3fd7 100644 --- a/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt +++ b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt @@ -11,7 +11,7 @@ class MapDiffUtilsTests { @Test fun testMapDiffRemoved() { val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) - val newMap = mapOf("a" to 1, "c" to 3, "d" to 4) + val newMap = mapOf("a" to 1, "c" to 3) val diff = oldMap.diff(newMap) From 119009d3fb51f6dfcb107ee761eac05a5ac31ef4 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 13 May 2023 15:28:47 +0600 Subject: [PATCH 06/10] fix in several new tests --- .../kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt index a0fe54d3fd7..a3726d13eb2 100644 --- a/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt +++ b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt @@ -24,7 +24,7 @@ class MapDiffUtilsTests { @Test fun testMapDiffAdded() { val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) - val newMap = mapOf("a" to 1, "c" to 3, "d" to 4) + val newMap = mapOf("a" to 1, "b" to 2, "c" to 3, "d" to 4) val diff = oldMap.diff(newMap) @@ -37,7 +37,7 @@ class MapDiffUtilsTests { @Test fun testMapDiffChanged() { val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) - val newMap = mapOf("a" to 1, "c" to 5, "d" to 4) + val newMap = mapOf("a" to 1, "b" to 2, "c" to 5) val diff = oldMap.diff(newMap) From fb48aa84f250965a18f2ac654cade241d7436ef5 Mon Sep 17 00:00:00 2001 From: akkkihi Date: Sun, 14 May 2023 19:29:01 +0300 Subject: [PATCH 07/10] rewrite and refactor --- .../micro_utils/common/MapDiffUtilsTests.kt | 272 ++++++++---------- 1 file changed, 121 insertions(+), 151 deletions(-) diff --git a/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt index a3726d13eb2..f7d4871dd22 100644 --- a/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt +++ b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt @@ -8,348 +8,318 @@ class MapDiffUtilsTests { private fun compareFun(): (K, V, V) -> Boolean = { _, a, b -> a == b } + private val originalMap = mapOf( + "a" to 1, + "b" to 2, + "c" to 3, + ) + private val newMapRemoved = Pair( + mapOf( + "a" to 1, + "c" to 3, + ), + mapOf("b" to 2) + ) + private val newMapAdded = Pair( + mapOf( + "a" to 1, + "b" to 2, + "c" to 3, + "d" to 4, + ), + mapOf("d" to 4) + ) + private val newMapChanged = Pair( + mapOf( + "a" to 1, + "b" to 2, + "c" to 5, + ), + mapOf("c" to (3 to 5)) + ) + private val newMapMixed = Pair( + mapOf( + "a" to 1, + "c" to 5, + "d" to 4, + ), + MapDiff( + newMapRemoved.second, + newMapChanged.second, + newMapAdded.second, + ) + ) + @Test fun testMapDiffRemoved() { - val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) - val newMap = mapOf("a" to 1, "c" to 3) - - val diff = oldMap.diff(newMap) + val diff = originalMap.diff(newMapRemoved.first) assertEquals( - mapOf("b" to 2), + newMapRemoved.second, diff.removed ) } @Test fun testMapDiffAdded() { - val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) - val newMap = mapOf("a" to 1, "b" to 2, "c" to 3, "d" to 4) - - val diff = oldMap.diff(newMap) + val diff = originalMap.diff(newMapAdded.first) assertEquals( - mapOf("d" to 4), + newMapAdded.second, diff.added ) } @Test fun testMapDiffChanged() { - val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) - val newMap = mapOf("a" to 1, "b" to 2, "c" to 5) - - val diff = oldMap.diff(newMap) + val diff = originalMap.diff(newMapChanged.first) assertEquals( - mapOf("c" to (3 to 5)), + newMapChanged.second, diff.changed ) } @Test fun testMapDiffRemovedWithCompareFun() { - val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) - val newMap = mapOf("a" to 1, "c" to 3, "d" to 4) - - val diff = oldMap.diff(newMap, compareFun()) + val diff = originalMap.diff(newMapRemoved.first, compareFun()) assertEquals( - mapOf("b" to 2), + newMapRemoved.second, diff.removed ) } @Test fun testMapDiffAddedWithCompareFun() { - val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) - val newMap = mapOf("a" to 1, "c" to 3, "d" to 4) - - val diff = oldMap.diff(newMap, compareFun()) + val diff = originalMap.diff(newMapAdded.first, compareFun()) assertEquals( - mapOf("d" to 4), + newMapAdded.second, diff.added ) } @Test fun testMapDiffChangedWithCompareFun() { - val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) - val newMap = mapOf("a" to 1, "c" to 5, "d" to 4) - - val diff = oldMap.diff(newMap) { _, v1, v2 -> v1 == v2 } + val diff = originalMap.diff(newMapChanged.first, compareFun()) assertEquals( - mapOf("c" to (3 to 5)), + newMapChanged.second, diff.changed ) } @Test fun testMapDiffRemovedStrictComparison() { - val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) - val newMap = mapOf("a" to 1, "c" to 3, "d" to 4) - - val diff = oldMap.diff(newMap, true) + val diff = originalMap.diff(newMapRemoved.first, true) assertEquals( - mapOf("b" to 2), + newMapRemoved.second, diff.removed ) } @Test fun testMapDiffAddedStrictComparison() { - val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) - val newMap = mapOf("a" to 1, "c" to 3, "d" to 4) - - val diff = oldMap.diff(newMap, true) + val diff = originalMap.diff(newMapAdded.first, true) assertEquals( - mapOf("d" to 4), + newMapAdded.second, diff.added ) } @Test fun testMapDiffChangedStrictComparison() { - val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) - val newMap = mapOf("a" to 1, "c" to 5, "d" to 4) - - val diff = oldMap.diff(newMap, true) + val diff = originalMap.diff(newMapChanged.first, true) assertEquals( - mapOf("c" to (3 to 5)), + newMapChanged.second, diff.changed ) } @Test - @OptIn(Warning::class) fun testApplyMapDiffRemoved() { - val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) - val diff = MapDiff(mapOf("b" to 2), emptyMap(), emptyMap()) + val originalMap = originalMap.toMutableMap() + val mapDiff = MapDiff.empty().copy( + removed = newMapRemoved.first + ) - originalMap.applyDiff(diff) + originalMap.applyDiff(mapDiff) assertEquals( - mapOf("a" to 1, "c" to 3), + newMapRemoved.second, originalMap ) } @Test - @OptIn(Warning::class) fun testApplyMapDiffAdded() { - val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) - val diff = MapDiff(emptyMap(), emptyMap(), mapOf("d" to 4)) + val originalMap = originalMap.toMutableMap() + val mapDiff = MapDiff.empty().copy( + added = newMapAdded.first + ) - originalMap.applyDiff(diff) + originalMap.applyDiff(mapDiff) assertEquals( - mapOf("a" to 1, "b" to 2, "c" to 3, "d" to 4), + newMapAdded.first, originalMap ) } @Test - @OptIn(Warning::class) fun testApplyMapDiffChanged() { - val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) - val diff = MapDiff(emptyMap(), mapOf("b" to (2 to 4)), emptyMap()) + val originalMap = originalMap.toMutableMap() + val mapDiff = MapDiff.empty().copy( + changed = newMapChanged.second + ) - originalMap.applyDiff(diff) + originalMap.applyDiff(mapDiff) assertEquals( - mapOf("a" to 1, "b" to 4, "c" to 3), + newMapChanged.first, originalMap ) } @Test fun testApplyMapDiffRemovedStrictComparison() { - val oldMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) - val newMap = mutableMapOf("a" to 1, "c" to 3, "d" to 4) - - val diff = oldMap.applyDiff(newMap, true) + val originalMap = originalMap.toMutableMap() + val diff = originalMap.applyDiff(newMapRemoved.first, true) assertEquals( - mapOf("b" to 2), + newMapRemoved.second, diff.removed ) } @Test fun testApplyMapDiffAddedStrictComparison() { - val oldMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) - val newMap = mutableMapOf("a" to 1, "c" to 3, "d" to 4) - - val diff = oldMap.applyDiff(newMap, true) + val originalMap = originalMap.toMutableMap() + val diff = originalMap.applyDiff(newMapAdded.first, true) assertEquals( - mapOf("d" to 4), + newMapAdded.second, diff.added ) } @Test fun testApplyMapDiffChangedStrictComparison() { - val oldMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) - val newMap = mutableMapOf("a" to 1, "c" to 5, "d" to 4) - - val diff = oldMap.applyDiff(newMap, true) + val originalMap = originalMap.toMutableMap() + val diff = originalMap.applyDiff(newMapChanged.first, true) assertEquals( - mapOf("c" to (3 to 5)), + newMapChanged.second, diff.changed ) } @Test fun testApplyMapDiffRemovedWithCompareFun() { - val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) - val oldMap = mutableMapOf() - val newMap = mutableMapOf() - - oldMap.putAll(originalMap) - newMap.putAll(originalMap) - - val remove = mapOf("b" to 2) - newMap.remove(remove.keys.first()) - - val diff = oldMap.applyDiff(newMap, compareFun()) + val oldMap = originalMap.toMutableMap() + val diff = oldMap.applyDiff(newMapRemoved.first, compareFun()) assertEquals( - remove, + newMapRemoved.second, diff.removed ) } @Test fun testApplyMapDiffAddedWithCompareFun() { - val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) - val apply = mapOf("d" to 4) - val diff = originalMap.applyDiff(apply, compareFun()) + val oldMap = originalMap.toMutableMap() + val diff = oldMap.applyDiff(newMapAdded.first, compareFun()) assertEquals( - apply, + newMapAdded.second, diff.added ) } @Test fun testApplyMapDiffChangedWithCompareFun() { - val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) - - val applyDiff = originalMap.applyDiff(mapOf("b" to 4), compareFun()) - - val changed = mapOf("b" to (2 to 4)) + val oldMap = originalMap.toMutableMap() + val diff = oldMap.applyDiff(newMapChanged.first, compareFun()) assertEquals( - changed, - applyDiff.changed + newMapChanged.second, + diff.changed ) } @Test fun testMapDiffMixed() { - val oldMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) - val newMap = mutableMapOf("a" to 1, "c" to 5, "d" to 4) - val mapDiff = MapDiff(mapOf("b" to 2), mapOf("c" to (3 to 5)), mapOf("d" to 4)) - - val diff = oldMap.diff(newMap) + val oldMap = originalMap.toMutableMap() + val diff = oldMap.applyDiff(newMapMixed.first) assertEquals( - mapDiff.removed, + newMapMixed.second.removed, diff.removed ) assertEquals( - mapDiff.changed, + newMapMixed.second.changed, diff.changed ) assertEquals( - mapDiff.added, + newMapMixed.second.added, diff.added ) } @Test fun testApplyMapDiffMixed() { - val oldMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) - val newMap = mutableMapOf("a" to 1, "c" to 5, "d" to 4) - - val mapDiff = MapDiff(mapOf("b" to 2), mapOf("c" to (3 to 5)), mapOf("d" to 4)) - - val diff = oldMap.applyDiff(newMap) + val oldMap = originalMap.toMutableMap() + val diff = oldMap.applyDiff(newMapMixed.first) assertEquals( - mapDiff.removed, + newMapMixed.second.removed, diff.removed ) assertEquals( - mapDiff.changed, + newMapMixed.second.changed, diff.changed ) assertEquals( - mapDiff.added, + newMapMixed.second.added, diff.added ) } @Test - @OptIn(Warning::class) - fun testMapDiffWorksStrictComparison() { - val oldMap = mutableMapOf() - val newMap = mutableMapOf() + fun testMapDiffWorksWithEqualValuesStrictComparison() { + val oldMap = mutableMapOf() + val newMap = mutableMapOf() for (i in 0 until 10) { - oldMap[i] = "old$i" - newMap[i] = "new$i" + val value = Random.nextInt() + oldMap[i.toString()] = value + newMap[i.toString()] = value } - oldMap[0] = "old0" - oldMap[2] = "old2" - oldMap[4] = "old4" - - newMap[0] = "new0" - newMap[2] = "new2" - newMap[4] = "new4" - newMap[10] = "new10" - newMap[11] = "new11" - - newMap.remove(1) - newMap.remove(3) - val mapDiff = oldMap.diff(newMap, true) - val removed = mapOf( - 1 to "old1", - 3 to "old3" - ) - val added = mapOf( - 10 to "new10", - 11 to "new11" - ) - val changed = oldMap.keys.intersect(newMap.keys).associateWith { + val mergedValues = oldMap.keys.intersect(newMap.keys).associateWith { oldMap[it] to newMap[it] } - val equalMapDiff = MapDiff(removed, changed, added) assertEquals( - added, - mapDiff.added + mergedValues, + mapDiff.changed, ) assertEquals( - equalMapDiff.removed, + emptyMap(), mapDiff.removed ) assertEquals( - equalMapDiff.changed, - mapDiff.changed + emptyMap(), + mapDiff.added ) } @Test - fun testMapDiffWorksWithEqualValuesStrictComparison() { + fun testApplyMapDiffWorksWithEqualValuesStrictComparison() { val oldMap = mutableMapOf() val newMap = mutableMapOf() @@ -359,7 +329,7 @@ class MapDiffUtilsTests { newMap[i.toString()] = value } - val mapDiff = oldMap.diff(newMap, true) + val mapDiff = oldMap.applyDiff(newMap, true) val mergedValues = oldMap.keys.intersect(newMap.keys).associateWith { oldMap[it] to newMap[it] From 8820da342b985df41268fc1998423b011677446e Mon Sep 17 00:00:00 2001 From: akkkihi Date: Sun, 14 May 2023 19:47:50 +0300 Subject: [PATCH 08/10] refactor --- .../micro_utils/common/MapDiffUtilsTests.kt | 42 +++++++++++++++---- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt index f7d4871dd22..0c290e786ef 100644 --- a/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt +++ b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt @@ -3,10 +3,15 @@ package dev.inmo.micro_utils.common import kotlin.random.Random import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertTrue class MapDiffUtilsTests { - private fun compareFun(): (K, V, V) -> Boolean = { _, a, b -> a == b } + private fun compareFun(wasCalled: BooleanArray): (K, V, V) -> Boolean = { _, a, b -> + wasCalled[0] = true + a != b + } + private val compareFunWasCalled = booleanArrayOf(false) private val originalMap = mapOf( "a" to 1, @@ -37,6 +42,13 @@ class MapDiffUtilsTests { ), mapOf("c" to (3 to 5)) ) + private val newMapChangedComparable = Pair( + newMapChanged.first, + mapOf( + "a" to (1 to 1), + "b" to (2 to 2), + ) + ) private val newMapMixed = Pair( mapOf( "a" to 1, @@ -82,8 +94,10 @@ class MapDiffUtilsTests { @Test fun testMapDiffRemovedWithCompareFun() { - val diff = originalMap.diff(newMapRemoved.first, compareFun()) + val wasCalled = compareFunWasCalled + val diff = originalMap.diff(newMapRemoved.first, compareFun(wasCalled)) + assertTrue(wasCalled[0]) assertEquals( newMapRemoved.second, diff.removed @@ -92,8 +106,10 @@ class MapDiffUtilsTests { @Test fun testMapDiffAddedWithCompareFun() { - val diff = originalMap.diff(newMapAdded.first, compareFun()) + val wasCalled = compareFunWasCalled + val diff = originalMap.diff(newMapAdded.first, compareFun(wasCalled)) + assertTrue(wasCalled[0]) assertEquals( newMapAdded.second, diff.added @@ -102,10 +118,12 @@ class MapDiffUtilsTests { @Test fun testMapDiffChangedWithCompareFun() { - val diff = originalMap.diff(newMapChanged.first, compareFun()) + val wasCalled = compareFunWasCalled + val diff = originalMap.diff(newMapChangedComparable.first, compareFun(wasCalled)) + assertTrue(wasCalled[0]) assertEquals( - newMapChanged.second, + newMapChangedComparable.second, diff.changed ) } @@ -219,9 +237,11 @@ class MapDiffUtilsTests { @Test fun testApplyMapDiffRemovedWithCompareFun() { + val wasCalled = compareFunWasCalled val oldMap = originalMap.toMutableMap() - val diff = oldMap.applyDiff(newMapRemoved.first, compareFun()) + val diff = oldMap.applyDiff(newMapRemoved.first, compareFun(wasCalled)) + assertTrue(wasCalled[0]) assertEquals( newMapRemoved.second, diff.removed @@ -229,9 +249,11 @@ class MapDiffUtilsTests { } @Test fun testApplyMapDiffAddedWithCompareFun() { + val wasCalled = compareFunWasCalled val oldMap = originalMap.toMutableMap() - val diff = oldMap.applyDiff(newMapAdded.first, compareFun()) + val diff = oldMap.applyDiff(newMapAdded.first, compareFun(wasCalled)) + assertTrue(wasCalled[0]) assertEquals( newMapAdded.second, diff.added @@ -240,11 +262,13 @@ class MapDiffUtilsTests { @Test fun testApplyMapDiffChangedWithCompareFun() { + val wasCalled = compareFunWasCalled val oldMap = originalMap.toMutableMap() - val diff = oldMap.applyDiff(newMapChanged.first, compareFun()) + val diff = oldMap.applyDiff(newMapChangedComparable.first, compareFun(wasCalled)) + assertTrue(wasCalled[0]) assertEquals( - newMapChanged.second, + newMapChangedComparable.second, diff.changed ) } From 99982dbdadeddccc894b195870f9d291146af345 Mon Sep 17 00:00:00 2001 From: akkkihi Date: Sun, 14 May 2023 19:48:57 +0300 Subject: [PATCH 09/10] refactor --- .../dev/inmo/micro_utils/common/MapDiffUtilsTests.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt index 0c290e786ef..0ff4408bd0b 100644 --- a/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt +++ b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt @@ -7,12 +7,6 @@ import kotlin.test.assertTrue class MapDiffUtilsTests { - private fun compareFun(wasCalled: BooleanArray): (K, V, V) -> Boolean = { _, a, b -> - wasCalled[0] = true - a != b - } - private val compareFunWasCalled = booleanArrayOf(false) - private val originalMap = mapOf( "a" to 1, "b" to 2, @@ -61,6 +55,12 @@ class MapDiffUtilsTests { newMapAdded.second, ) ) + private val compareFunWasCalled = booleanArrayOf(false) + + private fun compareFun(wasCalled: BooleanArray): (K, V, V) -> Boolean = { _, a, b -> + wasCalled[0] = true + a != b + } @Test fun testMapDiffRemoved() { From f03de8c8b407a470dcd0ed0fc8c0742c9032c7e4 Mon Sep 17 00:00:00 2001 From: akkkihi Date: Sun, 14 May 2023 19:49:27 +0300 Subject: [PATCH 10/10] micro refactor --- .../kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt index 0ff4408bd0b..8c3d4ad41f7 100644 --- a/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt +++ b/common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt @@ -57,7 +57,9 @@ class MapDiffUtilsTests { ) private val compareFunWasCalled = booleanArrayOf(false) - private fun compareFun(wasCalled: BooleanArray): (K, V, V) -> Boolean = { _, a, b -> + private fun compareFun( + wasCalled: BooleanArray + ): (K, V, V) -> Boolean = { _, a, b -> wasCalled[0] = true a != b }