MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
test_moosemodule.cpp
Go to the documentation of this file.
1 // test_moosemodule.cpp ---
2 //
3 // Filename: test_moosemodule.cpp
4 // Description:
5 // Author:
6 // Maintainer:
7 // Created: Tue Jul 23 11:37:57 2013 (+0530)
8 // Version:
9 // Last-Updated: Thu Jul 25 21:54:10 2013 (+0530)
10 // By: subha
11 // Update #: 85
12 // URL:
13 // Keywords:
14 // Compatibility:
15 //
16 //
17 
18 // Commentary:
19 //
20 //
21 //
22 //
23 
24 // Change log:
25 //
26 //
27 //
28 //
29 // This program is free software; you can redistribute it and/or
30 // modify it under the terms of the GNU General Public License as
31 // published by the Free Software Foundation; either version 3, or
32 // (at your option) any later version.
33 //
34 // This program is distributed in the hope that it will be useful,
35 // but WITHOUT ANY WARRANTY; without even the implied warranty of
36 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37 // General Public License for more details.
38 //
39 // You should have received a copy of the GNU General Public License
40 // along with this program; see the file COPYING. If not, write to
41 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth
42 // Floor, Boston, MA 02110-1301, USA.
43 //
44 //
45 
46 // Code:
47 
48 #include "Python.h"
49 #include <iostream>
50 #include <cstring>
51 #include "../basecode/header.h"
52 #include "../basecode/Id.h"
53 #include "../basecode/ObjId.h"
54 #include "../shell/Shell.h"
55 
56 #include "moosemodule.h"
57 #include "../utility/utility.h"
58 using namespace std;
59 
60 extern "C" {
61 
62  void test_to_py()
63  {
64 #ifdef DO_UNIT_TESTS
65  // conversion of double
66  PyObject * pv;
67  double dv = 10.0;
68  pv = to_py((void*)&dv, 'd');
69  assert(pv != NULL);
70  assert(PyFloat_AsDouble(pv) == dv);
71  Py_XDECREF(pv);
72  cout << "." << flush;
73 
74  // conversion of long
75  long lv = 1000000000;
76  pv = to_py((void*)&lv, 'l');
77  assert(pv != NULL);
78  assert(PyLong_AsLong(pv) == lv);
79  Py_XDECREF(pv);
80  cout << "." << flush;
81 
82  // conversion of int
83  int iv = 10;
84  pv = to_py((void*)&iv, 'i');
85  assert(pv != NULL);
86  assert(PyInt_AsLong(pv) == iv);
87  Py_XDECREF(pv);
88  cout << "." << flush;
89 
90  // conversion of float
91  float fv = 7e-3;
92  pv = to_py((void*)&fv, 'f');
93  assert(pv != NULL);
94  assert(PyFloat_AsDouble(pv) == fv);
95  Py_XDECREF(pv);
96  cout << "." << flush;
97 
98  // string char-array
99  string sv = "hello world";
100  // C++ string
101  pv = to_py((void*)&sv, 's');
102  assert(pv != NULL);
103  assert(strcmp(PyString_AsString(pv), sv.c_str()) == 0);
104  Py_XDECREF(pv);
105  cout << "." << flush;
106 
107  // Id
108  Shell * shell = reinterpret_cast< Shell * >(ObjId( Id(), 0).data());
109  Id id = shell->doCreate("Neutral", Id(), "n", 1);
110  pv = to_py((void*)&id, 'x');
111  assert(pv != NULL);
112  assert(((_Id*)pv)->id_ == id);
113  Py_XDECREF(pv);
114  cout << "." << flush;
115 
116  // ObjId
117  ObjId oid(id, 0);
118  pv = to_py((void*)&oid, 'y');
119  assert(pv != NULL);
120  assert(((_ObjId*)pv)->oid_.id == oid.id);
121  //Harsha: commeted this line to compile moose in debug mode
122  //assert(((_ObjId*)pv)->oid_.dataId == oid.dataId);
123  Py_XDECREF(pv);
124  cout << "." << flush;
125 
126  // Incorrect type code
127  pv = to_py((void*)&oid, '9');
128  assert(pv == NULL);
129  PyErr_Clear();
130  cout << "." << flush;
131  shell->doDelete(id);
132 
133 #endif
134  }
135 
136 } // extern "C"
137 
139 {
140  test_to_py();
141 }
142 
143 //
144 // test_moosemodule.cpp ends here
char * data() const
Definition: ObjId.cpp:113
Id id
Definition: ObjId.h:98
Definition: ObjId.h:20
Id doCreate(string type, ObjId parent, string name, unsigned int numData, NodePolicy nodePolicy=MooseBlockBalance, unsigned int preferredNode=1)
Definition: Shell.cpp:181
void test_moosemodule()
void test_to_py()
bool doDelete(ObjId oid)
Definition: Shell.cpp:259
Definition: Id.h:17
PyObject * to_py(void *obj, char typecode)
Definition: Shell.h:43