forked from bwaldvogel/liblinear-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParameterTest.java
More file actions
124 lines (101 loc) · 3.87 KB
/
Copy pathParameterTest.java
File metadata and controls
124 lines (101 loc) · 3.87 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
package liblinear;
import static org.fest.assertions.Assertions.assertThat;
import static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.Test;
public class ParameterTest {
private Parameter _param;
@Before
public void setUp() {
_param = new Parameter(SolverType.L2R_L1LOSS_SVC_DUAL, 100, 1e-3);
}
@Test
public void testSetWeights() {
assertThat(_param.weight).isNull();
assertThat(_param.getNumWeights()).isEqualTo(0);
double[] weights = new double[] {0, 1, 2, 3, 4, 5};
int[] weightLabels = new int[] {1, 1, 1, 1, 2, 3};
_param.setWeights(weights, weightLabels);
assertThat(_param.getNumWeights()).isEqualTo(6);
// assert parameter uses a copy
weights[0]++;
assertThat(_param.getWeights()[0]).isEqualTo(0);
weightLabels[0]++;
assertThat(_param.getWeightLabels()[0]).isEqualTo(1);
weights = new double[] {0, 1, 2, 3, 4, 5};
weightLabels = new int[] {1};
try {
_param.setWeights(weights, weightLabels);
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage()).contains("same").contains("length");
}
}
@Test
public void testGetWeights() {
double[] weights = new double[] {0, 1, 2, 3, 4, 5};
int[] weightLabels = new int[] {1, 1, 1, 1, 2, 3};
_param.setWeights(weights, weightLabels);
assertThat(_param.getWeights()).isEqualTo(weights);
_param.getWeights()[0]++; // shouldn't change the parameter as we should get a copy
assertThat(_param.getWeights()).isEqualTo(weights);
assertThat(_param.getWeightLabels()).isEqualTo(weightLabels);
_param.getWeightLabels()[0]++; // shouldn't change the parameter as we should get a copy
assertThat(_param.getWeightLabels()[0]).isEqualTo(1);
}
@Test
public void testSetC() {
_param.setC(0.0001);
assertThat(_param.getC()).isEqualTo(0.0001);
_param.setC(1);
_param.setC(100);
assertThat(_param.getC()).isEqualTo(100);
_param.setC(Double.MAX_VALUE);
try {
_param.setC(-1);
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage()).contains("must").contains("not").contains("<= 0");
}
try {
_param.setC(0);
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage()).contains("must").contains("not").contains("<= 0");
}
}
@Test
public void testSetEps() {
_param.setEps(0.0001);
assertThat(_param.getEps()).isEqualTo(0.0001);
_param.setEps(1);
_param.setEps(100);
assertThat(_param.getEps()).isEqualTo(100);
_param.setEps(Double.MAX_VALUE);
try {
_param.setEps(-1);
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage()).contains("must").contains("not").contains("<= 0");
}
try {
_param.setEps(0);
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage()).contains("must").contains("not").contains("<= 0");
}
}
@Test
public void testSetSolverType() {
for (SolverType type : SolverType.values()) {
_param.setSolverType(type);
assertThat(_param.getSolverType()).isEqualTo(type);
}
try {
_param.setSolverType(null);
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage()).contains("must").contains("not").contains("null");
}
}
}