Skip to content

Commit e657e64

Browse files
committed
[libc++] Split libc++-specific tests for the frozen headers
1 parent 33396d7 commit e657e64

File tree

223 files changed

+34382
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

223 files changed

+34382
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
9+
// <algorithm>
10+
11+
// template <class RandomAccessIterator>
12+
// void
13+
// random_shuffle(RandomAccessIterator first, RandomAccessIterator last);
14+
//
15+
// template <class RandomAccessIterator, class RandomNumberGenerator>
16+
// void
17+
// random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
18+
// RandomNumberGenerator& rand);
19+
20+
//
21+
// In C++17, random_shuffle has been removed.
22+
// However, for backwards compatibility, if _LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE
23+
// is defined before including <algorithm>, then random_shuffle will be restored.
24+
25+
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE
26+
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
27+
28+
#include <algorithm>
29+
#include <cstddef>
30+
#include <vector>
31+
32+
#include "test_macros.h"
33+
34+
struct gen
35+
{
36+
std::ptrdiff_t operator()(std::ptrdiff_t n)
37+
{
38+
return n-1;
39+
}
40+
};
41+
42+
43+
int main(int, char**)
44+
{
45+
std::vector<int> v;
46+
std::random_shuffle(v.begin(), v.end());
47+
gen r;
48+
std::random_shuffle(v.begin(), v.end(), r);
49+
50+
return 0;
51+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
9+
// std::sort
10+
11+
#include <algorithm>
12+
#include <iterator>
13+
#include <type_traits>
14+
#include <utility>
15+
16+
struct BadIter {
17+
struct Value {
18+
friend bool operator==(const Value& x, const Value& y);
19+
friend bool operator!=(const Value& x, const Value& y);
20+
friend bool operator< (const Value& x, const Value& y);
21+
friend bool operator<=(const Value& x, const Value& y);
22+
friend bool operator> (const Value& x, const Value& y);
23+
friend bool operator>=(const Value& x, const Value& y);
24+
friend void swap(Value, Value);
25+
};
26+
27+
using iterator_category = std::random_access_iterator_tag;
28+
using value_type = Value;
29+
using reference = Value&;
30+
using difference_type = long;
31+
using pointer = Value*;
32+
33+
Value operator*() const; // Not `Value&`.
34+
reference operator[](difference_type n) const;
35+
36+
BadIter& operator++();
37+
BadIter& operator--();
38+
BadIter operator++(int);
39+
BadIter operator--(int);
40+
41+
BadIter& operator+=(difference_type n);
42+
BadIter& operator-=(difference_type n);
43+
friend BadIter operator+(BadIter x, difference_type n);
44+
friend BadIter operator+(difference_type n, BadIter x);
45+
friend BadIter operator-(BadIter x, difference_type n);
46+
friend difference_type operator-(BadIter x, BadIter y);
47+
48+
friend bool operator==(const BadIter& x, const BadIter& y);
49+
friend bool operator!=(const BadIter& x, const BadIter& y);
50+
friend bool operator< (const BadIter& x, const BadIter& y);
51+
friend bool operator<=(const BadIter& x, const BadIter& y);
52+
friend bool operator> (const BadIter& x, const BadIter& y);
53+
friend bool operator>=(const BadIter& x, const BadIter& y);
54+
};
55+
56+
// Verify that iterators with incorrect `iterator_traits` are rejected. This protects against potential undefined
57+
// behavior when these iterators are passed to standard algorithms.
58+
void test() {
59+
std::sort(BadIter(), BadIter());
60+
//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}}
61+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
9+
// <algorithm>
10+
11+
// __half_positive divides an integer number by 2 as unsigned number for known types.
12+
// It can be an important optimization for lower bound, for example.
13+
14+
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
15+
16+
#include <__algorithm/half_positive.h>
17+
#include <cassert>
18+
#include <cstddef>
19+
#include <limits>
20+
21+
#include "test_macros.h"
22+
#include "user_defined_integral.h"
23+
24+
namespace {
25+
26+
template <class IntType, class UnderlyingType = IntType>
27+
TEST_CONSTEXPR bool test(IntType max_v = IntType(std::numeric_limits<UnderlyingType>::max())) {
28+
return std::__half_positive(max_v) == max_v / 2;
29+
}
30+
31+
} // namespace
32+
33+
int main(int, char**)
34+
{
35+
{
36+
assert(test<char>());
37+
assert(test<int>());
38+
assert(test<long>());
39+
assert((test<UserDefinedIntegral<int>, int>()));
40+
assert(test<std::size_t>());
41+
#if !defined(TEST_HAS_NO_INT128)
42+
assert(test<__int128_t>());
43+
#endif // !defined(TEST_HAS_NO_INT128)
44+
}
45+
46+
#if TEST_STD_VER >= 11
47+
{
48+
static_assert(test<char>(), "");
49+
static_assert(test<int>(), "");
50+
static_assert(test<long>(), "");
51+
static_assert(test<std::size_t>(), "");
52+
#if !defined(TEST_HAS_NO_INT128)
53+
static_assert(test<__int128_t>(), "");
54+
#endif // !defined(TEST_HAS_NO_INT128)
55+
}
56+
#endif // TEST_STD_VER >= 11
57+
58+
return 0;
59+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
9+
// <algorithm>
10+
11+
// REQUIRES: libcpp-pstl-backend-libdispatch
12+
13+
// __chunk_partitions __partition_chunks(ptrdiff_t);
14+
15+
#include <__pstl/backends/libdispatch.h>
16+
#include <cassert>
17+
#include <cstddef>
18+
19+
int main(int, char**) {
20+
{
21+
auto chunks = std::__pstl::__libdispatch::__partition_chunks(0);
22+
assert(chunks.__chunk_count_ == 1);
23+
assert(chunks.__first_chunk_size_ == 0);
24+
assert(chunks.__chunk_size_ == 0);
25+
}
26+
27+
{
28+
auto chunks = std::__pstl::__libdispatch::__partition_chunks(1);
29+
assert(chunks.__chunk_count_ == 1);
30+
assert(chunks.__first_chunk_size_ == 1);
31+
assert(chunks.__chunk_size_ == 1);
32+
}
33+
34+
for (std::ptrdiff_t i = 2; i != 2ll << 20; ++i) {
35+
auto chunks = std::__pstl::__libdispatch::__partition_chunks(i);
36+
assert(chunks.__chunk_count_ >= 1);
37+
assert(chunks.__chunk_count_ <= i);
38+
assert((chunks.__chunk_count_ - 1) * chunks.__chunk_size_ + chunks.__first_chunk_size_ == i);
39+
}
40+
return 0;
41+
}

0 commit comments

Comments
 (0)