1 /**********************************************************************
2 invalid-smarts.cpp - Test SMARTS pattern parsing (rejecting invalid patterns)
3 
4 This file is part of the Open Babel project.
5 For more information, see <http://openbabel.org/>
6 
7 Some portions Copyright (C) 2005-2006 Geoffrey R. Hutchison
8 
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation version 2 of the License.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17 ***********************************************************************/
18 
19 // used to set import/export for Cygwin DLLs
20 #ifdef WIN32
21 #define USING_OBDLL
22 #endif
23 
24 #include <openbabel/babelconfig.h>
25 
26 #include <fstream>
27 
28 #include <openbabel/mol.h>
29 #include <openbabel/obutil.h>
30 #include <openbabel/oberror.h>
31 #include <openbabel/parsmart.h>
32 
33 using namespace std;
34 using namespace OpenBabel;
35 
36 #ifdef TESTDATADIR
37   string htestdatadir = TESTDATADIR;
38   string hinvalid_file = htestdatadir + "invalid-smarts.txt";
39   string hrandom1_file = htestdatadir + "random";
40   string hrandom2_file = htestdatadir + "random2";
41   string hrandom3_file = htestdatadir + "random3";
42 #else
43   string hinvalid_file = "files/invalid-smarts.txt";
44   string hrandom1_file = "files/random";
45   string hrandom2_file = "files/random2";
46   string hrandom3_file = "files/random3";
47 #endif
48 
invalidsmarts(int argc,char * argv[])49 int invalidsmarts(int argc, char* argv[])
50 {
51   int defaultchoice = 1;
52 
53   int choice = defaultchoice;
54 
55   if (argc > 1) {
56     if(sscanf(argv[1], "%d", &choice) != 1) {
57       printf("Couldn't parse that input as a number\n");
58       return -1;
59     }
60   }
61 
62   cout << "# Testing invalid SMARTS parsing..." << endl;
63 
64   // make sure to kill off all error reporting
65   obErrorLog.StopLogging();
66 
67   unsigned int currentTest = 0;
68   OBSmartsPattern smarts;
69   string pattern, buffer;
70 
71   std::ifstream mifs;
72   if (!SafeOpen(mifs, hinvalid_file.c_str()))
73     {
74       cout << "Bail out! Cannot read file " << hinvalid_file << endl;
75       return -1; // test failed
76     }
77 
78   while (getline(mifs, pattern))
79     {
80       // each line is a potential test pattern
81 
82       if (smarts.Init(pattern))
83         cout << "not ok " << ++currentTest << " # pattern was parsed "
84              << " but should have been rejected\n";
85       else
86         cout << "ok " << ++currentTest << "\n";
87     }
88 
89   mifs.close();
90   mifs.clear();
91 
92   // random file#1
93   if (!SafeOpen(mifs, hrandom1_file.c_str()))
94     {
95       cout << "Bail out! Cannot read file " << hrandom1_file << endl;
96       return -1; // test failed
97     }
98 
99   pattern.clear();
100   while (getline(mifs, buffer))
101     pattern += buffer;
102 
103   if (smarts.Init(pattern))
104     cout << "not ok " << ++currentTest << " # random data #1 was parsed "
105          << " but should have been rejected\n";
106   else
107     cout << "ok " << ++currentTest << " # random data #1 \n";
108   cout << "# read " << pattern.size() << "\n";
109 
110   mifs.close();
111   mifs.clear();
112 
113   // random2
114   if (!SafeOpen(mifs, hrandom2_file.c_str()))
115     {
116       cout << "Bail out! Cannot read file " << hrandom2_file << endl;
117       return -1; // test failed
118     }
119 
120   pattern.clear();
121   while (getline(mifs, buffer))
122     pattern += buffer;
123   if (smarts.Init(pattern))
124     cout << "not ok " << ++currentTest << " # random data #2 was parsed "
125          << " but should have been rejected\n";
126   else
127     cout << "ok " << ++currentTest << " # random data #2 \n";
128   cout << "# read " << pattern.size() << "\n";
129 
130   mifs.close();
131   mifs.clear();
132 
133   // random3
134   if (!SafeOpen(mifs, hrandom3_file.c_str()))
135     {
136       cout << "Bail out! Cannot read file " << hrandom3_file << endl;
137       return -1; // test failed
138     }
139 
140   pattern.clear();
141   while (getline(mifs, buffer))
142     pattern += buffer;
143   if (smarts.Init(pattern))
144     cout << "not ok " << ++currentTest << " # random data #3 was parsed "
145          << " but should have been rejected\n";
146   else
147     cout << "ok " << ++currentTest << " # random data #3 \n";
148   cout << "# read " << pattern.size() << "\n";
149 
150   // return number of tests run
151   cout << "1.." << currentTest << endl;
152 
153   // Passed tests
154   return 0;
155 }
156