Skip to content

Commit 6d40913

Browse files
committed
Added JUnit tests for SparseIntegerVector
1 parent 670d56c commit 6d40913

File tree

4 files changed

+204
-3
lines changed

4 files changed

+204
-3
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@
127127

128128
</plugins>
129129
</build>
130+
<dependencies>
131+
<dependency>
132+
<groupId>junit</groupId>
133+
<artifactId>junit</artifactId>
134+
<version>4.10</version>
135+
<scope>test</scope>
136+
</dependency>
137+
</dependencies>
130138
</project>
131139

132140

src/main/java/info/debatty/java/stringsimilarity/examples/PrecomputedCosine.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import info.debatty.java.stringsimilarity.StringProfile;
2929

3030
/**
31-
*
31+
* Example of computing cosine similarity with pre-computed profiles
3232
* @author tibo
3333
*/
3434
public class PrecomputedCosine {
@@ -43,7 +43,7 @@ public static void main(String[] args) throws Exception {
4343
// Let's work with sequences of 2 characters...
4444
KShingling ks = new KShingling(2);
4545

46-
// For cosine similarity I need the profile of strings
46+
// Pre-compute the profile of strings
4747
StringProfile profile1 = ks.getProfile(s1);
4848
StringProfile profile2 = ks.getProfile(s2);
4949

src/main/java/info/debatty/java/utils/SparseIntegerVector.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,16 @@ public double dotProduct(SparseIntegerVector other) {
108108
return agg;
109109
}
110110

111+
public double dotProduct(double[] other) {
112+
double agg = 0;
113+
for (int i = 0; i < keys.length; i++) {
114+
agg += other[keys[i]] * values[i];
115+
}
116+
return agg;
117+
}
118+
111119
/**
112-
*
120+
* Compute and return the L2 norm of the vector
113121
* @return
114122
*/
115123
public double norm() {
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright 2015 tibo.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package info.debatty.java.utils;
26+
27+
import org.junit.After;
28+
import org.junit.AfterClass;
29+
import org.junit.Before;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
import static org.junit.Assert.*;
33+
34+
/**
35+
*
36+
* @author tibo
37+
*/
38+
public class SparseIntegerVectorTest {
39+
40+
public SparseIntegerVectorTest() {
41+
}
42+
43+
@BeforeClass
44+
public static void setUpClass() {
45+
}
46+
47+
@AfterClass
48+
public static void tearDownClass() {
49+
}
50+
51+
@Before
52+
public void setUp() {
53+
}
54+
55+
@After
56+
public void tearDown() {
57+
}
58+
59+
/**
60+
* Test of dotProduct method, of class SparseIntegerVector.
61+
*/
62+
@Test
63+
public void testDotProduct_SparseIntegerVector() {
64+
System.out.println("dotProduct");
65+
SparseIntegerVector other = new SparseIntegerVector(new int[]{0, 2, 0, 1});
66+
SparseIntegerVector instance = new SparseIntegerVector(new int[]{1, 2, 1, 0});
67+
double expResult = 4.0;
68+
double result = instance.dotProduct(other);
69+
assertEquals(expResult, result, 0.0);
70+
}
71+
72+
/**
73+
* Test of dotProduct method, of class SparseIntegerVector.
74+
*/
75+
@Test
76+
public void testDotProduct_doubleArr() {
77+
System.out.println("dotProduct");
78+
double[] other = new double[]{0, 1.5, 2.0, 3.0};
79+
SparseIntegerVector instance = new SparseIntegerVector(new int[]{1, 2, 0, 0});
80+
double expResult = 3.0;
81+
double result = instance.dotProduct(other);
82+
assertEquals(expResult, result, 0.0);
83+
}
84+
85+
/**
86+
* Test of norm method, of class SparseIntegerVector.
87+
*/
88+
@Test
89+
public void testNorm() {
90+
System.out.println("norm");
91+
SparseIntegerVector instance = new SparseIntegerVector(new int[]{0, 0, 2});
92+
double expResult = 2.0;
93+
double result = instance.norm();
94+
assertEquals(expResult, result, 0.0);
95+
}
96+
97+
/**
98+
* Test of jaccard method, of class SparseIntegerVector.
99+
*/
100+
@Test
101+
public void testJaccard() {
102+
System.out.println("jaccard");
103+
SparseIntegerVector other = null;
104+
SparseIntegerVector instance = new SparseIntegerVector();
105+
double expResult = 0.0;
106+
double result = instance.jaccard(other);
107+
assertEquals(expResult, result, 0.0);
108+
// TODO review the generated test code and remove the default call to fail.
109+
fail("The test case is a prototype.");
110+
}
111+
112+
/**
113+
* Test of union method, of class SparseIntegerVector.
114+
*/
115+
@Test
116+
public void testUnion() {
117+
System.out.println("union");
118+
SparseIntegerVector other = null;
119+
SparseIntegerVector instance = new SparseIntegerVector();
120+
int expResult = 0;
121+
int result = instance.union(other);
122+
assertEquals(expResult, result);
123+
// TODO review the generated test code and remove the default call to fail.
124+
fail("The test case is a prototype.");
125+
}
126+
127+
/**
128+
* Test of intersection method, of class SparseIntegerVector.
129+
*/
130+
@Test
131+
public void testIntersection() {
132+
System.out.println("intersection");
133+
SparseIntegerVector other = null;
134+
SparseIntegerVector instance = new SparseIntegerVector();
135+
int expResult = 0;
136+
int result = instance.intersection(other);
137+
assertEquals(expResult, result);
138+
// TODO review the generated test code and remove the default call to fail.
139+
fail("The test case is a prototype.");
140+
}
141+
142+
/**
143+
* Test of toString method, of class SparseIntegerVector.
144+
*/
145+
@Test
146+
public void testToString() {
147+
System.out.println("toString");
148+
SparseIntegerVector instance = new SparseIntegerVector();
149+
String expResult = "";
150+
String result = instance.toString();
151+
assertEquals(expResult, result);
152+
// TODO review the generated test code and remove the default call to fail.
153+
fail("The test case is a prototype.");
154+
}
155+
156+
/**
157+
* Test of qgram method, of class SparseIntegerVector.
158+
*/
159+
@Test
160+
public void testQgram() {
161+
System.out.println("qgram");
162+
SparseIntegerVector other = null;
163+
SparseIntegerVector instance = new SparseIntegerVector();
164+
double expResult = 0.0;
165+
double result = instance.qgram(other);
166+
assertEquals(expResult, result, 0.0);
167+
// TODO review the generated test code and remove the default call to fail.
168+
fail("The test case is a prototype.");
169+
}
170+
171+
/**
172+
* Test of size method, of class SparseIntegerVector.
173+
*/
174+
@Test
175+
public void testSize() {
176+
System.out.println("size");
177+
SparseIntegerVector instance = new SparseIntegerVector();
178+
int expResult = 0;
179+
int result = instance.size();
180+
assertEquals(expResult, result);
181+
// TODO review the generated test code and remove the default call to fail.
182+
fail("The test case is a prototype.");
183+
}
184+
185+
}

0 commit comments

Comments
 (0)