1*a1e26a70SApple OSS Distributions //
2*a1e26a70SApple OSS Distributions // Tests for
3*a1e26a70SApple OSS Distributions // void swap(safe_allocation& a, safe_allocation& b);
4*a1e26a70SApple OSS Distributions //
5*a1e26a70SApple OSS Distributions
6*a1e26a70SApple OSS Distributions #include <libkern/c++/safe_allocation.h>
7*a1e26a70SApple OSS Distributions #include <darwintest.h>
8*a1e26a70SApple OSS Distributions #include "test_utils.h"
9*a1e26a70SApple OSS Distributions
10*a1e26a70SApple OSS Distributions struct T {
11*a1e26a70SApple OSS Distributions int i;
12*a1e26a70SApple OSS Distributions };
13*a1e26a70SApple OSS Distributions
14*a1e26a70SApple OSS Distributions template <typename T>
15*a1e26a70SApple OSS Distributions static void
tests()16*a1e26a70SApple OSS Distributions tests()
17*a1e26a70SApple OSS Distributions {
18*a1e26a70SApple OSS Distributions // Swap non-null with non-null
19*a1e26a70SApple OSS Distributions {
20*a1e26a70SApple OSS Distributions tracked_safe_allocation<T> a(10, libkern::allocate_memory);
21*a1e26a70SApple OSS Distributions tracked_safe_allocation<T> b(20, libkern::allocate_memory);
22*a1e26a70SApple OSS Distributions T* a_mem = a.data();
23*a1e26a70SApple OSS Distributions T* b_mem = b.data();
24*a1e26a70SApple OSS Distributions tracking_allocator::reset();
25*a1e26a70SApple OSS Distributions
26*a1e26a70SApple OSS Distributions swap(a, b); // ADL call
27*a1e26a70SApple OSS Distributions
28*a1e26a70SApple OSS Distributions CHECK(!tracking_allocator::did_allocate);
29*a1e26a70SApple OSS Distributions CHECK(!tracking_allocator::did_deallocate);
30*a1e26a70SApple OSS Distributions CHECK(a.data() == b_mem);
31*a1e26a70SApple OSS Distributions CHECK(b.data() == a_mem);
32*a1e26a70SApple OSS Distributions CHECK(a.size() == 20);
33*a1e26a70SApple OSS Distributions CHECK(b.size() == 10);
34*a1e26a70SApple OSS Distributions }
35*a1e26a70SApple OSS Distributions
36*a1e26a70SApple OSS Distributions // Swap non-null with null
37*a1e26a70SApple OSS Distributions {
38*a1e26a70SApple OSS Distributions tracked_safe_allocation<T> a(10, libkern::allocate_memory);
39*a1e26a70SApple OSS Distributions tracked_safe_allocation<T> b = nullptr;
40*a1e26a70SApple OSS Distributions T* a_mem = a.data();
41*a1e26a70SApple OSS Distributions tracking_allocator::reset();
42*a1e26a70SApple OSS Distributions
43*a1e26a70SApple OSS Distributions swap(a, b); // ADL call
44*a1e26a70SApple OSS Distributions
45*a1e26a70SApple OSS Distributions CHECK(!tracking_allocator::did_allocate);
46*a1e26a70SApple OSS Distributions CHECK(!tracking_allocator::did_deallocate);
47*a1e26a70SApple OSS Distributions CHECK(a.data() == nullptr);
48*a1e26a70SApple OSS Distributions CHECK(b.data() == a_mem);
49*a1e26a70SApple OSS Distributions CHECK(a.size() == 0);
50*a1e26a70SApple OSS Distributions CHECK(b.size() == 10);
51*a1e26a70SApple OSS Distributions }
52*a1e26a70SApple OSS Distributions
53*a1e26a70SApple OSS Distributions // Swap null with non-null
54*a1e26a70SApple OSS Distributions {
55*a1e26a70SApple OSS Distributions tracked_safe_allocation<T> a = nullptr;
56*a1e26a70SApple OSS Distributions tracked_safe_allocation<T> b(20, libkern::allocate_memory);
57*a1e26a70SApple OSS Distributions T* b_mem = b.data();
58*a1e26a70SApple OSS Distributions tracking_allocator::reset();
59*a1e26a70SApple OSS Distributions
60*a1e26a70SApple OSS Distributions swap(a, b); // ADL call
61*a1e26a70SApple OSS Distributions
62*a1e26a70SApple OSS Distributions CHECK(!tracking_allocator::did_allocate);
63*a1e26a70SApple OSS Distributions CHECK(!tracking_allocator::did_deallocate);
64*a1e26a70SApple OSS Distributions CHECK(a.data() == b_mem);
65*a1e26a70SApple OSS Distributions CHECK(b.data() == nullptr);
66*a1e26a70SApple OSS Distributions CHECK(a.size() == 20);
67*a1e26a70SApple OSS Distributions CHECK(b.size() == 0);
68*a1e26a70SApple OSS Distributions }
69*a1e26a70SApple OSS Distributions
70*a1e26a70SApple OSS Distributions // Swap null with null
71*a1e26a70SApple OSS Distributions {
72*a1e26a70SApple OSS Distributions tracked_safe_allocation<T> a = nullptr;
73*a1e26a70SApple OSS Distributions tracked_safe_allocation<T> b = nullptr;
74*a1e26a70SApple OSS Distributions tracking_allocator::reset();
75*a1e26a70SApple OSS Distributions
76*a1e26a70SApple OSS Distributions swap(a, b); // ADL call
77*a1e26a70SApple OSS Distributions
78*a1e26a70SApple OSS Distributions CHECK(!tracking_allocator::did_allocate);
79*a1e26a70SApple OSS Distributions CHECK(!tracking_allocator::did_deallocate);
80*a1e26a70SApple OSS Distributions CHECK(a.data() == nullptr);
81*a1e26a70SApple OSS Distributions CHECK(b.data() == nullptr);
82*a1e26a70SApple OSS Distributions CHECK(a.size() == 0);
83*a1e26a70SApple OSS Distributions CHECK(b.size() == 0);
84*a1e26a70SApple OSS Distributions }
85*a1e26a70SApple OSS Distributions
86*a1e26a70SApple OSS Distributions // Swap with self
87*a1e26a70SApple OSS Distributions {
88*a1e26a70SApple OSS Distributions tracked_safe_allocation<T> a(10, libkern::allocate_memory);
89*a1e26a70SApple OSS Distributions T* a_mem = a.data();
90*a1e26a70SApple OSS Distributions tracking_allocator::reset();
91*a1e26a70SApple OSS Distributions
92*a1e26a70SApple OSS Distributions swap(a, a); // ADL call
93*a1e26a70SApple OSS Distributions
94*a1e26a70SApple OSS Distributions CHECK(!tracking_allocator::did_allocate);
95*a1e26a70SApple OSS Distributions CHECK(!tracking_allocator::did_deallocate);
96*a1e26a70SApple OSS Distributions CHECK(a.data() == a_mem);
97*a1e26a70SApple OSS Distributions CHECK(a.size() == 10);
98*a1e26a70SApple OSS Distributions }
99*a1e26a70SApple OSS Distributions }
100*a1e26a70SApple OSS Distributions
101*a1e26a70SApple OSS Distributions T_DECL(swap, "safe_allocation.swap", T_META_TAG_VM_PREFERRED) {
102*a1e26a70SApple OSS Distributions tests<T>();
103*a1e26a70SApple OSS Distributions tests<T const>();
104*a1e26a70SApple OSS Distributions }
105