// examples/length // BEGIN mycpp output #include "gc_heap.h" using gc_heap::Alloc; using gc_heap::kZeroMask; using gc_heap::StackRoots; #include "my_runtime.h" #include "mylib2.h" using gc_heap::NewStr; using gc_heap::NewList; using gc_heap::NewDict; GLOBAL_STR(str0, "foo"); GLOBAL_STR(str1, "bar"); GLOBAL_STR(str2, "foo"); GLOBAL_STR(str3, "a == b -> %d"); GLOBAL_STR(str4, "a != b -> %d"); GLOBAL_STR(str5, "a == y -> %d"); GLOBAL_STR(str6, "a != y -> %d"); GLOBAL_STR(str7, "a == n -> %d"); GLOBAL_STR(str8, "a != n -> %d"); GLOBAL_STR(str9, "abcd"); GLOBAL_STR(str10, "len(mystr) = %d"); GLOBAL_STR(str11, "mystr[1] = %s"); GLOBAL_STR(str12, "mystr[1:] = %s"); GLOBAL_STR(str13, "mystr[1:3] = %s"); GLOBAL_STR(str14, "mystr[:-2] = %s"); GLOBAL_STR(str15, "b"); GLOBAL_STR(str16, "c = %s"); GLOBAL_STR(str17, "c"); GLOBAL_STR(str18, ""); GLOBAL_STR(str19, "w"); GLOBAL_STR(str20, "x"); GLOBAL_STR(str21, "y"); GLOBAL_STR(str22, "z"); GLOBAL_STR(str23, "len(mylist) = %d"); GLOBAL_STR(str24, "mylist[1] = %s"); GLOBAL_STR(str25, "len(mylist[1:]) = %d"); GLOBAL_STR(str26, "x"); GLOBAL_STR(str27, "c = %s"); GLOBAL_STR(str28, "y"); GLOBAL_STR(str29, "a"); GLOBAL_STR(str30, "%s != a"); GLOBAL_STR(str31, ""); GLOBAL_STR(str32, "abcd"); GLOBAL_STR(str33, "w"); GLOBAL_STR(str34, "x"); GLOBAL_STR(str35, "y"); GLOBAL_STR(str36, "z"); GLOBAL_STR(str37, "result = %d"); GLOBAL_STR(str38, "iterations = %d"); namespace length { // forward declare } // forward declare namespace length namespace length { // declare void TestMaybeStrEquals(); void run_tests(); void run_benchmarks(); inline Str* fmt0(bool a0) { gBuf.reset(); gBuf.write_const("a == b -> ", 12); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt1(bool a0) { gBuf.reset(); gBuf.write_const("a != b -> ", 12); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt2(bool a0) { gBuf.reset(); gBuf.write_const("a == y -> ", 12); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt3(bool a0) { gBuf.reset(); gBuf.write_const("a != y -> ", 12); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt4(bool a0) { gBuf.reset(); gBuf.write_const("a == n -> ", 12); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt5(bool a0) { gBuf.reset(); gBuf.write_const("a != n -> ", 12); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt6(int a0) { gBuf.reset(); gBuf.write_const("len(mystr) = ", 13); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt7(Str* a0) { gBuf.reset(); gBuf.write_const("mystr[1] = ", 11); gBuf.format_s(a0); return gBuf.getvalue(); } inline Str* fmt8(Str* a0) { gBuf.reset(); gBuf.write_const("mystr[1:] = ", 12); gBuf.format_s(a0); return gBuf.getvalue(); } inline Str* fmt9(Str* a0) { gBuf.reset(); gBuf.write_const("mystr[1:3] = ", 13); gBuf.format_s(a0); return gBuf.getvalue(); } inline Str* fmt10(Str* a0) { gBuf.reset(); gBuf.write_const("mystr[:-2] = ", 13); gBuf.format_s(a0); return gBuf.getvalue(); } inline Str* fmt11(Str* a0) { gBuf.reset(); gBuf.write_const("c = ", 4); gBuf.format_s(a0); return gBuf.getvalue(); } inline Str* fmt12(int a0) { gBuf.reset(); gBuf.write_const("len(mylist) = ", 14); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt13(Str* a0) { gBuf.reset(); gBuf.write_const("mylist[1] = ", 12); gBuf.format_s(a0); return gBuf.getvalue(); } inline Str* fmt14(int a0) { gBuf.reset(); gBuf.write_const("len(mylist[1:]) = ", 18); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt15(Str* a0) { gBuf.reset(); gBuf.write_const("c = ", 4); gBuf.format_s(a0); return gBuf.getvalue(); } inline Str* fmt16(Str* a0) { gBuf.reset(); gBuf.format_s(a0); gBuf.write_const(" != a", 5); return gBuf.getvalue(); } inline Str* fmt17(int a0) { gBuf.reset(); gBuf.write_const("result = ", 9); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt18(int a0) { gBuf.reset(); gBuf.write_const("iterations = ", 13); gBuf.format_d(a0); return gBuf.getvalue(); } } // declare namespace length namespace length { // define void TestMaybeStrEquals() { Str* a = nullptr; Str* b = nullptr; Str* y = nullptr; Str* n = nullptr; StackRoots _roots({&a, &b, &y, &n}); a = str0; b = str1; y = str2; n = nullptr; println_stderr(fmt0(str_equals(a, b))); println_stderr(fmt1(!(str_equals(a, b)))); println_stderr(fmt2(maybe_str_equals(a, y))); println_stderr(fmt3(!(maybe_str_equals(a, y)))); println_stderr(fmt4(maybe_str_equals(a, n))); println_stderr(fmt5(!(maybe_str_equals(a, n)))); } void run_tests() { Str* mystr = nullptr; List* mylist = nullptr; Str* c2 = nullptr; StackRoots _roots({&mystr, &mylist, &c2}); mystr = str9; println_stderr(fmt6(len(mystr))); println_stderr(fmt7(mystr->index(1))); println_stderr(fmt8(mystr->slice(1))); println_stderr(fmt9(mystr->slice(1, 3))); println_stderr(fmt10(mystr->slice(0, -2))); for (StrIter it(mystr); !it.Done(); it.Next()) { Str* c = it.Value(); StackRoots _for({&c }); if (str_equals(c, str15)) { continue; } println_stderr(fmt11(c)); if (str_equals(c, str17)) { break; } } println_stderr(str18); mylist = NewList(std::initializer_list{str19, str20, str21, str22}); println_stderr(fmt12(len(mylist))); println_stderr(fmt13(mylist->index(1))); println_stderr(fmt14(len(mylist->slice(1)))); for (ListIter it(mylist); !it.Done(); it.Next()) { Str* c = it.Value(); StackRoots _for({&c }); if (str_equals(c, str26)) { continue; } println_stderr(fmt15(c)); if (str_equals(c, str28)) { break; } } c2 = nullptr; for (StrIter it(mystr); !it.Done(); it.Next()) { Str* c2 = it.Value(); StackRoots _for({&c2 }); if (!(str_equals(c2, str29))) { println_stderr(fmt16(c2)); } } println_stderr(str31); TestMaybeStrEquals(); } void run_benchmarks() { int n; Str* mystr = nullptr; List* mylist = nullptr; int result; int i; StackRoots _roots({&mystr, &mylist}); n = 1000000; mystr = str32; mylist = NewList(std::initializer_list{str33, str34, str35, str36}); result = 0; i = 0; while (i < n) { result += len(mystr->slice(1)); result += len(mylist->slice(1)); i += 1; } println_stderr(fmt17(result)); println_stderr(fmt18(n)); } } // define namespace length int main(int argc, char **argv) { // gc_heap::gHeap.Init(512); gc_heap::gHeap.Init(128 << 10); // 128 KiB; doubling in size // gc_heap::gHeap.Init(400 << 20); // 400 MiB to avoid garbage collection if (getenv("BENCHMARK")) { fprintf(stderr, "Benchmarking...\n"); length::run_benchmarks(); } else { length::run_tests(); } }