#include <array>
#include <string>
#include <vector>
namespace {
struct Aggregate {
int a;
int b;
int c;
};
Aggregate Build(int a, int b, int c) {
return Aggregate{a, b, c};
}
}
int UnsafeIndex();
struct Embedded {
struct Values {
int value;
};
std::array<Values, 3> values = {{{1}, {2}, {3}}};
};
void test_with_structs() {
auto buf0 = std::to_array<Aggregate>({{13, 1, 7}, {14, 2, 5}, {15, 2, 4}});
buf0[UnsafeIndex()].a = 0;
Embedded embedded;
embedded.values[UnsafeIndex()].value++;
std::array<Aggregate, 2> buf1 = {
Build(1, 2, 3),
Build(4, 5, 6),
};
buf1[UnsafeIndex()].a = 0;
std::array<Aggregate, 3> buf2 = {{
Build(1, 2, 3),
{1, 2, 3},
Build(4, 5, 6),
}};
buf2[UnsafeIndex()].a = 0;
}
void test_with_arrays() {
std::array<std::array<int, 3>, 3> buf0 = {{
{0, 1, 2},
{3, 4, 5},
{6, 7, 8},
}};
buf0[UnsafeIndex()][UnsafeIndex()] = 0;
}
void test_with_strings() {
auto buf0 = std::to_array<std::string>({"1", "2", "3"});
buf0[UnsafeIndex()] = "4";
}
void test_with_const() {
const auto data = std::to_array<bool>({false, true});
std::ignore = data[UnsafeIndex()];
}
void test_with_static() {
static auto data = std::to_array<int>({1, 2, 3});
std::ignore = data[UnsafeIndex()];
}
void test_with_constexpr() {
constexpr auto data = std::to_array<int>({1, 2, 3});
std::ignore = data[UnsafeIndex()];
}
void test_with_constexpr_const() {
constexpr auto data = std::to_array<int>({1, 2, 3});
std::ignore = data[UnsafeIndex()];
}
void test_with_volatile() {
auto data = std::to_array<volatile int>({1, 2, 3});
std::ignore = data[UnsafeIndex()];
}
void test_with_all_qualifiers() {
static const auto data = std::to_array<volatile int>({1, 2, 3});
std::ignore = data[UnsafeIndex()];
}
void test_with_const_char() {
static auto data = std::to_array<const char*>({" B", " kB", " MB"});
std::ignore = data[UnsafeIndex()];
}
void test_with_constant_const_char() {
static const auto data = std::to_array<const char*>({" B", " kB", " MB"});
std::ignore = data[UnsafeIndex()];
}
void test_with_computed_size() {
std::array<int, 2 + 2> data = {1, 2, 3, 4};
std::ignore = data[UnsafeIndex()];
}
void test_with_constexpr_size() {
constexpr int size = 2 + 2;
std::array<int, size> data = {1, 2, 3, 4};
std::ignore = data[UnsafeIndex()];
}
void test_with_const_size() {
const int size = 2 + 2;
std::array<int, size> data = {1, 2, 3, 4};
std::ignore = data[UnsafeIndex()];
}
void test_brace_elision_computed_size() {
constexpr int size = 2;
struct Aggregate {
int a;
int b;
};
std::array<Aggregate, size> buffer = {{{1, 2}, {3, 4}}};
std::ignore = buffer[UnsafeIndex()];
}
void test_with_nested_vector() {
auto data = std::to_array<std::vector<int>>({
{1, 2, 3},
{4, 5, 6},
});
std::ignore = data[UnsafeIndex()];
}
void test_with_nested_vector_const_size() {
std::array<std::vector<int>, 2> data = {{
{1, 2, 3},
{4, 5, 6},
}};
std::ignore = data[UnsafeIndex()];
}