str2list:
 Todo Clases Archivos Funciones 'defines'
test_str2list.cpp
Ir a la documentación de este archivo.
1 // test_str2list.cpp (C) 2009 adolfo@di-mare.com
2 
3 /** \file test_str2list.cpp
4  \brief Programa de prueba para \c str2list() y \c str2matrix().
5 
6  \author Adolfo Di Mare <adolfo@di-mare.com>
7  \date 2009
8 */
9 
10 #include "BUnit.h"
11 #include <iostream> // BORREME !!!
12 #include "str2list.h"
13 
14 #include <iostream> // cout
15 #include <iomanip> // setw()
16 
17 /// Prueba \c str2list() y \c str2matrix().
18 class test_str2list : public TestCase {
19 public:
20  bool run();
21  void str2list_example();
22  void test_iswhitespace();
23  void str2list_list();
24  void test_makeList_long();
25  void test_intlist();
26  void test_doublelist();
27  void test_makeList_char();
28  void test_str2matrix();
29  void test_intmatrix();
30  void test_longmatrix();
31  void test_doublematrix();
32  void test_charmatrix();
33 };
34 
35 /// <code> main() { test->run(); } </code>
39  str2list_list();
41  test_intlist();
49  return wasSuccessful();
50 };
51 
52 /// Test -> \c iswhitespace().
54 {{ // test::iswhitespace()
55  const char blank = ' ';
56  assertTrue( iswhitespace_str2list(blank) ); assertTrue( iswhitespace_str2list(',') );
57 
58  assertTrue( ! iswhitespace_str2list('.') ); assertFalse( iswhitespace_str2list('.') );
59  assertTrue( ! iswhitespace_str2list('\0') ); assertFalse( iswhitespace_str2list('\0') );
60 
61  assertTrue( iswhitespace_str2list('(') ); assertTrue( iswhitespace_str2list(')') );
62  assertTrue( iswhitespace_str2list('[') ); assertTrue( iswhitespace_str2list(']') );
63  assertTrue( iswhitespace_str2list('{') ); assertTrue( iswhitespace_str2list('}') );
64  assertTrue( iswhitespace_str2list('<') ); assertTrue( iswhitespace_str2list('>') );
65 
66  assertTrue( iswhitespace_str2list('\r') ); assertTrue( iswhitespace_str2list('\n') );
67  assertTrue( iswhitespace_str2list('\f') );
68  assertTrue( iswhitespace_str2list('\t') ); assertTrue( iswhitespace_str2list('\v') );
69 }}
70 }
71 
72 /// Test -> \c makeList_long().
74  {{ // test::makeList_long()
75  std::list<long> Li, Lj;
76  Li = makeList_long( "()" ); assertTrue( Li.empty() );
77  Li = makeList_long( " ( ) " ); assertTrue( Li.empty() );
78 
79  Li = makeList_long( "(1 2 3 4 5)" );
80  for ( int j=1; j<=5; ++j ) {
81  Lj.push_back( j );
82  }
83  assertTrue( Li == Lj );
84 
85  assertTrue( makeList_long( "( ... )" ) == makeList_long( "( 0 )" ) );
86  assertTrue( makeList_long( "( ... )" ).size() == 1 );
87 
88  assertTrue( makeList_long( "(0)" ) == makeList_long( "( - )" ) );
89  assertTrue( makeList_long( "(0)" ) == makeList_long( "( + )" ) );
90  assertTrue( makeList_long( "()" ) == makeList_long( "( )" ) );
91 
92  assertTrue( makeList_long( "( 1 2 3 4 5 )" ) == makeList_long( "(1,2,3,4,5)" ) );
93  assertTrue( makeList_long( "{ 1,2,3,4,5 }" ) == makeList_long( "[1 2 3 4 5]" ) );
94 
95  Li = makeList_long( "( 1,22,333,4444,5555 )" );
96  Lj = makeList_long( "(1,22,333,4444,5555)" );
97  assertTrue( Li == Lj );
98 
99  Li = makeList_long( "(1 , - + 22, -333, - 4444, --5555)" );
100  Lj = makeList_long( "(1, 0,0,22, -333, 0,4444, 0)" );
101  assertTrue( Li == Lj );
102 
103  Li = makeList_long( "( 1, 22, 333, 4444, 5555 )" );
104  Lj = makeList_long( "| [1] [22] [333] [4444] [5555] |... ignore");
105  assertTrue( Li == Lj );
106 
107  Li = makeList_long( "( 1, 22, 333, 4444, 5555, 0, 0 )" );
108  Lj = makeList_long( "( [1] [22] [333] [4444] [5555] )etc ...");
109  assertTrue( Li == Lj );
110  }}
111 { // Resto de las pruebas
112  void print_list( const char* str );
113  if (false) { // Estas son las que fallan con la nueva versión de makeList_long()
114  assertTrue( makeList_long( "()" ) == makeList_long( "( ... )" ) );
115  assertTrue( makeList_long( "( 1 2 3 4 5 )" ) == makeList_long( "(1.2.3.4.5)" ) );
116 
117  std::list<long> Li, Lj;
118  Li = makeList_long( "(1 , - + 22, -333, - 4444, --5555)" );
119  Lj = makeList_long( " (1,0,0,22,-333,0,4444,5555)" );
120  assertTrue( Li == Lj );
121 
122  Li = makeList_long( "( ++101 --202 +-+-303 -+-+404 -+-+--505 )" );
123  assertTrue( Li == Lj );
124 
125  assertTrue( makeList_long( "() " ) == makeList_long( " ( ... ) " ) );
126  assertTrue( makeList_long( " ()" ) == makeList_long( " ( ... ) " ) );
127  }
128  if (true) { // Estas sirven para ver en qué falla la nueva versión de makeList_long()
129  assertTrue( makeList_long( "( ... )" ) == makeList_long( "( [...] )" ) );
130  assertTrue( makeList_long( "( ... )" ) == makeList_long( "( 0 )" ) );
131  assertTrue( makeList_long( "( ... )" ).size() == 1 );
132 
133  std::list<long> Li, Lj;
134  if (false) {
135  print_list( "( ... )" );
136  print_list( "(1 , - + 22, -333, - 4444, --5555)" );
137  print_list( " (1,0,0,22,-333,0,4444,5555)" );
138  print_list( "( ++101 --202 +-+-303 -+-+404 -+-+--505 )" );
139 
140  }
141  Li = makeList_long( "(1 , - + 22, -333, - 4444, --5555)" );
142  Lj = makeList_long( "| [1] [-] [+] [22] [-333] [-] [4444] [--5555] | ==> (#8)");
143  assertTrue( Li == Lj );
144 
145  Li = makeList_long( " (1,0,0,22,-333,0,4444,5555)" );
146  Lj = makeList_long( "| [1] [0] [0] [22] [-333] [0] [4444] [5555] | ==> (#8)" );
147  assertTrue( makeList_long( " 1 0 0 22 -333 0 4444 5555 ") == Lj );
148  assertTrue( Li == Lj );
149 
150  Li = makeList_long( "( ++101 --202 +-+-303 -+-+404 -+-+--505 )" );
151  Lj = makeList_long( "| [++101] [--202] [+-+-303] [-+-+404] [-+-+--505] | ==> (#5)");
152  assertTrue( Li == Lj );
153  assertTrue( Lj == makeList_long( " 0 0 0 0 0 " ) );
154  }
155  { // Resto de las pruebas
156  std::list<long> Li, Lj;
157  Li = makeList_long( "( 101 202 303 404 505 )" );
158  for ( int j=101; j<=505; j+=101 ) {
159  Lj.push_back( j );
160  }
161  assertTrue( Li == Lj );
162  Li = makeList_long( "( ++101 --202 +-+-303 -+-+404 -+-+--505 )" );
163  Li = makeList_long( "( 101 202 303 404 505 )" );
164  assertTrue( Li == Lj );
165 
166  assertTrue( makeList_long( " (0)" ) == makeList_long( " ( - ) " ) );
167  assertTrue( makeList_long( " (0)" ) == makeList_long( " ( + ) " ) );
168  assertTrue( makeList_long( " ()" ) == makeList_long( " ( ) " ) );
169  assertTrue( makeList_long( "(0) " ) == makeList_long( " ( - ) " ) );
170  assertTrue( makeList_long( "(0) " ) == makeList_long( " ( + ) " ) );
171  assertTrue( makeList_long( "() " ) == makeList_long( " ( ) " ) );
172 
173  Li = makeList_long( "( +-1 -+2 ++3 --4 5 )" );
174  Lj = makeList_long( "(+-1,-+2,++3,--4,5)" );
175  assertTrue( Li == Lj );
176  makeList_long( "( +-+1 -+-2 -++-3 -+-4 5 )" );
177  makeList_long( "(+-1,+2,++3,--4,5)" );
178  assertTrue( Li == Lj );
179  }
180 }
181 }
182 
183 /// Test -> \c intlist().
185 {{ // test::intlist()
186  std::list<int> Li, Lj;
187  Li = intlist( "()" ); assertTrue( Li.empty() );
188  Li = intlist( " ( ) " ); assertTrue( Li.empty() );
189 
190  Li = intlist( "(1 2 3 4 5)" );
191  for ( int j=1; j<=5; ++j ) {
192  Lj.push_back( j );
193  }
194  assertTrue( Li == Lj );
195 
196  assertTrue( intlist( "( ... )" ) == intlist( "( 0 )" ) );
197  assertTrue( intlist( "( ... )" ).size() == 1 );
198 
199  assertTrue( intlist( "(0)" ) == intlist( "( - )" ) );
200  assertTrue( intlist( "(0)" ) == intlist( "( + )" ) );
201  assertTrue( intlist( "()" ) == intlist( "( )" ) );
202 
203  assertTrue( intlist( "( 1 2 3 4 5 )" ) == intlist( "(1,2,3,4,5)" ) );
204  assertTrue( intlist( "{ 1,2,3,4,5 }" ) == intlist( "[1 2 3 4 5]" ) );
205 
206  Li = intlist( "( 1,22,333,4444,5555 )" );
207  Lj = intlist( "(1,22,333,4444,5555)" );
208  assertTrue( Li == Lj );
209 
210  Li = intlist( "(1 , - + 22, -333, - 4444, --5555)" );
211  Lj = intlist( "(1, 0,0,22, -333, 0,4444, 0)" );
212  assertTrue( Li == Lj );
213 
214  Li = intlist( "( 1, 22, 333, 4444, 5555 )" );
215  Lj = intlist( "| [1] [22] [333] [4444] [5555] |... ignore");
216  assertTrue( Li == Lj );
217 
218  Li = intlist( "( 1, 22, 333, 4444, 5555, 0, 0 )" );
219  Lj = intlist( "( [1] [22] [333] [4444] [5555] )etc ...");
220  assertTrue( Li == Lj );
221 }}
222 }
223 
224 /// Test -> \c doublelist().
226 {{ // test::doublelist()
227  std::list<double> Li, Lj;
228  Li = doublelist( "()" ); assertTrue( Li.empty() );
229  Li = doublelist( " ( ) " ); assertTrue( Li.empty() );
230 
231  Li = doublelist( "(1. 2. 3. 4. 5.)" );
232  for ( double j=1; j<=5; ++j ) {
233  Lj.push_back( j );
234  }
235  assertTrue( Li == Lj );
236 
237  assertTrue( doublelist( "( ... )" ) == doublelist( "( 0 )" ) );
238  assertTrue( doublelist( "( ... )" ).size() == 1 );
239 
240  assertTrue( doublelist( "(0)" ) == doublelist( "( - )" ) );
241  assertTrue( doublelist( "(0)" ) == doublelist( "( + )" ) );
242  assertTrue( doublelist( "()" ) == doublelist( "( )" ) );
243 
244  assertTrue( doublelist( "( 1. 2. 3. 4. 5. )" ) == doublelist( "(1,2,3,4,5)" ) );
245  assertTrue( doublelist( "{ 1.,2.,3.,4.,5. }" ) == doublelist( "[1 2 3 4 5]" ) );
246 
247  Li = doublelist( "( 1,22,333,4444,5555 )" );
248  Lj = doublelist( "(1,22,333,4444,5555)" );
249  assertTrue( Li == Lj );
250 
251  Li = doublelist( "(1 , - + 22, -333, - 4444, --5555)" );
252  Lj = doublelist( "(1, 0,0,22, -333, 0,4444, 0)" );
253  assertTrue( Li == Lj );
254 
255  Li = doublelist( "( 1, 22, 333, 4444, 5555 )" );
256  Lj = doublelist( "| [1] [22] [333] [4444] [5555] |... ignore");
257  assertTrue( Li == Lj );
258 
259  Li = doublelist( "( 1, 22, 333, 4444, 5555, 0, 0 )" );
260  Lj = doublelist( "( [1] [22] [333] [4444] [5555] )etc ...");
261  assertTrue( Li == Lj );
262 }}
263 }
264 
265 /// Test -> \c str2list().
267 {{ // test::str2list::list()
268  std::list<std::string> Li,Lj,Lk,Ll;
269  str2list( Li , "( 1 22 333 4444 5555 )" );
270  str2list( Lj , "< 1,22,333,4444,5555 > | del " );
271  str2list( Lk , "{ 1 22 333 4444 5555 } | del " );
272  str2list( Ll , "[ 1.22.333.4444.5555 ] | del " ); // '.' NO '.'
273  std::list<std::string> :: const_iterator itI,itJ,itK,itL;
274  itI = Li.begin(); itJ = Lj.begin(); itK = Lk.begin(); itL = Ll.begin();
275  assertTrue( *itI == "1" ); assertTrue( *itI++ == *itJ++ );
276  assertTrue( *itI == "22" ); assertTrue( *itI++ == *itJ++ );
277  assertTrue( *itI == "333" ); assertTrue( *itI++ == *itJ++ );
278  assertTrue( *itI == "4444" ); assertTrue( *itI++ == *itJ++ );
279  assertTrue( *itI == "5555" ); assertTrue( *itI++ == *itJ++ );
280  assertTrue( 5 == Li.size() ); assertTrue( 5 == Lj.size() );
281 
282  assertTrue( Li == Lj && Lj == Lk && ((Lk != Ll)) ); // '.' NO '.'
283  assertTrue( *itL == "1.22.333.4444.5555" ); // '.' NO '.'
284  assertTrue( 1 == Ll.size() && Ll.size() < 2 );
285 }}
286 }
287 
288 #include <iterator> // std::ostream_iterator<char>
289 
290 /// Test -> \c makeList_char().
292 {{ // test::charlist()
293  std::list<char> Lcon,Lsin;
294  Lcon = charlist( " { 'a' ' ' 'c' '4' '\'' '.' } " );
295  Lsin = charlist( " ( a ' ' c 4 ''' . ) " );
296  // 1 2 3 4 5 6
297  assertTrue( Lcon == Lsin );
298  assertTrue( Lcon.size() == 6 );
299 
300  std::list<char>::const_iterator it;
301  it = Lcon.begin();
302  for ( unsigned i=0; i<Lcon.size()-1; ++i ) { ++it; }
303  assertTrue( *it == '.' );
304  --it;
305  assertTrue( *it == '\'' );
306 }}
307 {
308  std::list<char> Lcon,Lsin;
309  Lcon = charlist( " { 'a' ' ' 'c' '4' '\'' '.' } " );
310  Lsin = charlist( " ( a ' ' c 4 ''' . ) " );
311  // 1 2 3 4 5 6
312  if (false) {
313  // http://www.devx.com/getHelpOn/10MinuteSolution/20410/1954?pf=true
314  std::copy( Lcon.begin(), Lcon.end(), std::ostream_iterator<char> (std::cout, ",") );
315  }
316 }
317 }
318 
319 
320 /// Test -> \c str2list().
322 {{ // test::str2list::example()
323  assertTrue( str2list("").empty() && "list<>.empty() !!!" );
324  assertTrue( str2list("") == str2list("()") );
325  assertTrue( str2list("()") == str2list(" | | del ") );
326  assertTrue( str2list("{}").size() == 0 );
327  assertTrue( str2list("[0]") == str2list("| 0 |") );
328 
329  assertTrue( str2list("(0,1,, ,,2)") == str2list("( 0 1 2 )") );
330  assertTrue( str2list("( 1 2 3 4 5 )") == str2list("| 1 2 3 4 5 |") );
331  assertTrue( str2list("(,'c',,'j',)" ) == str2list( "< 'c' 'j' >") );
332 
333  assertTrue( str2list(" |a| etc... ") == str2list("( a )") );
334  assertTrue( str2list(" |a| etc... ").size() == 1 );
335  assertTrue( str2list(" |a| etc... ") == str2list("| a | #1") );
336 }}
337 {
338  assertTrue( str2list(" |a| etc... ") == str2list("| a | #1") );
339  assertTrue( str2list("") == str2list("| | #0") );
340  assertTrue( str2list("()") == str2list("| | #0") );
341  assertTrue( str2list("[0]") == str2list("| 0 | #1") );
342 
343  assertTrue( str2list("(0,1,22)") == str2list("| 0 1 22 | #3") );
344  assertTrue( str2list("(0,1,22)").size() == 3 );
345 
346  assertTrue( str2list("( 1 2 3 4 5 )") == str2list("| 1 2 3 4 5 | #5") );
347  assertTrue( str2list("( 1 2 3 4 5 )").size() == 5 );
348 
349  assertTrue( str2list(" | 22 | etc... ") == str2list("| 22 | #1") );
350  assertTrue( str2list("|| etc... ") == str2list("| | #0") );
351  assertTrue( str2list(" 22| |etc|") == str2list("| 22 | #1") );
352  assertTrue( str2list(" |1| ") == str2list("| 1 | #1") );
353  assertTrue( str2list(" | 2 | ") == str2list("| 2 | #1") );
354  assertTrue( str2list("1") == str2list("| 1 | #1") );
355  assertTrue( str2list("[-1]") == str2list("| -1 | #1") );
356  assertTrue( str2list("[1] [2] [3] [4] [5]") == str2list("| 1 2 3 4 5 | #5") );
357  assertTrue( str2list("{ [1] [2] [3] [4] [5] }") == str2list("| 1 2 3 4 5 | #5") );
358  assertTrue( str2list(" | 22 | |etc|") == str2list("| 22 | #1") );
359  assertTrue( str2list(" |22 | |etc|") == str2list("| 22 | #1") );
360  assertTrue( str2list(" | 22| |etc|") == str2list("| 22 | #1") );
361  assertTrue( str2list(" 22 | |etc|") == str2list("| 22 | #1") );
362  assertTrue( str2list(" 22 | |etc|") == str2list("| 22 | #1") );
363  assertTrue( str2list(" 22| |etc|") == str2list("| 22 | #1") );
364  assertTrue( str2list("(1,2,3,4,5)") == str2list("| 1 2 3 4 5 | #5") );
365  assertTrue( str2list("( ... )") == str2list("| ... | #1") );
366  assertTrue( str2list("( - )") == str2list("| - | #1") );
367  assertTrue( str2list("( 1 - 22 )") == str2list("| 1 - 22 | #3") );
368  assertTrue( str2list("(1,0,0,22,-333,0)") == str2list("| 1 0 0 22 -333 0 | #6") );
369  assertTrue( str2list("(1 , - - 22, -333, - )") == str2list("| 1 - - 22 -333 - | #6") );
370  assertTrue( str2list("( 0, 4444, 5555 )") == str2list("| 0 4444 5555 | #3") );
371  assertTrue( str2list("( - 4444, --5555)") == str2list("| - 4444 --5555 | #3") );
372 }
373 {
374  assertTrue( str2list( "()" ) == str2list( "()" ) );
375  assertTrue( str2list( "[-1]" ) == str2list( "-1" ) );
376  assertTrue( str2list( " |2| " ) == str2list( " { 2 } " ) );
377  assertTrue( str2list( "(1,2,3,4,5)" ) == str2list( "( 1 2 3 4 5 )" ) );
378 
379  assertTrue( str2list( "(1,22,333)") == str2list( "( 1,22,333 )" ) );
380  assertTrue( str2list( "(1,-22,333)") == str2list( "( 1 -22 333 )" ) );
381 
382  assertTrue( str2list( "..." ) == str2list( "( ... )" ) );
383  assertTrue( str2list( "(0)" ) == str2list( "( 0 )" ) );
384  assertTrue( str2list( "(1,0,22)" ) == str2list( "( 1 0 22 )" ) );
385 
386  assertTrue( str2list( "(1,-,-,22,-333,-)" ) == str2list( "(1 , - - 22, -333, - )" ) );
387  assertTrue( str2list( "( 0, 4444, 5555 )" ) == str2list( "( 0 4444, 5555)" ) );
388 }
389 {
390  assertTrue( str2list ( "{ 3, 3, 16}" ) == str2list ( "(3 3 16)" ) );
391 
392  assertTrue( str2list ( "(-, 5, 84, -5)" ) == str2list ( "(- 5 84 -5)" ) );
393  assertTrue( str2list ( "(5, 7, 89, 65 100)" ) == str2list ( "(5 7 89 65 100)" ) );
394  assertTrue( str2list ( "(2.0, 6, 76.)" ) == str2list ( "(2.0 6 76.)" ) );
395  assertTrue( str2list ( "('a',-6, 6, 7)" ) == str2list ( "('a' -6 6 7)" ) );
396  assertTrue( str2list ( "('$', 5, 7, 0)" ) == str2list ( "( '$' 5 7 0)" ) );
397  assertTrue( str2list ( "(al, 5, 6)" ) == str2list ( "(al 5 6)" ) );
398  assertTrue( str2list ( "(casa, 5, 6)" ) == str2list ( "(casa 5 6)" ) );
399  assertTrue( str2list ( "(---6, 78, 98)" ) == str2list ( "[ ---6 78 98 ]" ) );
400  assertTrue( str2list ( "( 4 33 758)" ) == str2list ( "| 4 33 758 | ..!" ) );
401  assertTrue( str2list ( "(-4, 44, - --,-7)" ) == str2list ( "(-4 44 - -- -7)" ) );
402  assertTrue( str2list ( "..." ) == str2list ( "(...)" ) );
403  assertTrue( str2list ( "(k, l, 0, 8)" ) == str2list ( "(k l 0 8)" ) );
404 }
405 }
406 
407 
408 /// Imprime por filas el valor de \c "M".
409 template <class MAT>
410 void print( const char* name, MAT & M ) {
411  std::cout << std::endl << name
412  << '[' << M.rows() << ',' << M.cols() << ']'
413  << std::endl;
414  for (unsigned i=0; i < M.rows(); ++i) {
415  for (unsigned j=0; j < M.cols(); ++j) {
416  std::cout << " " << std::setw(6) << M(i,j);
417  }
418  std::cout << std::endl;
419  }
420 }
421 
422 /// Prints the contents in \c L.
423 void print( const std::list< std::string >& L ) {
424  std::list< std::string >::const_iterator it;
425  for ( it= L.begin(); it!=L.end(); ++it ) {
426  std::cout << " [" << (*it) << ']';
427  }
428  std::cout << std::endl;
429 }
430 
431 /// Uses \c str2list() to print list values.
432 void print_list( const char* str ) {
433  std::list< std::string > L;
434  str2list( L, str );
435  #if 1
436  // print list values
437  std::cout << '\"'<< str << "\"\n";
438  std::list< std::string >::const_iterator it;
439  for ( it= L.begin(); it!=L.end(); ++it ) {
440  std::cout << " [" << (*it) << ']';
441  }
442  std::cout << " ==> (#" << L.size() << ")" << std::endl << std::endl;
443  #else
444  // generate the assertTrue() statements
445  std::cout << "assertTrue( str2list(\"" << str << "\") == str2list(\"|";
446  std::list< std::string >::const_iterator it;
447  for ( it= L.begin(); it!=L.end(); ++it ) {
448  std::cout << " " << (*it);
449  }
450  std::cout << " | #" << L.size() << "\") );" << std::endl << std::endl;
451  #endif
452 }
453 
454 #include <iostream>
455 #include <iomanip>
456 
457 /// Prints by rows in \c std::cout the contents of \c MATRIX[][].
458 /// Uses \c w chars to print each value.
459 template <class atype, unsigned Nrows, unsigned Ncols>
460 void printMatrix( atype MATRIX[Nrows][Ncols] , unsigned w=3 ) {
461  for (unsigned i=0; i < Nrows; ++i) {
462  for (unsigned j=0; j < Ncols; ++j) {
463  std::cout << std::setw(w) << MATRIX[i][j];
464  }
465  std:: cout << std::endl;
466  }
467 }
468 
469 /// Stores all zeroes in \c MATRIX[][].
470 template <class atype, unsigned Nrows, unsigned Ncols>
471 void setValue( atype MATRIX[Nrows][Ncols] , atype val ) {
472  for (unsigned i=0; i < Nrows; ++i) {
473  for (unsigned j=0; j < Ncols; ++j) {
474  MATRIX[i][j] = val;
475  }
476  }
477 }
478 
479 /// Stores all zeroes in \c MATRIX[][].
480 template <class atype, unsigned Nrows, unsigned Ncols>
481 void setZero( atype MATRIX[Nrows][Ncols] ) {
482  setValue<atype,Nrows,Ncols>( MATRIX, atype(0) );
483 }
484 
485 /// Set the values in \c MATRIX[][] as consecutive numbers from \c 10.
486 /// Stores values by rows.
487 template <class atype, unsigned Nrows, unsigned Ncols>
488 void setRectangularRow( atype MATRIX[Nrows][Ncols] ) {
489  unsigned k = 10;
490  for (unsigned i=0; i < Nrows; ++i) {
491  for (unsigned j=0; j < Ncols; ++j) {
492  MATRIX[i][j] = atype( k++ );
493  }
494  }
495 }
496 
497 /// Invoca print_list() de muchas formas diferentes
499  print_list( " |a| etc... " );
500  print_list( "" );
501  print_list( "()" );
502  print_list( "[0]" );
503  print_list( "(0,1,22)" );
504  print_list( "( 1 2 3 4 5 )" );
505 
506  print_list( " | 22 | etc... " );
507  print_list( "|| etc... " );
508  print_list( " 22| |etc|" );
509  print_list( " |1| " );
510  print_list( " | 2 | " );
511  print_list( "1" );
512  print_list( "[-1]" );
513  print_list( "[1] [2] [3] [4] [5]" );
514  print_list( "{ [1] [2] [3] [4] [5] }" );
515 
516  print_list( " | 22 | |etc|" );
517  print_list( " |22 | |etc|" );
518  print_list( " | 22| |etc|" );
519  print_list( " 22 | |etc|" );
520  print_list( " 22 | |etc|" );
521  print_list( " 22| |etc|" );
522  print_list( "(1,2,3,4,5)" );
523  print_list( "( ... )" );
524  print_list( "( - )" );
525  print_list( "( 1 - 22 )" );
526  print_list( "(1,0,0,22,-333,0)" );
527  print_list( "(1 , - - 22, -333, - )" );
528  print_list( "( 0, 4444, 5555 )" );
529  print_list( "( - 4444, --5555)" );
530 }
531 
532 /// Test -> intmatrix().
534 {{ // test::intmatrix()
535  int M[4][5]; // [0][1][2][3][4]
536  intmatrix<4,5>( M , " | 1 // 3 4 5 | " // [0]
537  " | :: 7 8 9 10 | " // [1]
538  " | 11 12 13 .. 15 | " // [2]
539  " | 16 17 // 19 20 | " );// [3]
540 
541  assertTrue( M[0][0] == 1 ); assertTrue( M[2][1] == 12 );
542  assertTrue( M[2][3] == 0 ); assertTrue( M[1][0] == 0 );
543 
544  int k=1;
545  for ( size_t i=0; i<4; ++i ) {
546  for ( size_t j=0; j<5; ++j ) {
547  assertTrue( M[i][j]==k++ || M[i][j]==0 );
548  }
549  }
550 }}
551 { // Resto de las pruebas
552  int M[3][3]; setZero<int,3,3>( M );
553  intmatrix<3,3>( M , " | 1 2 3 4 5 | " // | 1 2 3 | [0]
554  " | 6 7 8 9 10 | " // | 6 7 8 | [1]
555  " | 11 12 13 14 15 | " // | 11 12 13 | [2]
556  " | 16 17 18 19 20 | " ); // [0][1][2]
557 // printMatrix<int,3,3>( M );
558  assertTrue( M[2][1] == 12 );
559  int k=1;
560  for ( size_t i=0; i<3; ++i ) {
561  for ( size_t j=0; j<3; ++j ) {
562  if( M[i][j]!=k ) {
563  std::cout << k << "== k != M[" << i << "][" << j
564  << "] == " << M[i][j] << std::endl;
565  }
566  assertTrue(M[i][j]==k++ );
567  }
568  k += (3-1);
569  }
570 }
571 { // Resto de las pruebas
572  int M[7][8]; setZero<int,7,8>( M );
573  intmatrix<7,8>( M , " | 1 // 3 4 5 | "
574  " | :: 7 8 9 10 | "
575  " | 11 12 13 .. 15 | "
576  " | 16 17 // 19 20 | " );
577 // printMatrix<int,7,8>( M );
578  assertTrue( M[2][1] == 12 );
579  int k=1;
580  for ( size_t i=0; i<4; ++i ) {
581  for ( size_t j=0; j<5; ++j ) {
582  assertTrue( M[i][j]==k++ || M[i][j]==0 );
583  }
584  }
585 }
586 { // Resto de las pruebas
587  int M[8][7]; setZero<int,8,7>( M );
588  intmatrix<8,7>( M , " | 1 : : : : | "
589  " | : 2 : : : | "
590  " | : : 3 : : | "
591  " | : : : 4 : | " );
592 // printMatrix<int,8,7>( M ); std::cout << std::endl;
593  assertTrue( M[3][3] == 4 );
594  int k=1;
595  for ( size_t i=0; i<8; ++i ) {
596  for ( size_t j=0; j<7; ++j ) {
597  if ( i==j && i<4 ) {
598  assertTrue( M[i][j]==k++ );
599  }
600  else {
601  assertTrue( M[i][j]==0 );
602  }
603  }
604  }
605 }
606 { // Resto de las pruebas
607  int M[8][7]; setZero<int,8,7>( M );
608  intmatrix<8,7>( M , " | 1 | "
609  " | . 2 , , . | "
610  " | . . 3 , . | "
611  " | . . . 4 . | " );
612 // printMatrix<int,8,7>( M ); std::cout << std::endl;
613  assertTrue( M[1][1] == 2 );
614  int k=1;
615  for ( size_t i=0; i<8; ++i ) {
616  for ( size_t j=0; j<7; ++j ) {
617  if ( i==j && i<4 ) {
618  assertTrue( M[i][j]==k++ );
619  }
620  else {
621  assertTrue( M[i][j]==0 );
622  }
623  }
624  }
625 }
626 }
627 
628 /// Test -> str2matrix().
630 {{ // test::str2matrix()
631  std::list< std::list<std::string> > M;
632  size_t sz = str2matrix( M , " | 99 | " // L0
633  " | . 2 , , : | " // L1
634  " | . . 3 , . | " // L2
635  " | a b c 4 . | " ); // L3
636  std::list< std::list<std::string> > :: const_iterator itL = M.begin();
637  const std::list<std::string> & L0 = *itL; itL++;
638  const std::list<std::string> & L1 = *itL; itL++;
639  const std::list<std::string> & L2 = *itL; itL++;
640  const std::list<std::string> & L3 = *itL; itL++;
641  assertTrue( itL==M.end() && M.size() == 4 && sz == L3.size() );
642 
643  assertTrue( L0 == str2list( " ( 99 ) " ) ); // L0
644  assertTrue( L1 == str2list( " [ . 2 , , : ] " ) ); // L1
645  assertTrue( L2 == str2list( " < . . 3 , . > " ) ); // L2
646  assertTrue( L3 == str2list( " { a b c 4 . } " ) ); // L3
647 
648  assertTrue( L0.size() == 1 && L0.back() == "99" );
649  assertTrue( L1.size() == 3 && L1.back() == ":" );
650  assertTrue( L2.size() == 4 && L2.front() == "." );
651  assertTrue( L3.size() == 5 && L3.front() == "a" );
652 
653  assertTrue( L0 == str2list( "(99)" ) ); // L0
654  assertTrue( L1 == str2list( "(. 2 :)" ) ); // L1
655  assertTrue( L2 == str2list( "(. . 3 .)" ) ); // L2
656  assertTrue( L3 == str2list( "( a b c 4 .)" ) ); // L3
657 }}
658 { // Resto de las pruebas
659  std::list< std::list<std::string> > M;
660  size_t sz = str2matrix( M , " | 99 |" // L0
661  " | . 2 , , : |" // L1
662  " | . . 3 , . |" // L2
663  " | a b c 4 . |" ); // L3
664  assertTrue( ! M.back().empty() && M.size() == 4 && sz == 5 );
665  M.pop_back();
666  assertTrue( ! M.back().empty() && M.size() == 3 );
667 }
668 { // Resto de las pruebas
669  std::list< std::list<std::string> > M;
670  size_t sz = str2matrix( M , " | 1 | "
671  " | 2 | "
672  " | 3 | "
673  " | 4 | ");
674  assertTrue( ! M.back().empty() && M.size() == 4 && sz == 1 );
675 }
676 { // Resto de las pruebas
677  std::list< std::list<std::string> > M;
678  size_t sz = str2matrix( M , "|1||2||3||4|");
679  assertTrue( ! M.back().empty() && M.size() == 4 && sz == 1 );
680 }
681 { // Resto de las pruebas
682  std::list< std::list<std::string> > M;
683  size_t sz = str2matrix( M , "1|2|3|4");
684  assertTrue( ! M.back().empty() && M.size() == 4 && sz == 1 );
685 }
686 { // Resto de las pruebas
687  std::list< std::list<std::string> > M;
688  size_t sz = str2matrix( M , "[1|2|3|4 5]");
689  assertTrue( ! M.back().empty() && M.size() == 4 && sz == 2 );
690 }
691 }
692 
693 /// Test -> longmatrix().
695 {{ // test::longmatrix()
696  long M[4][5]; // [0][1][2][3][4]
697  longmatrix<4,5>( M , " | 1 // 3 4 5 | " // [0]
698  " | :: 7 8 9 10 | " // [1]
699  " | 11 12 13 .. 15 | " // [2]
700  " | 16 17 // 19 20 | " );// [3]
701 
702  assertTrue( M[0][0] == 1 ); assertTrue( M[2][1] == 12 );
703  assertTrue( M[0][1] == 0 ); assertTrue( M[3][2] == 0 );
704 
705  long k=1;
706  for ( size_t i=0; i<4; ++i ) {
707  for ( size_t j=0; j<5; ++j ) {
708  assertTrue( M[i][j]==k++ || M[i][j]==0 );
709  }
710  }
711 }}
712 { // Resto de las pruebas
713  long M[4][5];
714  longmatrix<4,5>( M, " | 1 2 3 4 5 | "
715  " | 6 7 8 9 10 | "
716  " | 11 12 13 14 15 | "
717  " | 16 17 18 19 20 | " );
718  int n = 1;
719  for ( int i=0; i<4; ++i ) {
720  for ( int j=0; j<5; ++j ) {
721  if (false) { M[2][3] = -1; }
722  std::string fail; // dice adónde falla str2matrix()
723  fail = TestCase::toString( M[i][j] ) + " == M";
724  fail += '[' + TestCase::toString(i) + ']';
725  fail += '[' + TestCase::toString(j) + ']' + " != ";
726  fail += TestCase::toString(n);
727  assertTrue_Msg( fail , M[i][j] == n );
728  ++n;
729  }
730  }
731  // =\_fail: -1 == M[2][3] != 14
732  // =/ (688) test_str2list.cpp
733 }
734 }
735 
736 /// Test -> doublematrix().
738 {{ // test::doublematrix()
739  double M[4][5]; // [0] [1] [2] [3] [4]
740  doublematrix<4,5>( M , " | 10. /// 30 40. 5.e1 | " // [0]
741  " | . 70. 80 90. 100. | " // [1]
742  " | 110 120 130 .. 150. | " // [2]
743  " | 160 170 /// 190 2.e2 | " );
744  assertTrue( M[2][1] == 120.0 );
745  assertTrue( M[1][0] == 0.0 && M[2][3] == 0.0 );
746  double k=10.0;
747  for ( size_t i=0; i<4; ++i ) {
748  for ( size_t j=0; j<5; ++j ) {
749  assertTrue( M[i][j]==k || M[i][j]==0 );
750  k += 10.0;
751  }
752  }
753 }}
754 { // Resto de las pruebas
755  double M[4][3]; setValue<double,4,3>( M,-1 );
756  doublematrix<4,3>( M , " 11. 22. | 33.0 0.44e2" );
757 // printMatrix<double,4,3>( M,4 ); std::cout << std::endl;
758 
759  assertTrue( M[0][0]==11.0 && M[0][1]==22.0 ); assertTrue( M[0][2]==-1.0 );
760  assertTrue( M[1][0]==33.0 && M[1][1]==44.0 ); assertTrue( M[1][2]==-1.0 );
761 
762  assertTrue( M[2][0]==-1.0 && M[2][1]==-1.0 ); assertTrue( M[2][2]==-1.0 );
763  assertTrue( M[3][0]==-1.0 && M[3][1]==-1.0 ); assertTrue( M[3][2]==-1.0 );
764 }
765 { // Resto de las pruebas
766  double M[4][5];
767  doublematrix<4,5>( M , " 11. "
768  "| 22. "
769  "| 33.0 "
770  "| 0.44e2 " );
771  assertTrue( M[0][0] == 11.0 );
772  assertTrue( M[1][0] == 22.0 );
773  assertTrue( M[2][0] == 33.0 );
774  assertTrue( M[3][0] == 44.0 );
775 }
776 { // Resto de las pruebas
777  double M[6][1];
778  doublematrix<6,1>( M , " 11e-4 "
779  "| 220.e-5 "
780  "| 33.0e-4 "
781  "| 0.44e-2 " );
782  assertTrue( M[0][0] == 0.0011 );
783  assertTrue( M[1][0] == 0.0022 );
784  assertTrue( M[2][0] == 0.0033 );
785  assertTrue( M[3][0] == 0.0044 );
786 }
787 { // Resto de las pruebas
788  long M[4][5];
789  longmatrix<4,5>( M , " | 1 | "
790  " | 2 | "
791  " | 3 | "
792  " | 4 | ");
793  long k = 1;
794  for ( int i=0; i<4; ++i ) {
795  for ( int j=0; j<1; ++j ) {
796  assertTrue( M[i][j] == k ); k++;
797  }
798  }
799 }
800 }
801 
802 /// Test -> \c charmatrix().
804 {{ // test::charmatrix()
805  char A[5][3];
806  charmatrix<5,3>( A , " | 1 a V | "
807  " | 2 b W | "
808  " | 3 c X | "
809  " | 4 d Y | "
810  " | 5 e Z | " );
811 
812  char D[5][3];
813  charmatrix<5,3>( D , " | 2 c Y | "
814  " | 3 d Z | "
815  " | 4 e V | "
816  " | 5 a W | "
817  " | 1 b X | " );
818 
819  for ( int j=0; j<5; ++j ) {
820  assertTrue( A[j][0] == (j)+'1' );
821  }
822  for ( int j=0; j<5; ++j ) {
823  assertTrue( A[j][1] == (j)+'a' );
824  }
825  for ( int j=0; j<5; ++j ) {
826  assertTrue( A[j][2] == (j)+'V' );
827  }
828 }}
829 {
830 }
831 }
832 
833 /// Programa principal.
834 int main() {
835  if (true) {
836  test_str2list tester;
837  tester.setUp();
838  tester.run();
839  std::cout << tester.report();
840  }
841  if (false) {
842  int M[4][5];
843  setZero<int,4,5>( M );
844  intmatrix<4,5>( M , " | 1 2 3 4 5 | " );
845  printMatrix<int,4,5>( M );
846  std::cout << std::endl;
847 
848  setRectangularRow<int,4,5>( M );
849  intmatrix<4,5>( M , " | 1 | 2 | 3 | 4 | 5 | " );
850  printMatrix<int,4,5>( M );
851  }
852  if ( false ) { do_print_list(); }
853 }
854 
855 // EOF: test_str2list.cpp