View Javadoc

1   package de.bwaldvogel.liblinear;
2   
3   import static org.fest.assertions.Assertions.assertThat;
4   import static org.junit.Assert.fail;
5   
6   import org.junit.Before;
7   import org.junit.Test;
8   
9   
10  public class ParameterTest {
11  
12      private Parameter _param;
13  
14      @Before
15      public void setUp() {
16          _param = new Parameter(SolverType.L2R_L1LOSS_SVC_DUAL, 100, 1e-3);
17      }
18  
19      @Test
20      public void testSetWeights() {
21  
22          assertThat(_param.weight).isNull();
23          assertThat(_param.getNumWeights()).isEqualTo(0);
24  
25          double[] weights = new double[] {0, 1, 2, 3, 4, 5};
26          int[] weightLabels = new int[] {1, 1, 1, 1, 2, 3};
27          _param.setWeights(weights, weightLabels);
28  
29          assertThat(_param.getNumWeights()).isEqualTo(6);
30  
31          // assert parameter uses a copy
32          weights[0]++;
33          assertThat(_param.getWeights()[0]).isEqualTo(0);
34          weightLabels[0]++;
35          assertThat(_param.getWeightLabels()[0]).isEqualTo(1);
36  
37          weights = new double[] {0, 1, 2, 3, 4, 5};
38          weightLabels = new int[] {1};
39          try {
40              _param.setWeights(weights, weightLabels);
41              fail("IllegalArgumentException expected");
42          } catch (IllegalArgumentException e) {
43              assertThat(e.getMessage()).contains("same").contains("length");
44          }
45      }
46  
47      @Test
48      public void testGetWeights() {
49          double[] weights = new double[] {0, 1, 2, 3, 4, 5};
50          int[] weightLabels = new int[] {1, 1, 1, 1, 2, 3};
51          _param.setWeights(weights, weightLabels);
52  
53          assertThat(_param.getWeights()).isEqualTo(weights);
54          _param.getWeights()[0]++; // shouldn't change the parameter as we should get a copy
55          assertThat(_param.getWeights()).isEqualTo(weights);
56  
57          assertThat(_param.getWeightLabels()).isEqualTo(weightLabels);
58          _param.getWeightLabels()[0]++; // shouldn't change the parameter as we should get a copy
59          assertThat(_param.getWeightLabels()[0]).isEqualTo(1);
60      }
61  
62      @Test
63      public void testSetC() {
64          _param.setC(0.0001);
65          assertThat(_param.getC()).isEqualTo(0.0001);
66          _param.setC(1);
67          _param.setC(100);
68          assertThat(_param.getC()).isEqualTo(100);
69          _param.setC(Double.MAX_VALUE);
70  
71          try {
72              _param.setC(-1);
73              fail("IllegalArgumentException expected");
74          } catch (IllegalArgumentException e) {
75              assertThat(e.getMessage()).contains("must").contains("not").contains("<= 0");
76          }
77  
78          try {
79              _param.setC(0);
80              fail("IllegalArgumentException expected");
81          } catch (IllegalArgumentException e) {
82              assertThat(e.getMessage()).contains("must").contains("not").contains("<= 0");
83          }
84      }
85  
86      @Test
87      public void testSetEps() {
88          _param.setEps(0.0001);
89          assertThat(_param.getEps()).isEqualTo(0.0001);
90          _param.setEps(1);
91          _param.setEps(100);
92          assertThat(_param.getEps()).isEqualTo(100);
93          _param.setEps(Double.MAX_VALUE);
94  
95          try {
96              _param.setEps(-1);
97              fail("IllegalArgumentException expected");
98          } catch (IllegalArgumentException e) {
99              assertThat(e.getMessage()).contains("must").contains("not").contains("<= 0");
100         }
101 
102         try {
103             _param.setEps(0);
104             fail("IllegalArgumentException expected");
105         } catch (IllegalArgumentException e) {
106             assertThat(e.getMessage()).contains("must").contains("not").contains("<= 0");
107         }
108     }
109 
110     @Test
111     public void testSetSolverType() {
112         for (SolverType type : SolverType.values()) {
113             _param.setSolverType(type);
114             assertThat(_param.getSolverType()).isEqualTo(type);
115         }
116         try {
117             _param.setSolverType(null);
118             fail("IllegalArgumentException expected");
119         } catch (IllegalArgumentException e) {
120             assertThat(e.getMessage()).contains("must").contains("not").contains("null");
121         }
122     }
123 
124 }