result.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Test memcached_result_st
  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/result.hpp"
  43. #include "libgearman/assert.hpp"
  44. #include "libgearman-1.0/visibility.h"
  45. #include "libgearman-1.0/result.h"
  46. #include <memory>
  47. static test_return_t declare_result_TEST(void*)
  48. {
  49. gearman_result_st result;
  50. ASSERT_EQ(0, result.size());
  51. ASSERT_EQ(0, result.capacity());
  52. return TEST_SUCCESS;
  53. }
  54. static test_return_t new_result_TEST(void*)
  55. {
  56. gearman_result_st* result= new gearman_result_st;
  57. ASSERT_EQ(0, result->size());
  58. ASSERT_EQ(0, result->capacity());
  59. delete result;
  60. return TEST_SUCCESS;
  61. }
  62. static test_return_t declare_result_size_TEST(void*)
  63. {
  64. gearman_result_st result(2048);
  65. ASSERT_EQ(0, result.size());
  66. ASSERT_TRUE(result.capacity() >= 2048);
  67. return TEST_SUCCESS;
  68. }
  69. static test_return_t new_result_size_TEST(void*)
  70. {
  71. gearman_result_st* result= new gearman_result_st(2023);
  72. ASSERT_EQ(0, result->size());
  73. ASSERT_TRUE(result->capacity() >= 2023);
  74. delete result;
  75. return TEST_SUCCESS;
  76. }
  77. static test_return_t zero_resize_TEST(void*)
  78. {
  79. gearman_result_st result(89);
  80. ASSERT_EQ(0, result.size());
  81. ASSERT_TRUE(result.capacity() >= 89);
  82. result.resize(0);
  83. ASSERT_EQ(0, result.size());
  84. ASSERT_EQ(0, result.capacity());
  85. return TEST_SUCCESS;
  86. }
  87. static test_return_t smaller_resize_TEST(void*)
  88. {
  89. gearman_result_st result(89);
  90. ASSERT_EQ(0, result.size());
  91. ASSERT_TRUE(result.capacity() >= 89);
  92. result.resize(20);
  93. ASSERT_EQ(0, result.size());
  94. ASSERT_TRUE(result.capacity() >= 20);
  95. return TEST_SUCCESS;
  96. }
  97. static test_return_t bigger_resize_TEST(void*)
  98. {
  99. gearman_result_st result(89);
  100. ASSERT_EQ(0, result.size());
  101. ASSERT_TRUE(result.capacity() >= 89);
  102. result.resize(181);
  103. ASSERT_EQ(0, result.size());
  104. ASSERT_TRUE(result.capacity() >= 181);
  105. return TEST_SUCCESS;
  106. }
  107. static test_return_t random_resize_TEST(void*)
  108. {
  109. const size_t max_block= 10 * GEARMAN_VECTOR_BLOCK_SIZE;
  110. for (size_t x= 0; x < 20; x++)
  111. {
  112. gearman_result_st result(random() % max_block);
  113. ASSERT_TRUE(result.capacity() >= result.size());
  114. result.resize(random() % max_block);
  115. ASSERT_TRUE(result.capacity() >= result.size());
  116. result.resize(random() % max_block +GEARMAN_VECTOR_BLOCK_SIZE);
  117. ASSERT_TRUE(result.capacity() >= result.size());
  118. }
  119. return TEST_SUCCESS;
  120. }
  121. static test_return_t append_TEST(void*)
  122. {
  123. const size_t max_block= 10 * GEARMAN_VECTOR_BLOCK_SIZE;
  124. for (size_t x= 0; x < 20; x++)
  125. {
  126. gearman_result_st result(random() % max_block);
  127. libtest::vchar_t random_string;
  128. libtest::vchar::make(random_string, (random() % max_block) +1);
  129. result.append(&random_string[0], random_string.size());
  130. if (random() % 2)
  131. {
  132. result.clear();
  133. }
  134. }
  135. return TEST_SUCCESS;
  136. }
  137. static test_return_t gearman_string_take_TEST(void*)
  138. {
  139. const size_t max_block= 10 * GEARMAN_VECTOR_BLOCK_SIZE;
  140. for (size_t x= 0; x < 20; x++)
  141. {
  142. gearman_result_st result((random() % max_block) +1);
  143. // Now we insert a random string
  144. libtest::vchar_t random_string;
  145. libtest::vchar::make(random_string, (random() % max_block) +1);
  146. result.append(&random_string[0], random_string.size());
  147. gearman_string_t temp= gearman_result_take_string(&result);
  148. ASSERT_TRUE(gearman_c_str(temp));
  149. free((void*)(gearman_c_str(temp)));
  150. }
  151. return TEST_SUCCESS;
  152. }
  153. static test_return_t gearman_string_allocate_take_TEST(void*)
  154. {
  155. const size_t max_block= 10 * GEARMAN_VECTOR_BLOCK_SIZE;
  156. for (size_t x= 0; x < 20; x++)
  157. {
  158. gearman_result_st* result= new gearman_result_st((random() % max_block) +1);
  159. { // Now we insert a random string
  160. libtest::vchar_t random_string;
  161. libtest::vchar::make(random_string, (random() % max_block) +1);
  162. result->append(&random_string[0], random_string.size());
  163. }
  164. gearman_string_t temp= gearman_result_take_string(result);
  165. ASSERT_TRUE(gearman_c_str(temp));
  166. free((void*)(gearman_c_str(temp)));
  167. if (random() % 2)
  168. { // Now we insert a random string
  169. libtest::vchar_t random_string;
  170. libtest::vchar::make(random_string, (random() % max_block) +1);
  171. result->append(&random_string[0], random_string.size());
  172. }
  173. delete result;
  174. }
  175. return TEST_SUCCESS;
  176. }
  177. static test_return_t gearman_result_integer_TEST(void*)
  178. {
  179. gearman_result_st result;
  180. ASSERT_TRUE(result.store(8976));
  181. ASSERT_EQ(0, gearman_result_integer(NULL));
  182. ASSERT_EQ(8976, gearman_result_integer(&result));
  183. return TEST_SUCCESS;
  184. }
  185. static test_return_t gearman_result_boolean_TEST(void*)
  186. {
  187. gearman_result_st result;
  188. ASSERT_TRUE(result.is_type(GEARMAN_RESULT_NULL));
  189. ASSERT_EQ(false, gearman_result_boolean(NULL));
  190. ASSERT_TRUE(result.boolean(true));
  191. ASSERT_EQ(true, gearman_result_boolean(&result));
  192. ASSERT_TRUE(result.is_type(GEARMAN_RESULT_BOOLEAN));
  193. ASSERT_TRUE(result.boolean(false));
  194. ASSERT_EQ(false, gearman_result_boolean(&result));
  195. ASSERT_TRUE(result.is_type(GEARMAN_RESULT_BOOLEAN));
  196. gearman_result_st result_false;
  197. ASSERT_TRUE(result_false.boolean(false));
  198. ASSERT_EQ(false, gearman_result_boolean(&result_false));
  199. ASSERT_TRUE(result.is_type(GEARMAN_RESULT_BOOLEAN));
  200. return TEST_SUCCESS;
  201. }
  202. static test_return_t gearman_result_string_TEST(void*)
  203. {
  204. gearman_string_t value= { test_literal_param("This is my echo test") };
  205. gearman_result_st result;
  206. ASSERT_EQ(GEARMAN_SUCCESS, gearman_result_store_string(&result, value));
  207. gearman_string_t ret_value= gearman_result_string(&result);
  208. ASSERT_EQ(test_literal_param_size(value), test_literal_param_size(ret_value));
  209. ASSERT_EQ(GEARMAN_INVALID_ARGUMENT, gearman_result_store_string(NULL, value));
  210. return TEST_SUCCESS;
  211. }
  212. static test_return_t gearman_result_store_string_TEST(void*)
  213. {
  214. gearman_string_t value= { test_literal_param("This is my echo test") };
  215. ASSERT_EQ(GEARMAN_INVALID_ARGUMENT, gearman_result_store_string(NULL, value));
  216. gearman_result_st result;
  217. ASSERT_EQ(GEARMAN_SUCCESS, gearman_result_store_string(&result, value));
  218. return TEST_SUCCESS;
  219. }
  220. static test_return_t gearman_result_store_integer_TEST(void*)
  221. {
  222. const int64_t value= __LINE__;
  223. gearman_result_st result;
  224. gearman_result_store_integer(&result, value);
  225. return TEST_SUCCESS;
  226. }
  227. static test_return_t gearman_result_store_value_TEST(void*)
  228. {
  229. ASSERT_EQ(GEARMAN_INVALID_ARGUMENT, gearman_result_store_value(NULL, NULL, 0));
  230. gearman_result_st result;
  231. ASSERT_EQ(GEARMAN_SUCCESS, gearman_result_store_value(&result, test_literal_param(__func__)));
  232. return TEST_SUCCESS;
  233. }
  234. static test_return_t gearman_result_size_TEST(void*)
  235. {
  236. gearman_result_st result;
  237. ASSERT_EQ(GEARMAN_SUCCESS, gearman_result_store_value(&result, test_literal_param(__func__)));
  238. ASSERT_EQ(strlen(__func__), gearman_result_size(&result));
  239. ASSERT_EQ(0, gearman_result_size(NULL));
  240. return TEST_SUCCESS;
  241. }
  242. static test_return_t gearman_result_value_TEST(void*)
  243. {
  244. gearman_result_st result;
  245. ASSERT_EQ(GEARMAN_SUCCESS, gearman_result_store_value(&result, test_literal_param(__func__)));
  246. ASSERT_EQ(GEARMAN_INVALID_ARGUMENT, gearman_result_store_value(NULL, test_literal_param(__func__)));
  247. return TEST_SUCCESS;
  248. }
  249. static test_return_t gearman_result_is_null_TEST(void*)
  250. {
  251. gearman_result_st result;
  252. ASSERT_TRUE(gearman_result_is_null(&result));
  253. ASSERT_TRUE(gearman_result_is_null(NULL));
  254. return TEST_SUCCESS;
  255. }
  256. test_st allocate_TESTS[] ={
  257. { "declare result", 0, declare_result_TEST },
  258. { "new result", 0, new_result_TEST },
  259. { "declare result(2048)", 0, declare_result_size_TEST },
  260. { "new result(2023)", 0, new_result_size_TEST },
  261. { 0, 0, 0 }
  262. };
  263. test_st resize_TESTS[] ={
  264. { "zero", 0, zero_resize_TEST },
  265. { "smaller", 0, smaller_resize_TEST },
  266. { "bigger", 0, bigger_resize_TEST },
  267. { "random", 0, random_resize_TEST },
  268. { 0, 0, 0 }
  269. };
  270. test_st append_TESTS[] ={
  271. { "append()", 0, append_TEST },
  272. { 0, 0, 0 }
  273. };
  274. test_st take_TESTS[] ={
  275. { "gearman_string_take_string()", 0, gearman_string_take_TEST },
  276. { "new gearman_result_st() gearman_string_take_string()", 0, gearman_string_allocate_take_TEST },
  277. { 0, 0, 0 }
  278. };
  279. test_st API_TESTS[] ={
  280. { "gearman_result_integer()", 0, gearman_result_integer_TEST },
  281. { "gearman_result_boolean()", 0, gearman_result_boolean_TEST },
  282. { "gearman_result_string()", 0, gearman_result_string_TEST },
  283. { "gearman_result_store_string()", 0, gearman_result_store_string_TEST },
  284. { "gearman_result_store_integer()", 0, gearman_result_store_integer_TEST },
  285. { "gearman_result_store_value()", 0, gearman_result_store_value_TEST },
  286. { "gearman_result_size()", 0, gearman_result_size_TEST },
  287. { "gearman_result_value()", 0, gearman_result_value_TEST },
  288. { "gearman_result_is_null()", 0, gearman_result_is_null_TEST },
  289. { 0, 0, 0 }
  290. };
  291. collection_st collection[] ={
  292. {"allocate", NULL, NULL, allocate_TESTS },
  293. {"resize", NULL, NULL, resize_TESTS },
  294. {"append", NULL, NULL, append_TESTS },
  295. {"take", NULL, NULL, take_TESTS },
  296. {"API", NULL, NULL, API_TESTS },
  297. {0, 0, 0, 0}
  298. };
  299. void get_world(libtest::Framework *world)
  300. {
  301. world->collections(collection);
  302. }