Skip to content

Commit 065380c

Browse files
committed
Add test and correct logic, trueBits needs to be set again.
1 parent 8429a77 commit 065380c

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

base/standard/src/main/java/org/openscience/cdk/fingerprint/IntArrayFingerprint.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,16 @@ public void set(int index, boolean value) {
169169
int[] tmp = new int[trueBits.length - 1];
170170
System.arraycopy(trueBits, 0, tmp, 0, i);
171171
System.arraycopy(trueBits, i + 1, tmp, i, trueBits.length - i - 1);
172+
trueBits = tmp;
172173
}
173174
// bit at index is set to false and shall be set to true
174175
else if (i < 0 && value) {
175176
int[] tmp = new int[trueBits.length + 1];
176177
System.arraycopy(trueBits, 0, tmp, 0, trueBits.length);
177178
tmp[tmp.length - 1] = index;
178179
trueBits = tmp;
180+
Arrays.sort(trueBits);
179181
}
180-
//rest of possible ops are no-ops
181-
Arrays.sort(trueBits);
182182
}
183183

184184
@Override
@@ -203,7 +203,8 @@ public boolean equals(Object obj) {
203203
if (obj == null) return false;
204204
if (getClass() != obj.getClass()) return false;
205205
IntArrayFingerprint other = (IntArrayFingerprint) obj;
206-
return Arrays.equals(trueBits, other.trueBits);
206+
if (!Arrays.equals(trueBits, other.trueBits)) return false;
207+
return true;
207208
}
208209

209210
@Override

descriptor/fingerprint/src/test/java/org/openscience/cdk/fingerprint/IntArrayFingerprintTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,33 @@
2222
*/
2323
package org.openscience.cdk.fingerprint;
2424

25+
import org.junit.Assert;
26+
import org.junit.Test;
27+
28+
import static org.hamcrest.CoreMatchers.is;
29+
2530
public class IntArrayFingerprintTest extends AbstractBitFingerprintTest {
2631

2732
public IntArrayFingerprintTest() throws Exception {
2833
super(IntArrayFingerprint.class);
2934
}
3035

36+
@Test
37+
public void testSetBit() {
38+
IntArrayFingerprint fp = new IntArrayFingerprint();
39+
fp.set(1, true);
40+
fp.set(55, true);
41+
fp.set(219, true);
42+
fp.set(3, true);
43+
fp.set(24, true);
44+
Assert.assertThat(new int[]{1, 3, 24, 55, 219},
45+
is(fp.getSetbits()));
46+
fp.set(24, false);
47+
Assert.assertThat(new int[]{1, 3, 55, 219},
48+
is(fp.getSetbits()));
49+
fp.set(26, true);
50+
Assert.assertThat(new int[]{1, 3, 26, 55, 219},
51+
is(fp.getSetbits()));
52+
}
53+
3154
}

0 commit comments

Comments
 (0)