File indexing completed on 2025-02-16 05:12:10
0001 /* 0002 * test/rectrace.cpp 0003 * Copyright 2013 Google Inc. All Rights Reserved. 0004 * 0005 * Permission is hereby granted, free of charge, to any person obtaining a copy 0006 * of this software and associated documentation files (the "Software"), to deal 0007 * in the Software without restriction, including without limitation the rights 0008 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 0009 * copies of the Software, and to permit persons to whom the Software is 0010 * furnished to do so, subject to the following conditions: 0011 * 0012 * The above copyright notice and this permission notice shall be included in 0013 * all copies or substantial portions of the Software. 0014 * 0015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 0016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 0017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 0018 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 0019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 0020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 0021 * SOFTWARE. 0022 */ 0023 0024 #include "backward.hpp" 0025 #include "test/test.hpp" 0026 #include <stdio.h> 0027 0028 using namespace backward; 0029 0030 typedef StackTrace stacktrace_t; 0031 0032 void end_of_our_journey(stacktrace_t &st) { 0033 if (!st.size()) { 0034 st.load_here(); 0035 } 0036 } 0037 0038 int rec(stacktrace_t &st, int level) { 0039 if (level <= 1) { 0040 end_of_our_journey(st); 0041 return 0; 0042 } 0043 return rec(st, level - 1); 0044 } 0045 0046 namespace toto { 0047 0048 namespace titi { 0049 0050 struct foo { 0051 0052 union bar { 0053 NOINLINE static int trampoline(stacktrace_t &st, int level) { 0054 return rec(st, level); 0055 } 0056 }; 0057 }; 0058 0059 } // namespace titi 0060 0061 } // namespace toto 0062 0063 TEST(recursion) { 0064 { // lexical scope. 0065 stacktrace_t st; 0066 const int input = 3; 0067 int r = toto::titi::foo::bar::trampoline(st, input); 0068 0069 std::cout << "rec(" << input << ") == " << r << std::endl; 0070 0071 Printer printer; 0072 // printer.address = true; 0073 printer.object = true; 0074 printer.print(st, stdout); 0075 } 0076 } 0077 0078 int fib(StackTrace &st, int level) { 0079 if (level == 2) { 0080 return 1; 0081 } 0082 if (level <= 1) { 0083 end_of_our_journey(st); 0084 return 0; 0085 } 0086 return fib(st, level - 1) + fib(st, level - 2); 0087 } 0088 0089 TEST(fibrecursive) { 0090 StackTrace st; 0091 const int input = 6; 0092 int r = fib(st, input); 0093 0094 std::cout << "fib(" << input << ") == " << r << std::endl; 0095 0096 Printer printer; 0097 printer.print(st, stdout); 0098 }