vector.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Cycle the Gearmand server
  4. *
  5. * Copyright (C) 2013 Data Differential, http://datadifferential.com/
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * * The names of its contributors may not be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. /*
  37. Test that we are cycling the servers we are creating during testing.
  38. */
  39. #include "gear_config.h"
  40. #include <libtest/test.hpp>
  41. using namespace libtest;
  42. #include "libgearman/vector.hpp"
  43. #include "libgearman/vector.h"
  44. static test_return_t declare_vector_TEST(void*)
  45. {
  46. gearman_vector_st vec;
  47. ASSERT_EQ(0, vec.size());
  48. ASSERT_EQ(0, vec.capacity());
  49. return TEST_SUCCESS;
  50. }
  51. static test_return_t new_vector_TEST(void*)
  52. {
  53. gearman_vector_st* vec= new gearman_vector_st;
  54. ASSERT_EQ(0, vec->size());
  55. ASSERT_EQ(0, vec->capacity());
  56. delete vec;
  57. return TEST_SUCCESS;
  58. }
  59. static test_return_t declare_vector_size_TEST(void*)
  60. {
  61. gearman_vector_st vec(2048);
  62. ASSERT_EQ(0, vec.size());
  63. ASSERT_TRUE(vec.capacity() >= 2048);
  64. return TEST_SUCCESS;
  65. }
  66. static test_return_t new_vector_size_TEST(void*)
  67. {
  68. gearman_vector_st* vec= new gearman_vector_st(2023);
  69. ASSERT_EQ(0, vec->size());
  70. ASSERT_TRUE(vec->capacity() >= 2023);
  71. delete vec;
  72. return TEST_SUCCESS;
  73. }
  74. static test_return_t gearman_string_create_NULL_NULL_TEST(void*)
  75. {
  76. ASSERT_EQ(NULL, gearman_string_create(NULL, NULL, 0));
  77. return TEST_SUCCESS;
  78. }
  79. static test_return_t gearman_string_create_TEST(void*)
  80. {
  81. gearman_vector_st* vec= gearman_string_create(NULL, 0);
  82. ASSERT_EQ(0, vec->size());
  83. ASSERT_EQ(0, vec->capacity());
  84. gearman_string_free(vec);
  85. return TEST_SUCCESS;
  86. }
  87. static test_return_t gearman_string_create_string_redo_size_TEST(void*)
  88. {
  89. gearman_vector_st* vec= gearman_string_create(NULL, test_literal_param(__func__));
  90. ASSERT_EQ(test_literal_param_size(__func__), vec->size());
  91. ASSERT_TRUE(vec->capacity() >= test_literal_param_size(__func__));
  92. vec= gearman_string_create(vec, test_literal_param("again"));
  93. ASSERT_EQ(test_literal_param_size("again"), vec->size());
  94. ASSERT_TRUE(vec->capacity() >= test_literal_param_size("again"));
  95. gearman_string_free(vec);
  96. return TEST_SUCCESS;
  97. }
  98. static test_return_t gearman_string_create_string_size_TEST(void*)
  99. {
  100. gearman_vector_st* vec= gearman_string_create(NULL, test_literal_param(__func__));
  101. ASSERT_EQ(test_literal_param_size(__func__), vec->size());
  102. ASSERT_TRUE(vec->capacity() >= test_literal_param_size(__func__));
  103. gearman_string_free(vec);
  104. return TEST_SUCCESS;
  105. }
  106. static test_return_t gearman_string_create_size_TEST(void*)
  107. {
  108. gearman_vector_st* vec= gearman_string_create(NULL, 2023);
  109. ASSERT_EQ(0, vec->size());
  110. ASSERT_TRUE(vec->capacity() >= 2023);
  111. gearman_string_free(vec);
  112. return TEST_SUCCESS;
  113. }
  114. static test_return_t gearman_string_create_nonnull_size_TEST(void*)
  115. {
  116. gearman_vector_st vec(2048);
  117. gearman_vector_st* vec_ptr= gearman_string_create(&vec, 0);
  118. ASSERT_EQ(0, vec.size());
  119. ASSERT_EQ(0, vec.capacity());
  120. gearman_string_free(vec_ptr);
  121. return TEST_SUCCESS;
  122. }
  123. static test_return_t zero_resize_TEST(void*)
  124. {
  125. gearman_vector_st vec(89);
  126. ASSERT_EQ(0, vec.size());
  127. ASSERT_TRUE(vec.capacity() >= 89);
  128. vec.resize(0);
  129. ASSERT_EQ(0, vec.size());
  130. ASSERT_EQ(0, vec.capacity());
  131. return TEST_SUCCESS;
  132. }
  133. static test_return_t smaller_resize_TEST(void*)
  134. {
  135. gearman_vector_st vec(89);
  136. ASSERT_EQ(0, vec.size());
  137. ASSERT_TRUE(vec.capacity() >= 89);
  138. vec.resize(20);
  139. ASSERT_EQ(0, vec.size());
  140. ASSERT_TRUE(vec.capacity() >= 20);
  141. return TEST_SUCCESS;
  142. }
  143. static test_return_t bigger_resize_TEST(void*)
  144. {
  145. gearman_vector_st vec(89);
  146. ASSERT_EQ(0, vec.size());
  147. ASSERT_TRUE(vec.capacity() >= 89);
  148. vec.resize(181);
  149. ASSERT_EQ(0, vec.size());
  150. ASSERT_TRUE(vec.capacity() >= 181);
  151. return TEST_SUCCESS;
  152. }
  153. #pragma GCC diagnostic push
  154. #pragma GCC diagnostic ignored "-Woverflow"
  155. static test_return_t MAX_resize_TEST(void*)
  156. {
  157. const size_t max_block= 10 * GEARMAN_VECTOR_BLOCK_SIZE;
  158. for (size_t x= 0; x < 20; x++)
  159. {
  160. gearman_vector_st vec(random() % max_block);
  161. ASSERT_FALSE(vec.resize(std::numeric_limits<size_t>::max() -1));
  162. }
  163. return TEST_SUCCESS;
  164. }
  165. #pragma GCC diagnostic pop
  166. static test_return_t random_resize_TEST(void*)
  167. {
  168. const size_t max_block= 10 * GEARMAN_VECTOR_BLOCK_SIZE;
  169. for (size_t x= 0; x < 20; x++)
  170. {
  171. gearman_vector_st vec(labs(random()) % max_block);
  172. ASSERT_TRUE(vec.capacity() >= vec.size());
  173. vec.resize(labs(random()) % max_block);
  174. ASSERT_TRUE(vec.capacity() >= vec.size());
  175. vec.resize(labs(random()) % max_block +GEARMAN_VECTOR_BLOCK_SIZE);
  176. ASSERT_TRUE(vec.capacity() >= vec.size());
  177. }
  178. return TEST_SUCCESS;
  179. }
  180. static test_return_t gearman_vector_st__vec_printf_TEST(void*)
  181. {
  182. for (size_t x= 0; x < 20; x++)
  183. {
  184. gearman_vector_st vec;
  185. vec.vec_printf("%s", __func__);
  186. ASSERT_EQ(0, strcmp(__func__, vec.value()));
  187. ASSERT_EQ(strlen(__func__), vec.size());
  188. }
  189. return TEST_SUCCESS;
  190. }
  191. static test_return_t gearman_vector_st__vec_printf_empty_TEST(void*)
  192. {
  193. const size_t max_block= 10 * GEARMAN_VECTOR_BLOCK_SIZE;
  194. libtest::vchar_t base_string;
  195. libtest::vchar::make(base_string, random() % max_block);
  196. gearman_vector_st base_vec(base_string.size());
  197. ASSERT_TRUE(base_vec.store(vchar_param(base_string)));
  198. gearman_vector_st vec;
  199. ASSERT_TRUE(vec.store(base_vec));
  200. vec.vec_printf("");
  201. ASSERT_EQ(0, vec.size());
  202. return TEST_SUCCESS;
  203. }
  204. static test_return_t gearman_vector_st__vec_append_printf_NULL_TEST(void*)
  205. {
  206. const size_t max_block= 10 * GEARMAN_VECTOR_BLOCK_SIZE;
  207. libtest::vchar_t base_string;
  208. libtest::vchar::make(base_string, random() % max_block);
  209. gearman_vector_st base_vec(base_string.size());
  210. ASSERT_TRUE(base_vec.store(vchar_param(base_string)));
  211. gearman_vector_st vec;
  212. ASSERT_TRUE(vec.store(base_vec));
  213. ASSERT_EQ(vec.size(), base_vec.size());
  214. // We are just testing to see if we set off NULL
  215. vec.vec_append_printf("%s", NULL);
  216. ASSERT_TRUE(vec.size() >= base_vec.size());
  217. return TEST_SUCCESS;
  218. }
  219. static test_return_t gearman_vector_st__vec_append_printf_empty_TEST(void*)
  220. {
  221. const size_t max_block= 10 * GEARMAN_VECTOR_BLOCK_SIZE;
  222. libtest::vchar_t base_string;
  223. libtest::vchar::make(base_string, random() % max_block);
  224. gearman_vector_st base_vec(base_string.size());
  225. ASSERT_TRUE(base_vec.store(vchar_param(base_string)));
  226. gearman_vector_st vec;
  227. ASSERT_TRUE(vec.store(base_vec));
  228. ASSERT_EQ(vec.size(), base_vec.size());
  229. vec.vec_append_printf("");
  230. ASSERT_EQ(vec.size(), base_vec.size());
  231. return TEST_SUCCESS;
  232. }
  233. static test_return_t gearman_vector_st__vec_printf_NULL_TEST(void*)
  234. {
  235. gearman_vector_st vec;
  236. // We are just testing to see if we set off NULL
  237. vec.vec_printf("%s", NULL);
  238. return TEST_SUCCESS;
  239. }
  240. static test_return_t gearman_vector_st__vec_append_printf_TEST(void*)
  241. {
  242. const size_t max_block= 10 * GEARMAN_VECTOR_BLOCK_SIZE;
  243. libtest::vchar_t base_string;
  244. libtest::vchar::make(base_string, random() % max_block);
  245. gearman_vector_st base_vec(base_string.size());
  246. ASSERT_TRUE(base_vec.store(vchar_param(base_string)));
  247. for (size_t x= 0; x < 20; x++)
  248. {
  249. gearman_vector_st vec;
  250. ASSERT_TRUE(vec.store(base_vec));
  251. vec.vec_printf("%s", __func__);
  252. ASSERT_EQ(0, strcmp(__func__, vec.value()));
  253. ASSERT_EQ(strlen(__func__), vec.size());
  254. }
  255. return TEST_SUCCESS;
  256. }
  257. static test_return_t gearman_vector_st__vec_append_printf_1_TEST(void*)
  258. {
  259. gearman_vector_st vec;
  260. for (size_t x= 0; x < 10000; x++)
  261. {
  262. vec.vec_append_printf("a");
  263. ASSERT_EQ(x +1, vec.size());
  264. }
  265. return TEST_SUCCESS;
  266. }
  267. static test_return_t gearman_vector_st__vec_printf_string2_TEST(void*)
  268. {
  269. const size_t max_block= 10 * GEARMAN_VECTOR_BLOCK_SIZE;
  270. libtest::vchar_t base_string;
  271. libtest::vchar::make(base_string, random() % max_block);
  272. gearman_vector_st base_vec(base_string.size());
  273. ASSERT_TRUE(base_vec.store(vchar_param(base_string)));
  274. for (size_t x= 0; x < 20; x++)
  275. {
  276. gearman_vector_st vec;
  277. ASSERT_TRUE(vec.store(base_vec));
  278. libtest::vchar_t random_string1;
  279. libtest::vchar::make(random_string1, random() % max_block);
  280. libtest::vchar_t random_string2;
  281. libtest::vchar::make(random_string2, random() % max_block);
  282. // We are just testing to see if we set off NULL
  283. int length= vec.vec_printf("%.*s %.*s", vchar_printf(random_string1), vchar_printf(random_string2));
  284. ASSERT_EQ(length, int(random_string1.size() +random_string2.size() +1));
  285. }
  286. return TEST_SUCCESS;
  287. }
  288. static test_return_t gearman_vector_st__vec_append_printf_string2_TEST(void*)
  289. {
  290. const size_t max_block= 10 * GEARMAN_VECTOR_BLOCK_SIZE;
  291. libtest::vchar_t base_string;
  292. libtest::vchar::make(base_string, random() % max_block);
  293. gearman_vector_st base_vec(base_string.size());
  294. ASSERT_TRUE(base_vec.store(vchar_param(base_string)));
  295. for (size_t x= 0; x < 20; x++)
  296. {
  297. gearman_vector_st vec;
  298. ASSERT_TRUE(vec.store(base_vec));
  299. libtest::vchar_t random_string1;
  300. libtest::vchar::make(random_string1, random() % max_block);
  301. libtest::vchar_t random_string2;
  302. libtest::vchar::make(random_string2, random() % max_block);
  303. // We are just testing to see if we set off NULL
  304. int length= vec.vec_append_printf("%.*s %.*s", vchar_printf(random_string1), vchar_printf(random_string2));
  305. ASSERT_TRUE(length > 0);
  306. ASSERT_EQ(vec.size(), size_t(random_string1.size() +random_string2.size() +base_vec.size() +1));
  307. }
  308. return TEST_SUCCESS;
  309. }
  310. static test_return_t gearman_vector_st__append_character_TEST(void*)
  311. {
  312. for (size_t x= 0; x < 20; x++)
  313. {
  314. char test_string[3];
  315. gearman_vector_st vec;
  316. int a= libtest::random_alpha_num();
  317. vec.append_character(a);
  318. int b= libtest::random_alpha_num();
  319. vec.append_character(b);
  320. test_string[0]= a;
  321. test_string[1]= b;
  322. test_string[2]= 0;
  323. ASSERT_EQ(0, strcmp(test_string, vec.value()));
  324. }
  325. return TEST_SUCCESS;
  326. }
  327. static test_return_t gearman_string_append_character_TEST(void*)
  328. {
  329. const size_t max_block= 10 * GEARMAN_VECTOR_BLOCK_SIZE;
  330. for (size_t x= 0; x < 20; x++)
  331. {
  332. gearman_vector_st vec(random() % max_block);
  333. ASSERT_TRUE(vec.capacity() >= vec.size());
  334. gearman_string_append_character(&vec, libtest::random_alpha_num());
  335. ASSERT_TRUE(vec.capacity() >= vec.size());
  336. if (random() % 2)
  337. {
  338. vec.clear();
  339. ASSERT_TRUE(vec.capacity() >= vec.size());
  340. }
  341. }
  342. return TEST_SUCCESS;
  343. }
  344. static test_return_t gearman_string_append_TEST(void*)
  345. {
  346. const size_t max_block= 10 * GEARMAN_VECTOR_BLOCK_SIZE;
  347. for (size_t x= 0; x < 20; x++)
  348. {
  349. gearman_vector_st vec(random() % max_block);
  350. libtest::vchar_t random_string;
  351. libtest::vchar::make(random_string, random() % max_block);
  352. gearman_string_append(&vec, &random_string[0], random_string.size());
  353. if (random() % 2)
  354. {
  355. vec.clear();
  356. }
  357. }
  358. return TEST_SUCCESS;
  359. }
  360. static test_return_t gearman_string_take_TEST(void*)
  361. {
  362. const size_t max_block= 10 * GEARMAN_VECTOR_BLOCK_SIZE;
  363. for (size_t x= 0; x < 20; x++)
  364. {
  365. gearman_vector_st vec((random() % max_block) +1);
  366. // Now we insert a random string
  367. libtest::vchar_t random_string;
  368. libtest::vchar::make(random_string, (random() % max_block) +1);
  369. gearman_string_append(&vec, &random_string[0], random_string.size());
  370. gearman_string_t temp= gearman_string_take_string(&vec);
  371. ASSERT_TRUE(gearman_c_str(temp));
  372. free((void*)(gearman_c_str(temp)));
  373. }
  374. return TEST_SUCCESS;
  375. }
  376. static test_return_t gearman_string_allocate_take_TEST(void*)
  377. {
  378. const size_t max_block= 10 * GEARMAN_VECTOR_BLOCK_SIZE;
  379. for (size_t x= 0; x < 20; x++)
  380. {
  381. gearman_vector_st* vec= new gearman_vector_st((random() % max_block) +1);
  382. { // Now we insert a random string
  383. libtest::vchar_t random_string;
  384. libtest::vchar::make(random_string, random() % max_block);
  385. gearman_string_append(vec, &random_string[0], random_string.size());
  386. }
  387. gearman_string_t temp= gearman_string_take_string(vec);
  388. ASSERT_TRUE(gearman_c_str(temp));
  389. free((void*)(gearman_c_str(temp)));
  390. if (random() % 2)
  391. { // Now we insert a random string
  392. libtest::vchar_t random_string;
  393. libtest::vchar::make(random_string, random() % max_block);
  394. gearman_string_append(vec, &random_string[0], random_string.size());
  395. }
  396. delete vec;
  397. }
  398. return TEST_SUCCESS;
  399. }
  400. test_st allocate_TESTS[] ={
  401. { "declare vector", 0, declare_vector_TEST },
  402. { "new vector", 0, new_vector_TEST },
  403. { "declare vector(2048)", 0, declare_vector_size_TEST },
  404. { "new vector(2023)", 0, new_vector_size_TEST },
  405. { "gearman_string_create(NULL, 0)", 0, gearman_string_create_TEST },
  406. { "gearman_string_create(NULL, NULL, 0)", 0, gearman_string_create_NULL_NULL_TEST },
  407. { "gearman_string_create(NULL, literal)", 0, gearman_string_create_string_size_TEST },
  408. { "gearman_string_create(NULL, literal)x2", 0, gearman_string_create_string_redo_size_TEST },
  409. { "gearman_string_create(NULL, 2023)", 0, gearman_string_create_size_TEST },
  410. { "gearman_string_create(vec, 2023)", 0, gearman_string_create_nonnull_size_TEST },
  411. { 0, 0, 0 }
  412. };
  413. test_st resize_TESTS[] ={
  414. { "zero", 0, zero_resize_TEST },
  415. { "smaller", 0, smaller_resize_TEST },
  416. { "bigger", 0, bigger_resize_TEST },
  417. { "random", 0, random_resize_TEST },
  418. { "INT64_MAX", 0, MAX_resize_TEST },
  419. { 0, 0, 0 }
  420. };
  421. test_st append_TESTS[] ={
  422. { "gearman_string_append_character()", 0, gearman_string_append_character_TEST },
  423. { "gearman_string_append()", 0, gearman_string_append_TEST },
  424. { "gearman_vector_st::append_character()", 0, gearman_vector_st__append_character_TEST },
  425. { 0, 0, 0 }
  426. };
  427. test_st take_TESTS[] ={
  428. { "gearman_string_take_string()", 0, gearman_string_take_TEST },
  429. { "new gearman_vector_st() gearman_string_take_string()", 0, gearman_string_allocate_take_TEST },
  430. { 0, 0, 0 }
  431. };
  432. test_st printf_TESTS[] ={
  433. { "gearman_vector_st::vec_printf()", 0, gearman_vector_st__vec_printf_TEST },
  434. { "gearman_vector_st::vec_printf(EMPTY)", 0, gearman_vector_st__vec_printf_empty_TEST },
  435. { "gearman_vector_st::vec_printf(, NULL)", 0, gearman_vector_st__vec_printf_NULL_TEST },
  436. { "gearman_vector_st::vec_printf(, string, string)", 0, gearman_vector_st__vec_printf_string2_TEST },
  437. { 0, 0, 0 }
  438. };
  439. test_st append_printf_TESTS[] ={
  440. { "gearman_vector_st::vec_append_printf(1)", 0, gearman_vector_st__vec_append_printf_1_TEST },
  441. { "gearman_vector_st::vec_append_printf()", 0, gearman_vector_st__vec_append_printf_TEST },
  442. { "gearman_vector_st::vec_append_printf(EMPTY)", 0, gearman_vector_st__vec_append_printf_empty_TEST },
  443. { "gearman_vector_st::vec_append_printf(, NULL)", 0, gearman_vector_st__vec_append_printf_NULL_TEST },
  444. { "gearman_vector_st::vec_append_printf(, string, string)", 0, gearman_vector_st__vec_append_printf_string2_TEST },
  445. { 0, 0, 0 }
  446. };
  447. collection_st collection[] ={
  448. {"allocate", NULL, NULL, allocate_TESTS },
  449. {"resize", NULL, NULL, resize_TESTS },
  450. {"append", NULL, NULL, append_TESTS },
  451. {"take", NULL, NULL, take_TESTS },
  452. {"printf", NULL, NULL, printf_TESTS },
  453. {"append_printf", NULL, NULL, append_printf_TESTS },
  454. {0, 0, 0, 0}
  455. };
  456. void get_world(libtest::Framework *world)
  457. {
  458. world->collections(collection);
  459. }