16
16
17
17
#include < type_traits>
18
18
19
+ #include " construct/into.h"
19
20
#include " marker/unsafe.h"
20
21
#include " mem/relocate.h"
22
+ #include " num/types.h"
21
23
#include " third_party/googletest/googletest/include/gtest/gtest.h"
22
24
23
25
using ::sus::containers::Array;
@@ -30,32 +32,32 @@ struct TriviallyRelocatable {
30
32
int i;
31
33
};
32
34
33
- static_assert (!std::is_trivially_constructible_v<Array<int , 2 >>, " " );
34
- static_assert (!std::is_trivial_v<Array<int , 2 >>, " " );
35
- static_assert (!std::is_aggregate_v<Array<int , 2 >>, " " );
36
- static_assert (std::is_standard_layout_v<Array<int , 2 >>, " " );
35
+ static_assert (!std::is_trivially_constructible_v<Array<int , 2 >>);
36
+ static_assert (!std::is_trivial_v<Array<int , 2 >>);
37
+ static_assert (!std::is_aggregate_v<Array<int , 2 >>);
38
+ static_assert (std::is_standard_layout_v<Array<int , 2 >>);
37
39
38
40
// TODO: This covers a trivially relocatable type, but what about the rest?
39
41
// (like Option unit tests.)
40
42
namespace trivially_relocatable {
41
43
using T = TriviallyRelocatable;
42
- static_assert (std::is_move_constructible_v<Array<T, 2 >>, " " );
43
- static_assert (std::is_move_assignable_v<Array<T, 2 >>, " " );
44
- static_assert (std::is_trivially_destructible_v<Array<T, 2 >>, " " );
45
- static_assert (std::is_trivially_move_constructible_v<Array<T, 2 >>, " " );
46
- static_assert (std::is_trivially_move_assignable_v<Array<T, 2 >>, " " );
47
- static_assert (std::is_nothrow_swappable_v<Array<T, 2 >>, " " );
48
- static_assert (std::is_trivially_copy_constructible_v<Array<T, 2 >>, " " );
49
- static_assert (std::is_trivially_copy_assignable_v<Array<T, 2 >>, " " );
50
- static_assert (!std::is_constructible_v<Array<T, 2 >, T&&>, " " );
51
- static_assert (!std::is_assignable_v<Array<T, 2 >, T&&>, " " );
52
- static_assert (!std::is_constructible_v<Array<T, 2 >, const T&>, " " );
53
- static_assert (!std::is_assignable_v<Array<T, 2 >, const T&>, " " );
54
- static_assert (!std::is_constructible_v<Array<T, 2 >, T>, " " );
55
- static_assert (!std::is_assignable_v<Array<T, 2 >, T>, " " );
56
- static_assert (std::is_nothrow_destructible_v<Array<T, 2 >>, " " );
57
- static_assert (relocate_one_by_memcpy<Array<T, 2 >>, " " );
58
- static_assert (relocate_array_by_memcpy<Array<T, 2 >>, " " );
44
+ static_assert (std::is_move_constructible_v<Array<T, 2 >>);
45
+ static_assert (std::is_move_assignable_v<Array<T, 2 >>);
46
+ static_assert (std::is_trivially_destructible_v<Array<T, 2 >>);
47
+ static_assert (std::is_trivially_move_constructible_v<Array<T, 2 >>);
48
+ static_assert (std::is_trivially_move_assignable_v<Array<T, 2 >>);
49
+ static_assert (std::is_nothrow_swappable_v<Array<T, 2 >>);
50
+ static_assert (std::is_trivially_copy_constructible_v<Array<T, 2 >>);
51
+ static_assert (std::is_trivially_copy_assignable_v<Array<T, 2 >>);
52
+ static_assert (!std::is_constructible_v<Array<T, 2 >, T&&>);
53
+ static_assert (!std::is_assignable_v<Array<T, 2 >, T&&>);
54
+ static_assert (!std::is_constructible_v<Array<T, 2 >, const T&>);
55
+ static_assert (!std::is_assignable_v<Array<T, 2 >, const T&>);
56
+ static_assert (!std::is_constructible_v<Array<T, 2 >, T>);
57
+ static_assert (!std::is_assignable_v<Array<T, 2 >, T>);
58
+ static_assert (std::is_nothrow_destructible_v<Array<T, 2 >>);
59
+ static_assert (relocate_one_by_memcpy<Array<T, 2 >>);
60
+ static_assert (relocate_array_by_memcpy<Array<T, 2 >>);
59
61
} // namespace trivially_relocatable
60
62
61
63
struct NonAggregate {
@@ -64,18 +66,18 @@ struct NonAggregate {
64
66
65
67
TEST (Array, WithDefault) {
66
68
auto a = Array<int , 5 >::with_default ();
67
- static_assert (sizeof (a) == sizeof (int [5 ]), " " );
69
+ static_assert (sizeof (a) == sizeof (int [5 ]));
68
70
for (size_t i = 0 ; i < 5 ; ++i) {
69
71
EXPECT_EQ (a.get (i), 0 );
70
72
}
71
73
72
- static_assert (sizeof (Array<int , 5 >::with_default ()) == sizeof (int [5 ]), " " );
74
+ static_assert (sizeof (Array<int , 5 >::with_default ()) == sizeof (int [5 ]));
73
75
}
74
76
75
77
TEST (Array, Zero) {
76
78
auto a = Array<int , 0 >::with_default ();
77
- static_assert (sizeof (a) == 1 , " " );
78
- static_assert (sizeof (Array<int , 0 >::with_default ()) == 1 , " " );
79
+ static_assert (sizeof (a) == 1 );
80
+ static_assert (sizeof (Array<int , 0 >::with_default ()) == 1 );
79
81
}
80
82
81
83
TEST (Array, WithUninitialized) {
@@ -86,7 +88,7 @@ TEST(Array, WithUninitialized) {
86
88
87
89
TEST (Array, WithInitializer) {
88
90
auto a = Array<int , 5 >::with_initializer ([i = 1 ]() mutable { return i++; });
89
- static_assert (sizeof (a) == sizeof (int [5 ]), " " );
91
+ static_assert (sizeof (a) == sizeof (int [5 ]));
90
92
for (size_t i = 0 ; i < 5 ; ++i) {
91
93
EXPECT_EQ (a.get (i), i + 1 );
92
94
}
@@ -101,14 +103,14 @@ TEST(Array, WithInitializer) {
101
103
" " );
102
104
auto b = Array<NotTriviallyDefaultConstructible, 5 >::with_initializer (
103
105
[i = 1 ]() mutable -> NotTriviallyDefaultConstructible { return {i++}; });
104
- static_assert (sizeof (b) == sizeof (NotTriviallyDefaultConstructible[5 ]), " " );
106
+ static_assert (sizeof (b) == sizeof (NotTriviallyDefaultConstructible[5 ]));
105
107
for (size_t i = 0 ; i < 5 ; ++i) {
106
108
EXPECT_EQ (b.get (i).i , i + 1 );
107
109
}
108
110
109
111
auto lvalue = [i = 1 ]() mutable { return i++; };
110
112
auto c = Array<int , 5 >::with_initializer (lvalue);
111
- static_assert (sizeof (c) == sizeof (int [5 ]), " " );
113
+ static_assert (sizeof (c) == sizeof (int [5 ]));
112
114
for (size_t i = 0 ; i < 5 ; ++i) {
113
115
EXPECT_EQ (c.get (i), i + 1 );
114
116
}
@@ -120,12 +122,31 @@ TEST(Array, WithInitializer) {
120
122
121
123
TEST (Array, WithValue) {
122
124
auto a = Array<int , 5 >::with_value (3 );
123
- static_assert (sizeof (a) == sizeof (int [5 ]), " " );
125
+ static_assert (sizeof (a) == sizeof (int [5 ]));
124
126
for (size_t i = 0 ; i < 5 ; ++i) {
125
127
EXPECT_EQ (a.get (i), 3 );
126
128
}
127
129
}
128
130
131
+ TEST (Array, WithValues) {
132
+ {
133
+ auto a = Array<int , 5 >::with_values (3 , 4 , 5 , 6 , 7 );
134
+ static_assert (sizeof (a) == sizeof (int [5 ]));
135
+ for (size_t i = 0 ; i < 5 ; ++i) {
136
+ EXPECT_EQ (a.get (i), 3 + i);
137
+ }
138
+ }
139
+ {
140
+ auto a = Array<u8 , 5 >::with_values (sus::into (3 ), sus::into (4 ), sus::into (5 ),
141
+ sus::into (6 ), sus::into (7 ));
142
+ static_assert (sizeof (a) == sizeof (u8 [5 ]));
143
+ for (auto i = 0_u8; i < 5_u8; i += 1_u8) {
144
+ EXPECT_EQ (a.get (/* TODO: usize */ usize::from (i).primitive_value ),
145
+ 3_u8 + i);
146
+ }
147
+ }
148
+ }
149
+
129
150
TEST (Array, Get) {
130
151
{
131
152
constexpr auto r = []() constexpr {
@@ -161,7 +182,6 @@ TEST(Array, GetMut) {
161
182
}
162
183
}
163
184
164
-
165
185
TEST (Array, AsPtr) {
166
186
auto a = Array<int , 5 >::with_initializer ([i = 0 ]() mutable { return ++i; });
167
187
auto r = a.as_ptr ();
0 commit comments