[WebAssembly] Fix asan issue from https://reviews.llvm.org/D121349
[llvm-project.git] / libcxx / test / support / test_constexpr_container.h
blobfa572446301772557c82b3c29a3a605c50222e3d
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
11 // A dummy container with enough constexpr support to test the standard
12 // insert iterators, such as `back_insert_iterator`.
14 #include <algorithm>
15 #include <cassert>
16 #include <utility>
18 #include "test_macros.h"
20 #if TEST_STD_VER >= 14
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 *;
31 constexpr ConstexprFixedCapacityDeque() = default;
32 constexpr iterator begin() { return data_; }
33 constexpr iterator end() { return data_ + size_; }
34 constexpr const_iterator begin() const { return data_; }
35 constexpr const_iterator end() const { return data_ + size_; }
36 constexpr size_t size() const { return size_; }
37 constexpr const T& front() const { assert(size_ >= 1); return data_[0]; }
38 constexpr const T& back() const { assert(size_ >= 1); return data_[size_-1]; }
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);
45 data_[i] = std::move(t);
46 size_ += 1;
47 return data_ + i;
50 constexpr void push_back(T t) { insert(end(), std::move(t)); }
51 constexpr void push_front(T t) { insert(begin(), std::move(t)); }
54 #endif // TEST_STD_VER >= 14
56 #endif // SUPPORT_TEST_CONSTEXPR_CONTAINER_H