source: branches/MetisMQI/src/test/java/weka/core/AlgVectorTest.java

Last change on this file was 29, checked in by gnappo, 15 years ago

Taggata versione per la demo e aggiunto branch.

File size: 4.9 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) 2007 University of Waikato
19 */
20
21package weka.core;
22
23import java.util.Random;
24
25import junit.framework.Test;
26import junit.framework.TestCase;
27import junit.framework.TestSuite;
28
29/**
30 * Tests AlgVector. Run from the command line with:<p/>
31 * java weka.core.AlgVectorTest
32 *
33 * @author FracPete (fracpete at waikato dot ac dot nz)
34 * @version $Revision: 1.1 $
35 */
36public class AlgVectorTest 
37  extends TestCase {
38
39  /** for generating the datasets */
40  protected Random m_Random;
41 
42  /**
43   * Constructs the <code>AlgVectorTest</code>.
44   *
45   * @param name        the name of the test class
46   */
47  public AlgVectorTest(String name) { 
48    super(name); 
49  }
50 
51  /**
52   * Called by JUnit before each test method.
53   *
54   * @throws Exception  if an error occurs
55   */
56  protected void setUp() throws Exception {
57    super.setUp();
58   
59    m_Random = new Random(1);
60  }
61
62  /**
63   * Called by JUnit after each test method
64   *
65   * @throws Exception  if an error occurs
66   */
67  protected void tearDown() throws Exception {
68    super.tearDown();
69
70    m_Random = null;
71  }
72
73  /**
74   * generates data with the given amount of nominal and numeric attributes
75   *
76   * @param nominal     the number of nominal attributes
77   * @param numeric     the number of numeric attributes
78   * @param rows        the number of rows to generate
79   * @return            the generated data
80   */
81  protected Instances generateData(int nominal, int numeric, int rows) {
82    Instances           result;
83    TestInstances       test;
84   
85    test = new TestInstances();
86    test.setClassIndex(TestInstances.NO_CLASS);
87    test.setNumNominal(nominal);
88    test.setNumNumeric(numeric);
89    test.setNumInstances(rows);
90   
91    try {
92      result = test.generate();
93    }
94    catch (Exception e) {
95      result = null;
96    }
97   
98    return result;
99  }
100 
101  /**
102   * tests constructing a vector with a given length
103   */
104  public void testLengthConstructor() {
105    int len = 22;
106    AlgVector v = new AlgVector(len);
107    assertEquals("Length differs", len, v.numElements());
108  }
109
110  /**
111   * tests constructing a vector from an array
112   */
113  public void testArrayConstructor() {
114    double[] data = {2.3, 1.2, 5.0};
115    AlgVector v = new AlgVector(data);
116    assertEquals("Length differs", data.length, v.numElements());
117    for (int i = 0; i < data.length; i++)
118      assertEquals((i+1) + ". value differs", data[i], v.getElement(i));
119  }
120
121  /**
122   * runs tests with the given data
123   *
124   * @param data        the data to test with
125   */
126  protected void runTestOnData(Instances data) {
127    // count numeric atts
128    int numeric = 0;
129    for (int n = 0; n < data.numAttributes(); n++) {
130      if (data.attribute(n).isNumeric())
131        numeric++;
132    }
133   
134    // perform tests
135    for (int n = 0; n < data.numInstances(); n++) {
136      try {
137        AlgVector v = new AlgVector(data.instance(n));
138       
139        // 1. is length correct?
140        assertEquals((n+1) + ": length differs", numeric, v.numElements());
141       
142        // 2. are values correct?
143        int index = 0;
144        for (int i = 0; i < data.numAttributes(); i++) {
145          if (!data.attribute(i).isNumeric())
146            continue;
147          assertEquals((n+1) + "/" + (i+1) + ": value differs", data.instance(n).value(i), v.getElement(index));
148          index++;
149        }
150       
151        // 3. is instance returned correct?
152        Instance inst = v.getAsInstance(data, new Random(1));
153        for (int i = 0; i < data.numAttributes(); i++) {
154          if (!data.attribute(i).isNumeric())
155            continue;
156          assertEquals((n+1) + "/" + (i+1) + ": returned value differs", data.instance(n).value(i), inst.value(i));
157        }
158      }
159      catch (Exception e) {
160        if (!(e instanceof IllegalArgumentException))
161          fail(e.toString());
162      }
163    }
164  }
165 
166  /**
167   * tests constructing a vector from a purely numeric instance
168   */
169  public void testNumericInstances() {
170    runTestOnData(generateData(0, 5, 5));
171  }
172 
173  /**
174   * tests constructing a vector from a purely nominal instance
175   */
176  public void testNominalInstances() {
177    runTestOnData(generateData(5, 0, 5));
178  }
179 
180  /**
181   * tests constructing a vector from a mixed instance
182   */
183  public void testMixedInstances() {
184    runTestOnData(generateData(5, 5, 5));
185  }
186 
187  public static Test suite() {
188    return new TestSuite(AlgVectorTest.class);
189  }
190
191  public static void main(String[] args){
192    junit.textui.TestRunner.run(suite());
193  }
194}
Note: See TracBrowser for help on using the repository browser.