Java iterators for C++:
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
test_iterJava.cpp
Go to the documentation of this file.
1 // test_iterJava.cpp (c) 2009 adolfo@di-mare.com
2 
3 /** \file test_iterJava.cpp
4  \brief Test ==> class iterJava<>.
5 
6  \author Adolfo Di Mare <adolfo@di-mare.com>
7  \date 2009
8 
9  - Why English names??? ==> http://www.di-mare.com/adolfo/binder/c01.htm#sc04
10 */
11 
12 #include "iterJava.h"
13 #include "str2list.h"
14 #include "BUnit.h"
15 #include <list>
16 
17 /// Test ==> \c iterJava<>.
18 class test_iterJava : public TestCase {
19 public:
20  bool run();
21 private:
22  void test_example();
23  void test_const_example();
24  void test_examplePtr();
25  void test_assign();
26  void test_set_CCC();
27  void test_next();
28  void test_erase();
29  void test_palindrome();
30  void test_palindromo();
31  void test_setReverse();
32  void test_isPalindrome();
33  void test_Tree_BF();
34  void test_Tree_PLR();
35  void test_Tree_LPR();
36  void test_Tree_LRP();
37 }; // test_iterJava
38 
39 /// Test ==> \c iterJava<> ==> \c run().
41  test_example();
44  test_assign();
45  test_set_CCC();
46  test_next();
47  test_erase();
52  test_Tree_BF();
53  test_Tree_PLR();
54  test_Tree_LPR();
55  test_Tree_LRP();
56  return wasSuccessful();
57 }
58 
59 std::list<long> makeList_long(const char* V); // forward declaration
60 
61 /// Test ==> \c iterJava<> ==> \c example().
63  {{ // test::example()
64  std::list<long> Lfwd, Lbck, L;
65  assertTrue( Lfwd.empty() && L.empty() );
66  L = makeList_long( "( 1 2 3 4 5 )" );
67  iterJava< std::list<long>::iterator > itFwd( L.begin(), L.end() );
68  iterJava< std::list<long>::iterator > itBck( L.begin(), L.end() );
69  itBck.setReverse();
70  while ( itFwd.hasNext() && itBck.hasNext() ) {
71  Lfwd.push_back( itFwd.next() );
72  Lbck.push_back( itBck.next() );
73  }
74  assertTrue( Lfwd == makeList_long( "( 1 2 3 4 5 )" ) );
75  assertTrue( Lbck == makeList_long( "( 5 4 3 2 1 )" ) );
76  }}
77  { std::list<long> Lcp, L;
78  { // L.empty()
79  L = makeList_long( "" );
80  iterJava< std::list<long>::iterator > iter( L.begin(), L.end() );
81  assertTrue( ! iter.hasNext() );
82  }
83  { // L.size() == 1
84  L = makeList_long( " 1 " );
85  iterJava< std::list<long>::iterator > iter( L.begin(), L.end() );
86  assertTrue( iter.hasNext() );
87  assertTrue( iter.next() == 1 );
88  assertTrue( ! iter.hasNext() );
89  }
90  { // L.size() == 2
91  L = makeList_long( " 1 2 " );
92  iterJava< std::list<long>::iterator > iter( L.begin(), L.end() );
93  assertTrue( iter.hasNext() );
94  assertTrue( iter.next() == 1 );
95  assertTrue( iter.hasNext() );
96  assertTrue( iter.next() == 2 );
97  assertTrue( ! iter.hasNext() );
98  iter.set( L );
99  assertTrue( iter.next() == 1 );
100  L.erase(iter);
101  assertTrue( iter.next() == 2 );
102  assertTrue( ! iter.hasNext() );
103  }
104  { // L.empty()
105  std::list<long> Lcp;
106  L = makeList_long( " 5 4 3 2 1 " );
107  iterJava< std::list<long>::iterator > iter( L.begin(), L.end() );
108  iter.setReverse();
109  while ( iter.hasNext() ) {
110  Lcp.push_back( iter.next() );
111  }
112  assertTrue( Lcp == makeList_long( "( 1 2 3 4 5 )" ) );
113  }
114  }
115 }
116 
117 /// Test ==> \c iterJava<> ==> \c examplePtr().
119  {{ // test::examplePtr()
120  #define dim(V) ( sizeof(V)/sizeof(*V) )
121  std::list<long> L;
122  int VEC[] = { 1, 2, 3, 4, 5 };
123  iterJava< int* > iter( VEC, VEC+dim(VEC) );
124  while ( iter.hasNext() ) {
125  L.push_back( iter.next() );
126  }
127  assertTrue( L == makeList_long( "( 1 2 3 4 5 )" ) );
128  }}
129  #undef dim
130 }
131 
132 /// Test ==> \c iterJava<> ==> \c const_example().
134  #ifdef const_error
135  #warning Macro [const_error] already defined
136  #endif
137  {{ // test::const_example()
138  #define const_error
139  #undef const_error
140  std::list<long> Lcp, L;
141  assertTrue( Lcp.empty() );
142  L = makeList_long( "( 1 2 3 4 5 )" );
143  iterJava< std::list<long>::const_iterator > iter( L.begin(),L.end() );
144  while ( iter.hasNext() ) {
145  Lcp.push_back( iter.next() );
146  #ifdef const_error
147  L.erase(iter); // forbidden for const_iterator
148  #endif
149  }
150  assertTrue( L == Lcp );
152  assertTrue( IT == iter && !( IT.hasNext() ) && !( IT != iter ) );
153  #undef const_error
154  }}
155  { // const list<>&
156  std::list<long> Lcp, L;
157  assertTrue( Lcp.empty() );
158  L = longlist( "( 1 2 3 4 5 )" );
159  const std::list<long> & L_const = L;
160  iterJava< std::list<long>::const_iterator > iter( L_const.begin(),L_const.end() );
161  while ( iter.hasNext() ) {
162  Lcp.push_back( iter.next() );
163  }
164  assertTrue( L == Lcp );
166  assertTrue( IT == iter && !( IT.hasNext() ) && !( IT != iter ) );
167  }
168 }
169 
170 #include <cassert>
171 
172 #ifdef English_dox
173 /// Implementation to show that const_iterator cannot be used to erase values.
174 #endif
175 #ifdef Spanish_dox
176 /// Implementación en la que se muestra que un \c const_iterator no se puede usar para eliminar valores.
177 #endif
178 void const_compile_error( std::list<long> &L ) {
180  Iter iter( L.begin(),L.end() );
181  while ( iter.hasNext() ) {
182  // L.erase(iter); // forbidden for const_iterator
183  }
184  assert( L.empty() );
185 }
186 
187 template <typename C>
188 bool isPalindrome( const C& CCC );
189 
190 /// Test ==> \c iterJava<> ==> \c isPalindrome().
192  {{ // test::isPalindrome()
193  assertFalse( isPalindrome( makeList_long( "( 1 2 3 4 5 )" ) ) );
194  assertTrue( isPalindrome( makeList_long( "( 1 2 3 2 1 )" ) ) );
195  assertTrue( isPalindrome( makeList_long( "( 1 )" ) ) );
196  assertTrue( isPalindrome( makeList_long( "( 1 2 1 )" ) ) );
197  assertTrue( isPalindrome( makeList_long( "( )" ) ) );
198  }}
199 }
200 
201 /// Test ==> \c iterJava<> ==> \c palindrome().
203  {{ // test::palindrome()
204  std::list<long> L = makeList_long( "( 1 2 3 2 1 )" );
205  typedef std::list<long>::const_iterator IterFwd;
206  typedef std::reverse_iterator<IterFwd> IterBck;
207 
208  iterJava< IterFwd > itFwd( L.begin(), L.end() );
209  iterJava< IterBck > itBck( L.rbegin(), L.rend() );
210 
211  while ( itFwd.hasNext() && itBck.hasNext() ) {
212  if ( itFwd.next() != itBck.next() ) {
213  assertTrue( "palindrome error" && false );
214  }
215  }
216  assertTrue( !itFwd.hasNext() && !itBck.hasNext() );
217  }}
218  {
219  std::list<long> L = makeList_long( "( 1 2 3 2 1 )" );
220  typedef std::list<long>::const_iterator Iter;
221  iterJava< Iter > itFwd, itBck;
222  itFwd.set(L);
223  itBck.set(L); itBck.setReverse();
224  while ( itFwd.hasNext() && itBck.hasNext() ) {
225  if ( itFwd.next() != itBck.next() ) {
226  assertTrue( "palindrome error" && false );
227  }
228  }
229  assertTrue( !itFwd.hasNext() && !itBck.hasNext() );
230  }
231 }
232 
233 /// Test ==> \c iterJava<> ==> \c setReverse().
235  {{ // test::setReverse()
236  std::list<long> L = makeList_long( "( 1 2 3 4 5 )" );
237  typedef std::list<long>::const_iterator Iter;
238  iterJava< Iter > itFwd, itBck;
239  itFwd.set(L);
240  assertTrue( itFwd.next() == 1 ); // next() !
241  itFwd.setReverse(); // NOP !!!
242  assertTrue( itFwd.isForward() );
243 
244  itBck.set(L); itBck.setReverse();
245  assertTrue( !itBck.isForward() );
246  assertTrue( itBck.isBackward() && itBck.isReverse() );
247  assertTrue( itBck.next() == 5 );
248 
249  itFwd.set(L); itFwd.setReverse();
250  assertTrue( ! itFwd.isForward() && itFwd.isReverse() );
251  }}
252  {
253  }
254 }
255 
256 #if 1
257  template <typename C> bool isPalindrome( const C& CCC ) {{
258  typedef typename C::const_iterator Iter;
259  iterJava< Iter > itFwd, itBck;
260  itFwd.set(CCC);
261  itBck.set(CCC); itBck.setReverse();
262  while ( itFwd.hasNext() && itBck.hasNext() ) {
263  if ( itFwd.next() != itBck.next() ) {
264  return false;
265  }
266  }
267  return ( !itFwd.hasNext() && !itBck.hasNext() );
268  }}
269 #else
270  template <typename C>
271  bool isPalindrome( const C& CCC ) {{
272  typedef typename C::const_iterator IterFwd;
273  typedef typename std::reverse_iterator<IterFwd> IterBck;
274 
275  iterJava< IterFwd > itFwd( CCC.begin(), CCC.end() );
276  iterJava< IterBck > itBck( CCC.rbegin(), CCC.rend() );
277 
278  while ( itFwd.hasNext() && itBck.hasNext() ) {
279  if ( itFwd.next() != itBck.next() ) {
280  return false;
281  }
282  }
283  return ( !itFwd.hasNext() && !itBck.hasNext() );
284  }}
285 #endif
286 
287 #ifdef English_dox
288 /**
289  \fn isPalindrome( const C& CCC )
290  Returns \c true when \c CCC is a palindrome.
291  - Traversing a palindrome forward yields the same
292  secuence of values as traversing it backwards.
293 
294  \dontinclude test_iterJava.cpp
295  \skipline test::palindrome()
296  \until }}
297 
298  \dontinclude test_iterJava.cpp
299  \skipline isPalindrome( const C& CCC ) {{
300  \until }}
301 
302  \dontinclude test_iterJava.cpp
303  \skipline test::isPalindrome()
304  \until }}
305  \see test_iterJava::test_palindrome()
306  \see test_iterJava::test_isPalindrome()
307 */
308 #endif
309 #ifdef Spanish_dox
310 /**
311 \fn isPalindrome( const C& CCC )
312  Retorna \c true si \c CCC es un palíndromo.
313  - Al recorrer un palíndromo hacia adelante se obtienen
314  los mismo valors que al recorrerlo hacia atrás.
315 
316  \dontinclude test_iterJava.cpp
317  \skipline test::palindrome()
318  \until }}
319 
320  \dontinclude test_iterJava.cpp
321  \skipline isPalindrome( const C& CCC ) {{
322  \until }}
323 
324  \dontinclude test_iterJava.cpp
325  \skipline test::isPalindrome()
326  \until }}
327  \see test_iterJava::test_palindrome()
328  \see test_iterJava::test_isPalindrome()
329 */
330 #endif
331 
332 /// Test ==> \c iterJava<> ==> \c palindrome().
334  bool palindromo(const std::list< std::string >& L);
335  {{ // test::palindromo()
336  assertTrue( palindromo( str2list( "[ 1 2 3 2 1 ]") ) );
337  assertFalse( palindromo( str2list( "[ 1 2 3 2 0 ]") ) );
338  assertTrue( palindromo( str2list( "[ r a d a r ]") ) );
339  assertFalse( palindromo( str2list( "[ M A J E M ]") ) );
340  }}
341 }
342 
343 /** Regresa true si al recorrer el contenedor "L" hacia adelante se obtiene
344  el mismo resultado que al recorrerlo hacia atrás.
345  Un palíndromo es una palabra que se lee igual al derecho que al revés.
346 
347  \dontinclude test_iterJava.cpp
348  \skipline test::palindromo()
349  \until }}
350 */
351 bool palindromo(const std::list< std::string >& L) {
352  return isPalindrome(L);
353 }
354 
355 /// Test ==> \c iterJava<> ==> \c next().
357  {{ // test::next()
358  std::list<long> L = makeList_long( "( 1 2 3 4 5 )" );
359  iterJava< std::list<long>::iterator > iter( L.begin(), L.end() );
360  int trngl = 3;
361  while ( iter.hasNext() ) {
362  switch (trngl++ % 3) {
363  case 0: { // reference
364  long & l = iter.next(); l++;
365  break;
366  }
367  case 1: { // pointer
368  long * p = & iter.next(); (*p)++;
369  break;
370  }
371  case 2: { // direct
372  iter.next()++;
373  break;
374  }
375  }
376  }
377  assertTrue( L == makeList_long( "( 2 3 4 5 6 )" ) );
378  }}
379  if (false) { // cannot erase with 2 iterators over the same container
380  std::list<long> L = makeList_long( "( 1 2 3 4 5 )" );
381  iterJava< std::list<long>::iterator > itFwd( L.begin(), L.end() );
382  iterJava< std::list<long>::iterator > itBck( L.begin(), L.end() );
383  itBck.setReverse(); int n=0;
384  while ( itFwd.hasNext() && itBck.hasNext() ) {
385  ++n;
386  L.push_back( itFwd.next() );
387  L.push_back( itBck.next() );
388  if ( itFwd.current() == itBck.current() ) {
389  break;
390  }
391  L.erase(itFwd);
392  L.erase(itBck);
393  }
394  assertTrue( L.size() != 7 );
395  assertTrue( n == 3 );
396  assertTrue( itFwd.next() == 3 );
397  assertTrue( itBck.next() == 3 );
398  }
399 }
400 
401 /// Test ==> \c iterJava<> ==> \c set(CCC).
403  {{ // test::set_CCC()
404  std::list<long> L1 = makeList_long( "( 1 2 3 4 5 )" );
405  std::list<long> L2 = makeList_long( "( 6 7 8 9 )" );
406  iterJava< std::list<long>::iterator > iter( L1.begin(), L1.end() );
407  int i = 0;
408  while ( iter.hasNext() ) { // L1
409  i++; assertTrue( i == iter.next() );
410  L1.erase( iter );
411  }
412  iter.set( L2 );
413  while ( iter.hasNext() ) { // L2
414  i++; assertTrue( i == iter.next() );
415  }
416  assertTrue( L1.empty() );
417  }}
418  { // same but reversed
419  std::list<long> L1 = makeList_long( "( 1 2 3 4 5 )" );
420  std::list<long> L2 = makeList_long( "( 6 7 8 9 )" );
422  iter.set( L2 ); iter.setReverse();
423  int i = 10;
424  while ( iter.hasNext() ) { // L2
425  i--; assertTrue( i == iter.next() );
426  }
427  iter.set( L1 ); iter.setReverse();
428  while ( iter.hasNext() ) { // L1
429  i--; assertTrue( i == iter.next() );
430  }
431  }
432 }
433 
434 /// Test ==> \c iterJava<> ==> \c assign(CCC).
436  {{ // test::assign()
437  std::list<long> L, Lcp;
438  L = makeList_long( "( 1 2 3 4 5 )" );
441  iter123.set( L.begin(), L.end() ); // full range
442  int i=1;
443  while ( i<=3 ) {
444  assertTrue( i == iter123.next() );
445  Lcp.push_back( i );
446  ++i;
447  }
448  iter45 = iter123; // iter45 <==> iter123
449  while ( i<=5 ) {
450  assertTrue( i == iter45.next() );
451  Lcp.push_back( i );
452  ++i;
453  }
454  assertTrue( Lcp == makeList_long( "( 1 2 3 4 5 )" ) );
455  }}
456  { // non-const test
457  std::list<long> L, Lcp;
458  L = makeList_long( "( 1 2 3 4 5 )" );
461  iter123.set( L.begin(), L.end() ); // full range
462  int i=1;
463  while ( i<=3 ) {
464  assertTrue( i == iter123.next() );
465  Lcp.push_back( i );
466  L.erase( iter123 );
467  ++i;
468  }
469  iter45 = iter123;
470  while ( i<=5 ) { // iter45 <==> iter123
471  assertTrue( i == iter45.next() );
472  Lcp.push_back( i );
473  L.erase( iter45 );
474  ++i;
475  }
476  assertTrue( Lcp == makeList_long( "( 1 2 3 4 5 )" ) );
477  assertTrue( L.empty() );
478  }
479 }
480 
481 /// Test ==> \c iterJava<> ==> \c erase().
483  {{ // test::erase()
484  std::list<long> L = makeList_long( "( 1 2 3 4 5 )" );
485  iterJava< std::list<long>::iterator > iter( L.begin(), L.end() );
486  int i = 0;
487  while ( iter.hasNext() ) {
488  assertTrue( ! L.empty() );
489  long *pL = & iter.next(); i++;
490  assertTrue( i == *pL );
491  L.erase( iter ); // L.erase( iter.current() );
492  }
493  assertTrue( L.empty() );
494  }}
495  {
496  }
497 }
498 
499 #include "Tree_BF.h" // test multiple inclusion
500 #include "Tree_LRP.h"
501 #include "Tree_LPR.h"
502 #include "Tree_PLR.h"
503 
504 #include "Tree_BF.h"
505 #include "Tree_LRP.h"
506 #include "Tree_LPR.h"
507 #include "Tree_PLR.h"
508 
509 /// Test ==> \c iterJava<> ==> \c Tree_BF().
511  void make_a_o(TL::Tree<char> & T); // forward declaration
512  {{ // test::Tree_BF()
513  TL::Tree<char> T; make_a_o(T);
514  Tree_BF<char> iter; std::string L;
515  iter.set(T);
516  while ( iter.hasNext() ) {
517  TL::Tree<char> S = iter.next();
518  L.push_back( *S );
519  }
520  assertTrue( L == "abcdefghijklmno" && "Tree_BF" );
521  }}
522  if (false) { // T.empty() && 1 == T.Count()
523  TL::Tree<char> T;
524  Tree_BF<char> iter;
525  iter.set(T);
526  assertTrue( ! iter.hasNext() && "Tree_BF" );
527  T = 'A';
528  iter.set(T);
529  assertTrue( iter.hasNext() && "Tree_BF" );
530  assertTrue( 'A' == iter.next().Data() && "Tree_BF" );
531  assertTrue( ! iter.hasNext() && "Tree_BF" );
532  }
533  {
534  TL::Tree<char> T;
535  Tree_BF<char> iter; std::string L;
536  T = 'A';
537  T.Change_Child(1,'1');
538  T.Child(1).Change_Child(2,'2');
539  T.Child(1).Child(2).Change_Child(3,'3');
540  iter.set(T); L.clear();
541  while ( iter.hasNext() ) {
542  TL::Tree<char> S = iter.next();
543  L.push_back( *S );
544  }
545  assertTrue( L == "A123" && "Tree_BF" );
546  }
547  // T = 0
548  // ./ \.
549  // 1 2
550  // ./ \ / \.
551  // 3 4 5 6
552  void make_0_6(TL::Tree<char> & T); // forward declaration
553  {
554  TL::Tree<char> T; make_0_6(T);
555  Tree_BF<char> iter; std::string L;
556  iter.set(T);
557  while ( iter.hasNext() ) {
558  TL::Tree<char> S = iter.next();
559  L.push_back( *S );
560  }
561  assertTrue( L == "0123456" && "Tree_BF" );
562  }
563  // T = F
564  // ./ \.
565  // B G
566  // ./ \ \.
567  // A D I
568  // ./ \ /.
569  // C E H
570  void make_A_H(TL::Tree<char> & T); // forward declaration
571  {
572  TL::Tree<char> T; make_A_H(T);
573  Tree_BF<char> iter; std::string L;
574  iter.set(T);
575  while ( iter.hasNext() ) {
576  TL::Tree<char> S = iter.next();
577  L.push_back( *S );
578  }
579  assertTrue( L == "FBGADICEH" && "Tree_BF" );
580  }
581  // T = A
582  // ./ \.
583  // ./ \.
584  // ./ \.
585  // B C
586  // ./ \ / \.
587  // D E F G
588  // ./ \ / \ / \ / \.
589  // H I J K L M N O
590  void make_A_O(TL::Tree<char> & T); // forward declaration
591  {
592  TL::Tree<char> T; make_A_O(T);
593  Tree_BF<char> iter; std::string L;
594  iter.set(T);
595  while ( iter.hasNext() ) {
596  TL::Tree<char> S = iter.next();
597  L.push_back( *S );
598  }
599  assertTrue( L == "ABCDEFGHIJKLMNO" && "Tree_BF" );
600  }
601 }
602 
603 /// Test ==> \c iterJava<> ==> \c Tree_PLR().
605  void make_a_o(TL::Tree<char> & T);
606  {{ // test::Tree_PLR()
607  TL::Tree<char> T; make_a_o(T);
608  Tree_PLR<char> iter; std::string L;
609  iter.set(T);
610  while ( iter.hasNext() ) {
611  TL::Tree<char> S = iter.next();
612  L.push_back( *S );
613  }
614  assertTrue( L == "abfghcdeijlmnok" && "Tree_PLR" );
615  }}
616  if (true) { // T.empty() && 1 == T.Count()
617  TL::Tree<char> T;
618  Tree_PLR<char> iter;
619  iter.set(T);
620  assertTrue( ! iter.hasNext() && "Tree_PLR" );
621  T = 'A';
622  iter.set(T);
623  assertTrue( iter.hasNext() && "Tree_PLR" );
624  assertTrue( 'A' == iter.next().Data() && "Tree_PLR" );
625  assertTrue( ! iter.hasNext() && "Tree_PLR" );
626  }
627  // T = 0
628  // ./ \.
629  // 1 2
630  // ./ \ / \.
631  // 3 4 5 6
632  void make_0_6(TL::Tree<char> & T); // forward declaration
633  {
634  TL::Tree<char> T; make_0_6(T);
635  Tree_PLR<char> iter; std::string L;
636  iter.set(T);
637  while ( iter.hasNext() ) {
638  TL::Tree<char> S = iter.next();
639  L.push_back( *S );
640  }
641  assertTrue( L == "0134256" && "Tree_PLR" );
642  }
643  // T = F
644  // ./ \.
645  // B G
646  // ./ \ \.
647  // A D I
648  // ./ \ /.
649  // C E H
650  void make_A_H(TL::Tree<char> & T); // forward declaration
651  {
652  TL::Tree<char> T; make_A_H(T);
653  Tree_PLR<char> iter; std::string L;
654  iter.set(T);
655  while ( iter.hasNext() ) {
656  TL::Tree<char> S = iter.next();
657  L.push_back( *S );
658  }
659  assertTrue( L == "FBACEDGIH" && "Tree_PLR" );
660  }
661  // T = A
662  // ./ \.
663  // ./ \.
664  // ./ \.
665  // B C
666  // ./ \ / \.
667  // D E F G
668  // ./ \ / \ / \ / \.
669  // H I J K L M N O
670  void make_A_O(TL::Tree<char> & T); // forward declaration
671  {
672  TL::Tree<char> T; make_A_O(T);
673  Tree_PLR<char> iter; std::string L;
674  iter.set(T);
675  while ( iter.hasNext() ) {
676  TL::Tree<char> S = iter.next();
677  L.push_back( *S );
678  }
679  assertTrue( L == "ABDHIEJKCFLMGNO" && "Tree_PLR" );
680  }
681 }
682 
683 /// Test ==> \c iterJava<> ==> \c Tree_LPR().
685  void make_a_o(TL::Tree<char> & T);
686  {{ // test::Tree_LPR()
687  TL::Tree<char> T; make_a_o(T);
688  Tree_LPR<char> iter; std::string L;
689  iter.set(T);
690  while ( iter.hasNext() ) {
691  TL::Tree<char> S = iter.next();
692  L.push_back( *S );
693  }
694  assertTrue( L == "fbghacdieljnmok" && "Tree_LPR" );
695  }}
696  if (false) { // T.empty() && 1 == T.Count() // WABE !!!
697  TL::Tree<char> T;
698  Tree_LPR<char> iter;
699  iter.set(T);
700  assertTrue( ! iter.hasNext() && "Tree_LPR" );
701  T = 'A';
702  iter.set(T);
703  assertTrue( iter.hasNext() && "Tree_LPR" );
704  assertTrue( 'A' == iter.next().Data() && "Tree_LPR" );
705  assertTrue( ! iter.hasNext() && "Tree_LPR" );
706  }
707  // T = 0
708  // ./ \.
709  // 1 2
710  // ./ \ / \.
711  // 3 4 5 6
712  void make_0_6(TL::Tree<char> & T); // forward declaration
713  {
714  TL::Tree<char> T; make_0_6(T);
715  Tree_LPR<char> iter; std::string L;
716  iter.set(T);
717  while ( iter.hasNext() ) {
718  TL::Tree<char> S = iter.next();
719  L.push_back( *S );
720  }
721  assertTrue( L == "3140526" && "Tree_LPR" );
722  assertTrue( L != "314526" && "Tree_LPR [T.Root() missing]" );
723  }
724  // T = F
725  // ./ \.
726  // B G
727  // ./ \ \.
728  // A D I
729  // ./ \ /.
730  // C E H
731  void make_A_H(TL::Tree<char> & T); // forward declaration
732  {
733  TL::Tree<char> T; make_A_H(T);
734  assertTrue( check_ok(T) );
735  Tree_LPR<char> iter; std::string L;
736  iter.set(T);
737  while ( iter.hasNext() ) {
738  TL::Tree<char> S = iter.next();
739  L.push_back( *S );
740  }
741  assertTrue( L == "CAEBDFGHI" && "Tree_LPR" );
742  assertTrue( L != "CAEDHI" && "Tree_LPR [ BFG missing ]" );
743  assertTrue( L != "CAEBDFGHIH" && "Tree_LPR [Extra H]" );
744  assertTrue( L != "CAEBDFG" && "Tree_LPR [ HI missing ]" );
745  }
746  // T = A
747  // ./ \.
748  // ./ \.
749  // ./ \.
750  // B C
751  // ./ \ / \.
752  // D E F G
753  // ./ \ / \ / \ / \.
754  // H I J K L M N O
755  void make_A_O(TL::Tree<char> & T); // forward declaration
756  {
757  TL::Tree<char> T; make_A_O(T);
758  Tree_LPR<char> iter; std::string L;
759  iter.set(T);
760  while ( iter.hasNext() ) {
761  TL::Tree<char> S = iter.next();
762  L.push_back( *S );
763  }
764  assertTrue( L == "HDIBJEKALFMCNGO" && "Tree_LPR" );
765  assertTrue( L != "HDIJEKLFMNGO" && "Tree_LPR [ BAC missing ]" );
766  }
767 }
768 
769 /// Test ==> \c iterJava<> ==> \c Tree_LRP().
771  void make_a_o(TL::Tree<char> & T);
772  {{ // test::Tree_LRP()
773  TL::Tree<char> T; make_a_o(T);
774  Tree_LRP<char> iter; std::string L;
775  iter.set(T);
776  while ( iter.hasNext() ) {
777  TL::Tree<char> S = iter.next();
778  L.push_back( *S );
779  }
780  assertTrue( L == "fghbcdilnomjkea" && "Tree_LRP" );
781  }}
782  if (false) { // T.empty() && 1 == T.Count() // WABE !!!
783  TL::Tree<char> T;
784  Tree_LRP<char> iter;
785  iter.set(T);
786  assertTrue( ! iter.hasNext() && "Tree_LRP" );
787  T = 'A';
788  iter.set(T);
789  assertTrue( iter.hasNext() && "Tree_LRP" );
790  assertTrue( 'A' == iter.next().Data() && "Tree_LRP" );
791  assertTrue( ! iter.hasNext() && "Tree_LRP" );
792  }
793  // T = 0
794  // ./ \.
795  // 1 2
796  // ./ \ / \.
797  // 3 4 5 6
798  void make_0_6(TL::Tree<char> & T); // forward declaration
799  {
800  TL::Tree<char> T; make_0_6(T);
801  Tree_LRP<char> iter; std::string L;
802  iter.set(T);
803  while ( iter.hasNext() ) {
804  TL::Tree<char> S = iter.next();
805  L.push_back( *S );
806  }
807  assertTrue( L == "3415620" && "Tree_LRP" );
808  }
809  // T = F
810  // ./ \.
811  // B G
812  // ./ \ \.
813  // A D I
814  // ./ \ /.
815  // C E H
816  void make_A_H(TL::Tree<char> & T); // forward declaration
817  {
818  TL::Tree<char> T; make_A_H(T);
819  Tree_LRP<char> iter; std::string L;
820  iter.set(T);
821  while ( iter.hasNext() ) {
822  TL::Tree<char> S = iter.next();
823  L.push_back( *S );
824  }
825  assertTrue( L == "CEADBHIGF" && "Tree_LRP [Wabe]" );
826  assertTrue( L != "CEADBHIF" && "Tree_LRP [ G missing ]" );
827  }
828  // T = A
829  // ./ \.
830  // ./ \.
831  // ./ \.
832  // B C
833  // ./ \ / \.
834  // D E F G
835  // ./ \ / \ / \ / \.
836  // H I J K L M N O
837  void make_A_O(TL::Tree<char> & T); // forward declaration
838  {
839  TL::Tree<char> T; make_A_O(T);
840  Tree_LRP<char> iter; std::string L;
841  iter.set(T);
842  while ( iter.hasNext() ) {
843  TL::Tree<char> S = iter.next();
844  L.push_back( *S );
845  }
846  assertTrue( L == "HIDJKEBLMFNOGCA" && "Tree_LRP [Wabe]" );
847  assertTrue( L != "HIDJKEBLMFNOGA" && "Tree_LRP [ C missing ]" );
848  }
849 }
850 
851 /** <code>T = ( a ( b ( f g h ) c d e ( i j ( l m ( n o ) ) k ) )</code>.
852  \code
853  T = a
854  |--b
855  | |--f
856  T = a | |--g
857  /|\ | +--h
858  / / \ \ |--c
859  b c d e |--d
860  /|\ /|\ +--e
861  f g h i j k |--i
862  / \ |--j
863  l m | |--l
864  / \ | +--m
865  n o | |--n
866  | +--o
867  +--k
868  \endcode
869 */
871  TL::Tree<char> Th;
872  T.Erase();
873  T = 'a'; // inserta la raiz 'A'
874 
875  /***********************************/
876 
877  T.Change_Child(0, 'b'); // a
878  T.Change_Child(1, 'c'); // ./|\.
879  T.Change_Child(2, 'd'); // ./ / \ \.
880  T.Change_Child(3, 'e'); // b c d e
881 
882  /***********************************/
883 
884  Th = T.Child(0); // 'b'
885 
886  Th.Change_Child(0, 'f'); // b
887  Th.Change_Child(1, 'g'); // ./|\.
888  Th.Change_Child(2, 'h'); // f g h
889 
890  /***********************************/
891 
892  Th = T.Child(3); // 'e'
893  Th.Change_Child(0, 'i'); // e
894  Th.Change_Child(1, 'j'); // ./|\.
895  Th.Change_Child(2, 'k'); // i j k
896 
897  /***********************************/
898 
899  Th = Th.Child(1); // 'j'
900  Th.Change_Child(0, 'l'); // ./ \.
901  Th.Change_Child(1, 'm'); // l m
902 
903  Th = Th.Child(1); // 'm'
904  Th.Change_Child(0, 'n'); // ./ \.
905  Th.Change_Child(1, 'o'); // n o
906 }
907 
908 /** <code>T = ( 0 ( 1 ( 3 4 ) 2 ( 5 6 ) ) )</code>.
909  \code
910  T = 0
911  T = 0 |--1
912  ./ \. | |--3
913  1 2 | +--4
914  ./ \ / \. +--2
915  3 4 5 6 |--5
916  +--6
917  \endcode
918 */
920  TL::Tree<char> Th;
921  T.Erase();
922  T = '0'; // inserta la raiz '0'
923 
924  /***********************************/
925 
926  // 0
927  T.Change_Child(0, '1'); // ./ \.
928  T.Change_Child(1, '2'); // 1 2
929 
930  /***********************************/
931 
932  Th = T.Child(0); // '1'
933  Th.Change_Child(0, '3'); // ./ \.
934  Th.Change_Child(1, '4'); // 3 4
935 
936  /***********************************/
937 
938  Th = T.Child(1); // '2'
939  Th.Change_Child(0, '5'); // ./ \.
940  Th.Change_Child(1, '6'); // 5 6
941 }
942 
943 /** <code>T = ( F ( B ( A ( C E ) D ) ) G ( I ( H ) ) )</code>.
944  \code
945  F
946  T = F |--B
947  ./ \. | |--A
948  B G | | |--C
949  ./ \ \. | | +--E
950  A D I | +--D
951  ./ \ /. +--G
952  C E H +--I
953  \endcode +--H
954 */
956  TL::Tree<char> A, B, G, I;
957  T.Erase();
958 
959  /***********************************/
960  A = 'A'; // A
961  A.Change_Child(0, 'C'); // ./ \.
962  A.Change_Child(1, 'E'); // C E
963 
964  /***********************************/
965  I = 'I'; // I
966  I.Change_Child(0, 'H'); // ./.
967  // H
968 
969  /***********************************/
970  B = 'B'; // B
971  B.Graft(0, A); // ./ \.
972  B.Change_Child(1, 'D'); // A D
973 
974  /***********************************/
975  G = 'G'; // G
976  // \.
977  G.Graft(1, I); // I
978 
979  /***********************************/
980  T = 'F'; // F
981  T.Graft(0, B); // ./ \.
982  T.Graft(1, G); // B G
983 }
984 
985 /** <code>T = ( A ( B ( D ( H I) E ( J K ) ) C ( F ( L M ) G ( N O ) ) ) )</code>.
986  \code
987  T = A
988  |--B
989  | |--D
990  T = A | | |--H
991  ./ \. | | +--I
992  ./ \. | +--E
993  ./ \. | |--J
994  B C | +--K
995  ./ \ / \. +--C
996  D E F G |--F
997  ./ \ / \ / \ / \. | |--L
998  H I J K L M N O | +--M
999  \endcode +--G
1000  |--N
1001  +--O
1002 */
1004  TL::Tree<char> B, D, E, C, F, G;
1005  T.Erase();
1006 
1007  /***********************************/
1008  D = 'D'; // D
1009  D.Change_Child(0, 'H'); // ./ \.
1010  D.Change_Child(1, 'I'); // H I
1011 
1012  /***********************************/
1013  E = 'E'; // E
1014  E.Change_Child(0, 'J'); // ./ \.
1015  E.Change_Child(1, 'K'); // J K
1016 
1017  /***********************************/
1018  F = 'F'; // F
1019  F.Change_Child(0, 'L'); // ./ \.
1020  F.Change_Child(1, 'M'); // L M
1021 
1022  /***********************************/
1023  G = 'G'; // G
1024  G.Change_Child(0, 'N'); // ./ \.
1025  G.Change_Child(1, 'O'); // N O
1026 
1027  /***********************************/
1028  B = 'B'; // B
1029  B.Graft(0, D); // ./ \.
1030  B.Graft(1, E); // N O
1031 
1032  /***********************************/
1033  C = 'C'; // B
1034  C.Graft(0, F); // ./ \.
1035  C.Graft(1, G); // F G
1036 
1037  /***********************************/
1038  T = 'A'; // A
1039  T.Graft(0, B); // ./ \.
1040  T.Graft(1, C); // B C
1041 }
1042 
1043 #include <cmath> // rand() && srand()
1044 
1045 #ifdef English_dox
1046 /// C++ encapsulation of a generator as a Java iterator.
1047 #endif
1048 #ifdef Spanish_dox
1049 /// Encapsulamiento C++ de un generador como una iteración Java.
1050 #endif
1051 class Random {
1052  unsigned m_qty; ///< #.
1053  unsigned m_last; ///< Max.
1054  long m_val; ///< current().
1055 public:
1056  /// \c Iterator::init()
1057  Random( unsigned n=1 , long seed=3145159 )
1058  : m_qty(0), m_last(n) { srand(seed); }
1059 
1060  /// \c Iterator::hastNext()
1061  bool hasNext() const { return m_qty < m_last; }
1062  /// \c Iterator::next()
1063  const long& next()
1064  { m_qty++; return (m_val = rand()) ; }
1065 };
1066 
1067 #include <iostream>
1068 
1069 #ifdef English_dox
1070 /// Example usage of Java iterators as generators.
1071 #endif
1072 #ifdef Spanish_dox
1073 /// Ejemplo de uso de iteraciones Java como generadores.
1074 #endif
1076  Random iter( 15, 271828 );
1077  while ( iter.hasNext() ) {
1078  std::cout << iter.next() << std::endl;
1079  }
1080 }
1081 
1082 #ifdef English_dox
1083 /// Example usage of the \c iterJava<> iterator class.
1084 #endif
1085 #ifdef Spanish_dox
1086 /// Ejemplo de uso de la clase de iteración \c iterJava<>.
1087 #endif
1088 template <class Collection>
1089 void useIterator( Collection& C ) {
1090 // iterJava< typename Collection::iterator > iter( C.begin(), C.end() );
1092  ITER iter( C.begin(), C.end() );
1093  while ( iter.hasNext() ) {
1094  std::cout << iter.next();
1095  C.erase( iter ); // C.erase( iter.current() );
1096  }
1097 }
1098 
1099 #undef ZTREE
1100 #ifdef ZTREE
1101  using namespace TL; // avoid errors within Tree_Ex.h
1102  #include "Tree_Ex.h"
1103 #endif
1104 
1105 /// Test ==> \c main() ==> \c iterJava().
1106 int main() {
1107  test_iterJava tester;
1108  tester.setUp();
1109  tester.run();
1110  std::cout << tester.report();
1111 
1112  if (false) {
1113  std::list<long> L = makeList_long( "( 1 2 3 4 5 )" );
1114  std::cout << "useIterator( L ); ==> ";
1115  useIterator( L );
1116  assert( L.empty() );
1117  }
1118  if (false) {
1119  generateRandom( );
1120  }
1121  #ifdef ZTREE
1122  TL::Tree<char> ZT;
1123  make_A_O( ZT );
1124  Ztree_Print( ZT );
1125  #endif
1126 }
1127 // EOF: test_iterJava.cpp
An iterator over a collection.
Definition: iterJava.h:23
int main()
Test ==&gt; main() ==&gt; iterJava().
std::list< long > makeList_long(const char *V)
bool check_ok(const E &)
Left-Right-Process iterator.
const TL::Tree< E > next()
Iterator::next().
Definition: Tree_BF.h:95
void set(Iter first, Iter end)
Resets the iterator to traverse from first up to end.
Definition: iterJava.h:60
Left-Process-Right iterator.
Definition: Tree_LPR.h:63
void test_examplePtr()
Test ==&gt; iterJava&lt;&gt; ==&gt; examplePtr().
void generateRandom()
Example usage of Java iterators as generators.
const TL::Tree< E > next()
Iterator::next().
Definition: Tree_LPR.h:119
Test ==&gt; iterJava&lt;&gt;.
void set(const TL::Tree< E > &T)
Iterator::set().
Definition: Tree_BF.h:68
const TL::Tree< E > next()
Iterator::next().
Definition: Tree_LRP.h:110
unsigned m_qty
#.
void test_palindrome()
Test ==&gt; iterJava&lt;&gt; ==&gt; palindrome().
void test_const_example()
Test ==&gt; iterJava&lt;&gt; ==&gt; const_example().
bool isBackward() const
Returns true if the iteration goes in reverse (backwards).
Definition: iterJava.h:78
bool palindromo(const std::list< std::string > &L)
Regresa true si al recorrer el contenedor &quot;L&quot; hacia adelante se obtiene el mismo resultado que al rec...
void make_A_H(TL::Tree< char > &T)
T = ( F ( B ( A ( C E ) D ) ) G ( I ( H ) ) ).
void useIterator(Collection &C)
Example usage of the iterJava&lt;&gt; iterator class.
Tree Graft(unsigned n, Tree &o)
Injerta &quot;o&quot; para que sea el &quot;n&quot;-ésimo hijo de &quot;*this&quot;.
Definition: Tree_L.h:1456
void make_0_6(TL::Tree< char > &T)
T = ( 0 ( 1 ( 3 4 ) 2 ( 5 6 ) ) ).
void test_erase()
Test ==&gt; iterJava&lt;&gt; ==&gt; erase().
void test_palindromo()
Test ==&gt; iterJava&lt;&gt; ==&gt; palindrome().
void test_set_CCC()
Test ==&gt; iterJava&lt;&gt; ==&gt; set(CCC).
void make_A_O(TL::Tree< char > &T)
T = ( A ( B ( D ( H I) E ( J K ) ) C ( F ( L M ) G ( N O ) ) ) ).
bool hasNext() const
Iterator::hasNext().
Definition: Tree_PLR.h:109
std::iterator_traits< Iter >::reference next()
Returns the next element in the iteration.
Definition: iterJava.h:48
bool run()
Test ==&gt; iterJava&lt;&gt; ==&gt; run().
Random(unsigned n=1, long seed=3145159)
Iterator::init()
void const_compile_error(std::list< long > &L)
Implementation to show that const_iterator cannot be used to erase values.
bool isReverse() const
Returns true if the iteration goes in reverse (backwards).
Definition: iterJava.h:76
void test_Tree_PLR()
Test ==&gt; iterJava&lt;&gt; ==&gt; Tree_PLR().
void test_example()
Test ==&gt; iterJava&lt;&gt; ==&gt; example().
bool hasNext() const
Iterator::hasNext().
Definition: Tree_LRP.h:104
void set(const TL::Tree< E > &T)
Iterator::set().
Definition: Tree_LPR.h:78
C++ encapsulation of a generator as a Java iterator.
void make_a_o(TL::Tree< char > &T)
T = ( a ( b ( f g h ) c d e ( i j ( l m ( n o ) ) k ) ).
long m_val
current().
Left-Right-Process iterator.
Definition: Tree_LRP.h:59
void test_setReverse()
Test ==&gt; iterJava&lt;&gt; ==&gt; setReverse().
void Erase()
Elimina el árbol y sus descendientes.
Definition: Tree_L.h:1183
bool isPalindrome(const C &CCC)
Returns true when CCC is a palindrome.
void test_Tree_LPR()
Test ==&gt; iterJava&lt;&gt; ==&gt; Tree_LPR().
Breadth first tree iterator.
Definition: Tree_BF.h:55
bool hasNext() const
Iterator::hasNext().
Definition: Tree_BF.h:90
void setReverse()
Sets the iterator to go reverse (backwards).
Definition: iterJava.h:66
bool hasNext() const
Iterator::hastNext()
bool isForward() const
Returns true if the iteration is natural, from first to last.
Definition: iterJava.h:75
Breadth first tree iterator.
#define dim(V)
Los métodos para trabajar con árboles regresan &quot;referencias&quot; que son sub-árboles. ...
Definition: Tree_L.h:25
void set(const TL::Tree< E > &T)
Iterator::set().
Definition: Tree_PLR.h:72
const long & next()
Iterator::next()
bool hasNext() const
Iterator::hasNext().
Definition: Tree_LPR.h:101
bool hasNext() const
Returns true if the iteration has more elements.
Definition: iterJava.h:44
unsigned m_last
Max.
void test_next()
Test ==&gt; iterJava&lt;&gt; ==&gt; next().
Java iterators for C++.
void set(const TL::Tree< E > &T)
Iterator::set().
Definition: Tree_LRP.h:72
Process-Left-Right iterator.
Definition: Tree_PLR.h:59
Tree Child(unsigned n) const
Acceso al &quot;n&quot;-ésimo hijo.
Definition: Tree_L.h:375
void test_isPalindrome()
Test ==&gt; iterJava&lt;&gt; ==&gt; isPalindrome().
Left-Process-Right iterator.
const TL::Tree< E > next()
Iterator::next().
Definition: Tree_PLR.h:115
void test_assign()
Test ==&gt; iterJava&lt;&gt; ==&gt; assign(CCC).
void test_Tree_LRP()
Test ==&gt; iterJava&lt;&gt; ==&gt; Tree_LRP().
void test_Tree_BF()
Test ==&gt; iterJava&lt;&gt; ==&gt; Tree_BF().
Tree Change_Child(unsigned n, const value_type &d)
Sustituye por &quot;d&quot; el valor almacenado en el hijo número &quot;n&quot; del árbol.
Definition: Tree_L.h:949
Process-Left-Right iterator.