Skip to content

Commit

Permalink
Additional patch to reduce atom count on setAtom(null, int) and unit …
Browse files Browse the repository at this point in the history
…tests for the setAtom(IAtom, int) behavior.

Signed-off-by: Rajarshi  Guha <rajarshi.guha@gmail.com>
  • Loading branch information
egonw authored and rajarshi committed Jul 27, 2010
1 parent 23d9685 commit 6cf95db
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/org/openscience/cdk/Bond.java
Expand Up @@ -314,6 +314,7 @@ public boolean contains(IAtom atom) {
*/
public void setAtom(IAtom atom, int position) {
if (atoms[position] == null && atom != null) atomCount++;
if (atoms[position] != null && atom == null) atomCount--;
atoms[position] = atom;
notifyChanged();
}
Expand Down
35 changes: 35 additions & 0 deletions src/test/org/openscience/cdk/interfaces/AbstractBondTest.java
Expand Up @@ -89,6 +89,41 @@ public void testSetAtoms_arrayIAtom() {
Assert.assertEquals(atomsToAdd[1], b.getAtom(1));
}

@Test
public void testSetAtom_SomeNull() {
IBond b = (IBond)newChemObject();
b.setAtom(b.getBuilder().newInstance(IAtom.class,"C"), 0);
Assert.assertEquals(1, b.getAtomCount());
}

@Test
public void testUnSetAtom() {
IBond b = (IBond)newChemObject();
b.setAtom(b.getBuilder().newInstance(IAtom.class,"C"), 0);
Assert.assertEquals(1, b.getAtomCount());
b.setAtom(b.getBuilder().newInstance(IAtom.class,"C"), 0);
Assert.assertEquals(1, b.getAtomCount());
b.setAtom(null, 0);
Assert.assertEquals(0, b.getAtomCount());
b.setAtom(null, 0);
Assert.assertEquals(0, b.getAtomCount());
}

@Test
public void testOverwriteAtom() {
IBond b = (IBond)newChemObject();
b.setAtom(b.getBuilder().newInstance(IAtom.class,"C"), 0);
Assert.assertEquals(1, b.getAtomCount());
b.setAtom(b.getBuilder().newInstance(IAtom.class,"C"), 0);
Assert.assertEquals(1, b.getAtomCount());

// test overwrite with null
b.setAtom(null, 0);
Assert.assertEquals(0, b.getAtomCount());
b.setAtom(null, 0);
Assert.assertEquals(0, b.getAtomCount());
}

@Test
public void testAtoms() {
IBond b = (IBond)newChemObject();
Expand Down

0 comments on commit 6cf95db

Please sign in to comment.