brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.7 KiB · ba95747 Raw
90 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// <vector>10// vector<bool>11 12// vector(const vector& v);13 14#include <array>15#include <cassert>16#include <vector>17 18#include "min_allocator.h"19#include "test_allocator.h"20#include "test_macros.h"21 22template <class C>23TEST_CONSTEXPR_CXX20 void test(const C& x) {24  typename C::size_type s = x.size();25  C c(x);26  LIBCPP_ASSERT(c.__invariants());27  assert(c.size() == s);28  assert(c == x);29#if TEST_STD_VER >= 1130  assert(c.get_allocator() ==31         std::allocator_traits<typename C::allocator_type>::select_on_container_copy_construction(x.get_allocator()));32#endif33}34 35TEST_CONSTEXPR_CXX20 bool tests() {36  std::array<int, 5> a1   = {1, 0, 1, 0, 1};37  std::array<int, 18> a2  = {0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0};38  std::array<int, 33> a3  = {0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0};39  std::array<int, 65> a4  = {0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0};40  std::array<int, 299> a5 = {};41  for (unsigned i = 0; i < a5.size(); i += 2)42    a5[i] = 1;43 44  // Tests for vector<bool> copy constructor with word size up to 5 (i.e., bit size > 256 on a 64-bit system)45  { // Test with default std::allocator46    test(std::vector<bool>(a1.begin(), a1.end()));47    test(std::vector<bool>(a2.begin(), a2.end()));48    test(std::vector<bool>(a3.begin(), a3.end()));49    test(std::vector<bool>(a4.begin(), a4.end()));50    test(std::vector<bool>(a5.begin(), a5.end()));51  }52  { // Test with test_allocator53    using A = test_allocator<bool>;54    using C = std::vector<bool, A>;55    test(C(a1.begin(), a1.end()));56    test(C(a2.begin(), a2.end()));57    test(C(a3.begin(), a3.end()));58    test(C(a4.begin(), a4.end()));59    test(C(a5.begin(), a5.end()));60  }61  { // Test with other_allocator62    using A = other_allocator<bool>;63    using C = std::vector<bool, A>;64    test(C(a1.begin(), a1.end()));65    test(C(a2.begin(), a2.end()));66    test(C(a3.begin(), a3.end()));67    test(C(a4.begin(), a4.end()));68    test(C(a5.begin(), a5.end()));69  }70  { // Test with min_allocator71    using A = min_allocator<bool>;72    using C = std::vector<bool, A>;73    test(C(a1.begin(), a1.end()));74    test(C(a2.begin(), a2.end()));75    test(C(a3.begin(), a3.end()));76    test(C(a4.begin(), a4.end()));77    test(C(a5.begin(), a5.end()));78  }79 80  return true;81}82 83int main(int, char**) {84  tests();85#if TEST_STD_VER > 1786  static_assert(tests());87#endif88  return 0;89}90