TEST(CjsonTest, saveFile) { CjsonFormat cjson; Molecule savedMolecule, molecule; bool success = cjson.readFile(std::string(AVOGADRO_DATA) + "/data/ethane.cjson", savedMolecule); EXPECT_TRUE(success); EXPECT_EQ(cjson.error(), ""); success = cjson.writeFile("ethanetmp.cjson", savedMolecule); EXPECT_TRUE(success); EXPECT_EQ(cjson.error(), ""); // Now read the file back in and check a few key values are still present. success = cjson.readFile("ethanetmp.cjson", molecule); EXPECT_TRUE(success); EXPECT_EQ(cjson.error(), ""); EXPECT_EQ(molecule.data("name").toString(), "Ethane"); EXPECT_EQ(molecule.atomCount(), static_cast<size_t>(8)); EXPECT_EQ(molecule.bondCount(), static_cast<size_t>(7)); Atom atom = molecule.atom(7); EXPECT_EQ(atom.atomicNumber(), static_cast<unsigned char>(1)); EXPECT_EQ(atom.position3d().x(), -1.184988); EXPECT_EQ(atom.position3d().y(), 0.004424); EXPECT_EQ(atom.position3d().z(), -0.987522); Bond bond = molecule.bond(0); EXPECT_EQ(bond.atom1().index(), static_cast<size_t>(0)); EXPECT_EQ(bond.atom2().index(), static_cast<size_t>(1)); EXPECT_EQ(bond.order(), static_cast<unsigned char>(1)); }
// Retrieve desired value bool BondVariable::retrieveAccessor(int i, ReturnValue& rv, bool hasArrayIndex, int arrayIndex) { Messenger::enter("BondVariable::retrieveAccessor"); // Cast 'i' into Accessors enum value if ((i < 0) || (i >= nAccessors)) { printf("Internal Error: Accessor id %i is out of range for Bond type.\n", i); Messenger::exit("BondVariable::retrieveAccessor"); return false; } Accessors acc = (Accessors) i; // Check for correct lack/presence of array index given if ((accessorData[i].arraySize == 0) && hasArrayIndex) { Messenger::print("Error: Unnecessary array index provided for member '%s'.", qPrintable(accessorData[i].name)); Messenger::exit("BondVariable::retrieveAccessor"); return false; } else if ((accessorData[i].arraySize > 0) && (hasArrayIndex)) { if ((arrayIndex < 1) || (arrayIndex > accessorData[i].arraySize)) { Messenger::print("Error: Array index out of bounds for member '%s' (%i, range is 1-%i).", qPrintable(accessorData[i].name), arrayIndex, accessorData[i].arraySize); Messenger::exit("BondVariable::retrieveAccessor"); return false; } } // Get current data from ReturnValue bool result = true; Bond* ptr = (Bond*) rv.asPointer(VTypes::BondData, result); if ((!result) || (ptr == NULL)) { Messenger::print("Invalid (NULL) %s reference encountered.", VTypes::dataType(VTypes::BondData)); result = false; } if (result) switch (acc) { case (BondVariable::I): rv.set(VTypes::AtomData, ptr->atomI()); break; case (BondVariable::J): rv.set(VTypes::AtomData, ptr->atomJ()); break; case (BondVariable::Order): rv.set(ptr->order()); break; case (BondVariable::Type): rv.set(Bond::bondType(ptr->type())); break; default: printf("Internal Error: Access to member '%s' has not been defined in BondVariable.\n", qPrintable(accessorData[i].name)); result = false; break; } Messenger::exit("BondVariable::retrieveAccessor"); return result; }
TEST_F(MoleculeTest, addBond) { Molecule molecule; EXPECT_EQ(molecule.bondCount(), static_cast<Index>(0)); Atom a = molecule.addAtom(1); Atom b = molecule.addAtom(1); Bond bondAB = molecule.addBond(a, b); EXPECT_TRUE(bondAB.isValid()); EXPECT_EQ(bondAB.molecule(), &molecule); EXPECT_EQ(molecule.bondCount(), static_cast<Index>(1)); EXPECT_EQ(bondAB.index(), static_cast<Index>(0)); EXPECT_EQ(bondAB.atom1().index(), a.index()); EXPECT_EQ(bondAB.atom2().index(), b.index()); EXPECT_EQ(bondAB.order(), static_cast<unsigned char>(1)); Atom c = molecule.addAtom(1); Bond bondBC = molecule.addBond(b, c, 2); EXPECT_TRUE(bondBC.isValid()); EXPECT_EQ(molecule.bondCount(), static_cast<Index>(2)); EXPECT_EQ(bondBC.index(), static_cast<Index>(1)); EXPECT_EQ(bondBC.order(), static_cast<unsigned char>(2)); // try to lookup nonexistant bond Bond bond = molecule.bond(a, c); EXPECT_FALSE(bond.isValid()); // try to lookup bond between a and b bond = molecule.bond(a, b); EXPECT_TRUE(bond.isValid()); EXPECT_EQ(bond.molecule(), &molecule); EXPECT_EQ(bond.atom1().index(), a.index()); EXPECT_EQ(bond.atom2().index(), b.index()); // try to lookup bond between b and c by index bond = molecule.bond(1); EXPECT_TRUE(bond.isValid()); EXPECT_EQ(bond.molecule(), &molecule); EXPECT_EQ(bond.atom1().index(), b.index()); EXPECT_EQ(bond.atom2().index(), c.index()); }
TEST(CjsonTest, bonds) { CjsonFormat cjson; Molecule molecule; bool success = cjson.readFile(std::string(AVOGADRO_DATA) + "/data/ethane.cjson", molecule); EXPECT_TRUE(success); EXPECT_EQ(cjson.error(), ""); EXPECT_EQ(molecule.data("name").toString(), "Ethane"); EXPECT_EQ(molecule.atomCount(), static_cast<size_t>(8)); EXPECT_EQ(molecule.bondCount(), static_cast<size_t>(7)); Bond bond = molecule.bond(0); EXPECT_EQ(bond.atom1().index(), static_cast<size_t>(0)); EXPECT_EQ(bond.atom2().index(), static_cast<size_t>(1)); EXPECT_EQ(bond.order(), static_cast<unsigned char>(1)); bond = molecule.bond(6); EXPECT_EQ(bond.atom1().index(), static_cast<size_t>(4)); EXPECT_EQ(bond.atom2().index(), static_cast<size_t>(7)); EXPECT_EQ(bond.order(), static_cast<unsigned char>(1)); }
TEST(FileFormatManagerTest, writeFileGuessCml) { Molecule readMol, writeMol; FileFormatManager::instance().readFile(readMol, std::string(AVOGADRO_DATA) + "/data/ethane.cml"); FileFormatManager::instance().writeFile(readMol, "ethanemanagertmp.cml"); // Now read the file back in and check a few key values are still present. FileFormatManager::instance().readFile(writeMol, "ethanemanagertmp.cml"); EXPECT_EQ(writeMol.data("name").toString(), "Ethane"); EXPECT_EQ(writeMol.atomCount(), static_cast<size_t>(8)); EXPECT_EQ(writeMol.bondCount(), static_cast<size_t>(7)); Atom atom = writeMol.atom(7); EXPECT_EQ(atom.atomicNumber(), static_cast<unsigned char>(1)); EXPECT_EQ(atom.position3d().x(), -1.18499); EXPECT_EQ(atom.position3d().y(), 0.004424); EXPECT_EQ(atom.position3d().z(), -0.987522); Bond bond = writeMol.bond(0); EXPECT_EQ(bond.atom1().index(), static_cast<size_t>(0)); EXPECT_EQ(bond.atom2().index(), static_cast<size_t>(1)); EXPECT_EQ(bond.order(), static_cast<unsigned char>(1)); }