brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.7 KiB · a828438 Raw
119 lines · cpp
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// UNSUPPORTED: c++03, c++11, c++14, c++17, c++2010 11// <flat_set>12 13// friend bool operator==(const flat_set& x, const flat_set& y);14// friend synth-three-way-result<value_type>15//   operator<=>(const flat_set& x, const flat_set& y);16 17#include <algorithm>18#include <cassert>19#include <deque>20#include <compare>21#include <flat_set>22#include <functional>23#include <limits>24#include <vector>25 26#include "MinSequenceContainer.h"27#include "test_macros.h"28#include "min_allocator.h"29#include "test_allocator.h"30#include "test_comparisons.h"31#include "test_container_comparisons.h"32 33template <class KeyContainer>34constexpr void test_one() {35  using Key = typename KeyContainer::value_type;36 37  {38    using C = std::flat_set<Key>;39    C s1    = {1};40    C s2    = {2};41    ASSERT_SAME_TYPE(decltype(s1 <=> s2), std::strong_ordering);42    AssertComparisonsReturnBool<C>();43    assert(testComparisons(s1, s2, false, true));44    s2 = {1};45    assert(testComparisons(s1, s2, true, false));46    s2 = {1, 2};47    assert(testComparisons(s1, s2, false, true));48    s1 = {0, 1, 2};49    assert(testComparisons(s1, s2, false, true));50    s2 = {0, 1, 3};51    assert(testComparisons(s1, s2, false, true));52  }53  {54    // Comparisons use value_type's native operators, not the comparator55    using C = std::flat_set<Key, std::greater<Key>>;56    C s1    = {1};57    C s2    = {2};58    ASSERT_SAME_TYPE(decltype(s1 <=> s2), std::strong_ordering);59    AssertComparisonsReturnBool<C>();60    assert(testComparisons(s1, s2, false, true));61    s2 = {1};62    assert(testComparisons(s1, s2, true, false));63    s2 = {1, 2};64    assert(testComparisons(s1, s2, false, true));65    s1 = {0, 1, 2};66    assert(testComparisons(s1, s2, false, false));67    s2 = {0, 1, 3};68    assert(testComparisons(s1, s2, false, true));69  }70}71 72constexpr bool test() {73  test_one<std::vector<int>>();74#ifndef __cpp_lib_constexpr_deque75  if (!TEST_IS_CONSTANT_EVALUATED)76#endif77    test_one<std::deque<int>>();78  test_one<MinSequenceContainer<int>>();79  test_one<std::vector<int, min_allocator<int>>>();80 81  {82    using C = std::flat_set<double>;83    C s1    = {1};84    C s2    = C(std::sorted_unique, {std::numeric_limits<double>::quiet_NaN()});85    ASSERT_SAME_TYPE(decltype(s1 <=> s2), std::partial_ordering);86    AssertComparisonsReturnBool<C>();87    assert(testComparisonsComplete(s1, s2, false, false, false));88  }89  {90    // Comparisons use value_type's native operators, not the comparator91    struct StrongComp {92      constexpr bool operator()(double a, double b) const { return std::strong_order(a, b) < 0; }93    };94    using C = std::flat_set<double, StrongComp>;95    C s1    = {1};96    C s2    = {std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN()};97    ASSERT_SAME_TYPE(decltype(s1 <=> s2), std::partial_ordering);98    AssertComparisonsReturnBool<C>();99    assert(testComparisonsComplete(s1, s2, false, false, false));100    s1 = {1, std::numeric_limits<double>::quiet_NaN(), 1};101    s2 = {std::numeric_limits<double>::quiet_NaN(), 1};102    assert(std::lexicographical_compare_three_way(s1.begin(), s1.end(), s2.begin(), s2.end(), std::strong_order) ==103           std::strong_ordering::equal);104    assert(s1 != s2);105    assert((s1 <=> s2) == std::partial_ordering::unordered);106  }107 108  return true;109}110 111int main(int, char**) {112  test();113#if TEST_STD_VER >= 26114  static_assert(test());115#endif116 117  return 0;118}119