Menu

[572a65]: / src / junit / tools / TestFileHandler.java  Maximize  Restore  History

Download this file

126 lines (108 with data), 3.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* %SVN.HEADER%
*/
package junit.tools;
import java.io.File;
import java.io.IOException;
import java.util.StringTokenizer;
import net.sf.javaml.core.Dataset;
import net.sf.javaml.core.DefaultDataset;
import net.sf.javaml.core.Instance;
import net.sf.javaml.tools.InstanceTools;
import net.sf.javaml.tools.data.FileHandler;
import org.junit.Assert;
import org.junit.Test;
import be.abeel.io.LineIterator;
public class TestFileHandler {
@Test
public void testReadDense() {
Dataset data = new DefaultDataset();
for (int i = 0; i < 10; i++) {
Instance tmpInstance = InstanceTools.randomInstance(25);
tmpInstance.setClassValue("test");
data.add(tmpInstance);
}
try {
FileHandler.exportDataset(data, new File("testfile.txt"));
} catch (IOException e) {
e.printStackTrace();
Assert.fail();
}
try {
data = FileHandler.loadDataset(new File("testfile.txt"),0);
} catch (IOException e) {
e.printStackTrace();
Assert.fail();
}
for (Instance i : data) {
Assert.assertEquals("test",i.classValue());
}
if (!new File("testfile.txt").delete()) {
Assert.fail("Failed to remove test file");
}
}
@Test
public void testWriteDense() {
Dataset data = new DefaultDataset();
for (int i = 0; i < 10; i++) {
Instance tmpInstance = InstanceTools.randomInstance(25);
tmpInstance.setClassValue("test");
data.add(tmpInstance);
}
try {
FileHandler.exportDataset(data, new File("testfile.txt"));
} catch (IOException e) {
e.printStackTrace();
Assert.fail();
}
LineIterator it = new LineIterator(new File("testfile.txt"));
for (String line : it) {
if (!line.startsWith("test"))
Assert.fail("All lines should start with the class value");
}
it.close();
if (!new File("testfile.txt").delete()) {
Assert.fail("Failed to remove test file");
}
}
@Test
public void testReadSparse() {
try {
Dataset data = FileHandler.loadSparseDataset(new File(
"devtools/data/testsparse.txt"), 0, " ", ":");
Assert.assertEquals(52, data.noAttributes());
} catch (IOException e) {
e.printStackTrace();
Assert.fail();
}
}
@Test
public void testReadSparseX() {
Dataset data = new DefaultDataset();
for (String line : new LineIterator("devtools/data/testsparse.txt")) {
net.sf.javaml.core.SparseInstance inst = new net.sf.javaml.core.SparseInstance();
StringTokenizer stok = new StringTokenizer(line);
while (stok.hasMoreTokens()) {
String tok = stok.nextToken();
if (tok.equals("-1")) {
inst.setClassValue(new Integer(0));
} else {
if (tok.equals("1")) {
inst.setClassValue(new Integer(1));
} else {
int pos = Integer.parseInt(tok.substring(0,
tok.indexOf(":")));
double value = Double.parseDouble(tok.substring(tok
.indexOf(":") + 1));
// System.out.println("pos/value : " + pos + "/" +
// value);
inst.put(pos, value);
}
}
}
data.add(inst);
// line = reader.readLine();
}
Assert.assertEquals(52, data.noAttributes());
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.