unit-concepts.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. __ _____ _____ _____
  3. __| | __| | | | JSON for Modern C++ (test suite)
  4. | | |__ | | | | | | version 3.7.3
  5. |_____|_____|_____|_|___| https://github.com/nlohmann/json
  6. Licensed under the MIT License <http://opensource.org/licenses/MIT>.
  7. SPDX-License-Identifier: MIT
  8. Copyright (c) 2013-2019 Niels Lohmann <http://nlohmann.me>.
  9. Permission is hereby granted, free of charge, to any person obtaining a copy
  10. of this software and associated documentation files (the "Software"), to deal
  11. in the Software without restriction, including without limitation the rights
  12. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. copies of the Software, and to permit persons to whom the Software is
  14. furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be included in all
  16. copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. SOFTWARE.
  24. */
  25. #include "doctest_compatibility.h"
  26. #include <nlohmann/json.hpp>
  27. using nlohmann::json;
  28. TEST_CASE("concepts")
  29. {
  30. SECTION("container requirements for json")
  31. {
  32. // X: container class: json
  33. // T: type of objects: json
  34. // a, b: values of type X: json
  35. // TABLE 96 - Container Requirements
  36. // X::value_type must return T
  37. CHECK((std::is_same<json::value_type, json>::value));
  38. // X::reference must return lvalue of T
  39. CHECK((std::is_same<json::reference, json&>::value));
  40. // X::const_reference must return const lvalue of T
  41. CHECK((std::is_same<json::const_reference, const json&>::value));
  42. // X::iterator must return iterator whose value_type is T
  43. CHECK((std::is_same<json::iterator::value_type, json>::value));
  44. // X::iterator must meet the forward iterator requirements
  45. CHECK((std::is_base_of<std::forward_iterator_tag, typename std::iterator_traits<json::iterator>::iterator_category>::value));
  46. // X::iterator must be convertible to X::const_iterator
  47. CHECK((std::is_convertible<json::iterator, json::const_iterator>::value));
  48. // X::const_iterator must return iterator whose value_type is T
  49. CHECK((std::is_same<json::const_iterator::value_type, json>::value));
  50. // X::const_iterator must meet the forward iterator requirements
  51. CHECK((std::is_base_of<std::forward_iterator_tag, typename std::iterator_traits<json::const_iterator>::iterator_category>::value));
  52. // X::difference_type must return a signed integer
  53. CHECK((std::is_signed<json::difference_type>::value));
  54. // X::difference_type must be identical to X::iterator::difference_type
  55. CHECK((std::is_same<json::difference_type, json::iterator::difference_type>::value));
  56. // X::difference_type must be identical to X::const_iterator::difference_type
  57. CHECK((std::is_same<json::difference_type, json::const_iterator::difference_type>::value));
  58. // X::size_type must return an unsigned integer
  59. CHECK((std::is_unsigned<json::size_type>::value));
  60. // X::size_type can represent any non-negative value of X::difference_type
  61. CHECK(static_cast<json::size_type>((std::numeric_limits<json::difference_type>::max)()) <=
  62. (std::numeric_limits<json::size_type>::max)());
  63. // the expression "X u" has the post-condition "u.empty()"
  64. {
  65. json u;
  66. CHECK(u.empty());
  67. }
  68. // the expression "X()" has the post-condition "X().empty()"
  69. CHECK(json().empty());
  70. }
  71. SECTION("class json")
  72. {
  73. SECTION("DefaultConstructible")
  74. {
  75. CHECK(std::is_nothrow_default_constructible<json>::value);
  76. }
  77. SECTION("MoveConstructible")
  78. {
  79. CHECK(std::is_move_constructible<json>::value);
  80. CHECK(std::is_nothrow_move_constructible<json>::value);
  81. }
  82. SECTION("CopyConstructible")
  83. {
  84. CHECK(std::is_copy_constructible<json>::value);
  85. }
  86. SECTION("MoveAssignable")
  87. {
  88. CHECK(std::is_nothrow_move_assignable<json>::value);
  89. }
  90. SECTION("CopyAssignable")
  91. {
  92. CHECK(std::is_copy_assignable<json>::value);
  93. }
  94. SECTION("Destructible")
  95. {
  96. CHECK(std::is_nothrow_destructible<json>::value);
  97. }
  98. SECTION("StandardLayoutType")
  99. {
  100. CHECK(std::is_standard_layout<json>::value);
  101. }
  102. }
  103. SECTION("class iterator")
  104. {
  105. SECTION("CopyConstructible")
  106. {
  107. CHECK(std::is_nothrow_copy_constructible<json::iterator>::value);
  108. CHECK(std::is_nothrow_copy_constructible<json::const_iterator>::value);
  109. }
  110. SECTION("CopyAssignable")
  111. {
  112. // STL iterators used by json::iterator don't pass this test in Debug mode
  113. #if !defined(_MSC_VER) || (_ITERATOR_DEBUG_LEVEL == 0)
  114. CHECK(std::is_nothrow_copy_assignable<json::iterator>::value);
  115. CHECK(std::is_nothrow_copy_assignable<json::const_iterator>::value);
  116. #endif
  117. }
  118. SECTION("Destructible")
  119. {
  120. CHECK(std::is_nothrow_destructible<json::iterator>::value);
  121. CHECK(std::is_nothrow_destructible<json::const_iterator>::value);
  122. }
  123. SECTION("Swappable")
  124. {
  125. {
  126. json j {1, 2, 3};
  127. json::iterator it1 = j.begin();
  128. json::iterator it2 = j.end();
  129. std::swap(it1, it2);
  130. CHECK(it1 == j.end());
  131. CHECK(it2 == j.begin());
  132. }
  133. {
  134. json j {1, 2, 3};
  135. json::const_iterator it1 = j.cbegin();
  136. json::const_iterator it2 = j.cend();
  137. std::swap(it1, it2);
  138. CHECK(it1 == j.end());
  139. CHECK(it2 == j.begin());
  140. }
  141. }
  142. }
  143. }