Home | History | Annotate | Download | only in test
      1 #include <assert.h>
      2 #include <stdlib.h>
      3 #include <unwind.h>
      4 
      5 #define EXPECTED_NUM_FRAMES 50
      6 #define NUM_FRAMES_UPPER_BOUND 100
      7 
      8 _Unwind_Reason_Code callback(_Unwind_Context *context, void *cnt) {
      9   (void)context;
     10   int *i = (int *)cnt;
     11   ++*i;
     12   if (*i > NUM_FRAMES_UPPER_BOUND) {
     13     abort();
     14   }
     15   return _URC_NO_REASON;
     16 }
     17 
     18 void test_backtrace() {
     19   int n = 0;
     20   _Unwind_Backtrace(&callback, &n);
     21   if (n < EXPECTED_NUM_FRAMES) {
     22     abort();
     23   }
     24 }
     25 
     26 int test(int i) {
     27   if (i == 0) {
     28     test_backtrace();
     29     return 0;
     30   } else {
     31     return i + test(i - 1);
     32   }
     33 }
     34 
     35 int main() {
     36   int total = test(50);
     37   assert(total == 1275);
     38 }
     39