source: mainline/uspace/lib/cpp/src/internal/test/memory.cpp@ 17012fcf

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 17012fcf was 17012fcf, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: added shared_ptr tests

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 * Copyright (c) 2018 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <initializer_list>
30#include <internal/test/mock.hpp>
31#include <internal/test/tests.hpp>
32#include <memory>
33#include <utility>
34
35namespace std::test
36{
37 bool memory_test::run(bool report)
38 {
39 report_ = report;
40 start();
41
42 test_unique_ptr();
43 test_shared_ptr();
44
45 return end();
46 }
47
48 const char* memory_test::name()
49 {
50 return "memory";
51 }
52
53 void memory_test::test_unique_ptr()
54 {
55 mock::clear();
56 {
57 auto ptr = std::make_unique<mock>();
58 test_eq("unique_ptr get() when non-null", (ptr.get() != nullptr), true);
59 test_eq("unique_ptr operator bool when non-null", (bool)ptr, true);
60 }
61 test_eq("unique_ptr make_unique", mock::constructor_calls, 1U);
62 test_eq("unique_ptr out of scope", mock::destructor_calls, 1U);
63
64 mock::clear();
65 {
66 auto ptr = std::make_unique<mock>();
67 delete ptr.release();
68 }
69 test_eq("unique_ptr release", mock::destructor_calls, 1U);
70
71 mock::clear();
72 {
73 auto ptr = std::make_unique<mock>();
74 ptr.reset(new mock{});
75 }
76 test_eq("unique_ptr reset", mock::destructor_calls, 2U);
77
78 mock::clear();
79 {
80 std::unique_ptr<mock> ptr1{};
81 test_eq("unique_ptr get() when null", ptr1.get(), nullptr);
82 test_eq("unique_ptr operator bool when null", (bool)ptr1, false);
83 {
84 auto ptr2 = std::make_unique<mock>();
85 ptr1 = std::move(ptr2);
86 }
87 test_eq("unique_ptr move pt1", mock::destructor_calls, 0U);
88 }
89 test_eq("unique_ptr move pt2", mock::destructor_calls, 1U);
90
91 mock::clear();
92 {
93 auto ptr = std::make_unique<mock[]>(10U);
94 test_eq("unique_ptr make_unique array version", mock::constructor_calls, 10U);
95
96 new(&ptr[5]) mock{};
97 test_eq("placement new into the array", mock::constructor_calls, 11U);
98 test_eq("original not destroyed during placement new", mock::destructor_calls, 0U);
99 }
100 test_eq("unique_ptr array out of scope", mock::destructor_calls, 10U);
101 }
102
103 void memory_test::test_shared_ptr()
104 {
105 mock::clear();
106 {
107 auto ptr1 = std::make_shared<mock>();
108 test_eq("shared_ptr make_shared", mock::constructor_calls, 1U);
109 test_eq("shared_ptr unique", ptr1.unique(), true);
110 {
111 auto ptr2 = ptr1;
112 test_eq("shared_ptr copy pt1", ptr1.use_count(), 2U);
113 test_eq("shared_ptr copy pt2", ptr2.use_count(), 2U);
114 test_eq("shared_ptr copy no constructor call", mock::copy_constructor_calls, 0U);
115 test_eq("shared_ptr not unique", ptr1.unique(), false);
116
117 auto ptr3 = std::move(ptr2);
118 test_eq("shared_ptr move pt1", ptr1.use_count(), 2U);
119 test_eq("shared_ptr move pt2", ptr3.use_count(), 2U);
120
121 test_eq("shared_ptr move origin empty", (bool)ptr2, false);
122 }
123 test_eq("shared_ptr copy out of scope", mock::destructor_calls, 0U);
124 }
125 test_eq("shared_ptr original out of scope", mock::destructor_calls, 1U);
126 }
127}
Note: See TracBrowser for help on using the repository browser.