unit-concepts.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // __ _____ _____ _____
  2. // __| | __| | | | JSON for Modern C++ (supporting code)
  3. // | | |__ | | | | | | version 3.11.2
  4. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5. //
  6. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  7. // SPDX-License-Identifier: MIT
  8. #include "doctest_compatibility.h"
  9. #include <nlohmann/json.hpp>
  10. using nlohmann::json;
  11. TEST_CASE("concepts")
  12. {
  13. SECTION("container requirements for json")
  14. {
  15. // X: container class: json
  16. // T: type of objects: json
  17. // a, b: values of type X: json
  18. // TABLE 96 - Container Requirements
  19. // X::value_type must return T
  20. CHECK((std::is_same<json::value_type, json>::value));
  21. // X::reference must return lvalue of T
  22. CHECK((std::is_same<json::reference, json&>::value));
  23. // X::const_reference must return const lvalue of T
  24. CHECK((std::is_same<json::const_reference, const json&>::value));
  25. // X::iterator must return iterator whose value_type is T
  26. CHECK((std::is_same<json::iterator::value_type, json>::value));
  27. // X::iterator must meet the forward iterator requirements
  28. CHECK((std::is_base_of<std::forward_iterator_tag, typename std::iterator_traits<json::iterator>::iterator_category>::value));
  29. // X::iterator must be convertible to X::const_iterator
  30. CHECK((std::is_convertible<json::iterator, json::const_iterator>::value));
  31. // X::const_iterator must return iterator whose value_type is T
  32. CHECK((std::is_same<json::const_iterator::value_type, json>::value));
  33. // X::const_iterator must meet the forward iterator requirements
  34. CHECK((std::is_base_of<std::forward_iterator_tag, typename std::iterator_traits<json::const_iterator>::iterator_category>::value));
  35. // X::difference_type must return a signed integer
  36. CHECK((std::is_signed<json::difference_type>::value));
  37. // X::difference_type must be identical to X::iterator::difference_type
  38. CHECK((std::is_same<json::difference_type, json::iterator::difference_type>::value));
  39. // X::difference_type must be identical to X::const_iterator::difference_type
  40. CHECK((std::is_same<json::difference_type, json::const_iterator::difference_type>::value));
  41. // X::size_type must return an unsigned integer
  42. CHECK((std::is_unsigned<json::size_type>::value));
  43. // X::size_type can represent any non-negative value of X::difference_type
  44. CHECK(static_cast<json::size_type>((std::numeric_limits<json::difference_type>::max)()) <=
  45. (std::numeric_limits<json::size_type>::max)());
  46. // the expression "X u" has the post-condition "u.empty()"
  47. {
  48. const json u;
  49. CHECK(u.empty());
  50. }
  51. // the expression "X()" has the post-condition "X().empty()"
  52. CHECK(json().empty());
  53. }
  54. SECTION("class json")
  55. {
  56. SECTION("DefaultConstructible")
  57. {
  58. CHECK(std::is_nothrow_default_constructible<json>::value);
  59. }
  60. SECTION("MoveConstructible")
  61. {
  62. CHECK(std::is_move_constructible<json>::value);
  63. CHECK(std::is_nothrow_move_constructible<json>::value);
  64. }
  65. SECTION("CopyConstructible")
  66. {
  67. CHECK(std::is_copy_constructible<json>::value);
  68. }
  69. SECTION("MoveAssignable")
  70. {
  71. CHECK(std::is_nothrow_move_assignable<json>::value);
  72. }
  73. SECTION("CopyAssignable")
  74. {
  75. CHECK(std::is_copy_assignable<json>::value);
  76. }
  77. SECTION("Destructible")
  78. {
  79. CHECK(std::is_nothrow_destructible<json>::value);
  80. }
  81. SECTION("StandardLayoutType")
  82. {
  83. CHECK(std::is_standard_layout<json>::value);
  84. }
  85. }
  86. SECTION("class iterator")
  87. {
  88. SECTION("CopyConstructible")
  89. {
  90. CHECK(std::is_nothrow_copy_constructible<json::iterator>::value);
  91. CHECK(std::is_nothrow_copy_constructible<json::const_iterator>::value);
  92. }
  93. SECTION("CopyAssignable")
  94. {
  95. // STL iterators used by json::iterator don't pass this test in Debug mode
  96. #if !defined(_MSC_VER) || (_ITERATOR_DEBUG_LEVEL == 0)
  97. CHECK(std::is_nothrow_copy_assignable<json::iterator>::value);
  98. CHECK(std::is_nothrow_copy_assignable<json::const_iterator>::value);
  99. #endif
  100. }
  101. SECTION("Destructible")
  102. {
  103. CHECK(std::is_nothrow_destructible<json::iterator>::value);
  104. CHECK(std::is_nothrow_destructible<json::const_iterator>::value);
  105. }
  106. SECTION("Swappable")
  107. {
  108. {
  109. json j {1, 2, 3};
  110. json::iterator it1 = j.begin();
  111. json::iterator it2 = j.end();
  112. swap(it1, it2);
  113. CHECK(it1 == j.end());
  114. CHECK(it2 == j.begin());
  115. }
  116. {
  117. json j {1, 2, 3};
  118. json::const_iterator it1 = j.cbegin();
  119. json::const_iterator it2 = j.cend();
  120. swap(it1, it2);
  121. CHECK(it1 == j.end());
  122. CHECK(it2 == j.begin());
  123. }
  124. }
  125. }
  126. }