+/*
+ +--------------------------------------------------------------------+
+ | libmemcached - C/C++ Client Library for memcached |
+ +--------------------------------------------------------------------+
+ | Redistribution and use in source and binary forms, with or without |
+ | modification, are permitted under the terms of the BSD license. |
+ | You should have received a copy of the license in a bundled file |
+ | named LICENSE; in case you did not receive a copy you can review |
+ | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
+ +--------------------------------------------------------------------+
+ | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
+ | Copyright (c) 2020 Michael Wallner <mike@php.net> |
+ +--------------------------------------------------------------------+
+*/
+
#pragma once
#include <chrono>
+#include <cstring>
#include <iostream>
#include <map>
#include <optional>
#include "test/conf.h"
#include "test/lib/catch.hpp"
+#include "test/lib/env.hpp"
#include "test/lib/random.hpp"
#include "libmemcached/memcached.h"
/**
* Useful macros for testing
*/
-#define S(s) (s),strlen(s)
-#define DECLARE_STREQUAL static auto strequal = equal_to<string>();
+#define S(s) (s), strlen(s)
#define LOOPED_SECTION(tests) \
for (auto &[name, test] : tests) DYNAMIC_SECTION("test " << name)
-#define REQUIRE_SUCCESS(rc) do { \
- INFO("expected: SUCCESS"); \
+#define REQUIRE_SUCCESS(rc) \
+ do { \
+ INFO("expected: SUCCESS"); \
REQUIRE_THAT(rc, test.returns.success()); \
- } while(0)
-#define REQUIRE_RC(rc, call) do { \
+ } 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);
+ REQUIRE_THAT(call, test.returns(rc)); \
+ } while (0)
-inline memcached_return_t fetch_all_results(memcached_st *memc, unsigned int &keys_returned, memcached_return_t &rc) {
+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;
class Tempfile {
public:
explicit Tempfile(const char templ_[] = "memc.test.XXXXXX") {
- *copy(S(templ_)+templ_, fn) = '\0';
+ *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;
- }
+ [[nodiscard]] int getFd() const { return fd; }
+ [[nodiscard]] const char *getFn() const { return fn; }
bool put(const char *buf, size_t len) const {
- return static_cast<ssize_t >(len) == write(fd, buf, len);
+ return static_cast<ssize_t>(len) == write(fd, buf, len);
}
+ string get() const {
+ string all;
+ char buf[200];
+ ssize_t len;
+
+ lseek(fd, 0, SEEK_SET);
+ while (0 < (len = read(fd, buf, sizeof(buf)))) {
+ all.append(buf, len);
+ }
+ return all;
+ }
+
private:
char fn[80];
int fd;
public:
memcached_st *memc;
- explicit
- MemcachedPtr(memcached_st *memc_) {
+ explicit MemcachedPtr(memcached_st *memc_) {
memc = memc_;
}
MemcachedPtr()
- : MemcachedPtr(memcached_create(nullptr))
- {}
+ : MemcachedPtr(memcached_create(nullptr)) {}
~MemcachedPtr() {
memcached_free(memc);
}
- memcached_st *operator * () {
+ memcached_st *operator*() const {
+ return memc;
+ }
+ auto operator->() const {
return memc;
}
};
template<class T>
class Malloced {
T *ptr;
+
public:
- explicit
- Malloced(T *ptr_)
- : ptr{ptr_}
- {}
- ~Malloced() {
- if(ptr)
+ explicit Malloced(T *ptr_)
+ : ptr{ptr_} {}
+ Malloced &operator=(T *ptr_) {
+ if (ptr)
free(ptr);
+ ptr = ptr_;
+ return *this;
}
- auto operator *() {
- return ptr;
- }
- auto operator ->() {
- return ptr;
+ ~Malloced() {
+ if (ptr)
+ free(ptr);
}
+ auto operator*() const { return ptr; }
+ auto operator->() const { return ptr; }
};