unit-alt-string.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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) 2018 Vitaliy Manushkin <agri@akamo.info>.
  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. #include <string>
  28. #include <utility>
  29. /* forward declarations */
  30. class alt_string;
  31. bool operator<(const char* op1, const alt_string& op2);
  32. void int_to_string(alt_string& target, std::size_t value);
  33. /*
  34. * This is virtually a string class.
  35. * It covers std::string under the hood.
  36. */
  37. class alt_string
  38. {
  39. public:
  40. using value_type = std::string::value_type;
  41. alt_string(const char* str): str_impl(str) {}
  42. alt_string(const char* str, std::size_t count): str_impl(str, count) {}
  43. alt_string(size_t count, char chr): str_impl(count, chr) {}
  44. alt_string() = default;
  45. template <typename...TParams>
  46. alt_string& append(TParams&& ...params)
  47. {
  48. str_impl.append(std::forward<TParams>(params)...);
  49. return *this;
  50. }
  51. void push_back(char c)
  52. {
  53. str_impl.push_back(c);
  54. }
  55. template <typename op_type>
  56. bool operator==(const op_type& op) const
  57. {
  58. return str_impl == op;
  59. }
  60. bool operator==(const alt_string& op) const
  61. {
  62. return str_impl == op.str_impl;
  63. }
  64. template <typename op_type>
  65. bool operator!=(const op_type& op) const
  66. {
  67. return str_impl != op;
  68. }
  69. bool operator!=(const alt_string& op) const
  70. {
  71. return str_impl != op.str_impl;
  72. }
  73. std::size_t size() const noexcept
  74. {
  75. return str_impl.size();
  76. }
  77. void resize (std::size_t n)
  78. {
  79. str_impl.resize(n);
  80. }
  81. void resize (std::size_t n, char c)
  82. {
  83. str_impl.resize(n, c);
  84. }
  85. template <typename op_type>
  86. bool operator<(const op_type& op) const
  87. {
  88. return str_impl < op;
  89. }
  90. bool operator<(const alt_string& op) const
  91. {
  92. return str_impl < op.str_impl;
  93. }
  94. const char* c_str() const
  95. {
  96. return str_impl.c_str();
  97. }
  98. char& operator[](std::size_t index)
  99. {
  100. return str_impl[index];
  101. }
  102. const char& operator[](std::size_t index) const
  103. {
  104. return str_impl[index];
  105. }
  106. char& back()
  107. {
  108. return str_impl.back();
  109. }
  110. const char& back() const
  111. {
  112. return str_impl.back();
  113. }
  114. void clear()
  115. {
  116. str_impl.clear();
  117. }
  118. const value_type* data()
  119. {
  120. return str_impl.data();
  121. }
  122. private:
  123. std::string str_impl {};
  124. friend bool ::operator<(const char*, const alt_string&);
  125. };
  126. void int_to_string(alt_string& target, std::size_t value)
  127. {
  128. target = std::to_string(value).c_str();
  129. }
  130. using alt_json = nlohmann::basic_json <
  131. std::map,
  132. std::vector,
  133. alt_string,
  134. bool,
  135. std::int64_t,
  136. std::uint64_t,
  137. double,
  138. std::allocator,
  139. nlohmann::adl_serializer >;
  140. bool operator<(const char* op1, const alt_string& op2)
  141. {
  142. return op1 < op2.str_impl;
  143. }
  144. TEST_CASE("alternative string type")
  145. {
  146. SECTION("dump")
  147. {
  148. {
  149. alt_json doc;
  150. doc["pi"] = 3.141;
  151. alt_string dump = doc.dump();
  152. CHECK(dump == R"({"pi":3.141})");
  153. }
  154. {
  155. alt_json doc;
  156. doc["happy"] = true;
  157. alt_string dump = doc.dump();
  158. CHECK(dump == R"({"happy":true})");
  159. }
  160. {
  161. alt_json doc;
  162. doc["name"] = "I'm Batman";
  163. alt_string dump = doc.dump();
  164. CHECK(dump == R"({"name":"I'm Batman"})");
  165. }
  166. {
  167. alt_json doc;
  168. doc["nothing"] = nullptr;
  169. alt_string dump = doc.dump();
  170. CHECK(dump == R"({"nothing":null})");
  171. }
  172. {
  173. alt_json doc;
  174. doc["answer"]["everything"] = 42;
  175. alt_string dump = doc.dump();
  176. CHECK(dump == R"({"answer":{"everything":42}})");
  177. }
  178. {
  179. alt_json doc;
  180. doc["list"] = { 1, 0, 2 };
  181. alt_string dump = doc.dump();
  182. CHECK(dump == R"({"list":[1,0,2]})");
  183. }
  184. {
  185. alt_json doc;
  186. doc["list"] = { 1, 0, 2 };
  187. alt_string dump = doc.dump();
  188. CHECK(dump == R"({"list":[1,0,2]})");
  189. }
  190. }
  191. SECTION("parse")
  192. {
  193. auto doc = alt_json::parse("{\"foo\": \"bar\"}");
  194. alt_string dump = doc.dump();
  195. CHECK(dump == R"({"foo":"bar"})");
  196. }
  197. SECTION("items")
  198. {
  199. auto doc = alt_json::parse("{\"foo\": \"bar\"}");
  200. for ( auto item : doc.items() )
  201. {
  202. CHECK( item.key() == "foo" );
  203. CHECK( item.value() == "bar" );
  204. }
  205. auto doc_array = alt_json::parse("[\"foo\", \"bar\"]");
  206. for ( auto item : doc_array.items() )
  207. {
  208. if (item.key() == "0" )
  209. {
  210. CHECK( item.value() == "foo" );
  211. }
  212. else if (item.key() == "1" )
  213. {
  214. CHECK( item.value() == "bar" );
  215. }
  216. else
  217. {
  218. CHECK( false );
  219. }
  220. }
  221. }
  222. SECTION("equality")
  223. {
  224. alt_json doc;
  225. doc["Who are you?"] = "I'm Batman";
  226. CHECK("I'm Batman" == doc["Who are you?"]);
  227. CHECK(doc["Who are you?"] == "I'm Batman");
  228. CHECK_FALSE("I'm Batman" != doc["Who are you?"]);
  229. CHECK_FALSE(doc["Who are you?"] != "I'm Batman");
  230. CHECK("I'm Bruce Wayne" != doc["Who are you?"]);
  231. CHECK(doc["Who are you?"] != "I'm Bruce Wayne");
  232. CHECK_FALSE("I'm Bruce Wayne" == doc["Who are you?"]);
  233. CHECK_FALSE(doc["Who are you?"] == "I'm Bruce Wayne");
  234. {
  235. const alt_json& const_doc = doc;
  236. CHECK("I'm Batman" == const_doc["Who are you?"]);
  237. CHECK(const_doc["Who are you?"] == "I'm Batman");
  238. CHECK_FALSE("I'm Batman" != const_doc["Who are you?"]);
  239. CHECK_FALSE(const_doc["Who are you?"] != "I'm Batman");
  240. CHECK("I'm Bruce Wayne" != const_doc["Who are you?"]);
  241. CHECK(const_doc["Who are you?"] != "I'm Bruce Wayne");
  242. CHECK_FALSE("I'm Bruce Wayne" == const_doc["Who are you?"]);
  243. CHECK_FALSE(const_doc["Who are you?"] == "I'm Bruce Wayne");
  244. }
  245. }
  246. }