File: tpyuniqueptr.cpp

package info (click to toggle)
wsclean 3.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,968 kB
  • sloc: cpp: 85,742; python: 3,526; sh: 245; makefile: 21
file content (60 lines) | stat: -rw-r--r-- 2,024 bytes parent folder | download | duplicates (4)
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
#include <aocommon/py/uniqueptr.h>

#include <complex>
#include <type_traits>

#include <boost/test/unit_test.hpp>

using aocommon::py::PyUniquePointer;

BOOST_AUTO_TEST_SUITE(pyuniquepointer)

BOOST_AUTO_TEST_CASE(construct_from_unique_pointer) {
  static_assert(!std::is_copy_assignable_v<PyUniquePointer<int>>);
  static_assert(!std::is_copy_constructible_v<PyUniquePointer<int>>);
  static_assert(std::is_move_assignable_v<PyUniquePointer<int>>);
  static_assert(std::is_move_constructible_v<PyUniquePointer<int>>);

  auto i = std::make_unique<int>(42);
  auto j = std::make_unique<int>(43);
  int* i_raw = i.get();
  int* j_raw = j.get();
  PyUniquePointer<int> pypointer(std::move(i));
  const PyUniquePointer<int> pypointer_const(std::move(j));

  // Test operator*
  BOOST_TEST(&(*pypointer) == i_raw);
  BOOST_TEST(&(*pypointer_const) == j_raw);
  BOOST_TEST(*pypointer == 42);
  BOOST_TEST(*pypointer_const == 43);

  // Test operator->
  BOOST_TEST(pypointer.operator->() == i_raw);
  BOOST_TEST(pypointer_const.operator->() == j_raw);
}

BOOST_AUTO_TEST_CASE(construct_from_value) {
  const PyUniquePointer<std::complex<float>> pypointer_default;
  BOOST_TEST(*pypointer_default == std::complex<float>(0.0f, 0.0f));

  const PyUniquePointer<std::complex<float>> pypointer_one_value{42.0f};
  BOOST_TEST(*pypointer_one_value == std::complex<float>(42.0f, 0.0f));

  const PyUniquePointer<std::complex<float>> pypointer_two_values{42.0f,
                                                                  -43.0f};
  BOOST_TEST(*pypointer_two_values == std::complex<float>(42.0f, -43.0f));
}

BOOST_AUTO_TEST_CASE(take) {
  auto i = std::make_unique<int>(42);
  int* i_raw = i.get();
  PyUniquePointer<int> pypointer(std::move(i));

  i = pypointer.take();
  BOOST_TEST(i.get() == i_raw);
  BOOST_CHECK_THROW((void)pypointer.take(), std::runtime_error);
  BOOST_CHECK_THROW((void)*pypointer, std::runtime_error);
  BOOST_CHECK_THROW((void)pypointer.operator->(), std::runtime_error);
}

BOOST_AUTO_TEST_SUITE_END()