Home | History | Annotate | Download | only in bitset.members
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // test bitset<N>& flip(size_t pos);
     11 
     12 #include <bitset>
     13 #include <cstdlib>
     14 #include <cassert>
     15 #include <stdexcept>
     16 
     17 #include "test_macros.h"
     18 
     19 #if defined(TEST_COMPILER_C1XX)
     20 #pragma warning(disable: 6294) // Ill-defined for-loop:  initial condition does not satisfy test.  Loop body not executed.
     21 #endif
     22 
     23 template <std::size_t N>
     24 std::bitset<N>
     25 make_bitset()
     26 {
     27     std::bitset<N> v;
     28     for (std::size_t i = 0; i < N; ++i)
     29         v[i] = static_cast<bool>(std::rand() & 1);
     30     return v;
     31 }
     32 
     33 template <std::size_t N>
     34 void test_flip_one(bool test_throws)
     35 {
     36     std::bitset<N> v = make_bitset<N>();
     37 #ifdef TEST_HAS_NO_EXCEPTIONS
     38     if (test_throws) return;
     39 #else
     40     try
     41     {
     42 #endif
     43         v.flip(50);
     44         bool b = v[50];
     45         if (50 >= v.size())
     46             assert(false);
     47         assert(v[50] == b);
     48         v.flip(50);
     49         assert(v[50] != b);
     50         v.flip(50);
     51         assert(v[50] == b);
     52         assert(!test_throws);
     53 #ifndef TEST_HAS_NO_EXCEPTIONS
     54     }
     55     catch (std::out_of_range&)
     56     {
     57         assert(test_throws);
     58     }
     59 #endif
     60 }
     61 
     62 int main()
     63 {
     64     test_flip_one<0>(true);
     65     test_flip_one<1>(true);
     66     test_flip_one<31>(true);
     67     test_flip_one<32>(true);
     68     test_flip_one<33>(true);
     69     test_flip_one<63>(false);
     70     test_flip_one<64>(false);
     71     test_flip_one<65>(false);
     72     test_flip_one<1000>(false);
     73 }
     74