Home | History | Annotate | Download | only in any.cons
      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 // UNSUPPORTED: c++98, c++03, c++11
     11 
     12 // <experimental/any>
     13 
     14 // any(any const &);
     15 
     16 #include <experimental/any>
     17 #include <cassert>
     18 
     19 #include "any_helpers.h"
     20 #include "count_new.hpp"
     21 #include "test_macros.h"
     22 
     23 using std::experimental::any;
     24 using std::experimental::any_cast;
     25 
     26 template <class Type>
     27 void test_copy_throws() {
     28 #if !defined(TEST_HAS_NO_EXCEPTIONS)
     29     assert(Type::count == 0);
     30     {
     31         any const a((Type(42)));
     32         assert(Type::count == 1);
     33         try {
     34             any const a2(a);
     35             assert(false);
     36         } catch (my_any_exception const &) {
     37             // do nothing
     38         } catch (...) {
     39             assert(false);
     40         }
     41         assert(Type::count == 1);
     42         assertContains<Type>(a, 42);
     43     }
     44     assert(Type::count == 0);
     45 #endif
     46 }
     47 
     48 void test_copy_empty() {
     49     DisableAllocationGuard g; ((void)g); // No allocations should occur.
     50     any a1;
     51     any a2(a1);
     52 
     53     assertEmpty(a1);
     54     assertEmpty(a2);
     55 }
     56 
     57 template <class Type>
     58 void test_copy()
     59 {
     60     // Copying small types should not perform any allocations.
     61     DisableAllocationGuard g(isSmallType<Type>()); ((void)g);
     62     assert(Type::count == 0);
     63     Type::reset();
     64     {
     65         any a((Type(42)));
     66         assert(Type::count == 1);
     67         assert(Type::copied == 0);
     68 
     69         any a2(a);
     70 
     71         assert(Type::copied == 1);
     72         assert(Type::count == 2);
     73         assertContains<Type>(a, 42);
     74         assertContains<Type>(a, 42);
     75 
     76         // Modify a and check that a2 is unchanged
     77         modifyValue<Type>(a, -1);
     78         assertContains<Type>(a, -1);
     79         assertContains<Type>(a2, 42);
     80 
     81         // modify a2 and check that a is unchanged
     82         modifyValue<Type>(a2, 999);
     83         assertContains<Type>(a, -1);
     84         assertContains<Type>(a2, 999);
     85 
     86         // clear a and check that a2 is unchanged
     87         a.clear();
     88         assertEmpty(a);
     89         assertContains<Type>(a2, 999);
     90     }
     91     assert(Type::count == 0);
     92 }
     93 
     94 int main() {
     95     test_copy<small>();
     96     test_copy<large>();
     97     test_copy_empty();
     98     test_copy_throws<small_throws_on_copy>();
     99     test_copy_throws<large_throws_on_copy>();
    100 }
    101