Debian Codegraph @

tests/core/templates/test_fixed_vector.h

raw symbols SCIP
1 /**************************************************************************/
2 /* test_fixed_vector.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "core/templates/fixed_vector.h"
34
35 #include "tests/test_macros.h"
36
37 namespace TestFixedVector {
38
39 struct MoveOnly {
40 bool is_alive = true;
41
42 MoveOnly() = default;
43 MoveOnly(const MoveOnly &p) = delete;
44 MoveOnly &operator=(const MoveOnly &p) = delete;
45
46 MoveOnly(MoveOnly &&p) {
47 is_alive = p.is_alive;
48 p.is_alive = false;
49 }
50 MoveOnly &operator=(MoveOnly &&p) {
51 if (&p == this) {
52 return *this;
53 }
54 is_alive = p.is_alive;
55 p.is_alive = false;
56 return *this;
57 }
58 };
59
60 static_assert(!std::is_trivially_copyable_v<MoveOnly>);
61 static_assert(!std::is_trivially_constructible_v<MoveOnly>);
62
63 TEST_CASE("[FixedVector] Basic Checks") {
64 FixedVector<uint16_t, 1> vector;
65 CHECK_EQ(vector.capacity(), 1);
66
67 CHECK_EQ(vector.size(), 0);
68 CHECK(vector.is_empty());
69 CHECK(!vector.is_full());
70
71 vector.push_back(5);
72 CHECK_EQ(vector.size(), 1);
73 CHECK_EQ(vector[0], 5);
74 CHECK_EQ(vector.ptr()[0], 5);
75 CHECK(!vector.is_empty());
76 CHECK(vector.is_full());
77
78 vector.pop_back();
79 CHECK_EQ(vector.size(), 0);
80 CHECK(vector.is_empty());
81 CHECK(!vector.is_full());
82
83 FixedVector<uint16_t, 2> vector1 = { 1, 2 };
84 CHECK_EQ(vector1.capacity(), 2);
85 CHECK_EQ(vector1.size(), 2);
86 CHECK_EQ(vector1[0], 1);
87 CHECK_EQ(vector1[1], 2);
88
89 FixedVector<Variant, 3> vector_variant;
90 CHECK_EQ(vector_variant.size(), 0);
91 CHECK_EQ(vector_variant.capacity(), 3);
92 vector_variant.resize_initialized(3);
93 vector_variant[0] = "Test";
94 vector_variant[1] = 1;
95 CHECK_EQ(vector_variant.capacity(), 3);
96 CHECK_EQ(vector_variant.size(), 3);
97 CHECK_EQ(vector_variant[0], "Test");
98 CHECK_EQ(vector_variant[1], Variant(1));
99 CHECK_EQ(vector_variant[2].get_type(), Variant::NIL);
100
101 // Test that move-only types are transferred.
102 FixedVector<MoveOnly, 1> a;
103 a.push_back(MoveOnly());
104 CHECK_EQ(a.size(), 1);
105 FixedVector<MoveOnly, 1> b(std::move(a));
106 CHECK_EQ(a.size(), 0);
107 CHECK_EQ(b.size(), 1);
108 a = std::move(b);
109 CHECK_EQ(a.size(), 1);
110 CHECK_EQ(b.size(), 0);
111 }
112
113 TEST_CASE("[FixedVector] Alignment Checks") {
114 FixedVector<uint16_t, 4> vector_uint16;
115 vector_uint16.resize_uninitialized(4);
116 CHECK((size_t)&vector_uint16[0] % alignof(uint16_t) == 0);
117 CHECK((size_t)&vector_uint16[1] % alignof(uint16_t) == 0);
118 CHECK((size_t)&vector_uint16[2] % alignof(uint16_t) == 0);
119 CHECK((size_t)&vector_uint16[3] % alignof(uint16_t) == 0);
120
121 FixedVector<uint32_t, 4> vector_uint32;
122 vector_uint32.resize_uninitialized(4);
123 CHECK((size_t)&vector_uint32[0] % alignof(uint32_t) == 0);
124 CHECK((size_t)&vector_uint32[1] % alignof(uint32_t) == 0);
125 CHECK((size_t)&vector_uint32[2] % alignof(uint32_t) == 0);
126 CHECK((size_t)&vector_uint32[3] % alignof(uint32_t) == 0);
127
128 FixedVector<uint64_t, 4> vector_uint64;
129 vector_uint64.resize_uninitialized(4);
130 CHECK((size_t)&vector_uint64[0] % alignof(uint64_t) == 0);
131 CHECK((size_t)&vector_uint64[1] % alignof(uint64_t) == 0);
132 CHECK((size_t)&vector_uint64[2] % alignof(uint64_t) == 0);
133 CHECK((size_t)&vector_uint64[3] % alignof(uint64_t) == 0);
134 }
135
136 } //namespace TestFixedVector