[libc++] Refactor tests to remove uses of std::rand()

This allows running these tests on systems that do not support std::rand().
This commit is contained in:
Louis Dionne
2020-10-28 15:04:39 -04:00
parent 0e94836989
commit 63aeadb484
33 changed files with 776 additions and 811 deletions

View File

@@ -17,7 +17,6 @@
#include <cassert>
#include <climits>
#include <cstdint>
#include <cstdlib> // for rand()
#include <type_traits>
#include "test_macros.h"
@@ -93,9 +92,9 @@ constexpr bool do_test(int = 0)
return accumulate;
}
int main(int, char**)
int main(int argc, char**)
{
auto non_cce = std::rand(); // a value that can't possibly be constexpr
int non_cce = argc; // a value that can't possibly be constexpr
static_assert(do_test<signed char>(), "");
static_assert(do_test<short>(), "");

View File

@@ -16,7 +16,6 @@
#include <cassert>
#include <climits>
#include <cstdint>
#include <cstdlib>
#include <type_traits>
#include "test_macros.h"
@@ -90,9 +89,9 @@ constexpr bool do_test(int = 0)
return accumulate;
}
int main(int, char**)
int main(int argc, char**)
{
auto non_cce = std::rand(); // a value that can't possibly be constexpr
int non_cce = argc; // a value that can't possibly be constexpr
static_assert(do_test<signed char>(), "");
static_assert(do_test<short>(), "");

View File

@@ -9,29 +9,25 @@
// test bool all() const;
#include <bitset>
#include <type_traits>
#include <cassert>
#include <cstddef>
#include "test_macros.h"
template <std::size_t N>
void test_all()
{
void test_all() {
std::bitset<N> v;
v.reset();
assert(v.all() == (N == 0));
v.set();
assert(v.all() == true);
const bool greater_than_1 = std::integral_constant<bool, (N > 1)>::value; // avoid compiler warnings
if (greater_than_1)
{
if (v.size() > 1) {
v[N/2] = false;
assert(v.all() == false);
}
}
int main(int, char**)
{
int main(int, char**) {
test_all<0>();
test_all<1>();
test_all<31>();
@@ -42,5 +38,5 @@ int main(int, char**)
test_all<65>();
test_all<1000>();
return 0;
return 0;
}

View File

@@ -9,22 +9,19 @@
// test bool any() const;
#include <bitset>
#include <type_traits>
#include <cassert>
#include <cstddef>
#include "test_macros.h"
template <std::size_t N>
void test_any()
{
void test_any() {
std::bitset<N> v;
v.reset();
assert(v.any() == false);
v.set();
assert(v.any() == (N != 0));
const bool greater_than_1 = std::integral_constant<bool, (N > 1)>::value; // avoid compiler warnings
if (greater_than_1)
{
if (v.size() > 1) {
v[N/2] = false;
assert(v.any() == true);
v.reset();
@@ -33,8 +30,7 @@ void test_any()
}
}
int main(int, char**)
{
int main(int, char**) {
test_any<0>();
test_any<1>();
test_any<31>();
@@ -45,5 +41,5 @@ int main(int, char**)
test_any<65>();
test_any<1000>();
return 0;
return 0;
}

View File

@@ -0,0 +1,177 @@
//===----------------------------------------------------------------------===//
//
// 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 LIBCPP_TEST_BITSET_TEST_CASES_H
#define LIBCPP_TEST_BITSET_TEST_CASES_H
#include <bitset>
#include <string>
#include <vector>
template <int N>
std::vector<std::bitset<N> > get_test_cases();
template <>
inline std::vector<std::bitset<0> > get_test_cases<0>() {
std::vector<std::bitset<0> > cases;
cases.push_back(std::bitset<0>());
return cases;
}
template <>
inline std::vector<std::bitset<1> > get_test_cases<1>() {
std::vector<std::bitset<1> > cases;
cases.push_back(std::bitset<1>("0"));
cases.push_back(std::bitset<1>("1"));
return cases;
}
template <>
inline std::vector<std::bitset<2> > get_test_cases<2>() {
std::vector<std::bitset<2> > cases;
cases.push_back(std::bitset<2>("00"));
cases.push_back(std::bitset<2>("01"));
cases.push_back(std::bitset<2>("10"));
cases.push_back(std::bitset<2>("11"));
return cases;
}
template <>
inline std::vector<std::bitset<31> > get_test_cases<31>() {
std::vector<std::bitset<31> > cases;
cases.push_back(std::bitset<31>("0000000000000000000000000000000"));
cases.push_back(std::bitset<31>("0000000000000000000000000000001"));
cases.push_back(std::bitset<31>("1000000000000000000000000000000"));
cases.push_back(std::bitset<31>("1000000000000000000000000000001"));
cases.push_back(std::bitset<31>("1000000000000000000001000000001"));
cases.push_back(std::bitset<31>("0000000000000000111111111111111"));
cases.push_back(std::bitset<31>("1000000000000000111111111111111"));
cases.push_back(std::bitset<31>("1111111111111111000000000000000"));
cases.push_back(std::bitset<31>("1111111111111111000000000000001"));
cases.push_back(std::bitset<31>("1010101010101010101010101010101"));
cases.push_back(std::bitset<31>("0101010101010101010101010101010"));
cases.push_back(std::bitset<31>("1111111111111111111111111111111"));
return cases;
}
template <>
inline std::vector<std::bitset<32> > get_test_cases<32>() {
std::vector<std::bitset<32> > cases;
cases.push_back(std::bitset<32>("00000000000000000000000000000000"));
cases.push_back(std::bitset<32>("00000000000000000000000000000001"));
cases.push_back(std::bitset<32>("10000000000000000000000000000000"));
cases.push_back(std::bitset<32>("10000000000000000000000000000001"));
cases.push_back(std::bitset<32>("10000000000000000000111000000001"));
cases.push_back(std::bitset<32>("00000000000000001111111111111111"));
cases.push_back(std::bitset<32>("10000000000000001111111111111111"));
cases.push_back(std::bitset<32>("11111111111111110000000000000000"));
cases.push_back(std::bitset<32>("11111111111111110000000000000001"));
cases.push_back(std::bitset<32>("10101010101010101010101010101010"));
cases.push_back(std::bitset<32>("01010101010101010101010101010101"));
cases.push_back(std::bitset<32>("11111111111111111111111111111111"));
return cases;
}
template <>
inline std::vector<std::bitset<33> > get_test_cases<33>() {
std::vector<std::bitset<33> > cases;
cases.push_back(std::bitset<33>("000000000000000000000000000000000"));
cases.push_back(std::bitset<33>("000000000000000000000000000000001"));
cases.push_back(std::bitset<33>("100000000000000000000000000000000"));
cases.push_back(std::bitset<33>("100000000000000000000000000000001"));
cases.push_back(std::bitset<33>("100000000000000000001110000000001"));
cases.push_back(std::bitset<33>("000000000000000011111111111111111"));
cases.push_back(std::bitset<33>("100000000000000011111111111111111"));
cases.push_back(std::bitset<33>("111111111111111100000000000000000"));
cases.push_back(std::bitset<33>("111111111111111100000000000000001"));
cases.push_back(std::bitset<33>("101010101010101010101010101010101"));
cases.push_back(std::bitset<33>("010101010101010101010101010101010"));
cases.push_back(std::bitset<33>("111111111111111111111111111111111"));
return cases;
}
template <>
inline std::vector<std::bitset<63> > get_test_cases<63>() {
std::vector<std::bitset<63> > cases;
cases.push_back(std::bitset<63>("000000000000000000000000000000000000000000000000000000000000000"));
cases.push_back(std::bitset<63>("000000000000000000000000000000000000000000000000000000000000001"));
cases.push_back(std::bitset<63>("100000000000000000000000000000000000000000000000000000000000000"));
cases.push_back(std::bitset<63>("100000000000000000000000000000000000000000000000000000000000001"));
cases.push_back(std::bitset<63>("100000000000000000000000001111100000000000000000000000000000001"));
cases.push_back(std::bitset<63>("000000000000000000000000000000001111111111111111111111111111111"));
cases.push_back(std::bitset<63>("100000000000000000000000000000001111111111111111111111111111111"));
cases.push_back(std::bitset<63>("111111111111111111111111111111110000000000000000000000000000000"));
cases.push_back(std::bitset<63>("111111111111111111111111111111110000000000000000000000000000001"));
cases.push_back(std::bitset<63>("101010101010101010101010101010101010101010101010101010101010101"));
cases.push_back(std::bitset<63>("010101010101010101010101010101010101010101010101010101010101010"));
cases.push_back(std::bitset<63>("111111111111111111111111111111111111111111111111111111111111111"));
return cases;
}
template <>
inline std::vector<std::bitset<64> > get_test_cases<64>() {
std::vector<std::bitset<64> > cases;
cases.push_back(std::bitset<64>("0000000000000000000000000000000000000000000000000000000000000000"));
cases.push_back(std::bitset<64>("0000000000000000000000000000000000000000000000000000000000000001"));
cases.push_back(std::bitset<64>("1000000000000000000000000000000000000000000000000000000000000000"));
cases.push_back(std::bitset<64>("1000000000000000000000000000000000000000000000000000000000000001"));
cases.push_back(std::bitset<64>("1000000000000000000000000011111000000000000000000000000000000001"));
cases.push_back(std::bitset<64>("0000000000000000000000000000000011111111111111111111111111111111"));
cases.push_back(std::bitset<64>("1000000000000000000000000000000011111111111111111111111111111111"));
cases.push_back(std::bitset<64>("1111111111111111111111111111111100000000000000000000000000000000"));
cases.push_back(std::bitset<64>("1111111111111111111111111111111100000000000000000000000000000001"));
cases.push_back(std::bitset<64>("1010101010101010101010101010101010101010101010101010101010101010"));
cases.push_back(std::bitset<64>("0101010101010101010101010101010101010101010101010101010101010101"));
cases.push_back(std::bitset<64>("1111111111111111111111111111111111111111111111111111111111111111"));
return cases;
}
template <>
inline std::vector<std::bitset<65> > get_test_cases<65>() {
std::vector<std::bitset<65> > cases;
cases.push_back(std::bitset<65>("00000000000000000000000000000000000000000000000000000000000000000"));
cases.push_back(std::bitset<65>("00000000000000000000000000000000000000000000000000000000000000001"));
cases.push_back(std::bitset<65>("10000000000000000000000000000000000000000000000000000000000000000"));
cases.push_back(std::bitset<65>("10000000000000000000000000000000000000000000000000000000000000001"));
cases.push_back(std::bitset<65>("10000000000000000000000000011111000000000000000000000000000000001"));
cases.push_back(std::bitset<65>("00000000000000000000000000000000011111111111111111111111111111111"));
cases.push_back(std::bitset<65>("10000000000000000000000000000000011111111111111111111111111111111"));
cases.push_back(std::bitset<65>("11111111111111111111111111111111000000000000000000000000000000000"));
cases.push_back(std::bitset<65>("11111111111111111111111111111111000000000000000000000000000000001"));
cases.push_back(std::bitset<65>("10101010101010101010101010101010101010101010101010101010101010101"));
cases.push_back(std::bitset<65>("01010101010101010101010101010101010101010101010101010101010101010"));
cases.push_back(std::bitset<65>("11111111111111111111111111111111111111111111111111111111111111111"));
return cases;
}
inline std::string str_repeat(std::string s, unsigned int n) {
std::string res = s;
for (; n != 0; --n)
res += s;
return res;
}
template <>
inline std::vector<std::bitset<1000> > get_test_cases<1000>() {
std::vector<std::bitset<1000> > cases;
cases.push_back(std::bitset<1000>(std::string(1000, '0')));
cases.push_back(std::bitset<1000>(std::string(999, '0') + std::string(1, '1')));
cases.push_back(std::bitset<1000>(std::string(1, '1') + std::string(999, '0')));
cases.push_back(std::bitset<1000>(std::string(1, '1') + std::string(998, '0') + std::string(1, '1')));
cases.push_back(std::bitset<1000>(std::string(1, '1') + std::string(400, '0') + std::string(99, '1') + std::string(499, '0') + std::string(1, '1')));
cases.push_back(std::bitset<1000>(std::string(500, '0') + std::string(500, '1')));
cases.push_back(std::bitset<1000>(std::string(1, '1') + std::string(499, '0') + std::string(500, '1')));
cases.push_back(std::bitset<1000>(std::string(500, '1') + std::string(500, '0')));
cases.push_back(std::bitset<1000>(std::string(500, '1') + std::string(499, '0') + std::string(1, '1')));
cases.push_back(std::bitset<1000>(str_repeat("10", 500)));
cases.push_back(std::bitset<1000>(str_repeat("01", 500)));
cases.push_back(std::bitset<1000>(std::string(1000, '1')));
return cases;
}
#endif // !LIBCPP_TEST_BITSET_TEST_CASES_H

View File

@@ -9,41 +9,28 @@
// test size_t count() const;
#include <bitset>
#include <cstdlib>
#include <cassert>
#include <cstddef>
#include <vector>
#include "bitset_test_cases.h"
#include "test_macros.h"
#if defined(TEST_COMPILER_CLANG)
#pragma clang diagnostic ignored "-Wtautological-compare"
#elif defined(TEST_COMPILER_C1XX)
#pragma warning(disable: 6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed.
#endif
template <std::size_t N>
std::bitset<N>
make_bitset()
{
std::bitset<N> v;
for (std::size_t i = 0; i < N; ++i)
v[i] = static_cast<bool>(std::rand() & 1);
return v;
void test_count() {
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
for (std::size_t c = 0; c != cases.size(); ++c) {
const std::bitset<N> v = cases[c];
std::size_t c1 = v.count();
std::size_t c2 = 0;
for (std::size_t i = 0; i < v.size(); ++i)
if (v[i])
++c2;
assert(c1 == c2);
}
}
template <std::size_t N>
void test_count()
{
const std::bitset<N> v = make_bitset<N>();
std::size_t c1 = v.count();
std::size_t c2 = 0;
for (std::size_t i = 0; i < N; ++i)
if (v[i])
++c2;
assert(c1 == c2);
}
int main(int, char**)
{
int main(int, char**) {
test_count<0>();
test_count<1>();
test_count<31>();
@@ -54,5 +41,5 @@ int main(int, char**)
test_count<65>();
test_count<1000>();
return 0;
return 0;
}

View File

@@ -9,39 +9,26 @@
// test bitset<N>& flip();
#include <bitset>
#include <cstdlib>
#include <cassert>
#include <cstddef>
#include <vector>
#include "bitset_test_cases.h"
#include "test_macros.h"
#if defined(TEST_COMPILER_CLANG)
#pragma clang diagnostic ignored "-Wtautological-compare"
#elif defined(TEST_COMPILER_C1XX)
#pragma warning(disable: 6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed.
#endif
template <std::size_t N>
std::bitset<N>
make_bitset()
{
std::bitset<N> v;
for (std::size_t i = 0; i < N; ++i)
v[i] = static_cast<bool>(std::rand() & 1);
return v;
void test_flip_all() {
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
for (std::size_t c = 0; c != cases.size(); ++c) {
std::bitset<N> v1 = cases[c];
std::bitset<N> v2 = v1;
v2.flip();
for (std::size_t i = 0; i < v1.size(); ++i)
assert(v2[i] == ~v1[i]);
}
}
template <std::size_t N>
void test_flip_all()
{
std::bitset<N> v1 = make_bitset<N>();
std::bitset<N> v2 = v1;
v2.flip();
for (std::size_t i = 0; i < N; ++i)
assert(v2[i] == ~v1[i]);
}
int main(int, char**)
{
int main(int, char**) {
test_flip_all<0>();
test_flip_all<1>();
test_flip_all<31>();
@@ -52,5 +39,5 @@ int main(int, char**)
test_flip_all<65>();
test_flip_all<1000>();
return 0;
return 0;
}

View File

@@ -0,0 +1,34 @@
//===----------------------------------------------------------------------===//
//
// 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: no-exceptions
// test bitset<N>& flip(size_t pos);
// Make sure we throw std::out_of_range when calling flip() on an OOB index.
#include <bitset>
#include <cassert>
#include <stdexcept>
int main(int, char**) {
{
std::bitset<0> v;
try { v.flip(0); assert(false); } catch (std::out_of_range const&) { }
}
{
std::bitset<1> v("0");
try { v.flip(2); assert(false); } catch (std::out_of_range const&) { }
}
{
std::bitset<10> v("0000000000");
try { v.flip(10); assert(false); } catch (std::out_of_range const&) { }
}
return 0;
}

View File

@@ -9,66 +9,41 @@
// test bitset<N>& flip(size_t pos);
#include <bitset>
#include <cstdlib>
#include <cassert>
#include <stdexcept>
#include <cstddef>
#include <vector>
#include "bitset_test_cases.h"
#include "test_macros.h"
#if defined(TEST_COMPILER_C1XX)
#pragma warning(disable: 6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed.
#endif
template <std::size_t N>
std::bitset<N>
make_bitset()
{
std::bitset<N> v;
for (std::size_t i = 0; i < N; ++i)
v[i] = static_cast<bool>(std::rand() & 1);
return v;
}
template <std::size_t N>
void test_flip_one(bool test_throws)
{
std::bitset<N> v = make_bitset<N>();
#ifdef TEST_HAS_NO_EXCEPTIONS
if (test_throws) return;
#else
try
{
#endif
v.flip(50);
bool b = v[50];
if (50 >= v.size())
assert(false);
assert(v[50] == b);
v.flip(50);
assert(v[50] != b);
v.flip(50);
assert(v[50] == b);
assert(!test_throws);
#ifndef TEST_HAS_NO_EXCEPTIONS
void test_flip_one() {
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
for (std::size_t c = 0; c != cases.size(); ++c) {
std::bitset<N> v = cases[c];
if (v.size() > 0) {
std::size_t middle = v.size() / 2;
v.flip(middle);
bool b = v[middle];
assert(v[middle] == b);
v.flip(middle);
assert(v[middle] != b);
v.flip(middle);
assert(v[middle] == b);
}
}
catch (std::out_of_range&)
{
assert(test_throws);
}
#endif
}
int main(int, char**)
{
test_flip_one<0>(true);
test_flip_one<1>(true);
test_flip_one<31>(true);
test_flip_one<32>(true);
test_flip_one<33>(true);
test_flip_one<63>(false);
test_flip_one<64>(false);
test_flip_one<65>(false);
test_flip_one<1000>(false);
int main(int, char**) {
test_flip_one<0>();
test_flip_one<1>();
test_flip_one<31>();
test_flip_one<32>();
test_flip_one<33>();
test_flip_one<63>();
test_flip_one<64>();
test_flip_one<65>();
test_flip_one<1000>();
return 0;
return 0;
}

View File

@@ -9,68 +9,52 @@
// test bitset<N>::reference operator[](size_t pos);
#include <bitset>
#include <type_traits>
#include <cstdlib>
#include <cassert>
#include <cstddef>
#include <vector>
#include "bitset_test_cases.h"
#include "test_macros.h"
#if defined(TEST_COMPILER_CLANG)
#pragma clang diagnostic ignored "-Wtautological-compare"
#elif defined(TEST_COMPILER_C1XX)
#pragma warning(disable: 6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed.
#endif
template <std::size_t N>
std::bitset<N>
make_bitset()
{
std::bitset<N> v;
for (std::size_t i = 0; i < N; ++i)
v[i] = static_cast<bool>(std::rand() & 1);
return v;
}
template <std::size_t N>
void test_index_const()
{
std::bitset<N> v1 = make_bitset<N>();
const bool greater_than_0 = std::integral_constant<bool, (N > 0)>::value; // avoid compiler warnings
if (greater_than_0)
{
assert(v1[N/2] == v1.test(N/2));
typename std::bitset<N>::reference r = v1[N/2];
assert(r == v1.test(N/2));
typename std::bitset<N>::reference r2 = v1[N/2];
r = r2;
assert(r == v1.test(N/2));
r = false;
assert(r == false);
assert(v1.test(N/2) == false);
r = true;
assert(r == true);
assert(v1.test(N/2) == true);
bool b = ~r;
assert(r == true);
assert(v1.test(N/2) == true);
assert(b == false);
r.flip();
assert(r == false);
assert(v1.test(N/2) == false);
void test_index() {
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
for (std::size_t c = 0; c != cases.size(); ++c) {
std::bitset<N> v1 = cases[c];
if (v1.size() > 0) {
assert(v1[N/2] == v1.test(N/2));
typename std::bitset<N>::reference r = v1[N/2];
assert(r == v1.test(N/2));
typename std::bitset<N>::reference r2 = v1[N/2];
r = r2;
assert(r == v1.test(N/2));
r = false;
assert(r == false);
assert(v1.test(N/2) == false);
r = true;
assert(r == true);
assert(v1.test(N/2) == true);
bool b = ~r;
assert(r == true);
assert(v1.test(N/2) == true);
assert(b == false);
r.flip();
assert(r == false);
assert(v1.test(N/2) == false);
}
}
}
int main(int, char**)
{
test_index_const<0>();
test_index_const<1>();
test_index_const<31>();
test_index_const<32>();
test_index_const<33>();
test_index_const<63>();
test_index_const<64>();
test_index_const<65>();
test_index_const<1000>();
int main(int, char**) {
test_index<0>();
test_index<1>();
test_index<31>();
test_index<32>();
test_index<33>();
test_index<63>();
test_index<64>();
test_index<65>();
test_index<1000>();
return 0;
return 0;
}

View File

@@ -9,41 +9,25 @@
// test constexpr bool operator[](size_t pos) const;
#include <bitset>
#include <type_traits>
#include <cstdlib>
#include <cassert>
#include <cstddef>
#include <vector>
#include "bitset_test_cases.h"
#include "test_macros.h"
#if defined(TEST_COMPILER_CLANG)
#pragma clang diagnostic ignored "-Wtautological-compare"
#elif defined(TEST_COMPILER_C1XX)
#pragma warning(disable: 6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed.
#endif
template <std::size_t N>
std::bitset<N>
make_bitset()
{
std::bitset<N> v;
for (std::size_t i = 0; i < N; ++i)
v[i] = static_cast<bool>(std::rand() & 1);
return v;
}
template <std::size_t N>
void test_index_const()
{
const std::bitset<N> v1 = make_bitset<N>();
const bool greater_than_0 = std::integral_constant<bool, (N > 0)>::value; // avoid compiler warnings
if (greater_than_0)
{
assert(v1[N/2] == v1.test(N/2));
void test_index_const() {
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
for (std::size_t c = 0; c != cases.size(); ++c) {
std::bitset<N> const v = cases[c];
if (v.size() > 0) {
assert(v[N/2] == v.test(N/2));
}
}
}
int main(int, char**)
{
int main(int, char**) {
test_index_const<0>();
test_index_const<1>();
test_index_const<31>();
@@ -54,5 +38,5 @@ int main(int, char**)
test_index_const<65>();
test_index_const<1000>();
return 0;
return 0;
}

View File

@@ -9,40 +9,26 @@
// test bitset<N> operator<<(size_t pos) const;
#include <bitset>
#include <cstdlib>
#include <cassert>
#include <cstddef>
#include <vector>
#include "bitset_test_cases.h"
#include "test_macros.h"
#if defined(TEST_COMPILER_CLANG)
#pragma clang diagnostic ignored "-Wtautological-compare"
#elif defined(TEST_COMPILER_C1XX)
#pragma warning(disable: 6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed.
#endif
template <std::size_t N>
std::bitset<N>
make_bitset()
{
std::bitset<N> v;
for (std::size_t i = 0; i < N; ++i)
v[i] = static_cast<bool>(std::rand() & 1);
return v;
}
template <std::size_t N>
void test_left_shift()
{
for (std::size_t s = 0; s <= N+1; ++s)
{
std::bitset<N> v1 = make_bitset<N>();
std::bitset<N> v2 = v1;
assert((v1 <<= s) == (v2 << s));
void test_left_shift() {
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
for (std::size_t c = 0; c != cases.size(); ++c) {
for (std::size_t s = 0; s <= N+1; ++s) {
std::bitset<N> v1 = cases[c];
std::bitset<N> v2 = v1;
assert((v1 <<= s) == (v2 << s));
}
}
}
int main(int, char**)
{
int main(int, char**) {
test_left_shift<0>();
test_left_shift<1>();
test_left_shift<31>();
@@ -53,5 +39,5 @@ int main(int, char**)
test_left_shift<65>();
test_left_shift<1000>();
return 0;
return 0;
}

View File

@@ -9,45 +9,31 @@
// test bitset<N>& operator<<=(size_t pos);
#include <bitset>
#include <cstdlib>
#include <cassert>
#include <cstddef>
#include <vector>
#include "bitset_test_cases.h"
#include "test_macros.h"
#if defined(TEST_COMPILER_CLANG)
#pragma clang diagnostic ignored "-Wtautological-compare"
#elif defined(TEST_COMPILER_C1XX)
#pragma warning(disable: 6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed.
#endif
template <std::size_t N>
std::bitset<N>
make_bitset()
{
std::bitset<N> v;
for (std::size_t i = 0; i < N; ++i)
v[i] = static_cast<bool>(std::rand() & 1);
return v;
}
template <std::size_t N>
void test_left_shift()
{
for (std::size_t s = 0; s <= N+1; ++s)
{
std::bitset<N> v1 = make_bitset<N>();
std::bitset<N> v2 = v1;
v1 <<= s;
for (std::size_t i = 0; i < N; ++i)
if (i < s)
assert(v1[i] == 0);
else
assert(v1[i] == v2[i-s]);
void test_left_shift() {
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
for (std::size_t c = 0; c != cases.size(); ++c) {
for (std::size_t s = 0; s <= N+1; ++s) {
std::bitset<N> v1 = cases[c];
std::bitset<N> v2 = v1;
v1 <<= s;
for (std::size_t i = 0; i < v1.size(); ++i)
if (i < s)
assert(v1[i] == 0);
else
assert(v1[i] == v2[i-s]);
}
}
}
int main(int, char**)
{
int main(int, char**) {
test_left_shift<0>();
test_left_shift<1>();
test_left_shift<31>();
@@ -58,5 +44,5 @@ int main(int, char**)
test_left_shift<65>();
test_left_shift<1000>();
return 0;
return 0;
}

View File

@@ -9,22 +9,19 @@
// test bool none() const;
#include <bitset>
#include <type_traits>
#include <cassert>
#include <cstddef>
#include "test_macros.h"
template <std::size_t N>
void test_none()
{
void test_none() {
std::bitset<N> v;
v.reset();
assert(v.none() == true);
v.set();
assert(v.none() == (N == 0));
const bool greater_than_1 = std::integral_constant<bool, (N > 1)>::value; // avoid compiler warnings
if (greater_than_1)
{
if (v.size() > 1) {
v[N/2] = false;
assert(v.none() == false);
v.reset();
@@ -33,8 +30,7 @@ void test_none()
}
}
int main(int, char**)
{
int main(int, char**) {
test_none<0>();
test_none<1>();
test_none<31>();
@@ -45,5 +41,5 @@ int main(int, char**)
test_none<65>();
test_none<1000>();
return 0;
return 0;
}

View File

@@ -9,38 +9,25 @@
// test bitset<N> operator~() const;
#include <bitset>
#include <cstdlib>
#include <cassert>
#include <cstddef>
#include <vector>
#include "bitset_test_cases.h"
#include "test_macros.h"
#if defined(TEST_COMPILER_CLANG)
#pragma clang diagnostic ignored "-Wtautological-compare"
#elif defined(TEST_COMPILER_C1XX)
#pragma warning(disable: 6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed.
#endif
template <std::size_t N>
std::bitset<N>
make_bitset()
{
std::bitset<N> v;
for (std::size_t i = 0; i < N; ++i)
v[i] = static_cast<bool>(std::rand() & 1);
return v;
void test_not_all() {
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
for (std::size_t c = 0; c != cases.size(); ++c) {
std::bitset<N> v1 = cases[c];
std::bitset<N> v2 = ~v1;
for (std::size_t i = 0; i < v1.size(); ++i)
assert(v2[i] == ~v1[i]);
}
}
template <std::size_t N>
void test_not_all()
{
std::bitset<N> v1 = make_bitset<N>();
std::bitset<N> v2 = ~v1;
for (std::size_t i = 0; i < N; ++i)
assert(v2[i] == ~v1[i]);
}
int main(int, char**)
{
int main(int, char**) {
test_not_all<0>();
test_not_all<1>();
test_not_all<31>();
@@ -51,5 +38,5 @@ int main(int, char**)
test_not_all<65>();
test_not_all<1000>();
return 0;
return 0;
}

View File

@@ -9,40 +9,29 @@
// test bitset<N>& operator&=(const bitset<N>& rhs);
#include <bitset>
#include <cstdlib>
#include <cassert>
#include <cstddef>
#include <vector>
#include "bitset_test_cases.h"
#include "test_macros.h"
#if defined(TEST_COMPILER_CLANG)
#pragma clang diagnostic ignored "-Wtautological-compare"
#elif defined(TEST_COMPILER_C1XX)
#pragma warning(disable: 6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed.
#endif
template <std::size_t N>
std::bitset<N>
make_bitset()
{
std::bitset<N> v;
for (std::size_t i = 0; i < N; ++i)
v[i] = static_cast<bool>(std::rand() & 1);
return v;
void test_op_and_eq() {
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
for (std::size_t c1 = 0; c1 != cases.size(); ++c1) {
for (std::size_t c2 = 0; c2 != cases.size(); ++c2) {
std::bitset<N> v1 = cases[c1];
std::bitset<N> v2 = cases[c2];
std::bitset<N> v3 = v1;
v1 &= v2;
for (std::size_t i = 0; i < v1.size(); ++i)
assert(v1[i] == (v3[i] && v2[i]));
}
}
}
template <std::size_t N>
void test_op_and_eq()
{
std::bitset<N> v1 = make_bitset<N>();
std::bitset<N> v2 = make_bitset<N>();
std::bitset<N> v3 = v1;
v1 &= v2;
for (std::size_t i = 0; i < N; ++i)
assert(v1[i] == (v3[i] && v2[i]));
}
int main(int, char**)
{
int main(int, char**) {
test_op_and_eq<0>();
test_op_and_eq<1>();
test_op_and_eq<31>();
@@ -53,5 +42,5 @@ int main(int, char**)
test_op_and_eq<65>();
test_op_and_eq<1000>();
return 0;
return 0;
}

View File

@@ -12,44 +12,28 @@
// bool operator!=(const bitset<N>& rhs) const;
#include <bitset>
#include <type_traits>
#include <cstdlib>
#include <cassert>
#include <cstddef>
#include <vector>
#include "bitset_test_cases.h"
#include "test_macros.h"
#if defined(TEST_COMPILER_CLANG)
#pragma clang diagnostic ignored "-Wtautological-compare"
#elif defined(TEST_COMPILER_C1XX)
#pragma warning(disable: 6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed.
#endif
template <std::size_t N>
std::bitset<N>
make_bitset()
{
std::bitset<N> v;
for (std::size_t i = 0; i < N; ++i)
v[i] = static_cast<bool>(std::rand() & 1);
return v;
}
template <std::size_t N>
void test_equality()
{
const std::bitset<N> v1 = make_bitset<N>();
std::bitset<N> v2 = v1;
assert(v1 == v2);
const bool greater_than_0 = std::integral_constant<bool, (N > 0)>::value; // avoid compiler warnings
if (greater_than_0)
{
v2[N/2].flip();
assert(v1 != v2);
void test_equality() {
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
for (std::size_t c = 0; c != cases.size(); ++c) {
std::bitset<N> const v1 = cases[c];
std::bitset<N> v2 = v1;
assert(v1 == v2);
if (v1.size() > 0) {
v2[N/2].flip();
assert(v1 != v2);
}
}
}
int main(int, char**)
{
int main(int, char**) {
test_equality<0>();
test_equality<1>();
test_equality<31>();
@@ -60,5 +44,5 @@ int main(int, char**)
test_equality<65>();
test_equality<1000>();
return 0;
return 0;
}

View File

@@ -9,40 +9,29 @@
// test bitset<N>& operator|=(const bitset<N>& rhs);
#include <bitset>
#include <cstdlib>
#include <cassert>
#include <cstddef>
#include <vector>
#include "bitset_test_cases.h"
#include "test_macros.h"
#if defined(TEST_COMPILER_CLANG)
#pragma clang diagnostic ignored "-Wtautological-compare"
#elif defined(TEST_COMPILER_C1XX)
#pragma warning(disable: 6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed.
#endif
template <std::size_t N>
std::bitset<N>
make_bitset()
{
std::bitset<N> v;
for (std::size_t i = 0; i < N; ++i)
v[i] = static_cast<bool>(std::rand() & 1);
return v;
void test_op_or_eq() {
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
for (std::size_t c1 = 0; c1 != cases.size(); ++c1) {
for (std::size_t c2 = 0; c2 != cases.size(); ++c2) {
std::bitset<N> v1 = cases[c1];
std::bitset<N> v2 = cases[c2];
std::bitset<N> v3 = v1;
v1 |= v2;
for (std::size_t i = 0; i < v1.size(); ++i)
assert(v1[i] == (v3[i] || v2[i]));
}
}
}
template <std::size_t N>
void test_op_or_eq()
{
std::bitset<N> v1 = make_bitset<N>();
std::bitset<N> v2 = make_bitset<N>();
std::bitset<N> v3 = v1;
v1 |= v2;
for (std::size_t i = 0; i < N; ++i)
assert(v1[i] == (v3[i] || v2[i]));
}
int main(int, char**)
{
int main(int, char**) {
test_op_or_eq<0>();
test_op_or_eq<1>();
test_op_or_eq<31>();
@@ -53,5 +42,5 @@ int main(int, char**)
test_op_or_eq<65>();
test_op_or_eq<1000>();
return 0;
return 0;
}

View File

@@ -9,40 +9,29 @@
// test bitset<N>& operator^=(const bitset<N>& rhs);
#include <bitset>
#include <cstdlib>
#include <cassert>
#include <cstddef>
#include <vector>
#include "bitset_test_cases.h"
#include "test_macros.h"
#if defined(TEST_COMPILER_CLANG)
#pragma clang diagnostic ignored "-Wtautological-compare"
#elif defined(TEST_COMPILER_C1XX)
#pragma warning(disable: 6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed.
#endif
template <std::size_t N>
std::bitset<N>
make_bitset()
{
std::bitset<N> v;
for (std::size_t i = 0; i < N; ++i)
v[i] = static_cast<bool>(std::rand() & 1);
return v;
void test_op_xor_eq() {
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
for (std::size_t c1 = 0; c1 != cases.size(); ++c1) {
for (std::size_t c2 = 0; c2 != cases.size(); ++c2) {
std::bitset<N> v1 = cases[c1];
std::bitset<N> v2 = cases[c2];
std::bitset<N> v3 = v1;
v1 ^= v2;
for (std::size_t i = 0; i < v1.size(); ++i)
assert(v1[i] == (v3[i] != v2[i]));
}
}
}
template <std::size_t N>
void test_op_xor_eq()
{
std::bitset<N> v1 = make_bitset<N>();
std::bitset<N> v2 = make_bitset<N>();
std::bitset<N> v3 = v1;
v1 ^= v2;
for (std::size_t i = 0; i < N; ++i)
assert(v1[i] == (v3[i] != v2[i]));
}
int main(int, char**)
{
int main(int, char**) {
test_op_xor_eq<0>();
test_op_xor_eq<1>();
test_op_xor_eq<31>();
@@ -53,5 +42,5 @@ int main(int, char**)
test_op_xor_eq<65>();
test_op_xor_eq<1000>();
return 0;
return 0;
}

View File

@@ -10,27 +10,20 @@
#include <bitset>
#include <cassert>
#include <cstddef>
#include "test_macros.h"
#if defined(TEST_COMPILER_CLANG)
#pragma clang diagnostic ignored "-Wtautological-compare"
#elif defined(TEST_COMPILER_C1XX)
#pragma warning(disable: 6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed.
#endif
template <std::size_t N>
void test_reset_all()
{
void test_reset_all() {
std::bitset<N> v;
v.set();
v.reset();
for (std::size_t i = 0; i < N; ++i)
for (std::size_t i = 0; i < v.size(); ++i)
assert(!v[i]);
}
int main(int, char**)
{
int main(int, char**) {
test_reset_all<0>();
test_reset_all<1>();
test_reset_all<31>();
@@ -41,5 +34,5 @@ int main(int, char**)
test_reset_all<65>();
test_reset_all<1000>();
return 0;
return 0;
}

View File

@@ -0,0 +1,34 @@
//===----------------------------------------------------------------------===//
//
// 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: no-exceptions
// test bitset<N>& reset(size_t pos);
// Make sure we throw std::out_of_range when calling reset() on an OOB index.
#include <bitset>
#include <cassert>
#include <stdexcept>
int main(int, char**) {
{
std::bitset<0> v;
try { v.reset(0); assert(false); } catch (std::out_of_range const&) { }
}
{
std::bitset<1> v("0");
try { v.reset(2); assert(false); } catch (std::out_of_range const&) { }
}
{
std::bitset<10> v("0000000000");
try { v.reset(10); assert(false); } catch (std::out_of_range const&) { }
}
return 0;
}

View File

@@ -10,50 +10,34 @@
#include <bitset>
#include <cassert>
#include <stdexcept>
#include <cstddef>
#include <vector>
#include "bitset_test_cases.h"
#include "test_macros.h"
template <std::size_t N>
void test_reset_one(bool test_throws)
{
std::bitset<N> v;
#ifdef TEST_HAS_NO_EXCEPTIONS
if (test_throws) return;
#else
try
{
#endif
v.set();
v.reset(50);
if (50 >= v.size())
assert(false);
for (unsigned i = 0; i < v.size(); ++i)
if (i == 50)
assert(!v[i]);
else
assert(v[i]);
assert(!test_throws);
#ifndef TEST_HAS_NO_EXCEPTIONS
void test_reset_one() {
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
for (std::size_t c = 0; c != cases.size(); ++c) {
for (std::size_t i = 0; i != N; ++i) {
std::bitset<N> v = cases[c];
v.reset(i);
assert(v[i] == false);
}
}
catch (std::out_of_range&)
{
assert(test_throws);
}
#endif
}
int main(int, char**)
{
test_reset_one<0>(true);
test_reset_one<1>(true);
test_reset_one<31>(true);
test_reset_one<32>(true);
test_reset_one<33>(true);
test_reset_one<63>(false);
test_reset_one<64>(false);
test_reset_one<65>(false);
test_reset_one<1000>(false);
int main(int, char**) {
test_reset_one<0>();
test_reset_one<1>();
test_reset_one<31>();
test_reset_one<32>();
test_reset_one<33>();
test_reset_one<63>();
test_reset_one<64>();
test_reset_one<65>();
test_reset_one<1000>();
return 0;
return 0;
}

View File

@@ -9,40 +9,26 @@
// test bitset<N> operator>>(size_t pos) const;
#include <bitset>
#include <cstdlib>
#include <cassert>
#include <cstddef>
#include <vector>
#include "bitset_test_cases.h"
#include "test_macros.h"
#if defined(TEST_COMPILER_CLANG)
#pragma clang diagnostic ignored "-Wtautological-compare"
#elif defined(TEST_COMPILER_C1XX)
#pragma warning(disable: 6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed.
#endif
template <std::size_t N>
std::bitset<N>
make_bitset()
{
std::bitset<N> v;
for (std::size_t i = 0; i < N; ++i)
v[i] = static_cast<bool>(std::rand() & 1);
return v;
}
template <std::size_t N>
void test_right_shift()
{
for (std::size_t s = 0; s <= N+1; ++s)
{
std::bitset<N> v1 = make_bitset<N>();
std::bitset<N> v2 = v1;
assert((v1 >>= s) == (v2 >> s));
void test_right_shift() {
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
for (std::size_t c = 0; c != cases.size(); ++c) {
for (std::size_t s = 0; s <= N+1; ++s) {
std::bitset<N> v1 = cases[c];
std::bitset<N> v2 = v1;
assert((v1 >>= s) == (v2 >> s));
}
}
}
int main(int, char**)
{
int main(int, char**) {
test_right_shift<0>();
test_right_shift<1>();
test_right_shift<31>();
@@ -53,5 +39,5 @@ int main(int, char**)
test_right_shift<65>();
test_right_shift<1000>();
return 0;
return 0;
}

View File

@@ -9,45 +9,31 @@
// test bitset<N>& operator<<=(size_t pos);
#include <bitset>
#include <cstdlib>
#include <cassert>
#include <cstddef>
#include <vector>
#include "bitset_test_cases.h"
#include "test_macros.h"
#if defined(TEST_COMPILER_CLANG)
#pragma clang diagnostic ignored "-Wtautological-compare"
#elif defined(TEST_COMPILER_C1XX)
#pragma warning(disable: 6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed.
#endif
template <std::size_t N>
std::bitset<N>
make_bitset()
{
std::bitset<N> v;
for (std::size_t i = 0; i < N; ++i)
v[i] = static_cast<bool>(std::rand() & 1);
return v;
}
template <std::size_t N>
void test_right_shift()
{
for (std::size_t s = 0; s <= N+1; ++s)
{
std::bitset<N> v1 = make_bitset<N>();
std::bitset<N> v2 = v1;
v1 >>= s;
for (std::size_t i = 0; i < N; ++i)
if (i + s < N)
assert(v1[i] == v2[i + s]);
else
assert(v1[i] == 0);
void test_right_shift() {
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
for (std::size_t c = 0; c != cases.size(); ++c) {
for (std::size_t s = 0; s <= N+1; ++s) {
std::bitset<N> v1 = cases[c];
std::bitset<N> v2 = v1;
v1 >>= s;
for (std::size_t i = 0; i < v1.size(); ++i)
if (i + s < N)
assert(v1[i] == v2[i + s]);
else
assert(v1[i] == 0);
}
}
}
int main(int, char**)
{
int main(int, char**) {
test_right_shift<0>();
test_right_shift<1>();
test_right_shift<31>();
@@ -58,5 +44,5 @@ int main(int, char**)
test_right_shift<65>();
test_right_shift<1000>();
return 0;
return 0;
}

View File

@@ -10,26 +10,19 @@
#include <bitset>
#include <cassert>
#include <cstddef>
#include "test_macros.h"
#if defined(TEST_COMPILER_CLANG)
#pragma clang diagnostic ignored "-Wtautological-compare"
#elif defined(TEST_COMPILER_C1XX)
#pragma warning(disable: 6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed.
#endif
template <std::size_t N>
void test_set_all()
{
void test_set_all() {
std::bitset<N> v;
v.set();
for (std::size_t i = 0; i < N; ++i)
for (std::size_t i = 0; i < v.size(); ++i)
assert(v[i]);
}
int main(int, char**)
{
int main(int, char**) {
test_set_all<0>();
test_set_all<1>();
test_set_all<31>();
@@ -40,5 +33,5 @@ int main(int, char**)
test_set_all<65>();
test_set_all<1000>();
return 0;
return 0;
}

View File

@@ -0,0 +1,34 @@
//===----------------------------------------------------------------------===//
//
// 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: no-exceptions
// test bitset<N>& set(size_t pos, bool val = true);
// Make sure we throw std::out_of_range when calling set() on an OOB index.
#include <bitset>
#include <cassert>
#include <stdexcept>
int main(int, char**) {
{
std::bitset<0> v;
try { v.set(0); assert(false); } catch (std::out_of_range const&) { }
}
{
std::bitset<1> v("0");
try { v.set(2); assert(false); } catch (std::out_of_range const&) { }
}
{
std::bitset<10> v("0000000000");
try { v.set(10); assert(false); } catch (std::out_of_range const&) { }
}
return 0;
}

View File

@@ -10,59 +10,37 @@
#include <bitset>
#include <cassert>
#include <stdexcept>
#include <cstddef>
#include <vector>
#include "bitset_test_cases.h"
#include "test_macros.h"
template <std::size_t N>
void test_set_one(bool test_throws)
{
std::bitset<N> v;
#ifdef TEST_HAS_NO_EXCEPTIONS
if (test_throws) return;
#else
try
#endif
{
v.set(50);
if (50 >= v.size())
assert(false);
assert(v[50]);
assert(!test_throws);
void test_set_one() {
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
for (std::size_t c = 0; c != cases.size(); ++c) {
std::bitset<N> v = cases[c];
if (v.size() > 0) {
std::size_t middle = v.size() / 2;
v.set(middle);
assert(v[middle] == true);
v.set(middle, false);
assert(v[middle] == false);
}
}
#ifndef TEST_HAS_NO_EXCEPTIONS
catch (std::out_of_range&)
{
assert(test_throws);
}
try
#endif
{
v.set(50, false);
if (50 >= v.size())
assert(false);
assert(!v[50]);
assert(!test_throws);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
catch (std::out_of_range&)
{
assert(test_throws);
}
#endif
}
int main(int, char**)
{
test_set_one<0>(true);
test_set_one<1>(true);
test_set_one<31>(true);
test_set_one<32>(true);
test_set_one<33>(true);
test_set_one<63>(false);
test_set_one<64>(false);
test_set_one<65>(false);
test_set_one<1000>(false);
int main(int, char**) {
test_set_one<0>();
test_set_one<1>();
test_set_one<31>();
test_set_one<32>();
test_set_one<33>();
test_set_one<63>();
test_set_one<64>();
test_set_one<65>();
test_set_one<1000>();
return 0;
return 0;
}

View File

@@ -14,14 +14,12 @@
#include "test_macros.h"
template <std::size_t N>
void test_size()
{
void test_size() {
const std::bitset<N> v;
assert(v.size() == N);
}
int main(int, char**)
{
int main(int, char**) {
test_size<0>();
test_size<1>();
test_size<31>();
@@ -32,5 +30,5 @@ int main(int, char**)
test_size<65>();
test_size<1000>();
return 0;
return 0;
}

View File

@@ -0,0 +1,34 @@
//===----------------------------------------------------------------------===//
//
// 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: no-exceptions
// test constexpr bool test(size_t pos) const;
// Make sure we throw std::out_of_range when calling test() on an OOB index.
#include <bitset>
#include <cassert>
#include <stdexcept>
int main(int, char**) {
{
std::bitset<0> v;
try { v.test(0); assert(false); } catch (std::out_of_range const&) { }
}
{
std::bitset<1> v("0");
try { v.test(2); assert(false); } catch (std::out_of_range const&) { }
}
{
std::bitset<10> v("0000000000");
try { v.test(10); assert(false); } catch (std::out_of_range const&) { }
}
return 0;
}

View File

@@ -9,61 +9,36 @@
// test constexpr bool test(size_t pos) const;
#include <bitset>
#include <cstdlib>
#include <cassert>
#include <stdexcept>
#include <cstddef>
#include <vector>
#include "bitset_test_cases.h"
#include "test_macros.h"
#if defined(TEST_COMPILER_C1XX)
#pragma warning(disable: 6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed.
#endif
template <std::size_t N>
std::bitset<N>
make_bitset()
{
std::bitset<N> v;
for (std::size_t i = 0; i < N; ++i)
v[i] = static_cast<bool>(std::rand() & 1);
return v;
}
template <std::size_t N>
void test_test(bool test_throws)
{
const std::bitset<N> v1 = make_bitset<N>();
#ifdef TEST_HAS_NO_EXCEPTIONS
if (test_throws) return;
#else
try
{
#endif
bool b = v1.test(50);
if (50 >= v1.size())
assert(false);
assert(b == v1[50]);
assert(!test_throws);
#ifndef TEST_HAS_NO_EXCEPTIONS
void test_test() {
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
for (std::size_t c = 0; c != cases.size(); ++c) {
std::bitset<N> const v = cases[c];
if (v.size() > 0) {
std::size_t middle = v.size() / 2;
bool b = v.test(middle);
assert(b == v[middle]);
}
}
catch (std::out_of_range&)
{
assert(test_throws);
}
#endif
}
int main(int, char**)
{
test_test<0>(true);
test_test<1>(true);
test_test<31>(true);
test_test<32>(true);
test_test<33>(true);
test_test<63>(false);
test_test<64>(false);
test_test<65>(false);
test_test<1000>(false);
int main(int, char**) {
test_test<0>();
test_test<1>();
test_test<31>();
test_test<32>();
test_test<33>();
test_test<63>();
test_test<64>();
test_test<65>();
test_test<1000>();
return 0;
return 0;
}

View File

@@ -21,140 +21,88 @@
// basic_string<char, char_traits<char>, allocator<char> > to_string() const;
#include <bitset>
#include <string>
#include <cstdlib>
#include <cassert>
#include <cstddef>
#include <memory> // for std::allocator
#include <string>
#include <vector>
#include "bitset_test_cases.h"
#include "test_macros.h"
#if defined(TEST_COMPILER_CLANG)
#pragma clang diagnostic ignored "-Wtautological-compare"
#elif defined(TEST_COMPILER_C1XX)
#pragma warning(disable: 6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed.
#endif
template <std::size_t N>
std::bitset<N>
make_bitset()
{
std::bitset<N> v;
for (std::size_t i = 0; i < N; ++i)
v[i] = static_cast<bool>(std::rand() & 1);
return v;
template <class CharT, std::size_t N>
void check_equal(std::basic_string<CharT> const& s, std::bitset<N> const& b, CharT zero, CharT one) {
assert(s.size() == b.size());
for (std::size_t i = 0; i < b.size(); ++i) {
if (b[i]) {
assert(s[b.size() - 1 - i] == one);
} else {
assert(s[b.size() - 1 - i] == zero);
}
}
}
template <std::size_t N>
void test_to_string()
{
{
std::bitset<N> v = make_bitset<N>();
{
std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >();
for (std::size_t i = 0; i < N; ++i)
if (v[i])
assert(s[N - 1 - i] == '1');
else
assert(s[N - 1 - i] == '0');
void test_to_string() {
std::vector<std::bitset<N> > const cases = get_test_cases<N>();
for (std::size_t c = 0; c != cases.size(); ++c) {
std::bitset<N> const v = cases[c];
{
std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >();
check_equal(s, v, L'0', L'1');
}
{
std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >();
check_equal(s, v, L'0', L'1');
}
{
std::string s = v.template to_string<char>();
check_equal(s, v, '0', '1');
}
{
std::string s = v.to_string();
check_equal(s, v, '0', '1');
}
{
std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >('0');
check_equal(s, v, L'0', L'1');
}
{
std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >('0');
check_equal(s, v, L'0', L'1');
}
{
std::string s = v.template to_string<char>('0');
check_equal(s, v, '0', '1');
}
{
std::string s = v.to_string('0');
check_equal(s, v, '0', '1');
}
{
std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >('0', '1');
check_equal(s, v, L'0', L'1');
}
{
std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >('0', '1');
check_equal(s, v, L'0', L'1');
}
{
std::string s = v.template to_string<char>('0', '1');
check_equal(s, v, '0', '1');
}
{
std::string s = v.to_string('0', '1');
check_equal(s, v, '0', '1');
}
{
std::string s = v.to_string('x', 'y');
check_equal(s, v, 'x', 'y');
}
}
{
std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >();
for (std::size_t i = 0; i < N; ++i)
if (v[i])
assert(s[N - 1 - i] == '1');
else
assert(s[N - 1 - i] == '0');
}
{
std::string s = v.template to_string<char>();
for (std::size_t i = 0; i < N; ++i)
if (v[i])
assert(s[N - 1 - i] == '1');
else
assert(s[N - 1 - i] == '0');
}
{
std::string s = v.to_string();
for (std::size_t i = 0; i < N; ++i)
if (v[i])
assert(s[N - 1 - i] == '1');
else
assert(s[N - 1 - i] == '0');
}
}
{
std::bitset<N> v = make_bitset<N>();
{
std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >('0');
for (std::size_t i = 0; i < N; ++i)
if (v[i])
assert(s[N - 1 - i] == '1');
else
assert(s[N - 1 - i] == '0');
}
{
std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >('0');
for (std::size_t i = 0; i < N; ++i)
if (v[i])
assert(s[N - 1 - i] == '1');
else
assert(s[N - 1 - i] == '0');
}
{
std::string s = v.template to_string<char>('0');
for (std::size_t i = 0; i < N; ++i)
if (v[i])
assert(s[N - 1 - i] == '1');
else
assert(s[N - 1 - i] == '0');
}
{
std::string s = v.to_string('0');
for (std::size_t i = 0; i < N; ++i)
if (v[i])
assert(s[N - 1 - i] == '1');
else
assert(s[N - 1 - i] == '0');
}
}
{
std::bitset<N> v = make_bitset<N>();
{
std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >('0', '1');
for (std::size_t i = 0; i < N; ++i)
if (v[i])
assert(s[N - 1 - i] == '1');
else
assert(s[N - 1 - i] == '0');
}
{
std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >('0', '1');
for (std::size_t i = 0; i < N; ++i)
if (v[i])
assert(s[N - 1 - i] == '1');
else
assert(s[N - 1 - i] == '0');
}
{
std::string s = v.template to_string<char>('0', '1');
for (std::size_t i = 0; i < N; ++i)
if (v[i])
assert(s[N - 1 - i] == '1');
else
assert(s[N - 1 - i] == '0');
}
{
std::string s = v.to_string('0', '1');
for (std::size_t i = 0; i < N; ++i)
if (v[i])
assert(s[N - 1 - i] == '1');
else
assert(s[N - 1 - i] == '0');
}
}
}
int main(int, char**)
{
int main(int, char**) {
test_to_string<0>();
test_to_string<1>();
test_to_string<31>();
@@ -165,5 +113,5 @@ int main(int, char**)
test_to_string<65>();
test_to_string<1000>();
return 0;
return 0;
}

View File

@@ -17,37 +17,36 @@
#include "test_macros.h"
template <std::size_t N>
void test_to_ullong()
{
void test_to_ullong() {
const std::size_t M = sizeof(unsigned long long) * CHAR_BIT < N ? sizeof(unsigned long long) * CHAR_BIT : N;
const bool is_M_zero = std::integral_constant<bool, M == 0>::value; // avoid compiler warnings
const std::size_t X = is_M_zero ? sizeof(unsigned long long) * CHAR_BIT - 1 : sizeof(unsigned long long) * CHAR_BIT - M;
const unsigned long long max = is_M_zero ? 0 : (unsigned long long)(-1) >> X;
unsigned long long tests[] = {0,
std::min<unsigned long long>(1, max),
std::min<unsigned long long>(2, max),
std::min<unsigned long long>(3, max),
std::min(max, max-3),
std::min(max, max-2),
std::min(max, max-1),
max};
for (std::size_t i = 0; i < sizeof(tests)/sizeof(tests[0]); ++i)
{
unsigned long long tests[] = {
0,
std::min<unsigned long long>(1, max),
std::min<unsigned long long>(2, max),
std::min<unsigned long long>(3, max),
std::min(max, max-3),
std::min(max, max-2),
std::min(max, max-1),
max
};
for (std::size_t i = 0; i < sizeof(tests)/sizeof(tests[0]); ++i) {
unsigned long long j = tests[i];
std::bitset<N> v(j);
assert(j == v.to_ullong());
}
{ // test values bigger than can fit into the bitset
const unsigned long long val = 0x55AAAAFFFFAAAA55ULL;
const bool canFit = N < sizeof(unsigned long long) * CHAR_BIT;
const unsigned long long mask = canFit ? (1ULL << (canFit ? N : 0)) - 1 : (unsigned long long)(-1); // avoid compiler warnings
std::bitset<N> v(val);
assert(v.to_ullong() == (val & mask)); // we shouldn't return bit patterns from outside the limits of the bitset.
const unsigned long long val = 0x55AAAAFFFFAAAA55ULL;
const bool canFit = N < sizeof(unsigned long long) * CHAR_BIT;
const unsigned long long mask = canFit ? (1ULL << (canFit ? N : 0)) - 1 : (unsigned long long)(-1); // avoid compiler warnings
std::bitset<N> v(val);
assert(v.to_ullong() == (val & mask)); // we shouldn't return bit patterns from outside the limits of the bitset.
}
}
int main(int, char**)
{
int main(int, char**) {
// test_to_ullong<0>();
test_to_ullong<1>();
test_to_ullong<31>();
@@ -58,5 +57,5 @@ int main(int, char**)
test_to_ullong<65>();
test_to_ullong<1000>();
return 0;
return 0;
}

View File

@@ -18,38 +18,37 @@
#include "test_macros.h"
template <std::size_t N>
void test_to_ulong()
{
void test_to_ulong() {
const std::size_t M = sizeof(unsigned long) * CHAR_BIT < N ? sizeof(unsigned long) * CHAR_BIT : N;
const bool is_M_zero = std::integral_constant<bool, M == 0>::value; // avoid compiler warnings
const std::size_t X = is_M_zero ? sizeof(unsigned long) * CHAR_BIT - 1 : sizeof(unsigned long) * CHAR_BIT - M;
const std::size_t max = is_M_zero ? 0 : std::size_t(std::numeric_limits<unsigned long>::max()) >> X;
std::size_t tests[] = {0,
std::min<std::size_t>(1, max),
std::min<std::size_t>(2, max),
std::min<std::size_t>(3, max),
std::min(max, max-3),
std::min(max, max-2),
std::min(max, max-1),
max};
for (std::size_t i = 0; i < sizeof(tests)/sizeof(tests[0]); ++i)
{
std::size_t tests[] = {
0,
std::min<std::size_t>(1, max),
std::min<std::size_t>(2, max),
std::min<std::size_t>(3, max),
std::min(max, max-3),
std::min(max, max-2),
std::min(max, max-1),
max
};
for (std::size_t i = 0; i < sizeof(tests)/sizeof(tests[0]); ++i) {
std::size_t j = tests[i];
std::bitset<N> v(j);
assert(j == v.to_ulong());
}
{ // test values bigger than can fit into the bitset
const unsigned long val = 0x5AFFFFA5UL;
const bool canFit = N < sizeof(unsigned long) * CHAR_BIT;
const unsigned long mask = canFit ? (1UL << (canFit ? N : 0)) - 1 : (unsigned long)(-1); // avoid compiler warnings
std::bitset<N> v(val);
assert(v.to_ulong() == (val & mask)); // we shouldn't return bit patterns from outside the limits of the bitset.
const unsigned long val = 0x5AFFFFA5UL;
const bool canFit = N < sizeof(unsigned long) * CHAR_BIT;
const unsigned long mask = canFit ? (1UL << (canFit ? N : 0)) - 1 : (unsigned long)(-1); // avoid compiler warnings
std::bitset<N> v(val);
assert(v.to_ulong() == (val & mask)); // we shouldn't return bit patterns from outside the limits of the bitset.
}
}
int main(int, char**)
{
int main(int, char**) {
test_to_ulong<0>();
test_to_ulong<1>();
test_to_ulong<31>();
@@ -60,5 +59,5 @@ int main(int, char**)
test_to_ulong<65>();
test_to_ulong<1000>();
return 0;
return 0;
}