nbttest.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * libnbt++ - A library for the Minecraft Named Binary Tag format.
  3. * Copyright (C) 2013, 2015 ljfa-ag
  4. *
  5. * This file is part of libnbt++.
  6. *
  7. * libnbt++ is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * libnbt++ is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with libnbt++. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <cxxtest/TestSuite.h>
  21. #include "nbt_tags.h"
  22. #include "nbt_visitor.h"
  23. #include <algorithm>
  24. #include <set>
  25. #include <stdexcept>
  26. using namespace nbt;
  27. class nbttest : public CxxTest::TestSuite
  28. {
  29. public:
  30. void test_tag()
  31. {
  32. TS_ASSERT(!is_valid_type(-1));
  33. TS_ASSERT(!is_valid_type(0));
  34. TS_ASSERT(is_valid_type(0, true));
  35. TS_ASSERT(is_valid_type(1));
  36. TS_ASSERT(is_valid_type(5, false));
  37. TS_ASSERT(is_valid_type(7, true));
  38. TS_ASSERT(is_valid_type(12));
  39. TS_ASSERT(!is_valid_type(13));
  40. //looks like TS_ASSERT_EQUALS can't handle abstract classes...
  41. TS_ASSERT(*tag::create(tag_type::Byte) == tag_byte());
  42. TS_ASSERT_THROWS(tag::create(tag_type::Null), std::invalid_argument);
  43. TS_ASSERT_THROWS(tag::create(tag_type::End), std::invalid_argument);
  44. tag_string tstr("foo");
  45. auto cl = tstr.clone();
  46. TS_ASSERT_EQUALS(tstr.get(), "foo");
  47. TS_ASSERT(tstr == *cl);
  48. cl = std::move(tstr).clone();
  49. TS_ASSERT(*cl == tag_string("foo"));
  50. TS_ASSERT(*cl != tag_string("bar"));
  51. cl = std::move(*cl).move_clone();
  52. TS_ASSERT(*cl == tag_string("foo"));
  53. tstr.assign(tag_string("bar"));
  54. TS_ASSERT_THROWS(tstr.assign(tag_int(6)), std::bad_cast);
  55. TS_ASSERT_EQUALS(tstr.get(), "bar");
  56. TS_ASSERT_EQUALS(&tstr.as<tag_string>(), &tstr);
  57. TS_ASSERT_THROWS(tstr.as<tag_byte_array>(), std::bad_cast);
  58. }
  59. void test_get_type()
  60. {
  61. TS_ASSERT_EQUALS(tag_byte().get_type() , tag_type::Byte);
  62. TS_ASSERT_EQUALS(tag_short().get_type() , tag_type::Short);
  63. TS_ASSERT_EQUALS(tag_int().get_type() , tag_type::Int);
  64. TS_ASSERT_EQUALS(tag_long().get_type() , tag_type::Long);
  65. TS_ASSERT_EQUALS(tag_float().get_type() , tag_type::Float);
  66. TS_ASSERT_EQUALS(tag_double().get_type() , tag_type::Double);
  67. TS_ASSERT_EQUALS(tag_byte_array().get_type(), tag_type::Byte_Array);
  68. TS_ASSERT_EQUALS(tag_string().get_type() , tag_type::String);
  69. TS_ASSERT_EQUALS(tag_list().get_type() , tag_type::List);
  70. TS_ASSERT_EQUALS(tag_compound().get_type() , tag_type::Compound);
  71. TS_ASSERT_EQUALS(tag_int_array().get_type() , tag_type::Int_Array);
  72. TS_ASSERT_EQUALS(tag_long_array().get_type(), tag_type::Long_Array);
  73. }
  74. void test_tag_primitive()
  75. {
  76. tag_int tag(6);
  77. TS_ASSERT_EQUALS(tag.get(), 6);
  78. int& ref = tag;
  79. ref = 12;
  80. TS_ASSERT(tag == 12);
  81. TS_ASSERT(tag != 6);
  82. tag.set(24);
  83. TS_ASSERT_EQUALS(ref, 24);
  84. tag = 7;
  85. TS_ASSERT_EQUALS(static_cast<int>(tag), 7);
  86. TS_ASSERT_EQUALS(tag, tag_int(7));
  87. TS_ASSERT_DIFFERS(tag_float(2.5), tag_float(-2.5));
  88. TS_ASSERT_DIFFERS(tag_float(2.5), tag_double(2.5));
  89. TS_ASSERT(tag_double() == 0.0);
  90. TS_ASSERT_EQUALS(tag_byte(INT8_MAX).get(), INT8_MAX);
  91. TS_ASSERT_EQUALS(tag_byte(INT8_MIN).get(), INT8_MIN);
  92. TS_ASSERT_EQUALS(tag_short(INT16_MAX).get(), INT16_MAX);
  93. TS_ASSERT_EQUALS(tag_short(INT16_MIN).get(), INT16_MIN);
  94. TS_ASSERT_EQUALS(tag_int(INT32_MAX).get(), INT32_MAX);
  95. TS_ASSERT_EQUALS(tag_int(INT32_MIN).get(), INT32_MIN);
  96. TS_ASSERT_EQUALS(tag_long(INT64_MAX).get(), INT64_MAX);
  97. TS_ASSERT_EQUALS(tag_long(INT64_MIN).get(), INT64_MIN);
  98. }
  99. void test_tag_string()
  100. {
  101. tag_string tag("foo");
  102. TS_ASSERT_EQUALS(tag.get(), "foo");
  103. std::string& ref = tag;
  104. ref = "bar";
  105. TS_ASSERT_EQUALS(tag.get(), "bar");
  106. TS_ASSERT_DIFFERS(tag.get(), "foo");
  107. tag.set("baz");
  108. TS_ASSERT_EQUALS(ref, "baz");
  109. tag = "quux";
  110. TS_ASSERT_EQUALS("quux", static_cast<std::string>(tag));
  111. std::string str("foo");
  112. tag = str;
  113. TS_ASSERT_EQUALS(tag.get(),str);
  114. TS_ASSERT_EQUALS(tag_string(str).get(), "foo");
  115. TS_ASSERT_EQUALS(tag_string().get(), "");
  116. }
  117. void test_tag_compound()
  118. {
  119. tag_compound comp{
  120. {"foo", int16_t(12)},
  121. {"bar", "baz"},
  122. {"baz", -2.0},
  123. {"list", tag_list{16, 17}}
  124. };
  125. //Test assignments and conversions, and exceptions on bad conversions
  126. TS_ASSERT_EQUALS(comp["foo"].get_type(), tag_type::Short);
  127. TS_ASSERT_EQUALS(static_cast<int32_t>(comp["foo"]), 12);
  128. TS_ASSERT_EQUALS(static_cast<int16_t>(comp.at("foo")), int16_t(12));
  129. TS_ASSERT(comp["foo"] == tag_short(12));
  130. TS_ASSERT_THROWS(static_cast<int8_t>(comp["foo"]), std::bad_cast);
  131. TS_ASSERT_THROWS(static_cast<std::string>(comp["foo"]), std::bad_cast);
  132. TS_ASSERT_THROWS(comp["foo"] = 32, std::bad_cast);
  133. comp["foo"] = int8_t(32);
  134. TS_ASSERT_EQUALS(static_cast<int16_t>(comp["foo"]), 32);
  135. TS_ASSERT_EQUALS(comp["bar"].get_type(), tag_type::String);
  136. TS_ASSERT_EQUALS(static_cast<std::string>(comp["bar"]), "baz");
  137. TS_ASSERT_THROWS(static_cast<int>(comp["bar"]), std::bad_cast);
  138. TS_ASSERT_THROWS(comp["bar"] = -128, std::bad_cast);
  139. comp["bar"] = "barbaz";
  140. TS_ASSERT_EQUALS(static_cast<std::string>(comp["bar"]), "barbaz");
  141. TS_ASSERT_EQUALS(comp["baz"].get_type(), tag_type::Double);
  142. TS_ASSERT_EQUALS(static_cast<double>(comp["baz"]), -2.0);
  143. TS_ASSERT_THROWS(static_cast<float>(comp["baz"]), std::bad_cast);
  144. //Test nested access
  145. comp["quux"] = tag_compound{{"Hello", "World"}, {"zero", 0}};
  146. TS_ASSERT_EQUALS(comp.at("quux").get_type(), tag_type::Compound);
  147. TS_ASSERT_EQUALS(static_cast<std::string>(comp["quux"].at("Hello")), "World");
  148. TS_ASSERT_EQUALS(static_cast<std::string>(comp["quux"]["Hello"]), "World");
  149. TS_ASSERT(comp["list"][1] == tag_int(17));
  150. TS_ASSERT_THROWS(comp.at("nothing"), std::out_of_range);
  151. //Test equality comparisons
  152. tag_compound comp2{
  153. {"foo", int16_t(32)},
  154. {"bar", "barbaz"},
  155. {"baz", -2.0},
  156. {"quux", tag_compound{{"Hello", "World"}, {"zero", 0}}},
  157. {"list", tag_list{16, 17}}
  158. };
  159. TS_ASSERT(comp == comp2);
  160. TS_ASSERT(comp != dynamic_cast<const tag_compound&>(comp2["quux"].get()));
  161. TS_ASSERT(comp != comp2["quux"]);
  162. TS_ASSERT(dynamic_cast<const tag_compound&>(comp["quux"].get()) == comp2["quux"]);
  163. //Test whether begin() through end() goes through all the keys and their
  164. //values. The order of iteration is irrelevant there.
  165. std::set<std::string> keys{"bar", "baz", "foo", "list", "quux"};
  166. TS_ASSERT_EQUALS(comp2.size(), keys.size());
  167. unsigned int i = 0;
  168. for(const std::pair<const std::string, value>& val: comp2)
  169. {
  170. TS_ASSERT_LESS_THAN(i, comp2.size());
  171. TS_ASSERT(keys.count(val.first));
  172. TS_ASSERT(val.second == comp2[val.first]);
  173. ++i;
  174. }
  175. TS_ASSERT_EQUALS(i, comp2.size());
  176. //Test erasing and has_key
  177. TS_ASSERT_EQUALS(comp.erase("nothing"), false);
  178. TS_ASSERT(comp.has_key("quux"));
  179. TS_ASSERT(comp.has_key("quux", tag_type::Compound));
  180. TS_ASSERT(!comp.has_key("quux", tag_type::List));
  181. TS_ASSERT(!comp.has_key("quux", tag_type::Null));
  182. TS_ASSERT_EQUALS(comp.erase("quux"), true);
  183. TS_ASSERT(!comp.has_key("quux"));
  184. TS_ASSERT(!comp.has_key("quux", tag_type::Compound));
  185. TS_ASSERT(!comp.has_key("quux", tag_type::Null));
  186. comp.clear();
  187. TS_ASSERT(comp == tag_compound{});
  188. //Test inserting values
  189. TS_ASSERT_EQUALS(comp.put("abc", tag_double(6.0)).second, true);
  190. TS_ASSERT_EQUALS(comp.put("abc", tag_long(-28)).second, false);
  191. TS_ASSERT_EQUALS(comp.insert("ghi", tag_string("world")).second, true);
  192. TS_ASSERT_EQUALS(comp.insert("abc", tag_string("hello")).second, false);
  193. TS_ASSERT_EQUALS(comp.emplace<tag_string>("def", "ghi").second, true);
  194. TS_ASSERT_EQUALS(comp.emplace<tag_byte>("def", 4).second, false);
  195. TS_ASSERT((comp == tag_compound{
  196. {"abc", tag_long(-28)},
  197. {"def", tag_byte(4)},
  198. {"ghi", tag_string("world")}
  199. }));
  200. }
  201. void test_value()
  202. {
  203. value val1;
  204. value val2(make_unique<tag_int>(42));
  205. value val3(tag_int(42));
  206. TS_ASSERT(!val1 && val2 && val3);
  207. TS_ASSERT(val1 == val1);
  208. TS_ASSERT(val1 != val2);
  209. TS_ASSERT(val2 == val3);
  210. TS_ASSERT(val3 == val3);
  211. value valstr(tag_string("foo"));
  212. TS_ASSERT_EQUALS(static_cast<std::string>(valstr), "foo");
  213. valstr = "bar";
  214. TS_ASSERT_THROWS(valstr = 5, std::bad_cast);
  215. TS_ASSERT_EQUALS(static_cast<std::string>(valstr), "bar");
  216. TS_ASSERT(valstr.as<tag_string>() == "bar");
  217. TS_ASSERT_EQUALS(&valstr.as<tag>(), &valstr.get());
  218. TS_ASSERT_THROWS(valstr.as<tag_float>(), std::bad_cast);
  219. val1 = int64_t(42);
  220. TS_ASSERT(val2 != val1);
  221. TS_ASSERT_THROWS(val2 = int64_t(12), std::bad_cast);
  222. TS_ASSERT_EQUALS(static_cast<int64_t>(val2), 42);
  223. tag_int* ptr = dynamic_cast<tag_int*>(val2.get_ptr().get());
  224. TS_ASSERT(*ptr == 42);
  225. val2 = 52;
  226. TS_ASSERT_EQUALS(static_cast<int32_t>(val2), 52);
  227. TS_ASSERT(*ptr == 52);
  228. TS_ASSERT_THROWS(val1["foo"], std::bad_cast);
  229. TS_ASSERT_THROWS(val1.at("foo"), std::bad_cast);
  230. val3 = 52;
  231. TS_ASSERT(val2 == val3);
  232. TS_ASSERT(val2.get_ptr() != val3.get_ptr());
  233. val3 = std::move(val2);
  234. TS_ASSERT(val3 == tag_int(52));
  235. TS_ASSERT(!val2);
  236. tag_int& tag = dynamic_cast<tag_int&>(val3.get());
  237. TS_ASSERT(tag == tag_int(52));
  238. tag = 21;
  239. TS_ASSERT_EQUALS(static_cast<int32_t>(val3), 21);
  240. val1.set_ptr(std::move(val3.get_ptr()));
  241. TS_ASSERT(val1.as<tag_int>() == 21);
  242. TS_ASSERT_EQUALS(val1.get_type(), tag_type::Int);
  243. TS_ASSERT_EQUALS(val2.get_type(), tag_type::Null);
  244. TS_ASSERT_EQUALS(val3.get_type(), tag_type::Null);
  245. val2 = val1;
  246. val1 = val3;
  247. TS_ASSERT(!val1 && val2 && !val3);
  248. TS_ASSERT(val1.get_ptr() == nullptr);
  249. TS_ASSERT(val2.get() == tag_int(21));
  250. TS_ASSERT(value(val1) == val1);
  251. TS_ASSERT(value(val2) == val2);
  252. val1 = val1;
  253. val2 = val2;
  254. TS_ASSERT(!val1);
  255. TS_ASSERT(val1 == value_initializer(nullptr));
  256. TS_ASSERT(val2 == tag_int(21));
  257. val3 = tag_short(2);
  258. TS_ASSERT_THROWS(val3 = tag_string("foo"), std::bad_cast);
  259. TS_ASSERT(val3.get() == tag_short(2));
  260. val2.set_ptr(make_unique<tag_string>("foo"));
  261. TS_ASSERT(val2 == tag_string("foo"));
  262. }
  263. void test_tag_list()
  264. {
  265. tag_list list;
  266. TS_ASSERT_EQUALS(list.el_type(), tag_type::Null);
  267. TS_ASSERT_THROWS(list.push_back(value(nullptr)), std::invalid_argument);
  268. list.emplace_back<tag_string>("foo");
  269. TS_ASSERT_EQUALS(list.el_type(), tag_type::String);
  270. list.push_back(tag_string("bar"));
  271. TS_ASSERT_THROWS(list.push_back(tag_int(42)), std::invalid_argument);
  272. TS_ASSERT_THROWS(list.emplace_back<tag_compound>(), std::invalid_argument);
  273. TS_ASSERT((list == tag_list{"foo", "bar"}));
  274. TS_ASSERT(list[0] == tag_string("foo"));
  275. TS_ASSERT_EQUALS(static_cast<std::string>(list.at(1)), "bar");
  276. TS_ASSERT_EQUALS(list.size(), 2u);
  277. TS_ASSERT_THROWS(list.at(2), std::out_of_range);
  278. TS_ASSERT_THROWS(list.at(-1), std::out_of_range);
  279. list.set(1, value(tag_string("baz")));
  280. TS_ASSERT_THROWS(list.set(1, value(nullptr)), std::invalid_argument);
  281. TS_ASSERT_THROWS(list.set(1, value(tag_int(-42))), std::invalid_argument);
  282. TS_ASSERT_EQUALS(static_cast<std::string>(list[1]), "baz");
  283. TS_ASSERT_EQUALS(list.size(), 2u);
  284. tag_string values[] = {"foo", "baz"};
  285. TS_ASSERT_EQUALS(list.end() - list.begin(), int(list.size()));
  286. TS_ASSERT(std::equal(list.begin(), list.end(), values));
  287. list.pop_back();
  288. TS_ASSERT(list == tag_list{"foo"});
  289. TS_ASSERT(list == tag_list::of<tag_string>({"foo"}));
  290. TS_ASSERT(tag_list::of<tag_string>({"foo"}) == tag_list{"foo"});
  291. TS_ASSERT((list != tag_list{2, 3, 5, 7}));
  292. list.clear();
  293. TS_ASSERT_EQUALS(list.size(), 0u);
  294. TS_ASSERT_EQUALS(list.el_type(), tag_type::String)
  295. TS_ASSERT_THROWS(list.push_back(tag_short(25)), std::invalid_argument);
  296. TS_ASSERT_THROWS(list.push_back(value(nullptr)), std::invalid_argument);
  297. list.reset();
  298. TS_ASSERT_EQUALS(list.el_type(), tag_type::Null);
  299. list.emplace_back<tag_int>(17);
  300. TS_ASSERT_EQUALS(list.el_type(), tag_type::Int);
  301. list.reset(tag_type::Float);
  302. TS_ASSERT_EQUALS(list.el_type(), tag_type::Float);
  303. list.emplace_back<tag_float>(17.0f);
  304. TS_ASSERT(list == tag_list({17.0f}));
  305. TS_ASSERT(tag_list() != tag_list(tag_type::Int));
  306. TS_ASSERT(tag_list() == tag_list());
  307. TS_ASSERT(tag_list(tag_type::Short) != tag_list(tag_type::Int));
  308. TS_ASSERT(tag_list(tag_type::Short) == tag_list(tag_type::Short));
  309. tag_list short_list = tag_list::of<tag_short>({25, 36});
  310. TS_ASSERT_EQUALS(short_list.el_type(), tag_type::Short);
  311. TS_ASSERT((short_list == tag_list{int16_t(25), int16_t(36)}));
  312. TS_ASSERT((short_list != tag_list{25, 36}));
  313. TS_ASSERT((short_list == tag_list{value(tag_short(25)), value(tag_short(36))}));
  314. TS_ASSERT_THROWS((tag_list{value(tag_byte(4)), value(tag_int(5))}), std::invalid_argument);
  315. TS_ASSERT_THROWS((tag_list{value(nullptr), value(tag_int(6))}), std::invalid_argument);
  316. TS_ASSERT_THROWS((tag_list{value(tag_int(7)), value(tag_int(8)), value(nullptr)}), std::invalid_argument);
  317. TS_ASSERT_EQUALS((tag_list(std::initializer_list<value>{})).el_type(), tag_type::Null);
  318. TS_ASSERT_EQUALS((tag_list{2, 3, 5, 7}).el_type(), tag_type::Int);
  319. }
  320. void test_tag_byte_array()
  321. {
  322. std::vector<int8_t> vec{1, 2, 127, -128};
  323. tag_byte_array arr{1, 2, 127, -128};
  324. TS_ASSERT_EQUALS(arr.size(), 4u);
  325. TS_ASSERT(arr.at(0) == 1 && arr[1] == 2 && arr[2] == 127 && arr.at(3) == -128);
  326. TS_ASSERT_THROWS(arr.at(-1), std::out_of_range);
  327. TS_ASSERT_THROWS(arr.at(4), std::out_of_range);
  328. TS_ASSERT(arr.get() == vec);
  329. TS_ASSERT(arr == tag_byte_array(std::vector<int8_t>(vec)));
  330. arr.push_back(42);
  331. vec.push_back(42);
  332. TS_ASSERT_EQUALS(arr.size(), 5u);
  333. TS_ASSERT_EQUALS(arr.end() - arr.begin(), int(arr.size()));
  334. TS_ASSERT(std::equal(arr.begin(), arr.end(), vec.begin()));
  335. arr.pop_back();
  336. arr.pop_back();
  337. TS_ASSERT_EQUALS(arr.size(), 3u);
  338. TS_ASSERT((arr == tag_byte_array{1, 2, 127}));
  339. TS_ASSERT((arr != tag_int_array{1, 2, 127}));
  340. TS_ASSERT((arr != tag_long_array{1, 2, 127}));
  341. TS_ASSERT((arr != tag_byte_array{1, 2, -1}));
  342. arr.clear();
  343. TS_ASSERT(arr == tag_byte_array());
  344. }
  345. void test_tag_int_array()
  346. {
  347. std::vector<int32_t> vec{100, 200, INT32_MAX, INT32_MIN};
  348. tag_int_array arr{100, 200, INT32_MAX, INT32_MIN};
  349. TS_ASSERT_EQUALS(arr.size(), 4u);
  350. TS_ASSERT(arr.at(0) == 100 && arr[1] == 200 && arr[2] == INT32_MAX && arr.at(3) == INT32_MIN);
  351. TS_ASSERT_THROWS(arr.at(-1), std::out_of_range);
  352. TS_ASSERT_THROWS(arr.at(4), std::out_of_range);
  353. TS_ASSERT(arr.get() == vec);
  354. TS_ASSERT(arr == tag_int_array(std::vector<int32_t>(vec)));
  355. arr.push_back(42);
  356. vec.push_back(42);
  357. TS_ASSERT_EQUALS(arr.size(), 5u);
  358. TS_ASSERT_EQUALS(arr.end() - arr.begin(), int(arr.size()));
  359. TS_ASSERT(std::equal(arr.begin(), arr.end(), vec.begin()));
  360. arr.pop_back();
  361. arr.pop_back();
  362. TS_ASSERT_EQUALS(arr.size(), 3u);
  363. TS_ASSERT((arr == tag_int_array{100, 200, INT32_MAX}));
  364. TS_ASSERT((arr != tag_int_array{100, -56, -1}));
  365. arr.clear();
  366. TS_ASSERT(arr == tag_int_array());
  367. }
  368. void test_tag_long_array()
  369. {
  370. std::vector<int64_t> vec{100, 200, INT64_MAX, INT64_MIN};
  371. tag_long_array arr{100, 200, INT64_MAX, INT64_MIN};
  372. TS_ASSERT_EQUALS(arr.size(), 4u);
  373. TS_ASSERT(arr.at(0) == 100 && arr[1] == 200 && arr[2] == INT64_MAX && arr.at(3) == INT64_MIN);
  374. TS_ASSERT_THROWS(arr.at(-1), std::out_of_range);
  375. TS_ASSERT_THROWS(arr.at(4), std::out_of_range);
  376. TS_ASSERT(arr.get() == vec);
  377. TS_ASSERT(arr == tag_long_array(std::vector<int64_t>(vec)));
  378. arr.push_back(42);
  379. vec.push_back(42);
  380. TS_ASSERT_EQUALS(arr.size(), 5u);
  381. TS_ASSERT_EQUALS(arr.end() - arr.begin(), int(arr.size()));
  382. TS_ASSERT(std::equal(arr.begin(), arr.end(), vec.begin()));
  383. arr.pop_back();
  384. arr.pop_back();
  385. TS_ASSERT_EQUALS(arr.size(), 3u);
  386. TS_ASSERT((arr == tag_long_array{100, 200, INT64_MAX}));
  387. TS_ASSERT((arr != tag_long_array{100, -56, -1}));
  388. arr.clear();
  389. TS_ASSERT(arr == tag_long_array());
  390. }
  391. void test_visitor()
  392. {
  393. struct : public nbt_visitor
  394. {
  395. tag* visited = nullptr;
  396. void visit(tag_byte& tag) { visited = &tag; }
  397. void visit(tag_short& tag) { visited = &tag; }
  398. void visit(tag_int& tag) { visited = &tag; }
  399. void visit(tag_long& tag) { visited = &tag; }
  400. void visit(tag_float& tag) { visited = &tag; }
  401. void visit(tag_double& tag) { visited = &tag; }
  402. void visit(tag_byte_array& tag) { visited = &tag; }
  403. void visit(tag_string& tag) { visited = &tag; }
  404. void visit(tag_list& tag) { visited = &tag; }
  405. void visit(tag_compound& tag) { visited = &tag; }
  406. void visit(tag_int_array& tag) { visited = &tag; }
  407. void visit(tag_long_array& tag) { visited = &tag; }
  408. } v;
  409. tag_byte b; b.accept(v); TS_ASSERT_EQUALS(v.visited, &b);
  410. tag_short s; s.accept(v); TS_ASSERT_EQUALS(v.visited, &s);
  411. tag_int i; i.accept(v); TS_ASSERT_EQUALS(v.visited, &i);
  412. tag_long l; l.accept(v); TS_ASSERT_EQUALS(v.visited, &l);
  413. tag_float f; f.accept(v); TS_ASSERT_EQUALS(v.visited, &f);
  414. tag_double d; d.accept(v); TS_ASSERT_EQUALS(v.visited, &d);
  415. tag_byte_array ba; ba.accept(v); TS_ASSERT_EQUALS(v.visited, &ba);
  416. tag_string st; st.accept(v); TS_ASSERT_EQUALS(v.visited, &st);
  417. tag_list ls; ls.accept(v); TS_ASSERT_EQUALS(v.visited, &ls);
  418. tag_compound c; c.accept(v); TS_ASSERT_EQUALS(v.visited, &c);
  419. tag_int_array ia; ia.accept(v); TS_ASSERT_EQUALS(v.visited, &ia);
  420. tag_long_array la; la.accept(v); TS_ASSERT_EQUALS(v.visited, &la);
  421. }
  422. };