X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=testing%2Flib%2Fcommon.hpp;h=17199839495be35da7fecec89893e65718387215;hb=400788eac8cccf9c30a616129a5dc2d67780790f;hp=b516d669a66cb43d470754bfdca058e78c8a2308;hpb=f33fd6e7d1df8e5878ce5c5605f64bab7b02ceb6;p=m6w6%2Flibmemcached diff --git a/testing/lib/common.hpp b/testing/lib/common.hpp index b516d669..17199839 100644 --- a/testing/lib/common.hpp +++ b/testing/lib/common.hpp @@ -11,60 +11,109 @@ #include #include -#include "../lib/catch.hpp" +#include "testing/conf.h" +#include "testing/lib/catch.hpp" +#include "testing/lib/random.hpp" #include "libmemcached/memcached.h" -#define LITERAL(s) (s),strlen(s) -#define LOOPED_SECTION(tests) \ - auto i_=0; \ - for (auto &&test : tests) DYNAMIC_SECTION("test" << i_++) - using namespace std; - using socket_or_port_t = variant; +/** + * Useful macros for testing + */ +#define S(s) (s),strlen(s) +#define DECLARE_STREQUAL static auto strequal = equal_to(); +#define LOOPED_SECTION(tests) \ + for (auto &[name, test] : tests) DYNAMIC_SECTION("test " << name) +#define REQUIRE_SUCCESS(rc) do { \ + INFO("expected: SUCCESS"); \ + REQUIRE_THAT(rc, test.returns.success()); \ + } while(0) +#define REQUIRE_RC(rc, call) do { \ + INFO("expected: " << memcached_strerror(nullptr, rc)); \ + REQUIRE_THAT(call, test.returns(rc)); \ + } while(0) + const char *getenv_else(const char *var, const char *defval); -unsigned random_num(unsigned min, unsigned max); -unsigned random_port(); -string random_socket(); -string random_socket_or_port_string(const string &what); -string random_socket_or_port_flag(const string &binary); -inline auto random_socket_or_port_arg() { - return make_pair(&random_socket_or_port_flag, &random_socket_or_port_string); +inline memcached_return_t fetch_all_results(memcached_st *memc, unsigned int &keys_returned, memcached_return_t &rc) { + keys_returned = 0; + + memcached_result_st *result = nullptr; + while ((result = memcached_fetch_result(memc, result, &rc))) { + REQUIRE(MEMCACHED_SUCCESS == rc); + keys_returned += 1; + } + memcached_result_free(result); + return MEMCACHED_SUCCESS; +} + +inline memcached_return_t fetch_all_results(memcached_st *memc, unsigned int &keys_returned) { + memcached_return_t rc; + fetch_all_results(memc, keys_returned, rc); + return rc; } +#include +#include + +class Tempfile { +public: + explicit Tempfile(const char templ_[] = "memc.test.XXXXXX") { + *copy(S(templ_)+templ_, fn) = '\0'; + fd = mkstemp(fn); + } + ~Tempfile() { + close(fd); + unlink(fn); + } + int getFd() const { + return fd; + } + const char *getFn() const { + return fn; + } + bool put(const char *buf, size_t len) const { + return static_cast(len) == write(fd, buf, len); + } +private: + char fn[80]; + int fd; +}; + class MemcachedPtr { public: - memcached_st memc; + memcached_st *memc; explicit MemcachedPtr(memcached_st *memc_) { - memset(&memc, 0, sizeof(memc)); - REQUIRE(memcached_clone(&memc, memc_)); + memc = memc_; } MemcachedPtr() - : MemcachedPtr(nullptr) + : MemcachedPtr(memcached_create(nullptr)) {} ~MemcachedPtr() { - memcached_free(&memc); + memcached_free(memc); } memcached_st *operator * () { - return &memc; + return memc; } }; +template class Malloced { - void *ptr; + T *ptr; public: - Malloced(void *ptr_) + Malloced(T *ptr_) : ptr{ptr_} {} ~Malloced() { - free(ptr); + if(ptr) + free(ptr); } - void *operator *() { + auto operator *() { return ptr; } };