Skip to content

Commit d144c9b

Browse files
committed
New test and implementing a proper equals for Location
1 parent 69244e9 commit d144c9b

File tree

2 files changed

+109
-10
lines changed

2 files changed

+109
-10
lines changed

biojava-genome/src/main/java/org/biojava/nbio/genome/parsers/gff/Location.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -849,18 +849,37 @@ public String toString()
849849
return new String( "[L=" + (mEnd - mStart) + "; S=" + mStart + "; E=" + mEnd +"]" );
850850
}
851851

852-
/**
853-
* Compare locations for equality.
854-
*
855-
* @param other The location to compare.
856-
* @return True if locations are the same.
852+
/* (non-Javadoc)
853+
* @see java.lang.Object#hashCode()
857854
*/
858-
public boolean equals( Location other )
859-
{
860-
return mStart == other.mStart && mEnd == other.mEnd;
855+
@Override
856+
public int hashCode() {
857+
final int prime = 31;
858+
int result = 1;
859+
result = prime * result + mEnd;
860+
result = prime * result + mStart;
861+
return result;
861862
}
862-
863-
863+
864+
/* (non-Javadoc)
865+
* @see java.lang.Object#equals(java.lang.Object)
866+
*/
867+
@Override
868+
public boolean equals(Object obj) {
869+
if (this == obj)
870+
return true;
871+
if (obj == null)
872+
return false;
873+
if (getClass() != obj.getClass())
874+
return false;
875+
Location other = (Location) obj;
876+
if (mEnd != other.mEnd)
877+
return false;
878+
if (mStart != other.mStart)
879+
return false;
880+
return true;
881+
}
882+
864883
/**
865884
*
866885
*/
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.biojava.nbio.genome;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.biojava.nbio.genome.parsers.gff.Location;
6+
import org.junit.Test;
7+
8+
public class TestLocation {
9+
10+
@Test
11+
public void testLocation() {
12+
// tests taken from Location.main()
13+
14+
//Location p3_7= new Location( 3, 7 );
15+
Location p16_19= new Location( 16, 19 );
16+
Location p15_19= new Location( 15, 19 );
17+
Location p15_16= new Location( 15, 16 );
18+
Location p10_17= new Location( 10, 17 );
19+
Location p10_12= new Location( 10, 12 );
20+
Location p14_17= new Location( 14, 17 );
21+
//Location p14_14= new Location( 14, 14 );
22+
23+
Location r13_17= new Location( 13, 17 );
24+
Location r21_25= new Location( 21, 25 );
25+
26+
Location r4_7= new Location( 4, 7 );
27+
Location r2_5= new Location( 2, 5 );
28+
Location r0_3= new Location( 0, 3 );
29+
Location r5_8= new Location( 5, 8 );
30+
31+
//distance
32+
assertEquals(7, L(14,14).distance( L(3,7) ));
33+
assertEquals(7, L(3,7).distance( L(14,14) ));
34+
assertEquals(3, L(1,4).distance( L(7, 10) ));
35+
36+
//union
37+
assertEquals(p10_17, p10_12.union( p14_17 ));
38+
assertEquals(p10_17, p14_17.union( p10_12 ));
39+
assertEquals(p15_19, p15_19.union( p15_16 ));
40+
41+
//intersection
42+
assertEquals(new Location( 21, 25 ), r13_17.union( r21_25 ).intersection( r21_25 ));
43+
44+
45+
//isBefore
46+
assertTrue( r2_5.isBefore( r5_8 ));
47+
assertTrue( !r2_5.isBefore( r4_7 ));
48+
49+
//isAfter
50+
assertTrue(r5_8.isAfter( r2_5 ));
51+
assertTrue(!r5_8.isAfter( r4_7 ));
52+
53+
//contains
54+
assertTrue(p15_19.contains( p16_19 ));
55+
56+
//overlaps
57+
assertTrue(r2_5.overlaps( r4_7 ));
58+
assertTrue(r2_5.overlaps( r0_3 ));
59+
assertTrue(!r5_8.overlaps( r2_5 ));
60+
assertTrue(!r2_5.overlaps( r5_8 ));
61+
62+
63+
//prefix
64+
assertEquals(L(2,3), L(2,20).prefix(1));
65+
assertEquals(L(2,19), L(2,20).prefix(-1));
66+
assertEquals( L(2,10), L(2,20).prefix( L(10,12)));
67+
68+
//suffix
69+
assertEquals(L(3,20), L(2,20).suffix(1));
70+
assertEquals(L(19,20), L(2,20).suffix(-1));
71+
assertEquals(L(12,20), L(2,20).suffix( L(10,12)));
72+
73+
}
74+
75+
//shorthand for testing
76+
private static Location L( int s, int e ) {
77+
return new Location( s, e );
78+
}
79+
80+
}

0 commit comments

Comments
 (0)