test.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright (c) 2009-2016 Vladimir Batov.
  2. // Use, modification and distribution are subject to the Boost Software License,
  3. // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
  4. #ifndef BOOST_CONVERT_TEST_HPP
  5. #define BOOST_CONVERT_TEST_HPP
  6. #include <boost/convert/detail/config.hpp>
  7. #include <boost/make_default.hpp>
  8. #include <boost/static_assert.hpp>
  9. #include <string>
  10. #include <istream>
  11. #include <string.h> // For strlen, strcmp, memcpy
  12. #include <memory.h> // Is needed for 'memset'
  13. #include <stdio.h>
  14. #include <time.h>
  15. #if defined(_MSC_VER)
  16. # pragma warning(disable: 4189) // local variable is initialized but not referenced.
  17. # pragma warning(disable: 4127) // conditional expression is constant.
  18. # pragma warning(disable: 4100) // unreferenced formal parameter.
  19. # pragma warning(disable: 4714) // marked as __forceinline not #endif
  20. # pragma warning(disable: 4706)
  21. # pragma warning(disable: 4005)
  22. # pragma warning(disable: 4459) // declaration hides global declaration
  23. # pragma warning(disable: 4456) // declaration hides previous local declaration
  24. #endif
  25. //[change_declaration
  26. struct change
  27. {
  28. enum value_type { no, up, dn };
  29. change(value_type v =no) : value_(v) {}
  30. bool operator==(change v) const { return value_ == v.value_; }
  31. value_type value() const { return value_; }
  32. private: value_type value_;
  33. };
  34. //]
  35. //[change_stream_operators
  36. std::istream& operator>>(std::istream& stream, change& chg)
  37. {
  38. std::string str; stream >> str;
  39. /**/ if (str == "up") chg = change::up;
  40. else if (str == "dn") chg = change::dn;
  41. else if (str == "no") chg = change::no;
  42. else stream.setstate(std::ios_base::failbit);
  43. return stream;
  44. }
  45. std::ostream& operator<<(std::ostream& stream, change const& chg)
  46. {
  47. return stream << (chg == change::up ? "up" : chg == change::dn ? "dn" : "no");
  48. }
  49. //]
  50. //[change_convert_operators
  51. inline void operator>>(change chg, boost::optional<std::string>& str)
  52. {
  53. str = chg == change::up ? "up" : chg == change::dn ? "dn" : "no";
  54. }
  55. inline void operator>>(std::string const& str, boost::optional<change>& chg)
  56. {
  57. /**/ if (str == "up") chg = change::up;
  58. else if (str == "dn") chg = change::dn;
  59. else if (str == "no") chg = change::no;
  60. }
  61. //]
  62. //[direction_declaration
  63. struct direction
  64. {
  65. // Note: the class does NOT have the default constructor.
  66. enum value_type { up, dn };
  67. direction(value_type value) : value_(value) {}
  68. bool operator==(direction that) const { return value_ == that.value_; }
  69. value_type value() const { return value_; }
  70. private: value_type value_;
  71. };
  72. //]
  73. //[direction_stream_operators
  74. std::istream& operator>>(std::istream& stream, direction& dir)
  75. {
  76. std::string str; stream >> str;
  77. /**/ if (str == "up") dir = direction::up;
  78. else if (str == "dn") dir = direction::dn;
  79. else stream.setstate(std::ios_base::failbit);
  80. return stream;
  81. }
  82. std::ostream& operator<<(std::ostream& stream, direction const& dir)
  83. {
  84. return stream << (dir.value() == direction::up ? "up" : "dn");
  85. }
  86. //]
  87. //[direction_declaration_make_default
  88. namespace boost
  89. {
  90. template<> inline direction make_default<direction>()
  91. {
  92. return direction(direction::up);
  93. }
  94. }
  95. //]
  96. // Quick and dirty small-string implementation for performance tests.
  97. //[my_string_declaration
  98. struct my_string
  99. {
  100. typedef my_string this_type;
  101. typedef char value_type;
  102. typedef value_type* iterator;
  103. typedef value_type const* const_iterator;
  104. my_string ();
  105. my_string (const_iterator, const_iterator =0);
  106. char const* c_str () const { return storage_; }
  107. const_iterator begin () const { return storage_; }
  108. const_iterator end () const { return storage_ + strlen(storage_); }
  109. this_type& operator= (char const*);
  110. private:
  111. static size_t const size_ = 12;
  112. char storage_[size_];
  113. };
  114. //]
  115. inline
  116. my_string::my_string()
  117. {
  118. storage_[0] = 0;
  119. }
  120. inline
  121. my_string::my_string(const_iterator beg, const_iterator end)
  122. {
  123. std::size_t const sz = end ? (end - beg) : strlen(beg);
  124. BOOST_ASSERT(sz < size_);
  125. memcpy(storage_, beg, sz); storage_[sz] = 0;
  126. }
  127. inline
  128. my_string&
  129. my_string::operator=(char const* str)
  130. {
  131. BOOST_ASSERT(strlen(str) < size_);
  132. strcpy(storage_, str);
  133. return *this;
  134. }
  135. inline bool operator==(char const* s1, my_string const& s2) { return strcmp(s1, s2.c_str()) == 0; }
  136. inline bool operator==(my_string const& s1, char const* s2) { return strcmp(s2, s1.c_str()) == 0; }
  137. namespace test
  138. {
  139. struct cnv
  140. {
  141. #if defined(__QNXNTO__)
  142. static bool const is_qnx = true;
  143. #else
  144. static bool const is_qnx = false;
  145. #endif
  146. #if defined(_MSC_VER) && _MSC_VER < 1900
  147. static bool const is_msc = true;
  148. static bool const is_old_msc = true;
  149. #elif defined(_MSC_VER)
  150. static bool const is_msc = true;
  151. static bool const is_old_msc = false;
  152. #elif defined(__MINGW32__)
  153. static bool const is_msc = true;
  154. static bool const is_old_msc = true;
  155. #else
  156. static bool const is_msc = false;
  157. static bool const is_old_msc = false;
  158. #endif
  159. };
  160. }
  161. #endif // BOOST_CONVERT_TEST_HPP