]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/unittests/vec-utils-selftests.c
gdb: convert separate-debug-file to new(ish) debug scheme
[thirdparty/binutils-gdb.git] / gdb / unittests / vec-utils-selftests.c
1 /* Self tests for vector utility routines for GDB, the GNU debugger.
2
3 Copyright (C) 2019-2024 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "gdbsupport/selftest.h"
21
22 #include "gdbsupport/gdb_vecs.h"
23
24 namespace selftests {
25 namespace vector_utils_tests {
26
27 static void
28 unordered_remove_tests ()
29 {
30 /* Create vector with a single object in, and then remove that object.
31 This test was added after a bug was discovered where unordered_remove
32 would cause a self move assign. If the C++ standard library is
33 compiled in debug mode (by passing -D_GLIBCXX_DEBUG=1) and the
34 operator= is removed from struct obj this test used to fail with an
35 error from the C++ standard library. */
36 struct obj
37 {
38 std::vector<void *> var;
39
40 obj() = default;
41
42 /* gcc complains if we provide an assignment operator but no copy
43 constructor, so provide one even if don't really care for this test. */
44 obj(const obj &other)
45 {
46 this->var = other.var;
47 }
48
49 obj &operator= (const obj &other)
50 {
51 if (this == &other)
52 error (_("detected self move assign"));
53 this->var = other.var;
54 return *this;
55 }
56 };
57
58 std::vector<obj> v;
59 v.emplace_back ();
60 auto it = v.end () - 1;
61 unordered_remove (v, it);
62 SELF_CHECK (v.empty ());
63 }
64
65 } /* namespace vector_utils_tests */
66 } /* namespace selftests */
67
68 void _initialize_vec_utils_selftests ();
69 void
70 _initialize_vec_utils_selftests ()
71 {
72 selftests::register_test
73 ("unordered_remove",
74 selftests::vector_utils_tests::unordered_remove_tests);
75 }