xref: /llvm-project/libcxx/test/support/test_constexpr_container.h (revision fb855eb941b6d740cc6560297d0b4d3201dcaf9f)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef SUPPORT_TEST_CONSTEXPR_CONTAINER_H
10 #define SUPPORT_TEST_CONSTEXPR_CONTAINER_H
11 
12 // A dummy container with enough constexpr support to test the standard
13 // insert iterators, such as `back_insert_iterator`.
14 
15 #include <algorithm>
16 #include <cassert>
17 #include <cstddef>
18 #include <utility>
19 
20 #include "test_macros.h"
21 
22 #if TEST_STD_VER >= 14
23 
24 template<class T, int N>
25 class ConstexprFixedCapacityDeque {
26     T data_[N];
27     int size_ = 0;
28 public:
29     using value_type = T;
30     using iterator = T *;
31     using const_iterator = T const *;
32 
33     constexpr ConstexprFixedCapacityDeque() = default;
begin()34     constexpr iterator begin() { return data_; }
end()35     constexpr iterator end() { return data_ + size_; }
begin()36     constexpr const_iterator begin() const { return data_; }
end()37     constexpr const_iterator end() const { return data_ + size_; }
size()38     constexpr std::size_t size() const { return size_; }
front()39     constexpr const T& front() const { assert(size_ >= 1); return data_[0]; }
back()40     constexpr const T& back() const { assert(size_ >= 1); return data_[size_-1]; }
41 
insert(const_iterator pos,T t)42     constexpr iterator insert(const_iterator pos, T t) {
43         int i = static_cast<int>(pos - data_);
44         if (i != size_) {
45             std::move_backward(data_ + i, data_ + size_, data_ + size_ + 1);
46         }
47         data_[i] = std::move(t);
48         size_ += 1;
49         return data_ + i;
50     }
51 
push_back(T t)52     constexpr void push_back(T t) { insert(end(), std::move(t)); }
push_front(T t)53     constexpr void push_front(T t) { insert(begin(), std::move(t)); }
54 };
55 
56 #endif // TEST_STD_VER >= 14
57 
58 #endif // SUPPORT_TEST_CONSTEXPR_CONTAINER_H
59