1   package net.sf.classifier4J.vector;
2   
3   import junit.framework.TestCase;
4   
5   public class VectorUtilsTest extends TestCase {
6   
7       public void testScalarProduct() {
8           try {
9               double result = VectorUtils.scalarProduct(new int[] { 1, 2, 3 }, null);
10              fail("Null argument allowed");
11          } catch (IllegalArgumentException e) {
12              // expected
13          }
14          
15          try {
16              double result = VectorUtils.scalarProduct(null, new int[] { 1, 2, 3 });
17              fail("Null argument allowed");
18          } catch (IllegalArgumentException e) {
19              // expected
20          }
21  
22          try {
23              double result = VectorUtils.scalarProduct(new int[] {1}, new int[] { 1, 2, 3 });
24              fail("Arguments of different size allowed");
25          } catch (IllegalArgumentException e) {
26              // expected
27          }
28          
29          assertEquals(3, VectorUtils.scalarProduct(new int[] {1,1,1}, new int[] { 1,1,1}));
30          assertEquals(6, VectorUtils.scalarProduct(new int[] {1,1,1}, new int[] { 1,2,3}));
31          assertEquals(14, VectorUtils.scalarProduct(new int[] {1,2,3}, new int[] { 1,2,3 }));
32          assertEquals(0, VectorUtils.scalarProduct(new int[] {0,0,0}, new int[] { 1,2,3 }));
33  
34      }
35  
36      public void testVectorLength() {
37          try {
38              double result = VectorUtils.vectorLength(null);
39              fail("Null argument allowed");
40          } catch (IllegalArgumentException e) {
41              // expected
42          }        
43      
44          assertEquals(Math.sqrt(2), VectorUtils.vectorLength(new int[]{1,1}),0.001d);
45          assertEquals(Math.sqrt(3), VectorUtils.vectorLength(new int[]{1,1,1}),0.001d);
46          assertEquals(Math.sqrt(12), VectorUtils.vectorLength(new int[]{2,2,2}),0.001d);
47      }
48  
49      public void testCosineOfVectors() {
50          try {
51              double result = VectorUtils.cosineOfVectors(new int[] { 1, 2, 3 }, null);
52              fail("Null argument allowed");
53          } catch (IllegalArgumentException e) {
54              // expected
55          }
56          
57          try {
58              double result = VectorUtils.cosineOfVectors(null, new int[] { 1, 2, 3 });
59              fail("Null argument allowed");
60          } catch (IllegalArgumentException e) {
61              // expected
62          }
63          
64          try {
65              double result = VectorUtils.cosineOfVectors(new int[] {1}, new int[] { 1, 2, 3 });
66              fail("Arguments of different size allowed");
67          } catch (IllegalArgumentException e) {
68              // expected
69          }        
70          
71          int[] one = new int[]{1,1,1};
72          int[] two = new int[]{1,1,1};
73          
74          assertEquals(1d, VectorUtils.cosineOfVectors(one, two), 0.001);
75      }    
76      
77  }