changeset 311:b9592e21176a

Tests for array sort and reverse index building helper
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 26 Sep 2011 04:06:04 +0200
parents 237de162be28
children f9f3e9b67ccc
files build.xml src/org/tmatesoft/hg/internal/ArrayHelper.java test/org/tmatesoft/hg/test/MapTagsToFileRevisions.java test/org/tmatesoft/hg/test/TestAuxUtilities.java
diffstat 4 files changed, 63 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- 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 @@
 			<sysproperty key="hg4j.tests.repos" value="${test-repos-root}"/>
 			<sysproperty key="hg4j.tests.remote" value="http://hg.serpentine.com/tutorial/hello"/>
 			<test name="org.tmatesoft.hg.test.TestIntMap" />
+			<test name="org.tmatesoft.hg.test.TestAuxUtilities" />
 			<test name="org.tmatesoft.hg.test.TestHistory" />
 			<test name="org.tmatesoft.hg.test.TestManifest" />
 			<test name="org.tmatesoft.hg.test.TestStatus" />
--- 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<Object> x[], int off, int len) {
     	// Insertion sort on smallest arrays
     	if (len < 7) {
--- 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<String> 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<Nodeid> revisions = new ArrayList<Nodeid>();
--- /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;
+	}
+}