001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.schematic.internal.document; 017 018import static org.junit.Assert.assertEquals; 019import java.util.Iterator; 020import java.util.List; 021import org.junit.Before; 022import org.junit.Test; 023 024public class BasicArrayTest { 025 026 private BasicArray array; 027 028 @Before 029 public void beforeTest() { 030 array = new BasicArray(); 031 for (int i = 0; i != 10; ++i) { 032 array.addValue(i); 033 } 034 } 035 036 @Test 037 public void shouldReturnProperSubarray() { 038 List<?> subArray = array.subList(3, 8); 039 assertEquals(3, ((Integer)subArray.get(0)).intValue()); 040 assertEquals(4, ((Integer)subArray.get(1)).intValue()); 041 assertEquals(5, ((Integer)subArray.get(2)).intValue()); 042 assertEquals(6, ((Integer)subArray.get(3)).intValue()); 043 assertEquals(7, ((Integer)subArray.get(4)).intValue()); 044 045 Iterator<?> iter = subArray.iterator(); 046 int value = 3; 047 while (iter.hasNext()) { 048 assertEquals(value++, ((Integer)iter.next()).intValue()); 049 } 050 } 051 052 @Test 053 public void shouldCreateCopy() { 054 BasicArray array2 = new BasicArray(array.size()); 055 array2.addAllValues(array); 056 for (int i = 0; i != 10; ++i) { 057 assertEquals(i, ((Integer)array2.get(i)).intValue()); 058 } 059 } 060 061 @Test 062 public void shouldCreateCopyOfSublist() { 063 BasicArray array2 = new BasicArray(5); 064 array2.addAllValues(array.subList(3, 8)); 065 for (int i = 0; i != 5; ++i) { 066 assertEquals(i + 3, ((Integer)array2.get(i)).intValue()); 067 } 068 } 069}