2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
29 #include "test/conf.h"
30 #include "test/lib/catch.hpp"
31 #include "test/lib/random.hpp"
33 #include "libmemcached/memcached.h"
36 using socket_or_port_t = variant<string, int>;
39 * Useful macros for testing
41 #define S(s) (s), strlen(s)
42 #define DECLARE_STREQUAL static auto strequal = equal_to<string>();
43 #define LOOPED_SECTION(tests) \
44 for (auto &[name, test] : tests) DYNAMIC_SECTION("test " << name)
45 #define REQUIRE_SUCCESS(rc) \
47 INFO("expected: SUCCESS"); \
48 REQUIRE_THAT(rc, test.returns.success()); \
50 #define REQUIRE_RC(rc, call) \
52 INFO("expected: " << memcached_strerror(nullptr, rc)); \
53 REQUIRE_THAT(call, test.returns(rc)); \
56 const char *getenv_else(const char *var, const char *defval);
58 inline memcached_return_t fetch_all_results(memcached_st *memc, unsigned int &keys_returned,
59 memcached_return_t &rc) {
62 memcached_result_st *result = nullptr;
63 while ((result = memcached_fetch_result(memc, result, &rc))) {
64 REQUIRE(MEMCACHED_SUCCESS == rc);
67 memcached_result_free(result);
68 return MEMCACHED_SUCCESS;
71 inline memcached_return_t fetch_all_results(memcached_st *memc, unsigned int &keys_returned) {
72 memcached_return_t rc;
73 fetch_all_results(memc, keys_returned, rc);
82 explicit Tempfile(const char templ_[] = "memc.test.XXXXXX") {
83 *copy(S(templ_) + templ_, fn) = '\0';
90 [[nodiscard]] int getFd() const { return fd; }
91 [[nodiscard]] const char *getFn() const { return fn; }
92 bool put(const char *buf, size_t len) const {
93 return static_cast<ssize_t>(len) == write(fd, buf, len);
105 explicit MemcachedPtr(memcached_st *memc_) { memc = memc_; }
107 : MemcachedPtr(memcached_create(nullptr)) {}
108 ~MemcachedPtr() { memcached_free(memc); }
109 memcached_st *operator*() const { return memc; }
110 auto operator->() const { return memc; }
118 explicit Malloced(T *ptr_)
120 Malloced &operator=(T *ptr_) {
130 auto operator*() { return ptr; }
131 auto operator->() { return ptr; }