Skip to content

[libc++] [P1222] std::flat_set #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[libc++] [P1222] Tests for std::flat_set
  • Loading branch information
Quuxplusone committed Apr 25, 2023
commit 5f5c36ed1bfa76426bbeb993b8ac306087f307e1
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// <flat_set>

#include <flat_set>

#ifndef _LIBCPP_VERSION
#error _LIBCPP_VERSION not defined
#endif

int main(int, char**)
{
return 0;
}
81 changes: 81 additions & 0 deletions libcxx/test/std/containers/container.adaptors/NaiveStaticVector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef SUPPORT_NAIVE_STATIC_VECTOR_H
#define SUPPORT_NAIVE_STATIC_VECTOR_H

#include "test_iterators.h"
#include "test_macros.h"

template<class T, size_t N>
struct NaiveStaticVector {
struct CapacityError {};

using value_type = T;
using difference_type = short;
using size_type = unsigned short;
using iterator = random_access_iterator<T*>;
using const_iterator = random_access_iterator<const T*>;

explicit NaiveStaticVector() = default;
template<class It> explicit NaiveStaticVector(It first, It last) { while (first != last) insert(*first++); }

// Moving-from a NaiveStaticVector leaves the source vector holding moved-from objects.
// This is intentional (the "Naive" in the name).
// Specifically, moving-out-of a sorted+uniqued NaiveStaticVector<MoveOnly>
// will leave it in a non-sorted+uniqued state.

NaiveStaticVector(const NaiveStaticVector&) = default;
NaiveStaticVector(NaiveStaticVector&&) = default; // deliberately don't reset size_
NaiveStaticVector& operator=(const NaiveStaticVector&) = default;
NaiveStaticVector& operator=(NaiveStaticVector&&) = default;

iterator begin() { return iterator(data_); }
const_iterator begin() const { return const_iterator(data_); }
const_iterator cbegin() const { return const_iterator(data_); }
iterator end() { return begin() + size(); }
const_iterator end() const { return begin() + size(); }
size_type size() const { return size_; }
bool empty() const { return size_ == 0; }

void clear() { size_ = 0; }

template<class It>
iterator insert(const_iterator pos, It first, It last) {
iterator result = pos - cbegin() + begin();
while (first != last) {
insert(pos++, *first++);
}
return result;
}

iterator insert(const_iterator pos, T value) {
if (size_ == N) {
throw CapacityError();
}
int i = pos - cbegin();
size_ += 1;
std::move_backward(&data_[i], &data_[size_ - 1], &data_[size_]);
data_[i] = std::move(value);
return begin() + i;
}

iterator erase(const_iterator first, const_iterator last) {
int i = first - cbegin();
int j = last - cbegin();
std::move(&data_[j], &data_[size_], &data_[i]);
size_ -= (last - first);
return begin() + i;
}

private:
T data_[N];
size_t size_ = 0;
};

#endif // SUPPORT_NAIVE_STATIC_VECTOR_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20

// <flat_set>

// class flat_set

// void clear() noexcept;

#include <cassert>
#include <deque>
#include <flat_set>
#include <functional>

#include "test_macros.h"
#include "min_allocator.h"

int main(int, char**)
{
{
using M = std::flat_set<int>;
M m = {1, 2, 3, 4, 5};
assert(m.size() == 5);
ASSERT_NOEXCEPT(m.clear());
ASSERT_SAME_TYPE(decltype(m.clear()), void);
m.clear();
assert(m.size() == 0);
}
{
using M = std::flat_set<int, std::greater<int>, std::deque<int, min_allocator<int>>>;
M m = {1, 2, 3, 4, 5};
assert(m.size() == 5);
ASSERT_NOEXCEPT(m.clear());
ASSERT_SAME_TYPE(decltype(m.clear()), void);
m.clear();
assert(m.size() == 0);
}
{
using M = std::flat_set<bool>;
M m = {true, false};
assert(m.size() == 2);
ASSERT_NOEXCEPT(m.clear());
ASSERT_SAME_TYPE(decltype(m.clear()), void);
m.clear();
assert(m.size() == 0);
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20

// <flat_set>

// key_compare key_comp() const;
// value_compare value_comp() const;

#include <cassert>
#include <flat_set>
#include <functional>

#include "test_macros.h"

int main(int, char**) {
{
using M = std::flat_set<int>;
using Comp = std::less<int>; // the default
M m = {1, 2, 3, 4};
ASSERT_SAME_TYPE(M::key_compare, Comp);
ASSERT_SAME_TYPE(M::value_compare, Comp);
ASSERT_SAME_TYPE(decltype(m.key_comp()), Comp);
ASSERT_SAME_TYPE(decltype(m.value_comp()), Comp);
Comp kc = m.key_comp();
assert(kc(1, 2));
assert(!kc(2, 1));
Comp vc = m.value_comp();
assert(vc(1, 2));
assert(!vc(2, 1));
}
{
using Comp = std::function<bool(int,int)>;
using M = std::flat_set<int, Comp>;
Comp comp = std::greater<int>();
M m({1, 2, 3, 4}, comp);
ASSERT_SAME_TYPE(M::key_compare, Comp);
ASSERT_SAME_TYPE(M::value_compare, Comp);
ASSERT_SAME_TYPE(decltype(m.key_comp()), Comp);
ASSERT_SAME_TYPE(decltype(m.value_comp()), Comp);
Comp kc = m.key_comp();
assert(!kc(1, 2));
assert(kc(2, 1));
Comp vc = m.value_comp();
assert(!vc(1, 2));
assert(vc(2, 1));
}
{
using Comp = std::less<>;
using M = std::flat_set<int, Comp>;
M m = {1, 2, 3, 4};
ASSERT_SAME_TYPE(M::key_compare, Comp);
ASSERT_SAME_TYPE(M::value_compare, Comp);
ASSERT_SAME_TYPE(decltype(m.key_comp()), Comp);
ASSERT_SAME_TYPE(decltype(m.value_comp()), Comp);
Comp kc = m.key_comp();
assert(kc(1, 2));
assert(!kc(2, 1));
Comp vc = m.value_comp();
assert(vc(1, 2));
assert(!vc(2, 1));
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20

// <flat_set>

// bool contains(const key_type& x) const;

#include <cassert>
#include <deque>
#include <flat_set>
#include <functional>
#include <utility>

#include "test_macros.h"
#include "min_allocator.h"

int main(int, char**)
{
{
using M = std::flat_set<int>;
M m = {1, 2, 4, 5, 8};
assert(!m.contains(0));
assert( m.contains(1));
assert( m.contains(2));
assert(!m.contains(3));
assert( m.contains(4));
assert( m.contains(5));
assert(!m.contains(6));
assert(!m.contains(7));
assert( std::as_const(m).contains(8));
assert(!std::as_const(m).contains(9));
m.clear();
assert(!m.contains(1));
}
{
using M = std::flat_set<int, std::greater<int>, std::deque<int, min_allocator<int>>>;
M m = {1, 2, 4, 5, 8};
assert(!m.contains(0));
assert( m.contains(1));
assert( m.contains(2));
assert(!m.contains(3));
assert( m.contains(4));
assert( m.contains(5));
assert(!m.contains(6));
assert(!m.contains(7));
assert( std::as_const(m).contains(8));
assert(!std::as_const(m).contains(9));
m.clear();
assert(!m.contains(1));
}
{
using M = std::flat_set<bool>;
M m = {true, false};
assert( m.contains(true));
assert( m.contains(false));
m = {true};
assert( m.contains(true));
assert(!m.contains(false));
m = {false};
assert(!std::as_const(m).contains(true));
assert( std::as_const(m).contains(false));
m.clear();
assert(!m.contains(true));
assert(!m.contains(false));
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20

// <flat_set>

// template<class K> bool contains(const K& x) const;

#include <cassert>
#include <flat_set>
#include <string>
#include <utility>

#include "test_macros.h"

struct StartsWith {
explicit StartsWith(char ch) : lower_(1, ch), upper_(1, ch+1) {}
StartsWith(const StartsWith&) = delete;
void operator=(const StartsWith&) = delete;
struct Less {
using is_transparent = void;
bool operator()(const std::string& a, const std::string& b) const { return a < b; }
bool operator()(const StartsWith& a, const std::string& b) const { return a.upper_ <= b; }
bool operator()(const std::string& a, const StartsWith& b) const { return a < b.lower_; }
};
private:
std::string lower_;
std::string upper_;
};

int main(int, char**)
{
{
using M = std::flat_set<std::string, StartsWith::Less>;
M m = { "alpha", "beta", "epsilon", "eta", "gamma" };
ASSERT_SAME_TYPE(decltype(m.contains(StartsWith('b'))), bool);
ASSERT_SAME_TYPE(decltype(std::as_const(m).contains(StartsWith('b'))), bool);
assert(m.contains("beta") == true);
assert(m.contains("delta") == false);
assert(m.contains("zeta") == false);
assert(m.contains(StartsWith('b')) == true);
assert(m.contains(StartsWith('d')) == false);
assert(m.contains(StartsWith('e')) == true);
assert(m.contains(StartsWith('z')) == false);
}
return 0;
}
Loading