Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
sort_allocation_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
41#include <gtest/gtest.h>
42
43#include "allocation_failure.H"
44
45#include <algorithm>
46#include <functional>
47#include <random>
48#include <utility>
49#include <vector>
50
51#include <ahSort.H>
52#include <tpl_dynDlist.H>
53#include <tpl_dynList.H>
54#include <tpl_sort_utils.H>
55
56using namespace Aleph;
57
58namespace
59{
60 // The tests need the operator new of allocation_failure.H
61 struct ListSortWithoutMemory : public ::testing::Test
62 {
63 void SetUp() override
64 {
66 GTEST_SKIP() << "ThreadSanitizer replaces the global operator new";
67 }
68 };
69
70 // Copies the items of l to v, which has room for them, so that nothing
71 // is allocated
72 template <class List>
73 void copy_items(const List & l, std::vector<int> & v)
74 {
75 size_t i = 0;
76 for (auto it = l.get_it(); it.has_curr(); it.next_ne())
77 v[i++] = it.get_curr();
78 }
79} // namespace
80
81// mergesort() of a DynList took each item out of its node, and put it in a
82// node that it allocated, at each merge: without memory, the list lost
83// items. The references to the items now stay valid, too.
84TEST_F(ListSortWithoutMemory, TheSortsInPlaceAllocateNothing)
85{
86 std::mt19937 gen(23);
87 std::vector<int> items;
88 for (int i = 0; i < 1024; ++i)
89 items.push_back(int(gen() % 1000));
90 std::vector<int> sorted = items;
91 std::sort(sorted.begin(), sorted.end());
92 std::vector<int> descending = sorted;
93 std::reverse(descending.begin(), descending.end());
94
95 DynList<int> list;
96 DynDlist<int> dlist;
97 for (const int x : items)
98 {
99 list.append(x);
100 dlist.append(x);
101 }
102 const int * first = &list.get_first();
103
104 // the items after each sort, of list and of dlist
105 std::vector<std::vector<int>> results(14, std::vector<int>(items.size()));
106 size_t refused = 0;
107 {
108 No_Memory no_memory;
109 mergesort(list);
110 mergesort(dlist);
111 copy_items(list, results[0]);
112 copy_items(dlist, results[1]);
113 mergesort(list, std::greater<int>());
114 mergesort(dlist, std::greater<int>());
115 copy_items(list, results[2]);
116 copy_items(dlist, results[3]);
117 mergesort<DynList, int>(list);
118 mergesort<DynDlist, int>(dlist);
119 copy_items(list, results[4]);
120 copy_items(dlist, results[5]);
121 mergeinsertsort(list, std::greater<int>());
122 mergeinsertsort(dlist, std::greater<int>());
123 copy_items(list, results[6]);
124 copy_items(dlist, results[7]);
125 (void) in_place_sort(list);
126 (void) in_place_sort(dlist);
127 copy_items(list, results[8]);
128 copy_items(dlist, results[9]);
129 list = sort(std::move(list), std::greater<int>());
130 dlist = sort(std::move(dlist), std::greater<int>());
131 copy_items(list, results[10]);
132 copy_items(dlist, results[11]);
133 insertion_sort(list);
134 insertion_sort(dlist);
135 copy_items(list, results[12]);
136 copy_items(dlist, results[13]);
137 refused = refused_allocations;
138 }
139 EXPECT_EQ(refused, 0u);
140
141 for (size_t i = 0; i < results.size(); ++i)
142 {
143 SCOPED_TRACE(i);
144 const bool down = i / 2 == 1 or i / 2 == 3 or i / 2 == 5;
145 EXPECT_EQ(results[i], down ? descending : sorted);
146 }
147
148 // the node of the first item is still in the list
149 bool found = false;
150 list.for_each([&found, first] (const int & x) { found = found or &x == first; });
151 EXPECT_TRUE(found);
152}
High-level sorting functions for Aleph containers.
A global operator new that a test can make fail.
#define ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
Node belonging to a double circular linked list with header node.
Definition tpl_dnode.H:106
Dynamic doubly linked list with O(1) size and bidirectional access.
T & append(const T &item)
Append a copied item at the end of the list.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & get_first()
Return the first item of the list.
Definition htlist.H:1548
T & append(const T &item)
Definition htlist.H:1362
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:1475
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
Main namespace for Aleph-w library functions.
void insertion_sort(T *a, const long l, const long r, const Compare &cmp=Compare()) noexcept(noexcept(static_cast< bool >(cmp(std::declval< T & >(), std::declval< T & >()))) and std::is_nothrow_move_constructible_v< T > and std::is_nothrow_move_assignable_v< T >)
Sort an array using insertion sort.
void mergeinsertsort(Tlist< T > &list, const Compare &cmp=Compare(), const size_t lsz=Aleph::Insertion_Threshold)
Sort a list by mergesort combined with the insert method.
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
Definition ahSort.H:234
DynArray< T > & in_place_sort(DynArray< T > &c, Cmp cmp=Cmp())
Sorts a DynArray in place.
Definition ahSort.H:328
void mergesort(T *a, const long l, const long r, Array< T > &buf, Compare cmp)
Sort an array using merge sort with a reusable buffer.
TEST_F(ListSortWithoutMemory, TheSortsInPlaceAllocateNothing)
Dynamic doubly linked list implementation.
Alias for htlist.H (DynList implementation).
Comprehensive sorting algorithms and search utilities for Aleph-w.
DynList< int > l