Skip to content

Commit

Permalink
Added unit test for methods related to ShortestPathFingerprint. This …
Browse files Browse the repository at this point in the history
…include unit test for RandomNumber, SimpleAtomComparator, SimpleAtomCanonicalisation.
  • Loading branch information
johnmay authored and egonw committed Dec 12, 2012
1 parent eda7ba7 commit bab373d
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/main/org/openscience/cdk/fingerprint/RandomNumber.java
Expand Up @@ -29,6 +29,8 @@
import org.apache.commons.math3.random.MersenneTwister;
import org.apache.commons.math3.random.RandomAdaptor;
import org.apache.commons.math3.random.RandomGenerator;
import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;

/**
* @author Syed Asad Rahman (2012)
Expand All @@ -37,6 +39,7 @@
* @cdk.module standard
* @cdk.githash
*/
@TestClass("org.openscience.cdk.fingerprint.RandomNumberTest")
public class RandomNumber implements Serializable {

private static final long serialVersionUID = 23345464573453571L;
Expand All @@ -48,6 +51,7 @@ public class RandomNumber implements Serializable {
* @param hashCode
* @return
*/
@TestMethod("testGenerateMersenneTwisterRandomNumber")
public static int generateMersenneTwisterRandomNumber(int maximum, long hashCode) {
RandomGenerator rg = new RandomAdaptor(new MersenneTwister(hashCode));
return rg.nextInt(maximum);
Expand Down
Expand Up @@ -26,6 +26,9 @@
package org.openscience.cdk.fingerprint;

import java.util.*;

import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;

Expand All @@ -41,12 +44,14 @@
* @cdk.githash
*
*/
@TestClass("org.openscience.cdk.fingerprint.SimpleAtomCanonicalisationTest")
public class SimpleAtomCanonicalisation {

/**
* @param container the container
* @return canonicalized atoms
*/
@TestMethod("testCanonicalizeAtoms")
public Collection<IAtom> canonicalizeAtoms(IAtomContainer container) {

List<IAtom> canonicalizedVertexList = new LinkedList<IAtom>();
Expand Down
16 changes: 13 additions & 3 deletions src/main/org/openscience/cdk/fingerprint/SimpleAtomComparator.java
Expand Up @@ -25,10 +25,14 @@
*/
package org.openscience.cdk.fingerprint;

import java.util.*;
import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IChemObject;

import java.io.Serializable;
import java.util.Comparator;

/**
* <P> This code returns a sorted set of atoms for a container according to its
* symbol and hybridization states. This will aid in finding a deterministic
Expand All @@ -41,8 +45,14 @@
* @cdk.githash
*
*/
class SimpleAtomComparator implements Comparator<IAtom> {
@Override
@TestClass("org.openscience.cdk.fingerprint.SimpleAtomComparatorTest")
public class SimpleAtomComparator implements Comparator<IAtom>, Serializable {

private static final long serialVersionUID = 2345252069991872083L;


@TestMethod("testCompare_NullHybridization,testCompare_SameHybridization,testCompare_DifferentHybridization,testCompare_DifferentSymbol")
@Override
public int compare(IAtom o1, IAtom o2) {
if (!(o1 instanceof IChemObject) || !(o2 instanceof IChemObject)) {
throw new ClassCastException();
Expand Down
56 changes: 56 additions & 0 deletions src/test/org/openscience/cdk/fingerprint/RandomNumberTest.java
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2012 John May <jwmay@users.sf.net>
*
* Contact: cdk-devel@lists.sourceforge.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
* All we ask is that proper credit is given for our work, which includes
* - but is not limited to - adding the above copyright notice to the beginning
* of your source code files, and to any copyright notice that you may distribute
* with programs based on this work.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/

package org.openscience.cdk.fingerprint;

import org.junit.Assert;
import org.junit.Test;

/**
* Unit tests for the {@link RandomNumber}.
*
* @author John May
* @cdk.module test-fingerprint
*/
public class RandomNumberTest {

private RandomNumber rn = new RandomNumber();

/**
* Tests the pseudorandom number generation to make sure we alway generate
* the same "next" random number.
*/
@Test
public void testGenerateMersenneTwisterRandomNumber() {

Assert.assertEquals("Expected next random number to be 444",
444,
rn.generateMersenneTwisterRandomNumber(1024, 42));
Assert.assertEquals("Expected next random number to be 748",
748,
rn.generateMersenneTwisterRandomNumber(1024, 444));

}

}
@@ -0,0 +1,58 @@
package org.openscience.cdk.fingerprint;

import org.junit.Assert;
import org.junit.Test;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IAtomType;
import org.openscience.cdk.templates.MoleculeFactory;
import org.openscience.cdk.tools.manipulator.AtomContainerManipulator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* @author John May
* @cdk.module test-fingerprint
*/
public class SimpleAtomCanonicalizationTest {

@Test public void testCanonicalizeAtoms() throws CDKException {

IAtomContainer container = MoleculeFactory.makeAdenine();
AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(container);

Collection<IAtom> atoms = new SimpleAtomCanonicalisation().canonicalizeAtoms(container);

List<IAtom> mutable = new ArrayList<IAtom>(atoms);
for(IAtom atom : mutable.subList(0, 5)){
Assert.assertEquals("expect sp2 carbons in first 4 entries",
"C", atom.getSymbol());
Assert.assertEquals("expect sp2 carbons in first 4 entries",
IAtomType.Hybridization.SP2, atom.getHybridization());
}
for(IAtom atom : mutable.subList(5, 8)){
Assert.assertEquals("expect sp2 nitrogen at indices 5-7",
"N", atom.getSymbol());
Assert.assertEquals("expect sp2 nitrogen at indices 5-7",
IAtomType.Hybridization.SP2, atom.getHybridization());
}

Assert.assertEquals("expect nitrogen at indices 8",
"N", mutable.get(8).getSymbol());
Assert.assertEquals("expect sp3 nitrogen at indices 8",
IAtomType.Hybridization.SP3,
mutable.get(8).getHybridization());

Assert.assertEquals("expect nitrogen at indices 9",
"N", mutable.get(9).getSymbol());
Assert.assertEquals("expect sp3 nitrogen at indices 9",
IAtomType.Hybridization.PLANAR3,
mutable.get(9).getHybridization());

}


}
@@ -0,0 +1,85 @@
package org.openscience.cdk.fingerprint;

import org.junit.Assert;
import org.junit.Test;
import org.openscience.cdk.DefaultChemObjectBuilder;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomType;
import org.openscience.cdk.interfaces.IChemObjectBuilder;

/**
* @author John May
* @cdk.module test-fingerprint
*/
public class SimpleAtomComparatorTest {

private IChemObjectBuilder builder = DefaultChemObjectBuilder.getInstance();

@Test
public void testCompare_NullHybridization() throws Exception {

SimpleAtomComparator comparator = new SimpleAtomComparator();

IAtom a1 = builder.newInstance(IAtom.class, "C");
IAtom a2 = builder.newInstance(IAtom.class, "C");

Assert.assertEquals("Null hybridzation should be equals",
0,
comparator.compare(a1, a2));

}

@Test
public void testCompare_SameHybridization() throws Exception {

SimpleAtomComparator comparator = new SimpleAtomComparator();

IAtom a1 = builder.newInstance(IAtom.class, "C");
IAtom a2 = builder.newInstance(IAtom.class, "C");

a1.setHybridization(IAtomType.Hybridization.SP3);
a2.setHybridization(IAtomType.Hybridization.SP3);

Assert.assertEquals("Same hybridzation should be equals",
0,
comparator.compare(a1, a2));

}

@Test
public void testCompare_DifferentHybridization() throws Exception {

SimpleAtomComparator comparator = new SimpleAtomComparator();

IAtom a1 = builder.newInstance(IAtom.class, "C");
IAtom a2 = builder.newInstance(IAtom.class, "C");

a1.setHybridization(IAtomType.Hybridization.SP2);
a2.setHybridization(IAtomType.Hybridization.SP3);

Assert.assertEquals("Atom 2 should have priority",
-1,
comparator.compare(a1, a2));

}

@Test
public void testCompare_DifferentSymbol() throws Exception {

SimpleAtomComparator comparator = new SimpleAtomComparator();

IAtom a1 = builder.newInstance(IAtom.class, "C");
IAtom a2 = builder.newInstance(IAtom.class, "O");


// can't do less than correctly without hamcrest?
Assert.assertTrue("oxygen should rank above carbon",
comparator.compare(a1, a2) < 0);
Assert.assertTrue("oxygen should rank above carbon (inverse)",
comparator.compare(a2, a1) > 0);

}



}

0 comments on commit bab373d

Please sign in to comment.