source: src/test/java/weka/core/RangeTest.java @ 27

Last change on this file since 27 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 5.4 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 * Copyright (C) 2006 University of Waikato
19 */
20
21package weka.core;
22
23import junit.framework.Test;
24import junit.framework.TestCase;
25import junit.framework.TestSuite;
26
27/**
28 * Tests Range. Run from the command line with:<p/>
29 * java weka.core.RangeTest
30 *
31 * @author FracPete (fracpete at waikato dot ac dot nz)
32 * @version $Revision: 1.1 $
33 */
34public class RangeTest 
35  extends TestCase {
36 
37  /**
38   * Constructs the <code>RangeTest</code>.
39   *
40   * @param name the name of the test class
41   */
42  public RangeTest(String name) { 
43    super(name); 
44  }
45 
46  /**
47   * Called by JUnit before each test method.
48   *
49   * @throws Exception if an error occurs
50   */
51  protected void setUp() throws Exception {
52    super.setUp();
53  }
54
55  /** Called by JUnit after each test method */
56  protected void tearDown() throws Exception {
57    super.tearDown();
58  }
59
60  /**
61   * returns a configured Range
62   *
63   * @param initial     the initial string, if null the default constructor
64   *                    is used (and "1" is set - otherwise setUpper doesn't
65   *                    work!)
66   * @param upper       the upper limit
67   */
68  protected Range getRange(String initial, int upper) {
69    Range   result; 
70
71    if (initial == null) {
72      result = new Range();
73      result.setRanges("1");
74      result.setUpper(upper);
75    }
76    else {
77      result = new Range(initial);
78      result.setUpper(upper);
79    }
80
81    return result;
82  }
83
84  /**
85   * test the default constructor
86   */
87  public void testDefaultConstructor() throws Exception {
88    int upper = 10;
89    int indexInt = 0;
90    String indexStr = "" + (indexInt + 1);
91    Range index = new Range();
92    index.setRanges(indexStr);
93    index.setUpper(upper);
94
95    assertEquals(indexStr, index.getRanges());
96    assertEquals(1, index.getSelection().length);
97    assertEquals(indexInt, index.getSelection()[0]);
98  }
99
100  /**
101   * tests the constructor with initial value
102   */
103  public void testInitialValueConstructor() throws Exception {
104    int upper = 10;
105    int indexInt = 0;
106    String indexStr = "" + (indexInt + 1);
107    Range index = getRange("1", upper);
108
109    assertEquals(indexStr, index.getRanges());
110    assertEquals(1, index.getSelection().length);
111    assertEquals(indexInt, index.getSelection()[0]);
112  }
113
114  /**
115   * tests whether "first" is interpreted correctly
116   */
117  public void testFirst() throws Exception {
118    int upper = 10;
119    Range index = getRange("first", upper);
120
121    assertEquals("first", index.getRanges());
122    assertEquals(1, index.getSelection().length);
123    assertEquals(0, index.getSelection()[0]);
124  }
125
126  /**
127   * tests whether "last" is interpreted correctly
128   */
129  public void testLast() throws Exception {
130    int upper = 10;
131    Range index = getRange("last", upper);
132
133    assertEquals("last", index.getRanges());
134    assertEquals(1, index.getSelection().length);
135    assertEquals(upper, index.getSelection()[0]);
136  }
137
138  /**
139   * tests whether "first-last" is interpreted correctly
140   */
141  public void testFirstLast() throws Exception {
142    int upper = 10;
143    Range index = getRange("first-last", upper);
144
145    assertEquals("first-last", index.getRanges());
146    assertEquals(upper + 1, index.getSelection().length);
147  }
148
149  /**
150   * tests whether a simple "range" is interpreted correctly
151   */
152  public void testSimpleRange() throws Exception {
153    int upper = 10;
154    String range = "1-3";
155    Range index = getRange(range, upper);
156    int[] expected = new int[]{0,1,2};
157
158    assertEquals(range, index.getRanges());
159    assertEquals(expected.length, index.getSelection().length);
160    for (int i = 0; i < expected.length; i++)
161      assertEquals(expected[i], index.getSelection()[i]);
162  }
163
164  /**
165   * tests whether a mixed "range" is interpreted correctly
166   */
167  public void testMixedRange() throws Exception {
168    int upper = 10;
169    String range = "1-3,6,8-last";
170    Range index = getRange(range, upper);
171    int[] expected = new int[]{0,1,2,5,7,8,9,10};
172
173    assertEquals(range, index.getRanges());
174    assertEquals(expected.length, index.getSelection().length);
175    for (int i = 0; i < expected.length; i++)
176      assertEquals(expected[i], index.getSelection()[i]);
177  }
178
179  /**
180   * tests whether an unordered "range" is interpreted correctly
181   */
182  public void testUnorderedRange() throws Exception {
183    int upper = 10;
184    String range = "8-last,1-3,6";
185    Range index = getRange(range, upper);
186    int[] expected = new int[]{7,8,9,10,0,1,2,5};
187
188    assertEquals(range, index.getRanges());
189    assertEquals(expected.length, index.getSelection().length);
190    for (int i = 0; i < expected.length; i++)
191      assertEquals(expected[i], index.getSelection()[i]);
192  }
193
194  public static Test suite() {
195    return new TestSuite(RangeTest.class);
196  }
197
198  public static void main(String[] args){
199    junit.textui.TestRunner.run(suite());
200  }
201}
Note: See TracBrowser for help on using the repository browser.