/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. */
  16. package org.diffkit.util.tst;
  17. import groovy.util.GroovyTestCase;
  18. import junit.framework.Assert;
  19. import org.diffkit.util.DKArrayUtil;
  20. /**
  21. * @author jpanico
  22. */
  23. public class JTestArrayUtil extends GroovyTestCase {
  24. public void testRemoveMe() {
  25. char var = 'A' + 1;
  26. System.out.println("var->" + var);
  27. System.out.println("A+B->" + ("" + 'A' + 'B' + '\0' + 'C'));
  28. }
  29. public void testIndicesOfIntersection() {
  30. String[] source = new String[] { "beware", "the", "jabberwocky" };
  31. String[] target = null;
  32. Assert.assertNull(DKArrayUtil.getIndicesOfIntersection(source, target));
  33. Assert.assertNull(DKArrayUtil.getIndicesOfIntersection(target, source));
  34. target = new String[0];
  35. GroovyTestCase.assertEquals(null,
  36. DKArrayUtil.getIndicesOfIntersection(source, target));
  37. GroovyTestCase.assertEquals(null,
  38. DKArrayUtil.getIndicesOfIntersection(target, source));
  39. target = new String[] { "ack", "awk", "hack" };
  40. GroovyTestCase.assertEquals(null,
  41. DKArrayUtil.getIndicesOfIntersection(source, target));
  42. GroovyTestCase.assertEquals(null,
  43. DKArrayUtil.getIndicesOfIntersection(target, source));
  44. target = new String[] { "beware", "the", "jabberwocky" };
  45. GroovyTestCase.assertEquals(new int[] { 0, 1, 2 },
  46. DKArrayUtil.getIndicesOfIntersection(source, target));
  47. GroovyTestCase.assertEquals(new int[] { 0, 1, 2 },
  48. DKArrayUtil.getIndicesOfIntersection(target, source));
  49. target = new String[] { "jabberwocky", "the", "beware" };
  50. GroovyTestCase.assertEquals(new int[] { 0, 1, 2 },
  51. DKArrayUtil.getIndicesOfIntersection(source, target));
  52. GroovyTestCase.assertEquals(new int[] { 0, 1, 2 },
  53. DKArrayUtil.getIndicesOfIntersection(target, source));
  54. target = new String[] { "jabberwocky" };
  55. GroovyTestCase.assertEquals(new int[] { 2 },
  56. DKArrayUtil.getIndicesOfIntersection(source, target));
  57. target = new String[] { "jabberwocky", "ack" };
  58. GroovyTestCase.assertEquals(new int[] { 2 },
  59. DKArrayUtil.getIndicesOfIntersection(source, target));
  60. }
  61. public void testRetain() {
  62. String[] target = new String[] { "beware", "the", "jabberwocky" };
  63. Assert.assertNull(DKArrayUtil.retainElementsAtIndices(null, null));
  64. Assert.assertNull(DKArrayUtil.retainElementsAtIndices(target, null));
  65. Assert.assertNull(DKArrayUtil.retainElementsAtIndices(null, new int[] { 2 }));
  66. Assert.assertNull(DKArrayUtil.retainElementsAtIndices(null, new int[0]));
  67. Assert.assertNull(DKArrayUtil.retainElementsAtIndices(target, new int[0]));
  68. GroovyTestCase.assertEquals(target,
  69. DKArrayUtil.retainElementsAtIndices(target, new int[] { 0, 1, 2 }));
  70. GroovyTestCase.assertEquals(new String[] { "the" },
  71. DKArrayUtil.retainElementsAtIndices(target, new int[] { 1 }));
  72. }
  73. public void testIntersection() {
  74. String[] source = new String[] { "beware", "the", "jabberwocky" };
  75. String[] target = null;
  76. Assert.assertNull(DKArrayUtil.getIntersection(source, target));
  77. Assert.assertNull(DKArrayUtil.getIntersection(target, source));
  78. target = new String[0];
  79. GroovyTestCase.assertEquals(new String[0],
  80. DKArrayUtil.getIntersection(source, target));
  81. GroovyTestCase.assertEquals(new String[0],
  82. DKArrayUtil.getIntersection(target, source));
  83. target = new String[] { "ack", "awk", "hack" };
  84. GroovyTestCase.assertEquals(new String[0],
  85. DKArrayUtil.getIntersection(source, target));
  86. GroovyTestCase.assertEquals(new String[0],
  87. DKArrayUtil.getIntersection(target, source));
  88. target = new String[] { "beware", "the", "jabberwocky" };
  89. GroovyTestCase.assertEquals(source, DKArrayUtil.getIntersection(source, target));
  90. GroovyTestCase.assertEquals(source, DKArrayUtil.getIntersection(target, source));
  91. target = new String[] { "jabberwocky", "the", "beware" };
  92. GroovyTestCase.assertEquals(source, DKArrayUtil.getIntersection(source, target));
  93. GroovyTestCase.assertEquals(target, DKArrayUtil.getIntersection(target, source));
  94. target = new String[] { "jabberwocky" };
  95. GroovyTestCase.assertEquals(target, DKArrayUtil.getIntersection(source, target));
  96. target = new String[] { "jabberwocky", "ack" };
  97. GroovyTestCase.assertEquals(new String[] { "jabberwocky" },
  98. DKArrayUtil.getIntersection(source, target));
  99. }
  100. public void testSubarray() {
  101. String[] array = new String[] { "beware", "the", "jabberwocky" };
  102. this.assertArrayEquals(array, DKArrayUtil.subarray(array, 0, array.length));
  103. this.assertArrayEquals(new String[] { "beware", "the" },
  104. DKArrayUtil.subarray(array, 0, 2));
  105. this.assertArrayEquals(new String[] { "the", "jabberwocky" },
  106. DKArrayUtil.subarray(array, 1, 3));
  107. }
  108. public void testCreateArray() {
  109. String[] array = DKArrayUtil.createArray(String.class, 5);
  110. Assert.assertNotNull(array);
  111. try {
  112. String[] oArray = (String[]) DKArrayUtil.createArray(Object.class, 5);
  113. fail("Should raise an ClassCastException");
  114. }
  115. catch (ClassCastException e) {
  116. }
  117. }
  118. }