summaryrefslogtreecommitdiff
path: root/tests/Stack_Trace_Test.cpp
blob: 9823d4321785a09e305759e673e2de2d1a1130c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// $Id$

// ============================================================================
//
// = LIBRARY
//    tests
//
// = DESCRIPTION
//    This program exercises the ACE_Stack_Trace class.
//
// = AUTHOR
//    Chris Cleeland <cleeland @ ociweb . com>
//
// ============================================================================

#include "ace/Stack_Trace.h"
#include "ace/OS_NS_string.h"
#include "test_config.h"

ACE_RCSID(tests, Stack_Trace_Test, "$Id$")

/*
 * Ultra-basic test of stack trace.
 *
 * Other things to test:
 * - throwing exceptions that contain the stack trace, and catching
 * - capturing multiple traces
 * - capture traces with different numbers of frames
 * - capture traces with different numbers of skipped frames
 */


struct SomeException
{
  static const ssize_t SKIP = 1; //@TODO: #ifdef this for MACOSX which needs 2
  SomeException () : where_(SKIP), explanation_ ("") { }
  SomeException (const char *explanation)
    : where_(SKIP), explanation_(explanation) { }
  ACE_Stack_Trace where_;
  const char *explanation_;
};

void
func ()
{
  throw SomeException();
}

int
run_main (int, ACE_TCHAR *[])
{
  ACE_START_TEST (ACE_TEXT ("Stack_Trace_Test"));

  ACE_Stack_Trace t1;
  ACE_DEBUG ((LM_DEBUG, "t1 trace is %C\n", t1.c_str()));

  ACE_Stack_Trace t2(t1);
  ACE_DEBUG ((LM_DEBUG, "t2 (copied) trace is %C\n",
        (ACE_OS::strcmp (t1.c_str(), t2.c_str()) == 0) ? "same" : "different"));

  ACE_Stack_Trace t3;
  ACE_DEBUG ((LM_DEBUG, "t3 trace before assignment is %C\n",
        (ACE_OS::strcmp (t1.c_str(), t3.c_str()) == 0) ? "same" : "different"));
  t3 = t1;
  ACE_DEBUG ((LM_DEBUG, "t3 trace after assignment is %C\n",
        (ACE_OS::strcmp (t1.c_str(), t3.c_str()) == 0) ? "same" : "different"));

  try {
    func();
  }
  catch (SomeException& e) {
    ACE_DEBUG ((LM_DEBUG,
    "SomeException caught at\n%?\n; thrown at\n%C",
    e.where_.c_str()));
  }

  ACE_Stack_Trace one_frame_only(0, 1);
  ACE_DEBUG ((LM_DEBUG,
              "stack trace of only one frame:\n%C",
              one_frame_only.c_str()));

  ACE_DEBUG ((LM_DEBUG, "getting ready to end the test at %?\n"));

  ACE_END_TEST;
  return 0;
}