1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.functor.generator.util;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.fail;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.commons.functor.BaseFunctorTest;
26  import org.junit.Test;
27  
28  /**
29   * @version $Revision: 1171255 $ $Date: 2011-09-15 22:27:39 +0200 (Thu, 15 Sep 2011) $
30   * @author Jason Horman (jason@jhorman.org)
31   * @author Rodney Waldhoff
32   */
33  @SuppressWarnings("unchecked")
34  public class TestIntegerRange extends BaseFunctorTest {
35  
36      protected Object makeFunctor() throws Exception {
37          return new IntegerRange(10, 20);
38      }
39  
40      // Tests
41      // ------------------------------------------------------------------------
42  
43      @Test
44      public void testGenerateListExample() {
45          // generates a collection of Integers from 0 (inclusive) to 10 (exclusive)
46          {
47              List list = (List)(new IntegerRange(0,10).to(new ArrayList()));
48              for (int i=0;i<10;i++) {
49                  assertEquals(new Integer(i),list.get(i));
50              }
51          }
52  
53          // generates a collection of Integers from 10 (inclusive) to 0 (exclusive)
54          {
55              List list = (List)(new IntegerRange(10,0).to(new ArrayList()));
56              for (int i=10;i>0;i--) {
57                  assertEquals(new Integer(i),list.get(10-i));
58              }
59          }
60      }
61  
62      @Test
63      public void testStepChecking() {
64          {
65              new IntegerRange(2, 2, 0); // step of 0 is ok when range is empty
66          }
67          {
68              new IntegerRange(2, 2, 1); // positive step is ok when range is empty
69          }
70          {
71              new IntegerRange(2, 2, -1); // negative step is ok when range is empty
72          }
73          {
74              new IntegerRange(0, 1, 10); // big steps are ok
75          }
76          {
77              new IntegerRange(1, 0, -10); // big steps are ok
78          }
79          try {
80              new IntegerRange(0, 1, 0);
81              fail("Expected IllegalArgumentException");
82          } catch(IllegalArgumentException e) {
83              // expected
84          }
85          try {
86              new IntegerRange(0, 1, -1);
87              fail("Expected IllegalArgumentException");
88          } catch(IllegalArgumentException e) {
89              // expected
90          }
91          try {
92              new IntegerRange(0, -1, 1);
93              fail("Expected IllegalArgumentException");
94          } catch(IllegalArgumentException e) {
95              // expected
96          }
97      }
98  
99      @Test
100     public void testObjectConstructor() {
101         IntegerRange range = new IntegerRange(new Integer(0), new Integer(5));
102         assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
103         range = new IntegerRange(new Integer(0), new Integer(5), new Integer(1));
104         assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
105     }
106 
107 
108     @Test
109     public void testReverseStep() {
110         IntegerRange range = new IntegerRange(10, 0, -2);
111         assertEquals("[10, 8, 6, 4, 2]", range.toCollection().toString());
112         assertEquals("[10, 8, 6, 4, 2]", range.toCollection().toString());
113     }
114 
115     @Test
116     public void testStep() {
117         IntegerRange range = new IntegerRange(0, 10, 2);
118         assertEquals("[0, 2, 4, 6, 8]", range.toCollection().toString());
119         assertEquals("[0, 2, 4, 6, 8]", range.toCollection().toString());
120     }
121 
122     @Test
123     public void testForwardRange() {
124         IntegerRange range = new IntegerRange(0, 5);
125         assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
126         assertEquals("[0, 1, 2, 3, 4]", range.toCollection().toString());
127     }
128 
129     @Test
130     public void testReverseRange() {
131         IntegerRange range = new IntegerRange(5, 0);
132         assertEquals("[5, 4, 3, 2, 1]", range.toCollection().toString());
133         assertEquals("[5, 4, 3, 2, 1]", range.toCollection().toString());
134     }
135 
136     @Test
137     public void testEdgeCase() {
138         IntegerRange range = new IntegerRange(Integer.MAX_VALUE - 3, Integer.MAX_VALUE);
139         assertEquals("[2147483644, 2147483645, 2147483646]", range.toCollection().toString());
140         assertEquals("[2147483644, 2147483645, 2147483646]", range.toCollection().toString());
141     }
142 
143     @Test
144     public void testEquals() {
145         IntegerRange range = new IntegerRange(1, 5);
146         assertObjectsAreEqual(range, range);
147         assertObjectsAreEqual(range, new IntegerRange(1, 5));
148         assertObjectsAreEqual(range, new IntegerRange(1, 5, 1));
149         assertObjectsAreEqual(range, new IntegerRange(new Long(1), new Long(5)));
150         assertObjectsAreEqual(range, new IntegerRange(new Long(1), new Long(5), new Long(1)));
151     }
152 
153 }