source: mainline/uspace/lib/cpp/src/internal/test/memory.cpp@ 122c3b8

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

cpp: added tests for unique_ptr<T[]>

  • Property mode set to 100644
File size: 3.6 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
44 return end();
45 }
46
47 const char* memory_test::name()
48 {
49 return "memory";
50 }
51
52 void memory_test::test_unique_ptr()
53 {
54 mock::clear();
55 {
56 auto ptr = std::make_unique<mock>();
57 test_eq("unique_ptr get() when non-null", (ptr.get() != nullptr), true);
58 test_eq("unique_ptr operator bool when non-null", (bool)ptr, true);
59 }
60 test_eq("unique_ptr make_unique", mock::constructor_calls, 1U);
61 test_eq("unique_ptr out of scope", mock::destructor_calls, 1U);
62
63 mock::clear();
64 {
65 auto ptr = std::make_unique<mock>();
66 delete ptr.release();
67 }
68 test_eq("unique_ptr release", mock::destructor_calls, 1U);
69
70 mock::clear();
71 {
72 auto ptr = std::make_unique<mock>();
73 ptr.reset(new mock{});
74 }
75 test_eq("unique_ptr reset", mock::destructor_calls, 2U);
76
77 mock::clear();
78 {
79 std::unique_ptr<mock> ptr1{};
80 test_eq("unique_ptr get() when null", ptr1.get(), nullptr);
81 test_eq("unique_ptr operator bool when null", (bool)ptr1, false);
82 {
83 auto ptr2 = std::make_unique<mock>();
84 ptr1 = std::move(ptr2);
85 }
86 test_eq("unique_ptr move pt1", mock::destructor_calls, 0U);
87 }
88 test_eq("unique_ptr move pt2", mock::destructor_calls, 1U);
89
90 mock::clear();
91 {
92 auto ptr = std::make_unique<mock[]>(10U);
93 test_eq("unique_ptr make_unique array version", mock::constructor_calls, 10U);
94
95 new(&ptr[5]) mock{};
96 test_eq("placement new into the array", mock::constructor_calls, 11U);
97 test_eq("original not destroyed during placement new", mock::destructor_calls, 0U);
98 }
99 test_eq("unique_ptr array out of scope", mock::destructor_calls, 10U);
100 }
101}
Note: See TracBrowser for help on using the repository browser.