195 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef TEST_STD_CONTAINERS_CONTAINER_ADAPTORS_FLAT_HELPERS_H10#define TEST_STD_CONTAINERS_CONTAINER_ADAPTORS_FLAT_HELPERS_H11 12#include <cstdint>13#include <vector>14 15#include "test_macros.h"16 17template <class T>18struct CopyOnlyVector : std::vector<T> {19 using std::vector<T>::vector;20 21 constexpr CopyOnlyVector(const CopyOnlyVector&) = default;22 constexpr CopyOnlyVector(CopyOnlyVector&& other) : CopyOnlyVector(other) {}23 constexpr CopyOnlyVector(CopyOnlyVector&& other, std::vector<T>::allocator_type alloc)24 : CopyOnlyVector(other, alloc) {}25 26 constexpr CopyOnlyVector& operator=(const CopyOnlyVector&) = default;27 constexpr CopyOnlyVector& operator=(CopyOnlyVector& other) { return this->operator=(other); }28};29 30template <class T>31struct SillyReserveVector : std::vector<T> {32 using std::vector<T>::vector;33 34 constexpr void reserve(std::size_t) { this->clear(); }35};36 37template <class T, bool ConvertibleToT = false>38struct Transparent {39 T t;40 41 constexpr explicit operator T() const42 requires ConvertibleToT43 {44 return t;45 }46};47 48template <class T>49using ConvertibleTransparent = Transparent<T, true>;50 51template <class T>52using ExplicitlyConvertibleTransparent = Transparent<T, true>;53 54template <class T>55using NonConvertibleTransparent = Transparent<T, false>;56 57struct TransparentComparator {58 using is_transparent = void;59 60 bool* transparent_used = nullptr;61 TransparentComparator() = default;62 constexpr TransparentComparator(bool& used) : transparent_used(&used) {}63 64 template <class T, bool Convertible>65 constexpr bool operator()(const T& t, const Transparent<T, Convertible>& transparent) const {66 if (transparent_used != nullptr) {67 *transparent_used = true;68 }69 return t < transparent.t;70 }71 72 template <class T, bool Convertible>73 constexpr bool operator()(const Transparent<T, Convertible>& transparent, const T& t) const {74 if (transparent_used != nullptr) {75 *transparent_used = true;76 }77 return transparent.t < t;78 }79 80 template <class T>81 constexpr bool operator()(const T& t1, const T& t2) const {82 return t1 < t2;83 }84};85 86struct NonTransparentComparator {87 template <class T, bool Convertible>88 bool operator()(const T&, const Transparent<T, Convertible>&) const;89 90 template <class T, bool Convertible>91 bool operator()(const Transparent<T, Convertible>&, const T&) const;92 93 template <class T>94 bool operator()(const T&, const T&) const;95};96 97struct NoDefaultCtr {98 NoDefaultCtr() = delete;99};100 101class Moveable {102 int int_;103 double double_;104 105public:106 TEST_CONSTEXPR Moveable() : int_(0), double_(0) {}107 TEST_CONSTEXPR Moveable(int i, double d) : int_(i), double_(d) {}108 TEST_CONSTEXPR Moveable(Moveable&& x) : int_(x.int_), double_(x.double_) {109 x.int_ = -1;110 x.double_ = -1;111 }112 TEST_CONSTEXPR Moveable& operator=(Moveable&& x) {113 int_ = x.int_;114 x.int_ = -1;115 double_ = x.double_;116 x.double_ = -1;117 return *this;118 }119 120 Moveable(const Moveable&) = delete;121 Moveable& operator=(const Moveable&) = delete;122 TEST_CONSTEXPR bool operator==(const Moveable& x) const { return int_ == x.int_ && double_ == x.double_; }123 TEST_CONSTEXPR bool operator<(const Moveable& x) const {124 return int_ < x.int_ || (int_ == x.int_ && double_ < x.double_);125 }126 127 TEST_CONSTEXPR int get() const { return int_; }128 TEST_CONSTEXPR bool moved() const { return int_ == -1; }129};130 131#ifndef TEST_HAS_NO_EXCEPTIONS132template <class T>133struct EmplaceUnsafeContainer : std::vector<T> {134 using std::vector<T>::vector;135 136 template <class... Args>137 auto emplace(Args&&... args) -> decltype(std::declval<std::vector<T>>().emplace(std::forward<Args>(args)...)) {138 if (this->size() > 1) {139 auto it1 = this->begin();140 auto it2 = it1 + 1;141 // messing up the container142 std::iter_swap(it1, it2);143 }144 145 throw 42;146 }147 148 template <class... Args>149 auto insert(Args&&... args) -> decltype(std::declval<std::vector<T>>().insert(std::forward<Args>(args)...)) {150 if (this->size() > 1) {151 auto it1 = this->begin();152 auto it2 = it1 + 1;153 // messing up the container154 std::iter_swap(it1, it2);155 }156 157 throw 42;158 }159 160 template <class... Args>161 auto insert_range(Args&&... args)162 -> decltype(std::declval<std::vector<T>>().insert_range(std::forward<Args>(args)...)) {163 if (this->size() > 1) {164 auto it1 = this->begin();165 auto it2 = it1 + 1;166 // messing up the container167 std::iter_swap(it1, it2);168 }169 170 throw 42;171 }172};173 174template <class T>175struct ThrowOnEraseContainer : std::vector<T> {176 using std::vector<T>::vector;177 178 template <class... Args>179 auto erase(Args&&... args) -> decltype(std::declval<std::vector<T>>().erase(std::forward<Args>(args)...)) {180 throw 42;181 }182};183 184template <class T>185struct ThrowOnMoveContainer : std::vector<T> {186 using std::vector<T>::vector;187 188 ThrowOnMoveContainer(ThrowOnMoveContainer&&) { throw 42; }189 190 ThrowOnMoveContainer& operator=(ThrowOnMoveContainer&&) { throw 42; }191};192 193#endif // TEST_HAS_NO_EXCEPTIONS194 195#endif // TEST_STD_CONTAINERS_CONTAINER_ADAPTORS_FLAT_HELPERS_H