// examples/test_strings translated by mycpp // BEGIN mycpp output #include "mycpp/runtime.h" GLOBAL_STR(str0, ""); GLOBAL_STR(str1, "=== %s ==="); GLOBAL_STR(str2, ""); GLOBAL_STR(str3, "mystr"); GLOBAL_STR(str4, "a1bc"); GLOBAL_STR(str5, ""); GLOBAL_STR(str6, "empty yes"); GLOBAL_STR(str7, "a1"); GLOBAL_STR(str8, "a1 yes"); GLOBAL_STR(str9, "zz"); GLOBAL_STR(str10, "zz no"); GLOBAL_STR(str11, ""); GLOBAL_STR(str12, "empty yes"); GLOBAL_STR(str13, "bc"); GLOBAL_STR(str14, "bc yes"); GLOBAL_STR(str15, "c"); GLOBAL_STR(str16, "bc yes"); GLOBAL_STR(str17, "zzzzzz"); GLOBAL_STR(str18, "zzzzzz no"); GLOBAL_STR(str19, "aaa-bb-cc"); GLOBAL_STR(str20, "aa"); GLOBAL_STR(str21, "b"); GLOBAL_STR(str22, "z"); GLOBAL_STR(str23, "aaaa"); GLOBAL_STR(str24, ""); GLOBAL_STR(str25, "%s find %s start:%d => %d"); GLOBAL_STR(str26, "---"); GLOBAL_STR(str27, "%s find %s end:%d => %d"); GLOBAL_STR(str28, "---"); GLOBAL_STR(str29, ""); GLOBAL_STR(str30, "%s find empty [%d, %d) => %d"); GLOBAL_STR(str31, "TestFormat"); GLOBAL_STR(str32, "foo"); GLOBAL_STR(str33, "bar"); GLOBAL_STR(str34, "foo"); GLOBAL_STR(str35, "foo"); GLOBAL_STR(str36, "mystr"); GLOBAL_STR(str37, "[%s]"); GLOBAL_STR(str38, "mystr"); GLOBAL_STR(str39, "[%s, %s]"); GLOBAL_STR(str40, "abc"); GLOBAL_STR(str41, "%s: 5%%-100%%"); GLOBAL_STR(str42, "abc"); GLOBAL_STR(str43, "%s"); GLOBAL_STR(str44, "anchor"); GLOBAL_STR(str45, "foo? %d"); GLOBAL_STR(str46, "f"); GLOBAL_STR(str47, "str? %d"); GLOBAL_STR(str48, "s"); GLOBAL_STR(str49, "int 5d %5d"); GLOBAL_STR(str50, "'single'"); GLOBAL_STR(str51, "\"double\""); GLOBAL_STR(str52, "a\tb\nc\td\n"); GLOBAL_STR(str53, "x"); GLOBAL_STR(str54, "%s\tb\n%s\td\n"); GLOBAL_STR(str55, "%dfoo"); GLOBAL_STR(str56, "foo%d"); GLOBAL_STR(str57, "foo "); GLOBAL_STR(str58, "%s"); GLOBAL_STR(str59, "bar"); GLOBAL_STR(str60, "spam\u0000%s"); GLOBAL_STR(str61, "eggs"); GLOBAL_STR(str62, "len(s) = %d"); GLOBAL_STR(str63, "foo%s"); GLOBAL_STR(str64, "\u0000bar"); GLOBAL_STR(str65, "len(s) = %d"); GLOBAL_STR(str66, "%o"); GLOBAL_STR(str67, "%17o"); GLOBAL_STR(str68, "%017o"); GLOBAL_STR(str69, "%%%d%%%%"); GLOBAL_STR(str70, "%r"); GLOBAL_STR(str71, "tab\tline\nline\r\n"); GLOBAL_STR(str72, "a1b2c3d4e5"); GLOBAL_STR(str73, "TestByteOperations"); GLOBAL_STR(str74, "foo"); GLOBAL_STR(str75, "total = %d"); GLOBAL_STR(str76, "total2 = %d"); GLOBAL_STR(str77, "TestBytes2"); GLOBAL_STR(str78, "len(b) = %d"); GLOBAL_STR(str79, "len(ch) = %d"); GLOBAL_STR(str80, ""); GLOBAL_STR(str81, "EQUAL =="); GLOBAL_STR(str82, "should be equal"); GLOBAL_STR(str83, "len(all_bytes) = %d"); GLOBAL_STR(str84, ""); GLOBAL_STR(str85, "["); GLOBAL_STR(str86, "LEFT"); GLOBAL_STR(str87, "]"); GLOBAL_STR(str88, "RIGHT"); GLOBAL_STR(str89, "\\"); GLOBAL_STR(str90, "BACKSLASH"); GLOBAL_STR(str91, "0xff"); GLOBAL_STR(str92, "abcXYZ"); GLOBAL_STR(str93, "abcXYZ"); GLOBAL_STR(str94, ""); namespace test_strings { // forward declare class Foo; } // forward declare namespace test_strings namespace test_strings { // declare void banner(BigStr* s); class Foo { public: Foo(); BigStr* s; static constexpr ObjHeader obj_header() { return ObjHeader::ClassScanned(1, sizeof(Foo)); } DISALLOW_COPY_AND_ASSIGN(Foo) }; void TestMethods(); void TestFormat(); void TestByteOperations(); void TestBytes2(); void run_tests(); void run_benchmarks(); } // declare namespace test_strings namespace test_strings { // define void banner(BigStr* s) { StackRoot _root0(&s); print(str0); print(StrFormat("=== %s ===", s)); print(str2); } Foo::Foo() { this->s = str3; } void TestMethods() { BigStr* s = nullptr; List* substrs = nullptr; int pos; StackRoot _root0(&s); StackRoot _root1(&substrs); s = str4; if (s->startswith(str5)) { print(str6); } if (s->startswith(str7)) { print(str8); } if (!s->startswith(str9)) { print(str10); } if (s->endswith(str11)) { print(str12); } if (s->endswith(str13)) { print(str14); } if (s->endswith(str15)) { print(str16); } if (!s->endswith(str17)) { print(str18); } s = str19; substrs = NewList(std::initializer_list{str20, str21, str22, str23, str24}); for (ListIter it(substrs); !it.Done(); it.Next()) { BigStr* substr = it.Value(); StackRoot _for(&substr ); for (int start = 0; start < len(s); ++start) { pos = s->find(substr, start); print(StrFormat("%s find %s start:%d => %d", s, substr, start, pos)); } } print(str26); for (ListIter it(substrs); !it.Done(); it.Next()) { BigStr* substr = it.Value(); StackRoot _for(&substr ); for (int end = 0; end < len(s); ++end) { pos = s->find(substr, 0, end); print(StrFormat("%s find %s end:%d => %d", s, substr, end, pos)); } } print(str28); for (int start = 0; start < 3; ++start) { for (int end = 0; end < 3; ++end) { pos = s->find(str29, start, end); print(StrFormat("%s find empty [%d, %d) => %d", s, start, end, pos)); } } } void TestFormat() { test_strings::Foo* obj = nullptr; BigStr* s = nullptr; BigStr* x = nullptr; BigStr* fmt = nullptr; List* fmts = nullptr; StackRoot _root0(&obj); StackRoot _root1(&s); StackRoot _root2(&x); StackRoot _root3(&fmt); StackRoot _root4(&fmts); banner(str31); print(str_concat(str32, str33)); print(str_repeat(str34, 3)); obj = Alloc(); print(str_concat(str35, obj->s)); s = str36; print(StrFormat("[%s]", s)); s = str38; print(StrFormat("[%s, %s]", s, str40)); print(StrFormat("%s: 5%%-100%%", str42)); print(StrFormat("%s", str44)); print(StrFormat("foo? %d", str_contains(s, str46))); print(StrFormat("str? %d", str_contains(s, str48))); print(StrFormat("int 5d %5d", 35)); print(str50); print(str51); print(str52); x = str53; print(StrFormat("%s\tb\n%s\td\n", x, x)); fmt = str55; print(StrFormat(fmt, 10)); fmts = NewList(std::initializer_list{str56}); print(StrFormat(fmts->at(0), 10)); print(StrFormat(str_concat(str57, str58), str59)); s = StrFormat("spam\u0000%s", str61); s = StrFormat("foo%s", str64); print(StrFormat("len(s) = %d", len(s))); print(StrFormat("%o", 12345)); print(StrFormat("%17o", 12345)); print(StrFormat("%017o", 12345)); print(StrFormat("%%%d%%%%", 12345)); print(StrFormat("%r", str71)); s = str72; print(s->upper()); } void TestByteOperations() { BigStr* s = nullptr; int i; int n; int total; int total2; int byte; int byte2; StackRoot _root0(&s); banner(str73); s = str_repeat(str74, 10); i = 0; n = len(s); total = 0; total2 = 0; while (i < n) { byte = ord(s->at(i)); byte2 = mylib::ByteAt(s, i); total += byte; total2 += byte2; i += 1; } if (total != total2) { assert(0); // AssertionError } print(StrFormat("total = %d", total)); print(StrFormat("total2 = %d", total2)); } void TestBytes2() { List* b = nullptr; List* ch = nullptr; int j; BigStr* all_bytes = nullptr; BigStr* b2 = nullptr; int n; int i; int byte; StackRoot _root0(&b); StackRoot _root1(&ch); StackRoot _root2(&all_bytes); StackRoot _root3(&b2); banner(str77); b = Alloc>(); ch = Alloc>(); for (int i = 0; i < 256; ++i) { j = (255 - i); if (j == 2) { j = 0; } b->append(j); ch->append(chr(j)); } print(StrFormat("len(b) = %d", len(b))); print(StrFormat("len(ch) = %d", len(ch))); all_bytes = str80->join(ch); b2 = mylib::JoinBytes(b); if (str_equals(all_bytes, b2)) { print(str81); } else { assert(0); // AssertionError } n = len(all_bytes); print(StrFormat("len(all_bytes) = %d", n)); print(str84); i = 0; while (i < n) { byte = mylib::ByteAt(all_bytes, i); if (mylib::ByteEquals(byte, str85)) { print(str86); } if (mylib::ByteEquals(byte, str87)) { print(str88); } if (mylib::ByteEquals(byte, str89)) { print(str90); } if (mylib::ByteEquals(byte, chr(255))) { print(str91); } if (mylib::ByteInSet(byte, str92)) { print(str93); } i += 1; } print(str94); } void run_tests() { TestFormat(); TestMethods(); TestByteOperations(); TestBytes2(); } void run_benchmarks() { ; // pass } } // define namespace test_strings 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_strings::run_benchmarks(); } else { test_strings::run_tests(); } gHeap.CleanProcessExit(); }