Ticket #3472: initialized_test.patch

File initialized_test.patch, 6.2 KB (added by niels_dekker, 12 years ago)

tests for boost::initialized<T> (fixed a few typo's)

  • initialized_test.cpp

     
     1// Copyright 2010, Niels Dekker.
     2//
     3// Distributed under the Boost Software License, Version 1.0. (See
     4// accompanying file LICENSE_1_0.txt or copy at
     5// http://www.boost.org/LICENSE_1_0.txt)
     6//
     7// Test program for boost::initialized<T>.
     8//
     9// 2 May 2010 (Created) Niels Dekker
     10
     11#include <boost/utility/value_init.hpp>
     12#include <boost/detail/lightweight_test.hpp>
     13
     14#include <string>
     15
     16namespace
     17{
     18  // Typical use case for boost::initialized<T>: A generic class that
     19  // holds a value of type T, which must be initialized by either
     20  // value-initialization or direct-initialization.
     21  template <class T> class key_value_pair
     22  {
     23    std::string m_key;
     24    boost::initialized<T> m_value;
     25  public:
     26 
     27    // Value-initializes the object held by m_value.
     28    key_value_pair() { }
     29
     30    // Value-initializes the object held by m_value.
     31    explicit key_value_pair(const std::string& key)
     32    :
     33    m_key(key)
     34    {
     35    }
     36
     37    // Direct-initializes the object held by m_value.
     38    key_value_pair(const std::string& key, const T& value)
     39    :
     40    m_key(key), m_value(value)
     41    {
     42    }
     43
     44    const T& get_value() const
     45    {
     46      return m_value;
     47    }
     48  };
     49
     50
     51  // Tells whether the argument is value-initialized.
     52  bool is_value_initialized(const int& arg)
     53  {
     54    return arg == 0;
     55  }
     56
     57
     58  // Tells whether the argument is value-initialized.
     59  bool is_value_initialized(const std::string& arg)
     60  {
     61    return arg.empty();
     62  }
     63
     64  struct foo
     65  {
     66    int data;
     67  };
     68
     69  bool operator==(const foo& lhs, const foo& rhs)
     70  {
     71    return lhs.data == rhs.data;
     72  }
     73
     74
     75  // Tells whether the argument is value-initialized.
     76  bool is_value_initialized(const foo& arg)
     77  {
     78    return arg.data == 0;
     79  }
     80
     81
     82  template <class T>
     83  void test_key_value_pair(const T& magic_value)
     84  {
     85    // The value component of a default key_value_pair must be value-initialized.
     86    key_value_pair<T> default_key_value_pair;
     87    BOOST_TEST( is_value_initialized(default_key_value_pair.get_value() ) );
     88
     89    // The value component of a key_value_pair that only has its key explicitly specified
     90    // must also be value-initialized.
     91    BOOST_TEST( is_value_initialized(key_value_pair<T>("key").get_value()) );
     92
     93    // However, the value component of the following key_value_pair must be
     94    // "magic_value", as it must be direct-initialized.
     95    BOOST_TEST( key_value_pair<T>("key", magic_value).get_value() == magic_value );
     96  }
     97}
     98
     99
     100// Tests boost::initialize for a fundamental type, a type with a
     101// user-defined constructor, and a user-defined type without
     102// a user-defined constructor.
     103int main()
     104{
     105
     106  const int magic_number = 42;
     107  test_key_value_pair(magic_number);
     108
     109  const std::string magic_string = "magic value";
     110  test_key_value_pair(magic_string);
     111
     112  const foo magic_foo = { 42 };
     113  test_key_value_pair(magic_foo);
     114
     115  return boost::report_errors();
     116}
  • initialized_test_fail1.cpp

     
     1// Copyright 2010, Niels Dekker.
     2//
     3// Distributed under the Boost Software License, Version 1.0. (See
     4// accompanying file LICENSE_1_0.txt or copy at
     5// http://www.boost.org/LICENSE_1_0.txt)
     6//
     7// Test program for boost::initialized<T>. Must fail to compile.
     8//
     9// Initial: 2 May 2010
     10
     11#include <boost/utility/value_init.hpp>
     12
     13namespace
     14{
     15  void direct_initialize_from_int()
     16  {
     17    // Okay: initialized<T> supports direct-initialization from T.
     18    boost::initialized<int> direct_initialized_int(1);
     19  }
     20
     21  void copy_initialize_from_int()
     22  {
     23    // The following line should not compile, because initialized<T>
     24    // was not intended to supports copy-initialization from T.
     25    boost::initialized<int> copy_initialized_int = 1;
     26  }
     27}
     28
     29int main()
     30{
     31  // This should fail to compile, so there is no need to call any function.
     32  return 0;
     33}
  • initialized_test_fail2.cpp

     
     1// Copyright 2010, Niels Dekker.
     2//
     3// Distributed under the Boost Software License, Version 1.0. (See
     4// accompanying file LICENSE_1_0.txt or copy at
     5// http://www.boost.org/LICENSE_1_0.txt)
     6//
     7// Test program for boost::initialized<T>. Must fail to compile.
     8//
     9// Initial: 2 May 2010
     10
     11#include <boost/utility/value_init.hpp>
     12
     13namespace
     14{
     15  void from_value_initialized_to_initialized()
     16  {
     17    boost::value_initialized<int> value_initialized_int;
     18
     19    // Okay: initialized<T> can be initialized by value_initialized<T>.
     20    boost::initialized<int> initialized_int(value_initialized_int);
     21  }
     22
     23  void from_initialized_to_value_initialized()
     24  {
     25    boost::initialized<int> initialized_int(13);
     26
     27    // The following line should not compile, because initialized<T>
     28    // should not be convertible to value_initialized<T>.
     29    boost::value_initialized<int> value_initialized_int(initialized_int);
     30  }
     31}
     32
     33int main()
     34{
     35  // This should fail to compile, so there is no need to call any function.
     36  return 0;
     37}
  • test/Jamfile.v2

     
    3232        [ compile result_of_test.cpp ]
    3333        [ run ../shared_iterator_test.cpp ]
    3434        [ run ../value_init_test.cpp ]
     35        [ run ../initialized_test.cpp ]
    3536        [ compile-fail ../value_init_test_fail1.cpp ]
    3637        [ compile-fail ../value_init_test_fail2.cpp ]
    3738        [ compile-fail ../value_init_test_fail3.cpp ]
     39        [ compile-fail ../initialized_test_fail1.cpp ]
     40        [ compile-fail ../initialized_test_fail2.cpp ]
    3841        [ run ../verify_test.cpp ]
    3942    ;
    4043