PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/casa/Arrays/test/tArrayUtil.cc

http://casacore.googlecode.com/
C++ | 412 lines | 346 code | 32 blank | 34 comment | 150 complexity | cdc73648628a2d1040a625dae79dff8d MD5 | raw file
Possible License(s): GPL-2.0
  1. //# tArrayUtil.cc: Test program for functions in ArrayUtil.h
  2. //# Copyright (C) 1995,1996,1998,1999,2000,2001,2002
  3. //# Associated Universities, Inc. Washington DC, USA.
  4. //#
  5. //# This library is free software; you can redistribute it and/or modify it
  6. //# under the terms of the GNU Library General Public License as published by
  7. //# the Free Software Foundation; either version 2 of the License, or (at your
  8. //# option) any later version.
  9. //#
  10. //# This library is distributed in the hope that it will be useful, but WITHOUT
  11. //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  13. //# License for more details.
  14. //#
  15. //# You should have received a copy of the GNU Library General Public License
  16. //# along with this library; if not, write to the Free Software Foundation,
  17. //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
  18. //#
  19. //# Correspondence concerning AIPS++ should be addressed as follows:
  20. //# Internet email: aips2-request@nrao.edu.
  21. //# Postal address: AIPS++ Project Office
  22. //# National Radio Astronomy Observatory
  23. //# 520 Edgemont Road
  24. //# Charlottesville, VA 22903-2475 USA
  25. //#
  26. //# $Id: tArrayUtil.cc 20551 2009-03-25 00:11:33Z Malte.Marquarding $
  27. #include <casa/Arrays/ArrayIO.h>
  28. #include <casa/Arrays/ArrayMath.h>
  29. #include <casa/Arrays/ArrayError.h>
  30. #include <casa/Arrays/Matrix.h>
  31. #include <casa/Arrays/Vector.h>
  32. #include <casa/Utilities/Regex.h>
  33. #include <casa/OS/Timer.h>
  34. #include <casa/iostream.h>
  35. #include <casa/Arrays/ArrayUtil.h>
  36. #include <casa/namespace.h>
  37. // <summary>
  38. // Test of functions in ArrayUtil.h.
  39. // </summary>
  40. // This program tests the various functions in ArrayUtil.h.
  41. // When an argument is given to the program, it will not invoke any
  42. // function resulting in an exception. This mode can be used to do
  43. // proper detection of memory leaks using tools like TestCenter.
  44. Bool testStringToVector (Bool)
  45. {
  46. cout << "stringToVector..." << endl;
  47. Bool ok = True;
  48. Vector<String> vec1 = stringToVector ("");
  49. if (vec1.nelements() != 0) {
  50. cout << "Empty string should result in vector length 0" << endl;
  51. ok = False;
  52. }
  53. Vector<String> vec2 = stringToVector ("abc");
  54. if (vec2.nelements() != 1 || vec2(0) != "abc") {
  55. cout << "<abc> should result in vector length 1" << endl;
  56. ok = False;
  57. }
  58. Vector<String> vec3 = stringToVector (",");
  59. if (vec3.nelements() != 2 || vec3(0) != "" || vec3(1) != "") {
  60. cout << "<,> should result in vector length 2" << endl;
  61. ok = False;
  62. }
  63. Vector<String> vec4 = stringToVector ("abc,defg,,h");
  64. if (vec4.nelements() != 4 || vec4(0) != "abc" || vec4(1) != "defg"
  65. || vec4(2) != "" || vec4(3) != "h") {
  66. cout << "<abc,defg,,h> should result in vector length 4" << endl;
  67. ok = False;
  68. }
  69. Vector<String> vec5 = stringToVector (",abc,defg,");
  70. if (vec5.nelements() != 4 || vec5(0) != "" || vec5(1) != "abc"
  71. || vec5(2) != "defg" || vec5(3) != "") {
  72. cout << "<,abc,defg,> should result in vector length 4" << endl;
  73. ok = False;
  74. }
  75. return ok;
  76. }
  77. Bool testStringToVectorRegex (Bool)
  78. {
  79. cout << "stringToVectorRegex..." << endl;
  80. // Test using multiple spaces and a single comma as delimiter.
  81. Regex delim(" *, *");
  82. Bool ok = True;
  83. Vector<String> vec1 = stringToVector ("", delim);
  84. if (vec1.nelements() != 0) {
  85. cout << "Empty string should result in vector length 0" << endl;
  86. ok = False;
  87. }
  88. Vector<String> vec2 = stringToVector ("abc", delim);
  89. if (vec2.nelements() != 1 || vec2(0) != "abc") {
  90. cout << "<abc> should result in vector length 1" << endl;
  91. ok = False;
  92. }
  93. Vector<String> vec3 = stringToVector (",", delim);
  94. if (vec3.nelements() != 2 || vec3(0) != "" || vec3(1) != "") {
  95. cout << "<,> should result in vector length 2" << endl;
  96. ok = False;
  97. }
  98. Vector<String> vec4 = stringToVector ("abc,defg,,h", delim);
  99. if (vec4.nelements() != 4 || vec4(0) != "abc" || vec4(1) != "defg"
  100. || vec4(2) != "" || vec4(3) != "h") {
  101. cout << "<abc,defg,,h> should result in vector length 4" << endl;
  102. ok = False;
  103. }
  104. Vector<String> vec5 = stringToVector (",abc,defg,", delim);
  105. if (vec5.nelements() != 4 || vec5(0) != "" || vec5(1) != "abc"
  106. || vec5(2) != "defg" || vec5(3) != "") {
  107. cout << "<,abc,defg,> should result in vector length 4" << endl;
  108. ok = False;
  109. }
  110. Vector<String> vec6 = stringToVector (" , ", delim);
  111. if (vec6.nelements() != 2 || vec6(0) != "" || vec6(1) != "") {
  112. cout << "< , > should result in vector length 2" << endl;
  113. ok = False;
  114. }
  115. Vector<String> vec7 = stringToVector ("abc , defg , , h", delim);
  116. if (vec7.nelements() != 4 || vec7(0) != "abc" || vec7(1) != "defg"
  117. || vec7(2) != "" || vec7(3) != "h") {
  118. cout << "<abc , defg , , h> should result in vector length 4" << endl;
  119. ok = False;
  120. }
  121. Vector<String> vec8 = stringToVector (" abc ", delim);
  122. if (vec8.nelements() != 1 || vec8(0) != " abc ") {
  123. cout << "< abc > should result in vector length 1" << endl;
  124. ok = False;
  125. }
  126. return ok;
  127. }
  128. Bool testConcatenateArray (Bool doExcp)
  129. {
  130. cout << "concatenateArray..." << endl;
  131. Bool ok = True;
  132. Matrix<Int> matrix1 (3u,4u);
  133. Matrix<Int> matrix2 (3u,5u);
  134. Matrix<Int> matrix3 (4u,4u);
  135. Vector<Int> vector1 (4);
  136. Vector<Int> vector2 (6);
  137. indgen (matrix1);
  138. indgen (matrix2, Int(matrix1.nelements()));
  139. indgen (matrix3);
  140. indgen (vector1);
  141. indgen (vector2, Int(vector1.nelements()));
  142. Matrix<Int> matrixConc = concatenateArray (matrix1, matrix2);
  143. if (matrixConc.shape() != IPosition(2,3,9)) {
  144. cout << "Error in shape of concatenated matrices" << endl;
  145. ok = False;
  146. }
  147. uInt i, j;
  148. Int value = 0;
  149. for (j=0; j<9; j++) {
  150. for (i=0; i<3; i++) {
  151. if (matrixConc(i,j) != value++) {
  152. cout << "Error in matrix on " << i << "," << j << endl;
  153. ok = False;
  154. }
  155. }
  156. }
  157. Vector<Int> vectorConc = concatenateArray (vector1, vector2);
  158. if (vectorConc.shape() != IPosition(1,10)) {
  159. cout << "Error in shape of concatenated vectors" << endl;
  160. ok = False;
  161. }
  162. value = 0;
  163. for (i=0; i<10; i++) {
  164. if (vectorConc(i) != value++) {
  165. cout << "Error in vector on " << i << endl;
  166. ok = False;
  167. }
  168. }
  169. if (doExcp) {
  170. try {
  171. concatenateArray (matrix1, matrix3);
  172. ok = False; // should not get here
  173. cout << "1st concatenateArray exception not thrown" << endl;
  174. } catch (ArrayConformanceError x) {
  175. }
  176. try {
  177. concatenateArray (matrix1, vector1);
  178. ok = False; // should not get here
  179. cout << "2nd concatenateArray exception not thrown" << endl;
  180. } catch (ArrayConformanceError x) {
  181. }
  182. }
  183. return ok;
  184. }
  185. Bool testReorderArray (Bool doExcp)
  186. {
  187. Bool ok = True;
  188. {
  189. cout << "arrayReorder 2D..." << endl;
  190. IPosition shape(2,3,4);
  191. Array<Int> arr(shape);
  192. indgen(arr);
  193. for (Int j0=0; j0<2; j0++) {
  194. for (Int j1=0; j1<2; j1++) {
  195. if (j1 != j0) {
  196. IPosition axisOrder(2, j0, j1);
  197. Array<Int> res = reorderArray (arr, axisOrder);
  198. const IPosition& resShape = res.shape();
  199. IPosition posOld(2);
  200. IPosition posNew(2);
  201. for (Int i1=0; i1<resShape(1); i1++) {
  202. posNew(1) = i1;
  203. posOld(axisOrder(1)) = i1;
  204. for (Int i0=0; i0<resShape(0); i0++) {
  205. posNew(0) = i0;
  206. posOld(axisOrder(0)) = i0;
  207. if (arr(posOld) != res(posNew)) {
  208. ok = False;
  209. cout << "for shape " << shape << resShape
  210. << ", axisorder " << axisOrder << endl;
  211. cout << " result is " << res << endl;
  212. }
  213. }
  214. }
  215. }
  216. }
  217. }
  218. }
  219. {
  220. cout << "arrayReorder 3D..." << endl;
  221. IPosition shape(3,3,4,5);
  222. Array<Int> arr(shape);
  223. indgen(arr);
  224. for (Int j0=0; j0<3; j0++) {
  225. for (Int j1=0; j1<3; j1++) {
  226. if (j1 != j0) {
  227. for (Int j2=0; j2<3; j2++) {
  228. if (j2 != j0 && j2 != j1) {
  229. IPosition axisOrder(3, j0, j1, j2);
  230. Array<Int> res = reorderArray (arr, axisOrder);
  231. const IPosition& resShape = res.shape();
  232. IPosition posOld(3);
  233. IPosition posNew(3);
  234. for (Int i2=0; i2<resShape(2); i2++) {
  235. posNew(2) = i2;
  236. posOld(axisOrder(2)) = i2;
  237. for (Int i1=0; i1<resShape(1); i1++) {
  238. posNew(1) = i1;
  239. posOld(axisOrder(1)) = i1;
  240. for (Int i0=0; i0<resShape(0); i0++) {
  241. posNew(0) = i0;
  242. posOld(axisOrder(0)) = i0;
  243. if (arr(posOld) != res(posNew)) {
  244. ok = False;
  245. cout << "for shape " << shape << resShape
  246. << ", axisorder " << axisOrder << endl;
  247. cout << " result is " << res << endl;
  248. }
  249. }
  250. }
  251. }
  252. }
  253. }
  254. }
  255. }
  256. }
  257. }
  258. {
  259. cout << "arrayReorder 4D..." << endl;
  260. IPosition shape(4,3,4,5,6);
  261. Array<Int> arr(shape);
  262. indgen(arr);
  263. for (Int j0=0; j0<4; j0++) {
  264. for (Int j1=0; j1<4; j1++) {
  265. if (j1 != j0) {
  266. for (Int j2=0; j2<4; j2++) {
  267. if (j2 != j0 && j2 != j1) {
  268. for (Int j3=0; j3<4; j3++) {
  269. if (j3 != j0 && j3 != j1 && j3 != j2) {
  270. IPosition axisOrder(4, j0, j1, j2, j3);
  271. Array<Int> res = reorderArray (arr, axisOrder);
  272. const IPosition& resShape = res.shape();
  273. IPosition posOld(4);
  274. IPosition posNew(4);
  275. for (Int i3=0; i3<resShape(3); i3++) {
  276. posNew(3) = i3;
  277. posOld(axisOrder(3)) = i3;
  278. for (Int i2=0; i2<resShape(2); i2++) {
  279. posNew(2) = i2;
  280. posOld(axisOrder(2)) = i2;
  281. for (Int i1=0; i1<resShape(1); i1++) {
  282. posNew(1) = i1;
  283. posOld(axisOrder(1)) = i1;
  284. for (Int i0=0; i0<resShape(0); i0++) {
  285. posNew(0) = i0;
  286. posOld(axisOrder(0)) = i0;
  287. if (arr(posOld) != res(posNew)) {
  288. ok = False;
  289. cout << "for shape " << shape << resShape
  290. << ", axisorder " << axisOrder << endl;
  291. cout << " result is " << res << endl;
  292. }
  293. }
  294. }
  295. }
  296. }
  297. }
  298. }
  299. }
  300. }
  301. }
  302. }
  303. }
  304. }
  305. {
  306. cout << "arrayReorder 10 timings on [30,40,50,60] ..." << endl;
  307. IPosition shape(4,30,40,50,60);
  308. Array<Int> arr(shape);
  309. indgen(arr);
  310. {
  311. Timer tim;
  312. for (Int i=0; i<10; i++) {
  313. reorderArray (arr, IPosition(1,0));
  314. }
  315. tim.show ("0,1,2,3");
  316. }
  317. {
  318. Timer tim;
  319. for (Int i=0; i<10; i++) {
  320. reorderArray (arr, IPosition(4,0,1,3,2));
  321. }
  322. tim.show ("0,1,3,2");
  323. }
  324. {
  325. Timer tim;
  326. for (Int i=0; i<10; i++) {
  327. reorderArray (arr, IPosition(4,0,2,1,3));
  328. }
  329. tim.show ("0,2,1,3");
  330. }
  331. {
  332. Timer tim;
  333. for (Int i=0; i<10; i++) {
  334. reorderArray (arr, IPosition(4,1,3,2,0));
  335. }
  336. tim.show ("1,3,2,0");
  337. }
  338. }
  339. if (doExcp) {
  340. try {
  341. reorderArray (Array<Int>(IPosition(2,3,4)), IPosition(2,1,1));
  342. ok = False; // should not get here
  343. cout << "1st reorderArray exception not thrown" << endl;
  344. } catch (AipsError x) {
  345. }
  346. try {
  347. reorderArray (Array<Int>(IPosition(2,3,4)), IPosition(2,1,2));
  348. ok = False; // should not get here
  349. cout << "2nd reorderArray exception not thrown" << endl;
  350. } catch (AipsError x) {
  351. }
  352. }
  353. return ok;
  354. }
  355. int main (int argc, const char*[])
  356. {
  357. Bool ok = True;
  358. try {
  359. if (! testStringToVector ( (argc < 2))) {
  360. ok = False;
  361. }
  362. if (! testStringToVectorRegex ( (argc < 2))) {
  363. ok = False;
  364. }
  365. if (! testConcatenateArray( (argc < 2))) {
  366. ok = False;
  367. }
  368. if (! testReorderArray( (argc < 2))) {
  369. ok = False;
  370. }
  371. } catch (AipsError x) {
  372. cout << "Caught an exception: " << x.getMesg() << endl;
  373. ok = False;
  374. }
  375. if (!ok) {
  376. return 1;
  377. }
  378. cout << "OK" << endl;
  379. return 0; // successfully executed
  380. }