# HG changeset patch # User Artem Tikhomirov # Date 1317002764 -7200 # Node ID b9592e21176a76b20a998812fd280756ef3a6138 # Parent 237de162be287365a4ff7c6418f7e76e8e2e28cf Tests for array sort and reverse index building helper diff -r 237de162be28 -r b9592e21176a build.xml --- a/build.xml Sat Sep 24 13:25:33 2011 +0200 +++ b/build.xml Mon Sep 26 04:06:04 2011 +0200 @@ -82,6 +82,7 @@ + diff -r 237de162be28 -r b9592e21176a src/org/tmatesoft/hg/internal/ArrayHelper.java --- a/src/org/tmatesoft/hg/internal/ArrayHelper.java Sat Sep 24 13:25:33 2011 +0200 +++ b/src/org/tmatesoft/hg/internal/ArrayHelper.java Mon Sep 26 04:06:04 2011 +0200 @@ -17,6 +17,7 @@ package org.tmatesoft.hg.internal; /** + * Internal alternative to Arrays.sort to build reversed index along with sorting * * @author Artem Tikhomirov * @author TMate Software Ltd. @@ -32,13 +33,17 @@ for (int i = 0; i < reverse.length; i++) { // element that was not moved don't have an index in reverse. // perhaps, can do it inside sort alg? - // TODO tests! + // Alternatively, may start with filling reverse[] array with initial indexes and + // avoid != 0 comparisons in #swap altogether? if (reverse[i] == 0) { reverse[i] = i+1; } } } - + + /** + * Slightly modified version of Arrays.sort1(int[], int, int) quicksort alg (just to deal with Object[]) + */ private void sort1(Comparable x[], int off, int len) { // Insertion sort on smallest arrays if (len < 7) { diff -r 237de162be28 -r b9592e21176a test/org/tmatesoft/hg/test/MapTagsToFileRevisions.java --- a/test/org/tmatesoft/hg/test/MapTagsToFileRevisions.java Sat Sep 24 13:25:33 2011 +0200 +++ b/test/org/tmatesoft/hg/test/MapTagsToFileRevisions.java Mon Sep 26 04:06:04 2011 +0200 @@ -55,20 +55,6 @@ * each 3000'th revision, total 24 revision: 410 vs 275 */ private void revisionMap() throws Exception { - ArrayHelper ah = new ArrayHelper(); - final List initial = Arrays.asList("d", "w", "k", "b", "c", "i", "a", "r", "e", "h"); - String[] a = (String[]) initial.toArray(); - ah.sort(a); - System.out.println(Arrays.toString(initial.toArray())); - System.out.println(Arrays.toString(a)); - System.out.println(Arrays.toString(ah.getReverse())); - Object[] rebuilt = new Object[a.length]; - for (int i = 0; i < a.length; i++) { - int indexInOriginal = ah.getReverse()[i]; - rebuilt[indexInOriginal-1] = a[i]; - } - System.out.println(Arrays.toString(rebuilt)); - // final HgRepository repository = new HgLookup().detect(new File("/temp/hg/cpython")); final HgChangelog clog = repository.getChangelog(); ArrayList revisions = new ArrayList(); diff -r 237de162be28 -r b9592e21176a test/org/tmatesoft/hg/test/TestAuxUtilities.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/org/tmatesoft/hg/test/TestAuxUtilities.java Mon Sep 26 04:06:04 2011 +0200 @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2011 TMate Software Ltd + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * For information on how to redistribute this software under + * the terms of a license other than GNU General Public License + * contact TMate Software at support@hg4j.com + */ +package org.tmatesoft.hg.test; + +import org.junit.Assert; +import org.junit.Test; +import org.tmatesoft.hg.internal.ArrayHelper; + +/** + * + * @author Artem Tikhomirov + * @author TMate Software Ltd. + */ +public class TestAuxUtilities { + + @Test + public void testArrayHelper() { + String[] initial = {"d", "w", "k", "b", "c", "i", "a", "r", "e", "h" }; + ArrayHelper ah = new ArrayHelper(); + String[] result = initial.clone(); + ah.sort(result); + String[] restored = restore(result, ah.getReverse()); + Assert.assertArrayEquals(initial, restored); + // + // few elements are on the right place from the very start and do not shift during sort. + // make sure for them we've got correct reversed indexes as well + initial = new String[] {"d", "h", "c", "b", "k", "i", "a", "r", "e", "w" }; + ah.sort(result = initial.clone()); + restored = restore(result, ah.getReverse()); + Assert.assertArrayEquals(initial, restored); + } + + private static String[] restore(String[] sorted, int[] sortReverse) { + String[] rebuilt = new String[sorted.length]; + for (int i = 0; i < sorted.length; i++) { + int indexInOriginal = sortReverse[i]; + rebuilt[indexInOriginal-1] = sorted[i]; + } + return rebuilt; + } +}