Mercurial > hg4j
comparison test/org/tmatesoft/hg/test/TestAuxUtilities.java @ 657:6334b0267103
ParentChildMap can supply RevisionMap. Refactor ArrayHelper to keep most of sorted/reverse index magic inside
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Thu, 04 Jul 2013 20:27:45 +0200 |
parents | 6526d8adbc0f |
children | cce0387c6041 |
comparison
equal
deleted
inserted
replaced
656:a937e63b6e02 | 657:6334b0267103 |
---|---|
21 import static org.tmatesoft.hg.repo.HgRepository.TIP; | 21 import static org.tmatesoft.hg.repo.HgRepository.TIP; |
22 import static org.tmatesoft.hg.util.Path.CompareResult.*; | 22 import static org.tmatesoft.hg.util.Path.CompareResult.*; |
23 | 23 |
24 import java.io.IOException; | 24 import java.io.IOException; |
25 import java.nio.ByteBuffer; | 25 import java.nio.ByteBuffer; |
26 import java.util.Arrays; | |
26 | 27 |
27 import org.junit.Assert; | 28 import org.junit.Assert; |
28 import org.junit.Rule; | 29 import org.junit.Rule; |
29 import org.junit.Test; | 30 import org.junit.Test; |
30 import org.tmatesoft.hg.core.HgCatCommand; | 31 import org.tmatesoft.hg.core.HgCatCommand; |
60 public ErrorCollectorExt errorCollector = new ErrorCollectorExt(); | 61 public ErrorCollectorExt errorCollector = new ErrorCollectorExt(); |
61 | 62 |
62 @Test | 63 @Test |
63 public void testArrayHelper() { | 64 public void testArrayHelper() { |
64 String[] initial = {"d", "w", "k", "b", "c", "i", "a", "r", "e", "h" }; | 65 String[] initial = {"d", "w", "k", "b", "c", "i", "a", "r", "e", "h" }; |
65 ArrayHelper ah = new ArrayHelper(); | 66 ArrayHelper<String> ah = new ArrayHelper<String>(initial); |
66 String[] result = initial.clone(); | 67 String[] result = initial.clone(); |
67 ah.sort(result); | 68 ah.sort(result, false, false); |
68 String[] restored = restore(result, ah.getReverse()); | 69 String[] restored = restore(result, ah.getReverseIndexes()); |
69 assertArrayEquals(initial, restored); | 70 assertArrayEquals(initial, restored); |
70 // | 71 // |
71 // few elements are on the right place from the very start and do not shift during sort. | 72 // few elements are on the right place from the very start and do not shift during sort. |
72 // make sure for them we've got correct reversed indexes as well | 73 // make sure for them we've got correct reversed indexes as well |
73 initial = new String[] {"d", "h", "c", "b", "k", "i", "a", "r", "e", "w" }; | 74 initial = new String[] {"d", "h", "c", "b", "k", "i", "a", "r", "e", "w" }; |
74 ah.sort(result = initial.clone()); | 75 ah = new ArrayHelper<String>(initial); |
75 restored = restore(result, ah.getReverse()); | 76 ah.sort(result = new String[initial.length], true, true); |
77 restored = restore(result, ah.getReverseIndexes()); | |
76 assertArrayEquals(initial, restored); | 78 assertArrayEquals(initial, restored); |
79 for (int i = 0; i < initial.length; i++) { | |
80 String s = initial[i]; | |
81 errorCollector.assertEquals(i, ah.binarySearch(s, -1)); | |
82 errorCollector.assertEquals(Arrays.binarySearch(result, s), ah.binarySearchSorted(s)); | |
83 } | |
77 } | 84 } |
78 | 85 |
79 private static String[] restore(String[] sorted, int[] sortReverse) { | 86 private static String[] restore(String[] sorted, int[] sortReverse) { |
80 String[] rebuilt = new String[sorted.length]; | 87 String[] rebuilt = new String[sorted.length]; |
81 for (int i = 0; i < sorted.length; i++) { | 88 for (int i = 0; i < sorted.length; i++) { |
82 int indexInOriginal = sortReverse[i]; | 89 int indexInOriginal = sortReverse[i]; |
83 rebuilt[indexInOriginal-1] = sorted[i]; | 90 rebuilt[indexInOriginal] = sorted[i]; |
84 } | 91 } |
85 return rebuilt; | 92 return rebuilt; |
86 } | 93 } |
87 | 94 |
88 | 95 |