vector_complex_test.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*-
  2. * Copyright 2012-1015 Matthew Endsley
  3. * All rights reserved
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted providing that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  18. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  22. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  23. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "test.h"
  27. #include <tinystl/allocator.h>
  28. #include <tinystl/vector.h>
  29. #include <algorithm>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #if !BX_COMPILER_MSVC
  33. # define _strdup strdup
  34. #endif // !BX_COMPILER_MSVC
  35. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4996) // warning C4996: 'strdup': The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _strdup. See online help for details.
  36. struct complex {
  37. complex() {data = 0;}
  38. complex(const char* s) { data = strdup(s); }
  39. ~complex() { free(data); }
  40. complex(const complex& other) { data = 0; if (other.data) data = strdup(other.data); }
  41. complex& operator=(const complex& other) { complex(other).swap(*this); return *this; }
  42. void swap(complex& other) { std::swap(data, other.data); }
  43. char* data;
  44. };
  45. static inline bool operator==(const complex& lhs, const complex& rhs) {
  46. if (lhs.data == 0 && rhs.data == 0)
  47. return true;
  48. if (lhs.data != 0 && rhs.data != 0)
  49. return 0 == strcmp(lhs.data, rhs.data);
  50. return false;
  51. }
  52. TEST(vector_complex_constructor) {
  53. typedef tinystl::vector<complex> vector;
  54. {
  55. vector v;
  56. CHECK( v.empty() );
  57. CHECK( v.size() == 0 );
  58. }
  59. {
  60. const complex array[10] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
  61. vector v(array, array + 10);
  62. CHECK( v.size() == 10 );
  63. CHECK( std::equal(v.begin(), v.end(), array) );
  64. }
  65. {
  66. const complex value = "127";
  67. const size_t count = 24;
  68. vector v(count, value);
  69. CHECK( v.size() == count );
  70. vector::iterator it = v.begin(), end = v.end();
  71. for (; it != end; ++it)
  72. CHECK(*it == value);
  73. }
  74. {
  75. const size_t count = 24;
  76. vector v(count);
  77. CHECK(v.size() == count);
  78. vector::iterator it = v.begin(), end = v.end();
  79. for (; it != end; ++it)
  80. CHECK(*it == complex());
  81. }
  82. {
  83. const complex array[10] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
  84. vector other(array, array + 10);
  85. vector v = other;
  86. CHECK( v.size() == other.size() );
  87. CHECK( std::equal(v.begin(), v.end(), other.begin()) );
  88. }
  89. }
  90. TEST(vector_complex_assignment) {
  91. typedef tinystl::vector<complex> vector;
  92. {
  93. const complex array[10] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
  94. vector other(array, array + 10);
  95. vector v;
  96. v = other;
  97. CHECK( v.size() == 10 );
  98. CHECK( std::equal(v.begin(), v.end(), array) );
  99. CHECK( other.size() == 10 );
  100. CHECK( std::equal(v.begin(), v.end(), other.begin()) );
  101. }
  102. }
  103. TEST(vector_complex_pushback) {
  104. tinystl::vector<complex> v;
  105. v.push_back("42");
  106. CHECK(v.size() == 1);
  107. CHECK(v[0] == "42");
  108. }
  109. TEST(vector_complex_vector) {
  110. tinystl::vector< tinystl::vector<complex> > v(10, tinystl::vector<complex>());
  111. tinystl::vector< tinystl::vector<complex> >::iterator it = v.begin(), end = v.end();
  112. for (; it != end; ++it) {
  113. CHECK( (*it).empty() );
  114. CHECK( (*it).size() == 0 );
  115. CHECK( (*it).begin() == (*it).end() );
  116. }
  117. }
  118. TEST(vector_complex_swap) {
  119. tinystl::vector<complex> v1;
  120. v1.push_back("12");
  121. v1.push_back("20");
  122. tinystl::vector<complex> v2;
  123. v2.push_back("54");
  124. v1.swap(v2);
  125. CHECK(v1.size() == 1);
  126. CHECK(v2.size() == 2);
  127. CHECK(v1[0] == "54");
  128. CHECK(v2[0] == "12");
  129. CHECK(v2[1] == "20");
  130. }
  131. TEST(vector_complex_popback) {
  132. tinystl::vector<complex> v;
  133. v.push_back("12");
  134. v.push_back("24");
  135. CHECK(v.back() == "24");
  136. v.pop_back();
  137. CHECK(v.back() == "12");
  138. CHECK(v.size() == 1);
  139. }
  140. TEST(vector_complex_assign) {
  141. tinystl::vector<complex> v;
  142. CHECK(v.size() == 0);
  143. const complex array[10] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
  144. v.assign(array, array + 10);
  145. CHECK(v.size() == 10);
  146. CHECK( std::equal(v.begin(), v.end(), array) );
  147. }
  148. TEST(vector_complex_erase) {
  149. const complex array[10] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
  150. tinystl::vector<complex> v(array, array + 10);
  151. tinystl::vector<complex>::iterator it = v.erase(v.begin());
  152. CHECK(*it == "2");
  153. CHECK(v.size() == 9);
  154. CHECK( std::equal(v.begin(), v.end(), array + 1) );
  155. it = v.erase(v.end() - 1);
  156. CHECK(it == v.end());
  157. CHECK(v.size() == 8);
  158. CHECK( std::equal(v.begin(), v.end(), array + 1) );
  159. v.erase(v.begin() + 1, v.end() - 1);
  160. CHECK(v.size() == 2);
  161. CHECK(v[0] == "2");
  162. CHECK(v[1] == "9");
  163. }
  164. TEST(vector_complex_erase_unordered) {
  165. const complex array[10] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
  166. typedef tinystl::vector<complex> vector;
  167. vector v(array, array + 10);
  168. complex first = *(v.begin());
  169. vector::iterator it = v.erase_unordered(v.begin());
  170. CHECK(it == v.begin());
  171. CHECK(v.size() == 9);
  172. CHECK( std::count(v.begin(), v.end(), first) == 0 );
  173. for (it = v.begin(); it != v.end(); ++it) {
  174. CHECK( std::count(v.begin(), v.end(), *it) == 1 );
  175. }
  176. complex last = *(v.end() - 1);
  177. it = v.erase_unordered(v.end() - 1);
  178. CHECK(it == v.end());
  179. CHECK(v.size() == 8);
  180. CHECK( std::count(v.begin(), v.end(), last) == 0 );
  181. for (it = v.begin(); it != v.end(); ++it) {
  182. CHECK( std::count(v.begin(), v.end(), *it) == 1 );
  183. }
  184. first = *(v.begin());
  185. last = *(v.end() - 1);
  186. v.erase_unordered(v.begin() + 1, v.end() - 1);
  187. CHECK(v.size() == 2);
  188. CHECK( std::count(v.begin(), v.end(), first) == 1 );
  189. CHECK( std::count(v.begin(), v.end(), last) == 1 );
  190. }
  191. TEST(vector_complex_insert) {
  192. const complex array[10] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
  193. tinystl::vector<complex> v(array, array + 10);
  194. v.insert(v.begin(), "0");
  195. CHECK(v.size() == 11);
  196. CHECK(v[0] == "0");
  197. CHECK( std::equal(v.begin() + 1, v.end(), array) );
  198. v.insert(v.end(), "11");
  199. CHECK(v.size() == 12);
  200. CHECK(v[0] == "0");
  201. CHECK( std::equal(array, array + 10, v.begin() + 1) );
  202. CHECK(v.back() == "11");
  203. const complex array2[3] = {"11", "12", "13"};
  204. const complex finalarray[] = {"0", "1", "2", "3", "11", "12", "13", "4", "5", "6", "7", "8", "9", "10", "11"};
  205. v.insert(v.begin() + 4, array2, array2 + 3);
  206. CHECK( v.size() == 15 );
  207. CHECK( std::equal(v.begin(), v.end(), finalarray) );
  208. }
  209. TEST(vector_complex_iterator) {
  210. const complex array[10] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
  211. tinystl::vector<complex> v(array, array + 10);
  212. const tinystl::vector<complex>& cv = v;
  213. CHECK(v.data() == &*v.begin());
  214. CHECK(v.data() == &v[0]);
  215. CHECK(v.data() + v.size() == &*v.end());
  216. CHECK(v.begin() == cv.begin());
  217. CHECK(v.end() == cv.end());
  218. CHECK(v.data() == cv.data());
  219. tinystl::vector<complex> w = v;
  220. CHECK(v.begin() != w.begin());
  221. CHECK(v.end() != w.end());
  222. }