unit-algorithms.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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("algorithms")
  29. {
  30. json j_array = {13, 29, 3, {{"one", 1}, {"two", 2}}, true, false, {1, 2, 3}, "foo", "baz"};
  31. json j_object = {{"one", 1}, {"two", 2}};
  32. SECTION("non-modifying sequence operations")
  33. {
  34. SECTION("std::all_of")
  35. {
  36. CHECK(std::all_of(j_array.begin(), j_array.end(), [](const json & value)
  37. {
  38. return value.size() > 0;
  39. }));
  40. CHECK(std::all_of(j_object.begin(), j_object.end(), [](const json & value)
  41. {
  42. return value.type() == json::value_t::number_integer;
  43. }));
  44. }
  45. SECTION("std::any_of")
  46. {
  47. CHECK(std::any_of(j_array.begin(), j_array.end(), [](const json & value)
  48. {
  49. return value.is_string() and value.get<std::string>() == "foo";
  50. }));
  51. CHECK(std::any_of(j_object.begin(), j_object.end(), [](const json & value)
  52. {
  53. return value.get<int>() > 1;
  54. }));
  55. }
  56. SECTION("std::none_of")
  57. {
  58. CHECK(std::none_of(j_array.begin(), j_array.end(), [](const json & value)
  59. {
  60. return value.size() == 0;
  61. }));
  62. CHECK(std::none_of(j_object.begin(), j_object.end(), [](const json & value)
  63. {
  64. return value.get<int>() <= 0;
  65. }));
  66. }
  67. SECTION("std::for_each")
  68. {
  69. SECTION("reading")
  70. {
  71. int sum = 0;
  72. std::for_each(j_array.cbegin(), j_array.cend(), [&sum](const json & value)
  73. {
  74. if (value.is_number())
  75. {
  76. sum += static_cast<int>(value);
  77. }
  78. });
  79. CHECK(sum == 45);
  80. }
  81. SECTION("writing")
  82. {
  83. auto add17 = [](json & value)
  84. {
  85. if (value.is_array())
  86. {
  87. value.push_back(17);
  88. }
  89. };
  90. std::for_each(j_array.begin(), j_array.end(), add17);
  91. CHECK(j_array[6] == json({1, 2, 3, 17}));
  92. }
  93. }
  94. SECTION("std::count")
  95. {
  96. CHECK(std::count(j_array.begin(), j_array.end(), json(true)) == 1);
  97. }
  98. SECTION("std::count_if")
  99. {
  100. CHECK(std::count_if(j_array.begin(), j_array.end(), [](const json & value)
  101. {
  102. return (value.is_number());
  103. }) == 3);
  104. CHECK(std::count_if(j_array.begin(), j_array.end(), [](const json&)
  105. {
  106. return true;
  107. }) == 9);
  108. }
  109. SECTION("std::mismatch")
  110. {
  111. json j_array2 = {13, 29, 3, {{"one", 1}, {"two", 2}, {"three", 3}}, true, false, {1, 2, 3}, "foo", "baz"};
  112. auto res = std::mismatch(j_array.begin(), j_array.end(), j_array2.begin());
  113. CHECK(*res.first == json({{"one", 1}, {"two", 2}}));
  114. CHECK(*res.second == json({{"one", 1}, {"two", 2}, {"three", 3}}));
  115. }
  116. SECTION("std::equal")
  117. {
  118. SECTION("using operator==")
  119. {
  120. CHECK(std::equal(j_array.begin(), j_array.end(), j_array.begin()));
  121. CHECK(std::equal(j_object.begin(), j_object.end(), j_object.begin()));
  122. CHECK(not std::equal(j_array.begin(), j_array.end(), j_object.begin()));
  123. }
  124. SECTION("using user-defined comparison")
  125. {
  126. // compare objects only by size of its elements
  127. json j_array2 = {13, 29, 3, {"Hello", "World"}, true, false, {{"one", 1}, {"two", 2}, {"three", 3}}, "foo", "baz"};
  128. CHECK(not std::equal(j_array.begin(), j_array.end(), j_array2.begin()));
  129. CHECK(std::equal(j_array.begin(), j_array.end(), j_array2.begin(),
  130. [](const json & a, const json & b)
  131. {
  132. return (a.size() == b.size());
  133. }));
  134. }
  135. }
  136. SECTION("std::find")
  137. {
  138. auto it = std::find(j_array.begin(), j_array.end(), json(false));
  139. CHECK(std::distance(j_array.begin(), it) == 5);
  140. }
  141. SECTION("std::find_if")
  142. {
  143. auto it = std::find_if(j_array.begin(), j_array.end(),
  144. [](const json & value)
  145. {
  146. return value.is_boolean();
  147. });
  148. CHECK(std::distance(j_array.begin(), it) == 4);
  149. }
  150. SECTION("std::find_if_not")
  151. {
  152. auto it = std::find_if_not(j_array.begin(), j_array.end(),
  153. [](const json & value)
  154. {
  155. return value.is_number();
  156. });
  157. CHECK(std::distance(j_array.begin(), it) == 3);
  158. }
  159. SECTION("std::adjacent_find")
  160. {
  161. CHECK(std::adjacent_find(j_array.begin(), j_array.end()) == j_array.end());
  162. CHECK(std::adjacent_find(j_array.begin(), j_array.end(),
  163. [](const json & v1, const json & v2)
  164. {
  165. return v1.type() == v2.type();
  166. }) == j_array.begin());
  167. }
  168. }
  169. SECTION("modifying sequence operations")
  170. {
  171. SECTION("std::reverse")
  172. {
  173. std::reverse(j_array.begin(), j_array.end());
  174. CHECK(j_array == json({"baz", "foo", {1, 2, 3}, false, true, {{"one", 1}, {"two", 2}}, 3, 29, 13}));
  175. }
  176. SECTION("std::rotate")
  177. {
  178. std::rotate(j_array.begin(), j_array.begin() + 1, j_array.end());
  179. CHECK(j_array == json({29, 3, {{"one", 1}, {"two", 2}}, true, false, {1, 2, 3}, "foo", "baz", 13}));
  180. }
  181. SECTION("std::partition")
  182. {
  183. auto it = std::partition(j_array.begin(), j_array.end(), [](const json & v)
  184. {
  185. return v.is_string();
  186. });
  187. CHECK(std::distance(j_array.begin(), it) == 2);
  188. CHECK(not it[2].is_string());
  189. }
  190. }
  191. SECTION("sorting operations")
  192. {
  193. SECTION("std::sort")
  194. {
  195. SECTION("with standard comparison")
  196. {
  197. json j = {13, 29, 3, {{"one", 1}, {"two", 2}}, true, false, {1, 2, 3}, "foo", "baz", nullptr};
  198. std::sort(j.begin(), j.end());
  199. CHECK(j == json({nullptr, false, true, 3, 13, 29, {{"one", 1}, {"two", 2}}, {1, 2, 3}, "baz", "foo"}));
  200. }
  201. SECTION("with user-defined comparison")
  202. {
  203. json j = {3, {{"one", 1}, {"two", 2}}, {1, 2, 3}, nullptr};
  204. std::sort(j.begin(), j.end(), [](const json & a, const json & b)
  205. {
  206. return a.size() < b.size();
  207. });
  208. CHECK(j == json({nullptr, 3, {{"one", 1}, {"two", 2}}, {1, 2, 3}}));
  209. }
  210. SECTION("sorting an object")
  211. {
  212. json j({{"one", 1}, {"two", 2}});
  213. CHECK_THROWS_AS(std::sort(j.begin(), j.end()), json::invalid_iterator&);
  214. CHECK_THROWS_WITH(std::sort(j.begin(), j.end()),
  215. "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
  216. }
  217. }
  218. SECTION("std::partial_sort")
  219. {
  220. json j = {13, 29, 3, {{"one", 1}, {"two", 2}}, true, false, {1, 2, 3}, "foo", "baz", nullptr};
  221. std::partial_sort(j.begin(), j.begin() + 4, j.end());
  222. CHECK(j == json({nullptr, false, true, 3, {{"one", 1}, {"two", 2}}, 29, {1, 2, 3}, "foo", "baz", 13}));
  223. }
  224. }
  225. SECTION("set operations")
  226. {
  227. SECTION("std::merge")
  228. {
  229. {
  230. json j1 = {2, 4, 6, 8};
  231. json j2 = {1, 2, 3, 5, 7};
  232. json j3;
  233. std::merge(j1.begin(), j1.end(), j2.begin(), j2.end(), std::back_inserter(j3));
  234. CHECK(j3 == json({1, 2, 2, 3, 4, 5, 6, 7, 8}));
  235. }
  236. }
  237. SECTION("std::set_difference")
  238. {
  239. json j1 = {1, 2, 3, 4, 5, 6, 7, 8};
  240. json j2 = {1, 2, 3, 5, 7};
  241. json j3;
  242. std::set_difference(j1.begin(), j1.end(), j2.begin(), j2.end(), std::back_inserter(j3));
  243. CHECK(j3 == json({4, 6, 8}));
  244. }
  245. SECTION("std::set_intersection")
  246. {
  247. json j1 = {1, 2, 3, 4, 5, 6, 7, 8};
  248. json j2 = {1, 2, 3, 5, 7};
  249. json j3;
  250. std::set_intersection(j1.begin(), j1.end(), j2.begin(), j2.end(), std::back_inserter(j3));
  251. CHECK(j3 == json({1, 2, 3, 5, 7}));
  252. }
  253. SECTION("std::set_union")
  254. {
  255. json j1 = {2, 4, 6, 8};
  256. json j2 = {1, 2, 3, 5, 7};
  257. json j3;
  258. std::set_union(j1.begin(), j1.end(), j2.begin(), j2.end(), std::back_inserter(j3));
  259. CHECK(j3 == json({1, 2, 3, 4, 5, 6, 7, 8}));
  260. }
  261. SECTION("std::set_symmetric_difference")
  262. {
  263. json j1 = {2, 4, 6, 8};
  264. json j2 = {1, 2, 3, 5, 7};
  265. json j3;
  266. std::set_symmetric_difference(j1.begin(), j1.end(), j2.begin(), j2.end(), std::back_inserter(j3));
  267. CHECK(j3 == json({1, 3, 4, 5, 6, 7, 8}));
  268. }
  269. }
  270. SECTION("heap operations")
  271. {
  272. std::make_heap(j_array.begin(), j_array.end());
  273. CHECK(std::is_heap(j_array.begin(), j_array.end()));
  274. std::sort_heap(j_array.begin(), j_array.end());
  275. CHECK(j_array == json({false, true, 3, 13, 29, {{"one", 1}, {"two", 2}}, {1, 2, 3}, "baz", "foo"}));
  276. }
  277. }