From 68d7db3dd327170659cbcba6cc2dc3f890f81db4 Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Tue, 20 Sep 2016 20:30:59 +0300 Subject: [PATCH] patch: apply-somehow - fix hunk sorting --- .../src/com/intellij/diff/util/DiffUtil.java | 4 +- .../com/intellij/diff/util/DiffUtilTest.kt | 61 +++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 platform/diff-impl/tests/com/intellij/diff/util/DiffUtilTest.kt diff --git a/platform/diff-impl/src/com/intellij/diff/util/DiffUtil.java b/platform/diff-impl/src/com/intellij/diff/util/DiffUtil.java index 7ebd0b86f0db..d58767f0bd26 100644 --- a/platform/diff-impl/src/com/intellij/diff/util/DiffUtil.java +++ b/platform/diff-impl/src/com/intellij/diff/util/DiffUtil.java @@ -692,8 +692,8 @@ public class DiffUtil { } ContainerUtil.sort(indexes, (i1, i2) -> { - T val1 = values.get(indexes.get(i1)); - T val2 = values.get(indexes.get(i2)); + T val1 = values.get(i1); + T val2 = values.get(i2); return comparator.compare(val1, val2); }); diff --git a/platform/diff-impl/tests/com/intellij/diff/util/DiffUtilTest.kt b/platform/diff-impl/tests/com/intellij/diff/util/DiffUtilTest.kt new file mode 100644 index 000000000000..f747b0466f21 --- /dev/null +++ b/platform/diff-impl/tests/com/intellij/diff/util/DiffUtilTest.kt @@ -0,0 +1,61 @@ +/* + * Copyright 2000-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * Copyright 2000-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.diff.util + +import com.intellij.diff.DiffTestCase +import com.intellij.util.containers.ContainerUtil + +class `DiffUtil getSortedIndexes() Test` : DiffTestCase() { + fun testSmth() { + doTest(1, 2, 3, 4, 5, 6, 7, 8) { v1, v2 -> v1 - v2 } + + doTest(8, 7, 6, 5, 4, 3, 2, 1) { v1, v2 -> v1 - v2 } + + doTest(1, 3, 5, 7, 8, 6, 4, 2) { v1, v2 -> v1 - v2 } + + doTest(1, 2, 3, 4, 5, 6, 7, 8) { v1, v2 -> v2 - v1 } + + doTest(8, 7, 6, 5, 4, 3, 2, 1) { v1, v2 -> v2 - v1 } + + doTest(1, 3, 5, 7, 8, 6, 4, 2) { v1, v2 -> v2 - v1 } + } + + private fun doTest(vararg values: T, comparator: (T, T) -> Int) { + val list = values.toList() + + val sortedIndexes = DiffUtil.getSortedIndexes(list, comparator); + val expected = ContainerUtil.sorted(list, comparator); + val actual = (0..values.size - 1).map { values[sortedIndexes[it]] } + + assertOrderedEquals(actual, expected) + assertEquals(sortedIndexes.toSet().size, list.size) + } +}