1 #ifndef OB_TEST_H
2 #define OB_TEST_H
3 
4 #include <iostream>
5 #include <cstdlib>
6 
7 #include <openbabel/mol.h>
8 #include <openbabel/obconversion.h>
9 #include <openbabel/shared_ptr.h>
10 
11 #ifdef _MSC_VER
12 #define FUNCTION_SIGNATURE __FUNCSIG__
13 #else
14 #define FUNCTION_SIGNATURE __PRETTY_FUNCTION__
15 #endif
16 
17 void report_error(const char* msg, const char* file, int line, const char* func_name, bool require = false);
18 
19 template <typename T1, typename T2>
ob_compare(T1 a,T2 b,const char * expr,const char * file,int line,const char * func_name)20 void ob_compare(T1 a, T2 b, const char *expr, const char *file, int line, const char *func_name)
21 {
22   if (!(a == b))
23     std::cout << file << ":" << line << ": " << expr << " [" << a << " == " << b << "] (FAIL)" << std::endl;
24 }
25 
26 #define OB_ASSERT(exp) \
27   ( (exp) ? static_cast<void>(0) : report_error(#exp, __FILE__, __LINE__, FUNCTION_SIGNATURE, false) )
28 
29 #define OB_REQUIRE(exp) \
30   ( (exp) ? static_cast<void>(0) : report_error(#exp, __FILE__, __LINE__, FUNCTION_SIGNATURE, true) )
31 
32 const char* ob_expr(const char *expr);
33 #define OB_EXPR(expr) ob_expr(#expr)
34 
35 #define OB_COMPARE(a,b) \
36   ob_compare(a, b, OB_EXPR( a == b ), __FILE__, __LINE__, FUNCTION_SIGNATURE)
37 
38 
39 
40 // some utility functions
41 typedef obsharedptr<OpenBabel::OBMol> OBMolPtr;
42 
43 struct OBTestUtil
44 {
GetFilenameOBTestUtil45   static std::string GetFilename(const std::string &filename)
46   {
47     std::string path = TESTDATADIR + filename;
48     return path;
49   }
50 
ReadFileOBTestUtil51   static OBMolPtr ReadFile(const std::string &filename)
52   {
53     std::string file = GetFilename(filename);
54 
55     std::ifstream ifs;
56     ifs.open(file.c_str());
57     OB_REQUIRE( ifs );
58 
59     OpenBabel::OBConversion conv;
60     OpenBabel::OBFormat *format = conv.FormatFromExt(file.c_str());
61     OB_REQUIRE(format);
62     OB_REQUIRE(conv.SetInFormat(format));
63 
64     OBMolPtr mol(new OpenBabel::OBMol);
65     OB_REQUIRE(conv.Read(mol.get(), &ifs));
66 
67     return mol;
68   }
69 
ReadFileContentOBTestUtil70   static std::string ReadFileContent(const std::string &filename)
71   {
72     std::string fn = GetFilename(filename);
73 
74     std::ifstream ifs(fn.c_str());
75     std::stringstream buffer;
76     buffer << ifs.rdbuf();
77     std::string content = buffer.str();
78 
79     return content;
80   }
81 };
82 
83 #endif // OB_TEST_H
84