Mercurial > hg4j
comparison test/org/tmatesoft/hg/test/TestAuxUtilities.java @ 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 | |
children | f9f3e9b67ccc |
comparison
equal
deleted
inserted
replaced
310:237de162be28 | 311:b9592e21176a |
---|---|
1 /* | |
2 * Copyright (c) 2011 TMate Software Ltd | |
3 * | |
4 * This program is free software; you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as published by | |
6 * the Free Software Foundation; version 2 of the License. | |
7 * | |
8 * This program is distributed in the hope that it will be useful, | |
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 * GNU General Public License for more details. | |
12 * | |
13 * For information on how to redistribute this software under | |
14 * the terms of a license other than GNU General Public License | |
15 * contact TMate Software at support@hg4j.com | |
16 */ | |
17 package org.tmatesoft.hg.test; | |
18 | |
19 import org.junit.Assert; | |
20 import org.junit.Test; | |
21 import org.tmatesoft.hg.internal.ArrayHelper; | |
22 | |
23 /** | |
24 * | |
25 * @author Artem Tikhomirov | |
26 * @author TMate Software Ltd. | |
27 */ | |
28 public class TestAuxUtilities { | |
29 | |
30 @Test | |
31 public void testArrayHelper() { | |
32 String[] initial = {"d", "w", "k", "b", "c", "i", "a", "r", "e", "h" }; | |
33 ArrayHelper ah = new ArrayHelper(); | |
34 String[] result = initial.clone(); | |
35 ah.sort(result); | |
36 String[] restored = restore(result, ah.getReverse()); | |
37 Assert.assertArrayEquals(initial, restored); | |
38 // | |
39 // few elements are on the right place from the very start and do not shift during sort. | |
40 // make sure for them we've got correct reversed indexes as well | |
41 initial = new String[] {"d", "h", "c", "b", "k", "i", "a", "r", "e", "w" }; | |
42 ah.sort(result = initial.clone()); | |
43 restored = restore(result, ah.getReverse()); | |
44 Assert.assertArrayEquals(initial, restored); | |
45 } | |
46 | |
47 private static String[] restore(String[] sorted, int[] sortReverse) { | |
48 String[] rebuilt = new String[sorted.length]; | |
49 for (int i = 0; i < sorted.length; i++) { | |
50 int indexInOriginal = sortReverse[i]; | |
51 rebuilt[indexInOriginal-1] = sorted[i]; | |
52 } | |
53 return rebuilt; | |
54 } | |
55 } |