unit-alt-string.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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-FileCopyrightText: 2018 Vitaliy Manushkin <agri@akamo.info>
  8. // SPDX-License-Identifier: MIT
  9. #include "doctest_compatibility.h"
  10. #include <nlohmann/json.hpp>
  11. #include <string>
  12. #include <utility>
  13. /* forward declarations */
  14. class alt_string;
  15. bool operator<(const char* op1, const alt_string& op2) noexcept;
  16. void int_to_string(alt_string& target, std::size_t value);
  17. /*
  18. * This is virtually a string class.
  19. * It covers std::string under the hood.
  20. */
  21. class alt_string
  22. {
  23. public:
  24. using value_type = std::string::value_type;
  25. static constexpr auto npos = static_cast<std::size_t>(-1);
  26. alt_string(const char* str): str_impl(str) {}
  27. alt_string(const char* str, std::size_t count): str_impl(str, count) {}
  28. alt_string(size_t count, char chr): str_impl(count, chr) {}
  29. alt_string() = default;
  30. template <typename...TParams>
  31. alt_string& append(TParams&& ...params)
  32. {
  33. str_impl.append(std::forward<TParams>(params)...);
  34. return *this;
  35. }
  36. void push_back(char c)
  37. {
  38. str_impl.push_back(c);
  39. }
  40. template <typename op_type>
  41. bool operator==(const op_type& op) const
  42. {
  43. return str_impl == op;
  44. }
  45. bool operator==(const alt_string& op) const
  46. {
  47. return str_impl == op.str_impl;
  48. }
  49. template <typename op_type>
  50. bool operator!=(const op_type& op) const
  51. {
  52. return str_impl != op;
  53. }
  54. bool operator!=(const alt_string& op) const
  55. {
  56. return str_impl != op.str_impl;
  57. }
  58. std::size_t size() const noexcept
  59. {
  60. return str_impl.size();
  61. }
  62. void resize (std::size_t n)
  63. {
  64. str_impl.resize(n);
  65. }
  66. void resize (std::size_t n, char c)
  67. {
  68. str_impl.resize(n, c);
  69. }
  70. template <typename op_type>
  71. bool operator<(const op_type& op) const noexcept
  72. {
  73. return str_impl < op;
  74. }
  75. bool operator<(const alt_string& op) const noexcept
  76. {
  77. return str_impl < op.str_impl;
  78. }
  79. const char* c_str() const
  80. {
  81. return str_impl.c_str();
  82. }
  83. char& operator[](std::size_t index)
  84. {
  85. return str_impl[index];
  86. }
  87. const char& operator[](std::size_t index) const
  88. {
  89. return str_impl[index];
  90. }
  91. char& back()
  92. {
  93. return str_impl.back();
  94. }
  95. const char& back() const
  96. {
  97. return str_impl.back();
  98. }
  99. void clear()
  100. {
  101. str_impl.clear();
  102. }
  103. const value_type* data() const
  104. {
  105. return str_impl.data();
  106. }
  107. bool empty() const
  108. {
  109. return str_impl.empty();
  110. }
  111. std::size_t find(const alt_string& str, std::size_t pos = 0) const
  112. {
  113. return str_impl.find(str.str_impl, pos);
  114. }
  115. std::size_t find_first_of(char c, std::size_t pos = 0) const
  116. {
  117. return str_impl.find_first_of(c, pos);
  118. }
  119. alt_string substr(std::size_t pos = 0, std::size_t count = npos) const
  120. {
  121. const std::string s = str_impl.substr(pos, count);
  122. return {s.data(), s.size()};
  123. }
  124. alt_string& replace(std::size_t pos, std::size_t count, const alt_string& str)
  125. {
  126. str_impl.replace(pos, count, str.str_impl);
  127. return *this;
  128. }
  129. private:
  130. std::string str_impl {};
  131. friend bool operator<(const char* /*op1*/, const alt_string& /*op2*/) noexcept;
  132. };
  133. void int_to_string(alt_string& target, std::size_t value)
  134. {
  135. target = std::to_string(value).c_str();
  136. }
  137. using alt_json = nlohmann::basic_json <
  138. std::map,
  139. std::vector,
  140. alt_string,
  141. bool,
  142. std::int64_t,
  143. std::uint64_t,
  144. double,
  145. std::allocator,
  146. nlohmann::adl_serializer >;
  147. bool operator<(const char* op1, const alt_string& op2) noexcept
  148. {
  149. return op1 < op2.str_impl;
  150. }
  151. TEST_CASE("alternative string type")
  152. {
  153. SECTION("dump")
  154. {
  155. {
  156. alt_json doc;
  157. doc["pi"] = 3.141;
  158. alt_string dump = doc.dump();
  159. CHECK(dump == R"({"pi":3.141})");
  160. }
  161. {
  162. alt_json doc;
  163. doc["happy"] = true;
  164. alt_string dump = doc.dump();
  165. CHECK(dump == R"({"happy":true})");
  166. }
  167. {
  168. alt_json doc;
  169. doc["name"] = "I'm Batman";
  170. alt_string dump = doc.dump();
  171. CHECK(dump == R"({"name":"I'm Batman"})");
  172. }
  173. {
  174. alt_json doc;
  175. doc["nothing"] = nullptr;
  176. alt_string dump = doc.dump();
  177. CHECK(dump == R"({"nothing":null})");
  178. }
  179. {
  180. alt_json doc;
  181. doc["answer"]["everything"] = 42;
  182. alt_string dump = doc.dump();
  183. CHECK(dump == R"({"answer":{"everything":42}})");
  184. }
  185. {
  186. alt_json doc;
  187. doc["list"] = { 1, 0, 2 };
  188. alt_string dump = doc.dump();
  189. CHECK(dump == R"({"list":[1,0,2]})");
  190. }
  191. {
  192. alt_json doc;
  193. doc["object"] = { {"currency", "USD"}, {"value", 42.99} };
  194. alt_string dump = doc.dump();
  195. CHECK(dump == R"({"object":{"currency":"USD","value":42.99}})");
  196. }
  197. }
  198. SECTION("parse")
  199. {
  200. auto doc = alt_json::parse(R"({"foo": "bar"})");
  201. alt_string dump = doc.dump();
  202. CHECK(dump == R"({"foo":"bar"})");
  203. }
  204. SECTION("items")
  205. {
  206. auto doc = alt_json::parse(R"({"foo": "bar"})");
  207. for (const auto& item : doc.items())
  208. {
  209. CHECK(item.key() == "foo");
  210. CHECK(item.value() == "bar");
  211. }
  212. auto doc_array = alt_json::parse(R"(["foo", "bar"])");
  213. for (const auto& item : doc_array.items())
  214. {
  215. if (item.key() == "0" )
  216. {
  217. CHECK( item.value() == "foo" );
  218. }
  219. else if (item.key() == "1" )
  220. {
  221. CHECK(item.value() == "bar");
  222. }
  223. else
  224. {
  225. CHECK(false);
  226. }
  227. }
  228. }
  229. SECTION("equality")
  230. {
  231. alt_json doc;
  232. doc["Who are you?"] = "I'm Batman";
  233. CHECK("I'm Batman" == doc["Who are you?"]);
  234. CHECK(doc["Who are you?"] == "I'm Batman");
  235. CHECK_FALSE("I'm Batman" != doc["Who are you?"]);
  236. CHECK_FALSE(doc["Who are you?"] != "I'm Batman");
  237. CHECK("I'm Bruce Wayne" != doc["Who are you?"]);
  238. CHECK(doc["Who are you?"] != "I'm Bruce Wayne");
  239. CHECK_FALSE("I'm Bruce Wayne" == doc["Who are you?"]);
  240. CHECK_FALSE(doc["Who are you?"] == "I'm Bruce Wayne");
  241. {
  242. const alt_json& const_doc = doc;
  243. CHECK("I'm Batman" == const_doc["Who are you?"]);
  244. CHECK(const_doc["Who are you?"] == "I'm Batman");
  245. CHECK_FALSE("I'm Batman" != const_doc["Who are you?"]);
  246. CHECK_FALSE(const_doc["Who are you?"] != "I'm Batman");
  247. CHECK("I'm Bruce Wayne" != const_doc["Who are you?"]);
  248. CHECK(const_doc["Who are you?"] != "I'm Bruce Wayne");
  249. CHECK_FALSE("I'm Bruce Wayne" == const_doc["Who are you?"]);
  250. CHECK_FALSE(const_doc["Who are you?"] == "I'm Bruce Wayne");
  251. }
  252. }
  253. SECTION("JSON pointer")
  254. {
  255. // conversion from json to alt_json fails to compile (see #3425);
  256. // attempted fix(*) produces: [[['b','a','r'],['b','a','z']]] (with each char being an integer)
  257. // (*) disable implicit conversion for json_refs of any basic_json type
  258. // alt_json j = R"(
  259. // {
  260. // "foo": ["bar", "baz"]
  261. // }
  262. // )"_json;
  263. auto j = alt_json::parse(R"({"foo": ["bar", "baz"]})");
  264. CHECK(j.at(alt_json::json_pointer("/foo/0")) == j["foo"][0]);
  265. CHECK(j.at(alt_json::json_pointer("/foo/1")) == j["foo"][1]);
  266. }
  267. }