// examples/test_iterators translated by mycpp // BEGIN mycpp output #include "mycpp/runtime.h" GLOBAL_STR(str0, "--- simple iterators"); GLOBAL_STR(str1, "f() gave %d"); GLOBAL_STR(str2, "Foo.bar() gave %d"); GLOBAL_STR(str3, "--- nested iterators"); GLOBAL_STR(str4, "g() gave (%d, %r)"); GLOBAL_STR(str5, "Foo.baz() gave (%d, %r)"); GLOBAL_STR(str6, "f() gave %d, %d"); GLOBAL_STR(str7, "--- iterator assignment"); GLOBAL_STR(str8, "next(f()) gave %d"); GLOBAL_STR(str9, "next(g()) gave (%d, %r)"); GLOBAL_STR(str10, "l[%d] = %d"); namespace test_iterators { // forward declare class Foo; } // forward declare namespace test_iterators namespace test_iterators { // declare void g(int n, List*>* _out_yield_acc); void f(int n, List* _out_yield_acc); class Foo { public: Foo(); void bar(int n, List* _out_yield_acc); void baz(int n, List*>* _out_yield_acc); static constexpr ObjHeader obj_header() { return ObjHeader::ClassScanned(0, sizeof(Foo)); } DISALLOW_COPY_AND_ASSIGN(Foo) }; void run_tests(); void run_benchmarks(); } // declare namespace test_iterators namespace test_iterators { // define void g(int n, List*>* _out_yield_acc) { List _for_yield_acc0; f(n, &_for_yield_acc0); for (ListIter it(&_for_yield_acc0); !it.Done(); it.Next()) { int i = it.Value(); _out_yield_acc->append((Alloc>(i, StrFormat("2 * %d = %d", i, (2 * i))))); ; } } void f(int n, List* _out_yield_acc) { for (int i = 0; i < n; ++i) { _out_yield_acc->append(i); ; } } Foo::Foo() { ; // pass } void Foo::bar(int n, List* _out_yield_acc) { for (int i = 0; i < n; ++i) { _out_yield_acc->append(i); ; } } void Foo::baz(int n, List*>* _out_yield_acc) { List*> _iter_buf_it_g; g(n, &_iter_buf_it_g); ListIter*> it_g(&_iter_buf_it_g); while (true) { try { _out_yield_acc->append(it_g.iterNext()); ; } catch (StopIteration*) { break; } } } void run_tests() { test_iterators::Foo* foo = nullptr; int i; BigStr* s = nullptr; List* l = nullptr; StackRoot _root0(&foo); StackRoot _root1(&s); StackRoot _root2(&l); mylib::print_stderr(str0); List _for_yield_acc1; f(3, &_for_yield_acc1); for (ListIter it(&_for_yield_acc1); !it.Done(); it.Next()) { int i = it.Value(); mylib::print_stderr(StrFormat("f() gave %d", i)); } foo = Alloc(); List _for_yield_acc2; foo->bar(4, &_for_yield_acc2); for (ListIter it(&_for_yield_acc2); !it.Done(); it.Next()) { int i = it.Value(); mylib::print_stderr(StrFormat("Foo.bar() gave %d", i)); } mylib::print_stderr(str3); List*> _for_yield_acc3; g(3, &_for_yield_acc3); for (ListIter*> it(&_for_yield_acc3); !it.Done(); it.Next()) { Tuple2* tup4 = it.Value(); i = tup4->at0(); s = tup4->at1(); mylib::print_stderr(StrFormat("g() gave (%d, %r)", i, s)); } List*> _for_yield_acc5; foo->baz(3, &_for_yield_acc5); for (ListIter*> it(&_for_yield_acc5); !it.Done(); it.Next()) { Tuple2* tup6 = it.Value(); i = tup6->at0(); s = tup6->at1(); mylib::print_stderr(StrFormat("Foo.baz() gave (%d, %r)", i, s)); } List _for_yield_acc7; f(3, &_for_yield_acc7); for (ListIter it(&_for_yield_acc7); !it.Done(); it.Next()) { int i = it.Value(); List _for_yield_acc8; f(3, &_for_yield_acc8); for (ListIter it(&_for_yield_acc8); !it.Done(); it.Next()) { int j = it.Value(); mylib::print_stderr(StrFormat("f() gave %d, %d", i, j)); } } mylib::print_stderr(str7); List _iter_buf_it_f; f(5, &_iter_buf_it_f); ListIter it_f(&_iter_buf_it_f); while (true) { try { mylib::print_stderr(StrFormat("next(f()) gave %d", it_f.iterNext())); } catch (StopIteration*) { break; } } List*> _iter_buf_it_g; g(5, &_iter_buf_it_g); ListIter*> it_g(&_iter_buf_it_g); while (true) { try { Tuple2* tup9 = it_g.iterNext(); i = tup9->at0(); s = tup9->at1(); mylib::print_stderr(StrFormat("next(g()) gave (%d, %r)", i, s)); } catch (StopIteration*) { break; } } List _iter_buf_it; f(5, &_iter_buf_it); ListIter it(&_iter_buf_it); l = list(it); i = 0; for (ListIter it(l); !it.Done(); it.Next(), ++i) { int x = it.Value(); mylib::print_stderr(StrFormat("l[%d] = %d", i, x)); } } void run_benchmarks() { ; // pass } } // define namespace test_iterators int main(int argc, char **argv) { gHeap.Init(); char* b = getenv("BENCHMARK"); if (b && strlen(b)) { // match Python's logic fprintf(stderr, "Benchmarking...\n"); test_iterators::run_benchmarks(); } else { test_iterators::run_tests(); } gHeap.CleanProcessExit(); }