Skip to content

[libc++][C++03] Split libc++-specific tests for the frozen headers #144093

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 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <algorithm>

// template <class RandomAccessIterator>
// void
// random_shuffle(RandomAccessIterator first, RandomAccessIterator last);
//
// template <class RandomAccessIterator, class RandomNumberGenerator>
// void
// random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
// RandomNumberGenerator& rand);

//
// In C++17, random_shuffle has been removed.
// However, for backwards compatibility, if _LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE
// is defined before including <algorithm>, then random_shuffle will be restored.

// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS

#include <algorithm>
#include <cstddef>
#include <vector>

#include "test_macros.h"

struct gen
{
std::ptrdiff_t operator()(std::ptrdiff_t n)
{
return n-1;
}
};


int main(int, char**)
{
std::vector<int> v;
std::random_shuffle(v.begin(), v.end());
gen r;
std::random_shuffle(v.begin(), v.end(), r);

return 0;
}
61 changes: 61 additions & 0 deletions libcxx/test/libcxx-03/algorithms/bad_iterator_traits.verify.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// std::sort

#include <algorithm>
#include <iterator>
#include <type_traits>
#include <utility>

struct BadIter {
struct Value {
friend bool operator==(const Value& x, const Value& y);
friend bool operator!=(const Value& x, const Value& y);
friend bool operator< (const Value& x, const Value& y);
friend bool operator<=(const Value& x, const Value& y);
friend bool operator> (const Value& x, const Value& y);
friend bool operator>=(const Value& x, const Value& y);
friend void swap(Value, Value);
};

using iterator_category = std::random_access_iterator_tag;
using value_type = Value;
using reference = Value&;
using difference_type = long;
using pointer = Value*;

Value operator*() const; // Not `Value&`.
reference operator[](difference_type n) const;

BadIter& operator++();
BadIter& operator--();
BadIter operator++(int);
BadIter operator--(int);

BadIter& operator+=(difference_type n);
BadIter& operator-=(difference_type n);
friend BadIter operator+(BadIter x, difference_type n);
friend BadIter operator+(difference_type n, BadIter x);
friend BadIter operator-(BadIter x, difference_type n);
friend difference_type operator-(BadIter x, BadIter y);

friend bool operator==(const BadIter& x, const BadIter& y);
friend bool operator!=(const BadIter& x, const BadIter& y);
friend bool operator< (const BadIter& x, const BadIter& y);
friend bool operator<=(const BadIter& x, const BadIter& y);
friend bool operator> (const BadIter& x, const BadIter& y);
friend bool operator>=(const BadIter& x, const BadIter& y);
};

// Verify that iterators with incorrect `iterator_traits` are rejected. This protects against potential undefined
// behavior when these iterators are passed to standard algorithms.
void test() {
std::sort(BadIter(), BadIter());
//expected-error-re@*:* {{static assertion failed {{.*}}It looks like your iterator's `iterator_traits<It>::reference` does not match the return type of dereferencing the iterator}}
}
59 changes: 59 additions & 0 deletions libcxx/test/libcxx-03/algorithms/half_positive.pass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <algorithm>

// __half_positive divides an integer number by 2 as unsigned number for known types.
// It can be an important optimization for lower bound, for example.

// XFAIL: FROZEN-CXX03-HEADERS-FIXME

#include <__algorithm/half_positive.h>
#include <cassert>
#include <cstddef>
#include <limits>

#include "test_macros.h"
#include "user_defined_integral.h"

namespace {

template <class IntType, class UnderlyingType = IntType>
TEST_CONSTEXPR bool test(IntType max_v = IntType(std::numeric_limits<UnderlyingType>::max())) {
return std::__half_positive(max_v) == max_v / 2;
}

} // namespace

int main(int, char**)
{
{
assert(test<char>());
assert(test<int>());
assert(test<long>());
assert((test<UserDefinedIntegral<int>, int>()));
assert(test<std::size_t>());
#if !defined(TEST_HAS_NO_INT128)
assert(test<__int128_t>());
#endif // !defined(TEST_HAS_NO_INT128)
}

#if TEST_STD_VER >= 11
{
static_assert(test<char>(), "");
static_assert(test<int>(), "");
static_assert(test<long>(), "");
static_assert(test<std::size_t>(), "");
#if !defined(TEST_HAS_NO_INT128)
static_assert(test<__int128_t>(), "");
#endif // !defined(TEST_HAS_NO_INT128)
}
#endif // TEST_STD_VER >= 11

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <algorithm>

// REQUIRES: libcpp-pstl-backend-libdispatch

// __chunk_partitions __partition_chunks(ptrdiff_t);

#include <__pstl/backends/libdispatch.h>
#include <cassert>
#include <cstddef>

int main(int, char**) {
{
auto chunks = std::__pstl::__libdispatch::__partition_chunks(0);
assert(chunks.__chunk_count_ == 1);
assert(chunks.__first_chunk_size_ == 0);
assert(chunks.__chunk_size_ == 0);
}

{
auto chunks = std::__pstl::__libdispatch::__partition_chunks(1);
assert(chunks.__chunk_count_ == 1);
assert(chunks.__first_chunk_size_ == 1);
assert(chunks.__chunk_size_ == 1);
}

for (std::ptrdiff_t i = 2; i != 2ll << 20; ++i) {
auto chunks = std::__pstl::__libdispatch::__partition_chunks(i);
assert(chunks.__chunk_count_ >= 1);
assert(chunks.__chunk_count_ <= i);
assert((chunks.__chunk_count_ - 1) * chunks.__chunk_size_ + chunks.__first_chunk_size_ == i);
}
return 0;
}
Loading
Loading