File: cptest.cpp

package info (click to toggle)
rdkit 202209.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 203,880 kB
  • sloc: cpp: 334,239; python: 80,247; ansic: 24,579; java: 7,667; sql: 2,123; yacc: 1,884; javascript: 1,358; lex: 1,260; makefile: 576; xml: 229; fortran: 183; cs: 181; sh: 101
file content (148 lines) | stat: -rw-r--r-- 3,981 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
//  Copyright (C) 2003-2015 Greg Landrum and Rational Discovery LLC
//
//   @@ All Rights Reserved @@
//  This file is part of the RDKit.
//  The contents are covered by the terms of the BSD license
//  which is included in the file license.txt, found at the root
//  of the RDKit source tree.
//
#include <RDGeneral/test.h>
#include <GraphMol/RDKitBase.h>
#include <GraphMol/RDKitQueries.h>
#include <GraphMol/SmilesParse/SmilesParse.h>

#include <iostream>
using namespace std;
using namespace RDKit;

void test1() {
  auto *a1 = new Atom(6);
  auto *a2 = new Atom(*a1);
  Atom *a3 = a1->copy();
  delete a1;
  delete a2;
  delete a3;
}

void test2() {
  auto *mol = new RWMol();

  mol->addAtom(new Atom(6), false, true);
  mol->addAtom(new Atom(6), false, true);
  mol->addAtom(new Atom(8), false, true);
  mol->addBond(0, 1, Bond::SINGLE);
  mol->addBond(1, 2, Bond::SINGLE);
  mol->setAtomBookmark(mol->getAtomWithIdx(1), 1);
  mol->setBondBookmark(mol->getBondWithIdx(0), 2);
  CHECK_INVARIANT(mol->hasAtomBookmark(1), "");
  CHECK_INVARIANT(mol->getAtomWithBookmark(1)->getIdx() == 1, "");
  CHECK_INVARIANT(mol->hasBondBookmark(2), "");
  CHECK_INVARIANT(mol->getBondWithBookmark(2)->getIdx() == 0, "");

  auto *mol2 = new RWMol(*mol);
  CHECK_INVARIANT(mol2->hasAtomBookmark(1), "");
  CHECK_INVARIANT(mol2->getAtomWithBookmark(1)->getIdx() == 1, "");
  CHECK_INVARIANT(mol2->hasBondBookmark(2), "");
  CHECK_INVARIANT(mol2->getBondWithBookmark(2)->getIdx() == 0, "");
  delete mol;
  delete mol2;
}

void testQueryCopying() {
  BOOST_LOG(rdInfoLog)
      << "-----------------------\n Testing behavior of copied queries"
      << std::endl;

  {
    std::string smi =
        "[$([O,S;H1;v2]-[!$(*=[O,N,P,S])]),$([O,S;H0;v2]),$([O,S;-]),$([N;v3;!$"
        "(N-*=!@[O,N,P,S])]),$([nH0,o,s;+0])]";
    auto *m = static_cast<ROMol *>(SmartsToMol(smi));
    TEST_ASSERT(m);
    std::cerr << "\n\n\nCOPY" << std::endl;
    auto *m2 = new ROMol(*m, true);

    delete m;
    delete m2;
  }

  BOOST_LOG(rdInfoLog) << "Finished" << std::endl;
}

void testConformerCopying() {
  BOOST_LOG(rdInfoLog) << "-----------------------\n Testing copying conformers"
                       << std::endl;

  {
    std::string smi = "CCC";
    auto *m = static_cast<ROMol *>(SmilesToMol(smi));
    TEST_ASSERT(m);

    auto *conf = new Conformer(m->getNumAtoms());
    conf->setId(1);
    m->addConformer(conf, false);
    conf = new Conformer(m->getNumAtoms());
    conf->setId(2);
    m->addConformer(conf, false);
    {
      auto *m2 = new ROMol(*m);
      TEST_ASSERT(m2->getNumConformers() == 2);
      delete m2;
    }
    {
      auto *m2 = new ROMol(*m, false, 1);
      TEST_ASSERT(m2->getNumConformers() == 1);
      TEST_ASSERT(m2->getConformer().getId() == 1);
      delete m2;
    }
    {
      auto *m2 = new ROMol(*m, false, 2);
      TEST_ASSERT(m2->getNumConformers() == 1);
      TEST_ASSERT(m2->getConformer().getId() == 2);
      delete m2;
    }
    {
      auto *m2 = new ROMol(*m, false, 3);
      TEST_ASSERT(m2->getNumConformers() == 0);
      delete m2;
    }

    delete m;
  }

  BOOST_LOG(rdInfoLog) << "Finished" << std::endl;
}

void testGithub2144() {
  BOOST_LOG(rdInfoLog) << "-----------------------\n Testing Github #2144: no "
                          "RWMol(RWMol) copy constructor"
                       << std::endl;

  {
    auto mol1 = "C1CCCCC1"_smiles;
    TEST_ASSERT(mol1);
    RWMol mol2(*mol1);
    TEST_ASSERT(mol2.getNumBonds() == mol1->getNumBonds())
    TEST_ASSERT(mol2.getNumAtoms() == mol1->getNumAtoms())
    RWMol mol3(mol2);
    TEST_ASSERT(mol2.getNumBonds() == mol3.getNumBonds())
    TEST_ASSERT(mol2.getNumAtoms() == mol3.getNumAtoms())
  }

  BOOST_LOG(rdInfoLog) << "Finished" << std::endl;
}

// -------------------------------------------------------------------
int main() {
  RDLog::InitLogs();

#if 1
  test1();
  test2();
#endif
  testQueryCopying();
  testConformerCopying();
  testGithub2144();
  return 0;
}