Added various variants of splice() to facilitate array manipulations by

those who are accustomed to slinging backticks and slurping files with
Perl.  Someday perhaps we'll go whole-hog and implement more of the
Perl-style splicing antics (allowing negative offset and/or length, and
optionally passing a list of items to splice into the array.)


git-svn-id: https://samskivert.googlecode.com/svn/trunk@827 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
shaper
2002-09-06 02:12:26 +00:00
parent 9f0e3fc3c8
commit 66be01eb29
2 changed files with 275 additions and 17 deletions
@@ -1,11 +1,13 @@
//
// $Id: ArrayUtilTest.java,v 1.1 2002/08/12 01:10:32 mdb Exp $
// $Id: ArrayUtilTest.java,v 1.2 2002/09/06 02:12:26 shaper Exp $
package com.samskivert.util;
import junit.framework.Test;
import junit.framework.TestCase;
import com.samskivert.Log;
/**
* Tests the {@link ArrayUtil} class.
*/
@@ -21,73 +23,121 @@ public class ArrayUtilTest extends TestCase
// test reversing an array
int[] values = new int[] { 0 };
int[] work = (int[])values.clone();
reverse(work);
ArrayUtil.reverse(work);
Log.info("reverse: " + StringUtil.toString(work));
values = new int[] { 0, 1, 2 };
work = (int[])values.clone();
reverse(work);
ArrayUtil.reverse(work);
Log.info("reverse: " + StringUtil.toString(work));
work = (int[])values.clone();
reverse(work, 0, 2);
ArrayUtil.reverse(work, 0, 2);
Log.info("reverse first-half: " + StringUtil.toString(work));
work = (int[])values.clone();
reverse(work, 1, 2);
ArrayUtil.reverse(work, 1, 2);
Log.info("reverse second-half: " + StringUtil.toString(work));
values = new int[] { 0, 1, 2, 3, 4 };
work = (int[])values.clone();
reverse(work, 1, 3);
ArrayUtil.reverse(work, 1, 3);
Log.info("reverse middle: " + StringUtil.toString(work));
values = new int[] { 0, 1, 2, 3 };
work = (int[])values.clone();
reverse(work);
ArrayUtil.reverse(work);
Log.info("reverse even: " + StringUtil.toString(work));
// test shuffling two elements
values = new int[] { 0, 1 };
work = (int[])values.clone();
shuffle(work, 0, 1);
ArrayUtil.shuffle(work, 0, 1);
Log.info("first-half shuffle: " + StringUtil.toString(work));
work = (int[])values.clone();
shuffle(work, 1, 1);
ArrayUtil.shuffle(work, 1, 1);
Log.info("second-half shuffle: " + StringUtil.toString(work));
work = (int[])values.clone();
shuffle(work);
ArrayUtil.shuffle(work);
Log.info("full shuffle: " + StringUtil.toString(work));
// test shuffling three elements
values = new int[] { 0, 1, 2 };
work = (int[])values.clone();
shuffle(work, 0, 2);
ArrayUtil.shuffle(work, 0, 2);
Log.info("first-half shuffle: " + StringUtil.toString(work));
work = (int[])values.clone();
shuffle(work, 1, 2);
ArrayUtil.shuffle(work, 1, 2);
Log.info("second-half shuffle: " + StringUtil.toString(work));
work = (int[])values.clone();
shuffle(work);
ArrayUtil.shuffle(work);
Log.info("full shuffle: " + StringUtil.toString(work));
// test shuffling ten elements
values = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
work = (int[])values.clone();
shuffle(work, 0, 5);
ArrayUtil.shuffle(work, 0, 5);
Log.info("first-half shuffle: " + StringUtil.toString(work));
work = (int[])values.clone();
shuffle(work, 5, 5);
ArrayUtil.shuffle(work, 5, 5);
Log.info("second-half shuffle: " + StringUtil.toString(work));
work = (int[])values.clone();
shuffle(work);
ArrayUtil.shuffle(work);
Log.info("full shuffle: " + StringUtil.toString(work));
// test splicing with simple truncate beyond offset
values = new int[] { 0, 1, 2 };
work = (int[])values.clone();
work = ArrayUtil.splice(work, 0);
Log.info("splice truncate 0: " + StringUtil.toString(work));
work = (int[])values.clone();
work = ArrayUtil.splice(work, 1);
Log.info("splice truncate 1: " + StringUtil.toString(work));
work = (int[])values.clone();
work = ArrayUtil.splice(work, 2);
Log.info("splice truncate 2: " + StringUtil.toString(work));
values = new int[] { 0 };
work = (int[])values.clone();
work = ArrayUtil.splice(work, 0);
Log.info("single element splice truncate 0: " +
StringUtil.toString(work));
// test splicing out a single element
values = new int[] { 0, 1, 2 };
work = (int[])values.clone();
work = ArrayUtil.splice(work, 0, 1);
Log.info("splice concat 0, 1: " + StringUtil.toString(work));
work = (int[])values.clone();
work = ArrayUtil.splice(work, 1, 1);
Log.info("splice concat 1, 1: " + StringUtil.toString(work));
work = (int[])values.clone();
work = ArrayUtil.splice(work, 2, 1);
Log.info("splice concat 2, 1: " + StringUtil.toString(work));
// test splicing out two elements
values = new int[] { 0, 1, 2, 3 };
work = (int[])values.clone();
work = ArrayUtil.splice(work, 0, 2);
Log.info("splice concat 0, 2: " + StringUtil.toString(work));
work = (int[])values.clone();
work = ArrayUtil.splice(work, 1, 2);
Log.info("splice concat 1, 2: " + StringUtil.toString(work));
work = (int[])values.clone();
work = ArrayUtil.splice(work, 2, 2);
Log.info("splice concat 2, 2: " + StringUtil.toString(work));
}
public static Test suite ()