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 #ifndef SUPPORT_TEST_CONSTEXPR_CONTAINER_H
9 #define SUPPORT_TEST_CONSTEXPR_CONTAINER_H
10 
11 // A dummy container with enough constexpr support to test the standard
12 // insert iterators, such as `back_insert_iterator`.
13 
14 #include <algorithm>
15 #include <cassert>
16 #include <utility>
17 
18 #include "test_macros.h"
19 
20 #if TEST_STD_VER >= 14
21 
22 template<class T, int N>
23 class ConstexprFixedCapacityDeque {
24     T data_[N];
25     int size_ = 0;
26 public:
27     using value_type = T;
28     using iterator = T *;
29     using const_iterator = T const *;
30 
31     constexpr ConstexprFixedCapacityDeque() = default;
begin()32     constexpr iterator begin() { return data_; }
end()33     constexpr iterator end() { return data_ + size_; }
begin()34     constexpr const_iterator begin() const { return data_; }
end()35     constexpr const_iterator end() const { return data_ + size_; }
size()36     constexpr size_t size() const { return size_; }
front()37     constexpr const T& front() const { assert(size_ >= 1); return data_[0]; }
back()38     constexpr const T& back() const { assert(size_ >= 1); return data_[size_-1]; }
39 
insert(const_iterator pos,T t)40     constexpr iterator insert(const_iterator pos, T t) {
41         int i = static_cast<int>(pos - data_);
42         if (i != size_) {
43             std::move_backward(data_ + i, data_ + size_, data_ + size_ + 1);
44         }
45         data_[i] = std::move(t);
46         size_ += 1;
47         return data_ + i;
48     }
49 
push_back(T t)50     constexpr void push_back(T t) { insert(end(), std::move(t)); }
push_front(T t)51     constexpr void push_front(T t) { insert(begin(), std::move(t)); }
52 };
53 
54 #endif // TEST_STD_VER >= 14
55 
56 #endif // SUPPORT_TEST_CONSTEXPR_CONTAINER_H
57