summaryrefslogtreecommitdiff
path: root/tests/test_weak_raw_ptr.cc
blob: c6ffc38d705f10ead2afda01fdcfd584cbbf21bb (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
/* Copyright 2016, The libsigc++ Development Team
 *  Assigned to public domain.  Use as you wish without restriction.
 */

#include "testutilities.h"
#include <sigc++/weak_raw_ptr.h>
#include <cassert>

namespace
{

TestUtilities* util = nullptr;
std::ostringstream result_stream;

class A : public sigc::trackable
{
public:
  void something() { result_stream << "method called"; }
};

} // end anonymous namespace

void
test_weak_ptr_becomes_null()
{
  const auto a = new A();
  sigc::internal::weak_raw_ptr<A> raw_ptr(a);
  assert(raw_ptr);

  // Call something on A, via the weak_raw_ptr<>,
  // just to make sure that it doesn't get optimised away:
  raw_ptr->something();
  util->check_result(result_stream, "method called");

  delete a;

  // Deleting the A should have made the weak_raw_ptr<A> become invalid.
  assert(!raw_ptr);
}

void
test_weak_ptr_disconnects_self()
{
  const auto a = new A();
  {
    sigc::internal::weak_raw_ptr<A> raw_ptr(a);

    // Call something on A, via the weak_raw_ptr<>,
    // just to make sure that it doesn't get optimised away:
    raw_ptr->something();
    util->check_result(result_stream, "method called");
  }

  // If the weak_raw_ptr has not asked A to stop notifying it,
  // then we would expect some undefined behaviour here,
  // when a tries to notify the now-destroyed weak_raw_ptr.
  delete a;
}

int
main(int argc, char* argv[])
{
  util = TestUtilities::get_instance();

  if (!util->check_command_args(argc, argv))
    return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;

  test_weak_ptr_becomes_null();
  test_weak_ptr_disconnects_self();

  return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
}