PageRenderTime 18ms CodeModel.GetById 1ms app.highlight 13ms RepoModel.GetById 2ms app.codeStats 0ms

/tst/org/diffkit/util/tst/JTestArrayUtil.java

http://diffkit.googlecode.com/
Java | 143 lines | 100 code | 25 blank | 18 comment | 0 complexity | cf0cce8ac58aa4709f94bbbd491bed8e MD5 | raw file
  1/**
  2 * Copyright 2010-2011 Joseph Panico
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 *   http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16package org.diffkit.util.tst;
 17
 18import groovy.util.GroovyTestCase;
 19import junit.framework.Assert;
 20
 21import org.diffkit.util.DKArrayUtil;
 22
 23/**
 24 * @author jpanico
 25 */
 26public class JTestArrayUtil extends GroovyTestCase {
 27
 28   public void testRemoveMe() {
 29      char var = 'A' + 1;
 30      System.out.println("var->" + var);
 31      System.out.println("A+B->" + ("" + 'A' + 'B' + '\0' + 'C'));
 32   }
 33
 34   public void testIndicesOfIntersection() {
 35      String[] source = new String[] { "beware", "the", "jabberwocky" };
 36      String[] target = null;
 37      Assert.assertNull(DKArrayUtil.getIndicesOfIntersection(source, target));
 38      Assert.assertNull(DKArrayUtil.getIndicesOfIntersection(target, source));
 39
 40      target = new String[0];
 41      GroovyTestCase.assertEquals(null,
 42         DKArrayUtil.getIndicesOfIntersection(source, target));
 43      GroovyTestCase.assertEquals(null,
 44         DKArrayUtil.getIndicesOfIntersection(target, source));
 45
 46      target = new String[] { "ack", "awk", "hack" };
 47      GroovyTestCase.assertEquals(null,
 48         DKArrayUtil.getIndicesOfIntersection(source, target));
 49      GroovyTestCase.assertEquals(null,
 50         DKArrayUtil.getIndicesOfIntersection(target, source));
 51
 52      target = new String[] { "beware", "the", "jabberwocky" };
 53      GroovyTestCase.assertEquals(new int[] { 0, 1, 2 },
 54         DKArrayUtil.getIndicesOfIntersection(source, target));
 55      GroovyTestCase.assertEquals(new int[] { 0, 1, 2 },
 56         DKArrayUtil.getIndicesOfIntersection(target, source));
 57
 58      target = new String[] { "jabberwocky", "the", "beware" };
 59      GroovyTestCase.assertEquals(new int[] { 0, 1, 2 },
 60         DKArrayUtil.getIndicesOfIntersection(source, target));
 61      GroovyTestCase.assertEquals(new int[] { 0, 1, 2 },
 62         DKArrayUtil.getIndicesOfIntersection(target, source));
 63
 64      target = new String[] { "jabberwocky" };
 65      GroovyTestCase.assertEquals(new int[] { 2 },
 66         DKArrayUtil.getIndicesOfIntersection(source, target));
 67
 68      target = new String[] { "jabberwocky", "ack" };
 69      GroovyTestCase.assertEquals(new int[] { 2 },
 70         DKArrayUtil.getIndicesOfIntersection(source, target));
 71
 72   }
 73
 74   public void testRetain() {
 75      String[] target = new String[] { "beware", "the", "jabberwocky" };
 76
 77      Assert.assertNull(DKArrayUtil.retainElementsAtIndices(null, null));
 78      Assert.assertNull(DKArrayUtil.retainElementsAtIndices(target, null));
 79      Assert.assertNull(DKArrayUtil.retainElementsAtIndices(null, new int[] { 2 }));
 80      Assert.assertNull(DKArrayUtil.retainElementsAtIndices(null, new int[0]));
 81      Assert.assertNull(DKArrayUtil.retainElementsAtIndices(target, new int[0]));
 82      GroovyTestCase.assertEquals(target,
 83         DKArrayUtil.retainElementsAtIndices(target, new int[] { 0, 1, 2 }));
 84      GroovyTestCase.assertEquals(new String[] { "the" },
 85         DKArrayUtil.retainElementsAtIndices(target, new int[] { 1 }));
 86
 87   }
 88
 89   public void testIntersection() {
 90      String[] source = new String[] { "beware", "the", "jabberwocky" };
 91      String[] target = null;
 92      Assert.assertNull(DKArrayUtil.getIntersection(source, target));
 93      Assert.assertNull(DKArrayUtil.getIntersection(target, source));
 94
 95      target = new String[0];
 96      GroovyTestCase.assertEquals(new String[0],
 97         DKArrayUtil.getIntersection(source, target));
 98      GroovyTestCase.assertEquals(new String[0],
 99         DKArrayUtil.getIntersection(target, source));
100
101      target = new String[] { "ack", "awk", "hack" };
102      GroovyTestCase.assertEquals(new String[0],
103         DKArrayUtil.getIntersection(source, target));
104      GroovyTestCase.assertEquals(new String[0],
105         DKArrayUtil.getIntersection(target, source));
106
107      target = new String[] { "beware", "the", "jabberwocky" };
108      GroovyTestCase.assertEquals(source, DKArrayUtil.getIntersection(source, target));
109      GroovyTestCase.assertEquals(source, DKArrayUtil.getIntersection(target, source));
110
111      target = new String[] { "jabberwocky", "the", "beware" };
112      GroovyTestCase.assertEquals(source, DKArrayUtil.getIntersection(source, target));
113      GroovyTestCase.assertEquals(target, DKArrayUtil.getIntersection(target, source));
114
115      target = new String[] { "jabberwocky" };
116      GroovyTestCase.assertEquals(target, DKArrayUtil.getIntersection(source, target));
117
118      target = new String[] { "jabberwocky", "ack" };
119      GroovyTestCase.assertEquals(new String[] { "jabberwocky" },
120         DKArrayUtil.getIntersection(source, target));
121
122   }
123
124   public void testSubarray() {
125      String[] array = new String[] { "beware", "the", "jabberwocky" };
126      this.assertArrayEquals(array, DKArrayUtil.subarray(array, 0, array.length));
127      this.assertArrayEquals(new String[] { "beware", "the" },
128         DKArrayUtil.subarray(array, 0, 2));
129      this.assertArrayEquals(new String[] { "the", "jabberwocky" },
130         DKArrayUtil.subarray(array, 1, 3));
131   }
132
133   public void testCreateArray() {
134      String[] array = DKArrayUtil.createArray(String.class, 5);
135      Assert.assertNotNull(array);
136      try {
137         String[] oArray = (String[]) DKArrayUtil.createArray(Object.class, 5);
138         fail("Should raise an ClassCastException");
139      }
140      catch (ClassCastException e) {
141      }
142   }
143}