unlimited_storage_test.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. // Copyright 2015-2017 Hans Dembinski
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <algorithm>
  7. #include <boost/core/lightweight_test.hpp>
  8. #include <boost/core/lightweight_test_trait.hpp>
  9. #include <boost/histogram/detail/detect.hpp>
  10. #include <boost/histogram/storage_adaptor.hpp>
  11. #include <boost/histogram/unlimited_storage.hpp>
  12. #include <boost/histogram/unsafe_access.hpp>
  13. #include <boost/mp11.hpp>
  14. #include <iosfwd>
  15. #include <limits>
  16. #include <memory>
  17. #include <numeric>
  18. #include <vector>
  19. #include "std_ostream.hpp"
  20. #include "throw_exception.hpp"
  21. #include "utility_allocator.hpp"
  22. namespace boost {
  23. namespace histogram {
  24. namespace detail {
  25. template <class Allocator>
  26. std::ostream& operator<<(std::ostream& os, const large_int<Allocator>& x) {
  27. os << "large_int";
  28. os << x.data;
  29. return os;
  30. }
  31. } // namespace detail
  32. } // namespace histogram
  33. } // namespace boost
  34. using namespace boost::histogram;
  35. using unlimited_storage_type = unlimited_storage<>;
  36. template <typename T>
  37. using vector_storage = storage_adaptor<std::vector<T>>;
  38. using large_int = unlimited_storage_type::large_int;
  39. template <typename T = std::uint8_t>
  40. unlimited_storage_type prepare(std::size_t n, T x = T{}) {
  41. std::unique_ptr<T[]> v(new T[n]);
  42. std::fill(v.get(), v.get() + n, static_cast<T>(0));
  43. v.get()[0] = x;
  44. return unlimited_storage_type(n, v.get());
  45. }
  46. template <class T>
  47. auto limits_max() {
  48. return (std::numeric_limits<T>::max)();
  49. }
  50. template <>
  51. inline auto limits_max<large_int>() {
  52. return large_int(limits_max<uint64_t>());
  53. }
  54. template <typename T>
  55. void copy() {
  56. const auto b = prepare<T>(1);
  57. auto a(b);
  58. BOOST_TEST(a == b);
  59. ++a[0];
  60. BOOST_TEST(!(a == b));
  61. a = b;
  62. BOOST_TEST(a == b);
  63. ++a[0];
  64. BOOST_TEST(!(a == b));
  65. a = prepare<T>(2);
  66. BOOST_TEST(!(a == b));
  67. a = b;
  68. BOOST_TEST(a == b);
  69. }
  70. template <typename T>
  71. void equal_1() {
  72. auto a = prepare(1);
  73. auto b = prepare(1, T(0));
  74. BOOST_TEST_EQ(a[0], 0.0);
  75. BOOST_TEST(a == b);
  76. ++b[0];
  77. BOOST_TEST(!(a == b));
  78. }
  79. template <typename T, typename U>
  80. void equal_2() {
  81. auto a = prepare<T>(1);
  82. vector_storage<U> b;
  83. b.reset(1);
  84. BOOST_TEST(a == b);
  85. ++b[0];
  86. BOOST_TEST(!(a == b));
  87. }
  88. template <typename T>
  89. void increase_and_grow() {
  90. auto tmax = limits_max<T>();
  91. auto s = prepare(2, tmax);
  92. auto n = s;
  93. auto n2 = s;
  94. ++n[0];
  95. auto x = prepare(2);
  96. ++x[0];
  97. n2[0] += x[0];
  98. auto v = static_cast<double>(tmax);
  99. ++v;
  100. BOOST_TEST_EQ(n[0], v);
  101. BOOST_TEST_EQ(n2[0], v);
  102. BOOST_TEST_EQ(n[1], 0.0);
  103. BOOST_TEST_EQ(n2[1], 0.0);
  104. }
  105. template <typename T>
  106. void convert_foreign_storage() {
  107. {
  108. vector_storage<T> s;
  109. s.reset(1);
  110. ++s[0];
  111. BOOST_TEST_EQ(s[0], 1);
  112. // test converting copy ctor
  113. unlimited_storage_type u(s);
  114. using buffer_t = std::decay_t<decltype(unsafe_access::unlimited_storage_buffer(u))>;
  115. BOOST_TEST_EQ(unsafe_access::unlimited_storage_buffer(u).type,
  116. buffer_t::template type_index<T>());
  117. BOOST_TEST(u == s);
  118. BOOST_TEST_EQ(u.size(), 1u);
  119. BOOST_TEST_EQ(u[0], 1.0);
  120. ++s[0];
  121. BOOST_TEST_NOT(u == s);
  122. }
  123. vector_storage<uint8_t> s;
  124. s.reset(1);
  125. ++s[0];
  126. // test assign and equal
  127. auto a = prepare<T>(1);
  128. a = s;
  129. BOOST_TEST_EQ(a[0], 1.0);
  130. BOOST_TEST(a == s);
  131. ++a[0];
  132. BOOST_TEST_NOT(a == s);
  133. // test radd
  134. auto c = prepare<T>(1);
  135. c[0] += s[0];
  136. BOOST_TEST_EQ(c[0], 1);
  137. BOOST_TEST(c == s);
  138. c[0] += s[0];
  139. BOOST_TEST_EQ(c[0], 2);
  140. BOOST_TEST_NOT(c == s);
  141. // test assign from float
  142. vector_storage<float> t;
  143. t.reset(1);
  144. t[0] = 1.5;
  145. auto d = prepare<T>(1);
  146. d = t;
  147. BOOST_TEST(d == t);
  148. BOOST_TEST(d[0] == 1.5);
  149. // test "copy" ctor from float
  150. unlimited_storage_type f(t);
  151. BOOST_TEST_EQ(f[0], 1.5);
  152. BOOST_TEST(f == t);
  153. // test radd from float
  154. auto g = prepare<T>(1);
  155. g[0] += t[0];
  156. BOOST_TEST_EQ(g[0], 1.5);
  157. BOOST_TEST(g == t);
  158. vector_storage<int8_t> u;
  159. u.reset(1);
  160. u[0] = -10;
  161. auto h = prepare<T>(1);
  162. BOOST_TEST_NOT(h == u);
  163. h = u;
  164. BOOST_TEST(h == u);
  165. BOOST_TEST_EQ(h[0], -10);
  166. h[0] -= u[0];
  167. BOOST_TEST_EQ(h[0], 0);
  168. }
  169. struct adder {
  170. template <class LHS, class RHS>
  171. void operator()(boost::mp11::mp_list<LHS, RHS>) {
  172. using buffer_type =
  173. std::remove_reference_t<decltype(unsafe_access::unlimited_storage_buffer(
  174. std::declval<unlimited_storage_type&>()))>;
  175. constexpr auto iLHS = buffer_type::template type_index<LHS>();
  176. constexpr auto iRHS = buffer_type::template type_index<RHS>();
  177. {
  178. auto a = prepare<LHS>(1);
  179. BOOST_TEST_EQ(unsafe_access::unlimited_storage_buffer(a).type, iLHS);
  180. a[0] += static_cast<RHS>(2);
  181. // LHS is never downgraded, only upgraded to RHS.
  182. // If RHS is normal integer, LHS doesn't change.
  183. BOOST_TEST_EQ(unsafe_access::unlimited_storage_buffer(a).type,
  184. iRHS < 4 ? iLHS : (std::max)(iLHS, iRHS));
  185. BOOST_TEST_EQ(a[0], 2);
  186. }
  187. {
  188. auto a = prepare<LHS>(1);
  189. a[0] += 2;
  190. BOOST_TEST_EQ(a[0], 2);
  191. // subtracting converts to double
  192. a[0] -= 2;
  193. BOOST_TEST_EQ(unsafe_access::unlimited_storage_buffer(a).type, 5);
  194. BOOST_TEST_EQ(a[0], 0);
  195. }
  196. {
  197. auto a = prepare<LHS>(1);
  198. auto b = prepare<RHS>(1, static_cast<RHS>(2u));
  199. // LHS is never downgraded, only upgraded to RHS.
  200. // If RHS is normal integer, LHS doesn't change.
  201. a[0] += b[0];
  202. BOOST_TEST_EQ(unsafe_access::unlimited_storage_buffer(a).type,
  203. iRHS < 4 ? iLHS : (std::max)(iLHS, iRHS));
  204. BOOST_TEST_EQ(a[0], 2);
  205. a[0] -= b[0];
  206. BOOST_TEST_EQ(a[0], 0);
  207. a[0] -= b[0];
  208. BOOST_TEST_EQ(a[0], -2);
  209. }
  210. {
  211. auto a = prepare<LHS>(1);
  212. auto b = limits_max<RHS>();
  213. // LHS is never downgraded, only upgraded to RHS.
  214. // If RHS is normal integer, LHS doesn't change.
  215. a[0] += b;
  216. // BOOST_TEST_EQ(unsafe_access::unlimited_storage_buffer(a).type,
  217. // iRHS < 4 ? iLHS : std::max(iLHS, iRHS));
  218. BOOST_TEST_EQ(a[0], limits_max<RHS>());
  219. a[0] += prepare<RHS>(1, b)[0];
  220. // BOOST_TEST_EQ(unsafe_access::unlimited_storage_buffer(a).type,
  221. // iRHS < 4 ? iLHS + 1 : std::max(iLHS, iRHS));
  222. BOOST_TEST_EQ(a[0], 2 * double(limits_max<RHS>()));
  223. }
  224. }
  225. };
  226. int main() {
  227. // empty state
  228. {
  229. unlimited_storage_type a;
  230. BOOST_TEST_EQ(a.size(), 0);
  231. }
  232. // copy
  233. {
  234. copy<uint8_t>();
  235. copy<uint16_t>();
  236. copy<uint32_t>();
  237. copy<uint64_t>();
  238. copy<large_int>();
  239. copy<double>();
  240. }
  241. // equal_operator
  242. {
  243. equal_1<uint8_t>();
  244. equal_1<uint16_t>();
  245. equal_1<uint32_t>();
  246. equal_1<uint64_t>();
  247. equal_1<large_int>();
  248. equal_1<double>();
  249. equal_2<uint8_t, unsigned>();
  250. equal_2<uint16_t, unsigned>();
  251. equal_2<uint32_t, unsigned>();
  252. equal_2<uint64_t, unsigned>();
  253. equal_2<large_int, unsigned>();
  254. equal_2<double, unsigned>();
  255. equal_2<large_int, double>();
  256. auto a = prepare<double>(1);
  257. auto b = prepare<large_int>(1);
  258. BOOST_TEST(a == b);
  259. ++a[0];
  260. BOOST_TEST_NOT(a == b);
  261. }
  262. // increase_and_grow
  263. {
  264. increase_and_grow<uint8_t>();
  265. increase_and_grow<uint16_t>();
  266. increase_and_grow<uint32_t>();
  267. increase_and_grow<uint64_t>();
  268. // only increase for large_int
  269. auto a = prepare<large_int>(2, static_cast<large_int>(1));
  270. BOOST_TEST_EQ(a[0], 1);
  271. BOOST_TEST_EQ(a[1], 0);
  272. ++a[0];
  273. BOOST_TEST_EQ(a[0], 2);
  274. BOOST_TEST_EQ(a[1], 0);
  275. }
  276. // add
  277. {
  278. using namespace boost::mp11;
  279. using L = mp_list<uint8_t, uint16_t, uint64_t, large_int, double>;
  280. mp_for_each<mp_product<mp_list, L, L>>(adder());
  281. }
  282. // add_and_grow
  283. {
  284. auto a = prepare(1);
  285. a[0] += a[0];
  286. BOOST_TEST_EQ(a[0], 0);
  287. ++a[0];
  288. double x = 1;
  289. auto b = prepare(1);
  290. ++b[0];
  291. BOOST_TEST_EQ(b[0], x);
  292. for (unsigned i = 0; i < 80; ++i) {
  293. x += x;
  294. a[0] += a[0];
  295. b[0] += b[0];
  296. BOOST_TEST_EQ(a[0], x);
  297. BOOST_TEST_EQ(b[0], x);
  298. auto c = prepare(1);
  299. c[0] += a[0];
  300. BOOST_TEST_EQ(c[0], x);
  301. c[0] += 0;
  302. BOOST_TEST_EQ(c[0], x);
  303. auto d = prepare(1);
  304. d[0] += x;
  305. BOOST_TEST_EQ(d[0], x);
  306. }
  307. }
  308. // multiply
  309. {
  310. auto a = prepare(2);
  311. ++a[0];
  312. a *= 3;
  313. BOOST_TEST_EQ(a[0], 3);
  314. BOOST_TEST_EQ(a[1], 0);
  315. a[1] += 2;
  316. a *= 3;
  317. BOOST_TEST_EQ(a[0], 9);
  318. BOOST_TEST_EQ(a[1], 6);
  319. }
  320. // convert_foreign_storage
  321. {
  322. convert_foreign_storage<uint8_t>();
  323. convert_foreign_storage<uint16_t>();
  324. convert_foreign_storage<uint32_t>();
  325. convert_foreign_storage<uint64_t>();
  326. convert_foreign_storage<large_int>();
  327. convert_foreign_storage<double>();
  328. }
  329. // reference
  330. {
  331. auto a = prepare(1);
  332. auto b = prepare<uint32_t>(1);
  333. BOOST_TEST_EQ(a[0], b[0]);
  334. BOOST_TEST_GE(a[0], b[0]);
  335. BOOST_TEST_LE(a[0], b[0]);
  336. a[0] = 1;
  337. BOOST_TEST_NE(a[0], b[0]);
  338. BOOST_TEST_LT(b[0], a[0]);
  339. BOOST_TEST_GT(a[0], b[0]);
  340. BOOST_TEST_EQ(a[0], 1);
  341. BOOST_TEST_GE(a[0], 1);
  342. BOOST_TEST_LE(a[0], 1);
  343. BOOST_TEST_NE(a[0], 2);
  344. BOOST_TEST_GT(2, a[0]);
  345. BOOST_TEST_LT(0, a[0]);
  346. BOOST_TEST_GE(1, a[0]);
  347. BOOST_TEST_GE(2, a[0]);
  348. BOOST_TEST_LE(0, a[0]);
  349. BOOST_TEST_LE(1, a[0]);
  350. BOOST_TEST_EQ(1, a[0]);
  351. BOOST_TEST_NE(2, a[0]);
  352. ++b[0];
  353. BOOST_TEST_EQ(a[0], b[0]);
  354. b[0] += 2;
  355. a[0] = b[0];
  356. BOOST_TEST_EQ(a[0], 3);
  357. a[0] -= 10;
  358. BOOST_TEST_EQ(a[0], -7);
  359. auto c = prepare(2);
  360. c[0] = c[1] = 1;
  361. BOOST_TEST_EQ(c[0], 1);
  362. BOOST_TEST_EQ(c[1], 1);
  363. auto d = prepare(2);
  364. d[1] = unlimited_storage_type::large_int{2};
  365. BOOST_TEST_EQ(unsafe_access::unlimited_storage_buffer(d).type, 4);
  366. d[0] = -2;
  367. BOOST_TEST_EQ(unsafe_access::unlimited_storage_buffer(d).type, 5);
  368. BOOST_TEST_EQ(d[0], -2);
  369. BOOST_TEST_EQ(d[1], 2);
  370. BOOST_TEST_TRAIT_TRUE((detail::has_operator_preincrement<decltype(d[0])>));
  371. }
  372. // iterators
  373. {
  374. using iterator = typename unlimited_storage_type::iterator;
  375. using value_type = typename std::iterator_traits<iterator>::value_type;
  376. using reference = typename std::iterator_traits<iterator>::reference;
  377. BOOST_TEST_TRAIT_SAME(value_type, double);
  378. BOOST_TEST_TRAIT_FALSE((std::is_same<reference, double&>));
  379. auto a = prepare(2);
  380. for (auto&& x : a) BOOST_TEST_EQ(x, 0);
  381. std::vector<double> b(2, 1);
  382. std::copy(b.begin(), b.end(), a.begin());
  383. const auto& aconst = a;
  384. BOOST_TEST(std::equal(aconst.begin(), aconst.end(), b.begin(), b.end()));
  385. unlimited_storage_type::iterator it1 = a.begin();
  386. BOOST_TEST_EQ(*it1, 1);
  387. *it1 = 3;
  388. BOOST_TEST_EQ(*it1, 3);
  389. unlimited_storage_type::const_iterator it2 = a.begin();
  390. BOOST_TEST_EQ(*it2, 3);
  391. unlimited_storage_type::const_iterator it3 = aconst.begin();
  392. BOOST_TEST_EQ(*it3, 3);
  393. std::copy(b.begin(), b.end(), a.begin());
  394. std::partial_sum(a.begin(), a.end(), a.begin());
  395. BOOST_TEST_EQ(a[0], 1);
  396. BOOST_TEST_EQ(a[1], 2);
  397. }
  398. // memory exhaustion
  399. {
  400. using S = unlimited_storage<tracing_allocator<char>>;
  401. using alloc_t = typename S::allocator_type;
  402. {
  403. // check that large_int allocates in ctor
  404. tracing_allocator_db db;
  405. typename S::large_int li{1, alloc_t{db}};
  406. BOOST_TEST_GT(db.first, 0);
  407. }
  408. tracing_allocator_db db;
  409. // db.tracing = true; // uncomment this to monitor allocator activity
  410. S s(alloc_t{db});
  411. s.reset(10); // should work
  412. BOOST_TEST_EQ(db.at<uint8_t>().first, 10);
  413. #ifndef BOOST_NO_EXCEPTIONS
  414. db.failure_countdown = 0;
  415. BOOST_TEST_THROWS(s.reset(5), std::bad_alloc);
  416. // storage must be still in valid state
  417. BOOST_TEST_EQ(s.size(), 0);
  418. auto& buffer = unsafe_access::unlimited_storage_buffer(s);
  419. BOOST_TEST_EQ(buffer.ptr, nullptr);
  420. BOOST_TEST_EQ(buffer.type, 0);
  421. // all allocated memory should have returned
  422. BOOST_TEST_EQ(db.first, 0);
  423. // test failure in buffer.make<large_int>(n, iter), AT::construct
  424. s.reset(3);
  425. s[1] = (std::numeric_limits<std::uint64_t>::max)();
  426. db.failure_countdown = 2;
  427. const auto old_ptr = buffer.ptr;
  428. BOOST_TEST_THROWS(++s[1], std::bad_alloc);
  429. // storage remains in previous state
  430. BOOST_TEST_EQ(buffer.size, 3);
  431. BOOST_TEST_EQ(buffer.ptr, old_ptr);
  432. BOOST_TEST_EQ(buffer.type, 3);
  433. // test buffer.make<large_int>(n), AT::construct, called by serialization code
  434. db.failure_countdown = 1;
  435. BOOST_TEST_THROWS(buffer.make<typename S::large_int>(2), std::bad_alloc);
  436. // storage still in valid state
  437. BOOST_TEST_EQ(s.size(), 0);
  438. BOOST_TEST_EQ(buffer.ptr, nullptr);
  439. BOOST_TEST_EQ(buffer.type, 0);
  440. // all memory returned
  441. BOOST_TEST_EQ(db.first, 0);
  442. #endif
  443. }
  444. return boost::report_errors();
  445. }