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) 2009 University of Waikato, Hamilton, New Zealand |
---|
19 | */ |
---|
20 | |
---|
21 | package weka.core.converters; |
---|
22 | |
---|
23 | import weka.core.Instance; |
---|
24 | import weka.core.Instances; |
---|
25 | |
---|
26 | import junit.framework.Test; |
---|
27 | import junit.framework.TestSuite; |
---|
28 | |
---|
29 | /** |
---|
30 | * Tests SVMLightLoader/SVMLightSaver. Run from the command line with:<p/> |
---|
31 | * java weka.core.converters.SVMLightTest |
---|
32 | * |
---|
33 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
34 | * @version $Revision: 5410 $ |
---|
35 | */ |
---|
36 | public class SVMLightTest |
---|
37 | extends AbstractFileConverterTest { |
---|
38 | |
---|
39 | /** |
---|
40 | * Constructs the <code>SVMLightTest</code>. |
---|
41 | * |
---|
42 | * @param name the name of the test class |
---|
43 | */ |
---|
44 | public SVMLightTest(String name) { |
---|
45 | super(name); |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * returns the loader used in the tests. |
---|
50 | * |
---|
51 | * @return the configured loader |
---|
52 | */ |
---|
53 | public AbstractLoader getLoader() { |
---|
54 | return new SVMLightLoader(); |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * returns the saver used in the tests. |
---|
59 | * |
---|
60 | * @return the configured saver |
---|
61 | */ |
---|
62 | public AbstractSaver getSaver() { |
---|
63 | return new SVMLightSaver(); |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | * Compare two datasets to see if they differ. Skips the equalHeaders |
---|
68 | * method, since the SVMLight format doesn't have any notion of attribute |
---|
69 | * names. |
---|
70 | * |
---|
71 | * @param data1 one set of instances |
---|
72 | * @param data2 the other set of instances |
---|
73 | * @throws Exception if the datasets differ |
---|
74 | */ |
---|
75 | protected void compareDatasets(Instances data1, Instances data2) |
---|
76 | throws Exception { |
---|
77 | |
---|
78 | if (!(data2.numInstances() == data1.numInstances())) { |
---|
79 | throw new Exception("number of instances has changed"); |
---|
80 | } |
---|
81 | for (int i = 0; i < data2.numInstances(); i++) { |
---|
82 | Instance orig = data1.instance(i); |
---|
83 | Instance copy = data2.instance(i); |
---|
84 | for (int j = 0; j < orig.numAttributes(); j++) { |
---|
85 | if (orig.isMissing(j)) { |
---|
86 | if (!copy.isMissing(j)) { |
---|
87 | throw new Exception("instances have changed"); |
---|
88 | } |
---|
89 | } else if (orig.value(j) != copy.value(j)) { |
---|
90 | throw new Exception("instances have changed"); |
---|
91 | } |
---|
92 | if (orig.weight() != copy.weight()) { |
---|
93 | throw new Exception("instance weights have changed"); |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * returns a test suite. |
---|
101 | * |
---|
102 | * @return the test suite |
---|
103 | */ |
---|
104 | public static Test suite() { |
---|
105 | return new TestSuite(SVMLightTest.class); |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * for running the test from commandline. |
---|
110 | * |
---|
111 | * @param args the commandline arguments - ignored |
---|
112 | */ |
---|
113 | public static void main(String[] args){ |
---|
114 | junit.textui.TestRunner.run(suite()); |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|