113 lines
3.2 KiB
C++
113 lines
3.2 KiB
C++
#include "gmock/gmock.h"
|
|
#include <gtest/gtest.h>
|
|
#include <memory_resource>
|
|
#include <string>
|
|
|
|
#include <rublon/utils.hpp>
|
|
|
|
using namespace rublon;
|
|
using namespace testing;
|
|
|
|
TEST(Utils, toBoolReturnsTrueWhenStringIsPassed) {
|
|
EXPECT_TRUE(conv::to_bool("TrUe"));
|
|
}
|
|
|
|
TEST(Utils, toBoolReturnsFalseWhenStringIsPassed) {
|
|
EXPECT_FALSE(conv::to_bool("False"));
|
|
}
|
|
|
|
std::string response = R"http(HTTP/2 200
|
|
date: Thu, 22 Jun 2023 13:24:58 GMT
|
|
content-type: application/json
|
|
server: nginx
|
|
cache-control: no-cache, private
|
|
x-rublon-signature: 1a01558bedaa2dd92ff659fb8ee3c65a89163d63e312fcb9b6f60463cce864d7
|
|
x-ratelimit-limit: 300
|
|
x-ratelimit-remaining: 299
|
|
|
|
{"status":"OK","result":{"tid":"2039132542F6465691BF8C41D7CC38C5","status":"pending","companyName":"rublon","applicationName":"Bartoszek_SSH","methods":["email","totp","qrcode","push"]}})http";
|
|
|
|
class LoggingAllocator : public std::pmr::memory_resource {
|
|
public:
|
|
int count{};
|
|
int bytes{};
|
|
|
|
void clear() {
|
|
count = 0;
|
|
bytes = 0;
|
|
}
|
|
// memory_resource interface
|
|
private:
|
|
void * do_allocate(std::size_t __bytes, std::size_t __alignment) override {
|
|
bytes += __bytes;
|
|
count++;
|
|
return std::pmr::get_default_resource()->allocate(__bytes, __alignment);
|
|
}
|
|
|
|
void do_deallocate(void * __p, std::size_t __bytes, std::size_t __alignment) override {
|
|
bytes -= __bytes;
|
|
count--;
|
|
std::pmr::get_default_resource()->deallocate(__p, __bytes, __alignment);
|
|
}
|
|
|
|
bool do_is_equal(const memory_resource & __other) const noexcept override {
|
|
return std::pmr::get_default_resource()->is_equal(__other);
|
|
}
|
|
};
|
|
|
|
TEST(Utils, responseParser) {
|
|
memory::MonotonicStackResource< 2048 > resource;
|
|
std::map<std::string, std::string> sut{};
|
|
rublon::details::headers(response, sut);
|
|
EXPECT_THAT(sut,
|
|
AllOf(Contains(Pair("date", "Thu, 22 Jun 2023 13:24:58 GMT")),
|
|
Contains(Pair("x-rublon-signature", "1a01558bedaa2dd92ff659fb8ee3c65a89163d63e312fcb9b6f60463cce864d7")),
|
|
Contains(Pair("x-ratelimit-remaining", "299"))));
|
|
}
|
|
|
|
class Memory : public testing::Test {
|
|
public:
|
|
// Test interface
|
|
protected:
|
|
void SetUp() override {
|
|
memory::set_default_resource(&alloc);
|
|
}
|
|
void TearDown() override {
|
|
memory::set_default_resource(std::pmr::get_default_resource());
|
|
}
|
|
|
|
LoggingAllocator alloc;
|
|
};
|
|
|
|
class X {};
|
|
|
|
TEST_F(Memory, setSetsProperResource) {
|
|
LoggingAllocator s;
|
|
memory::set_default_resource(&s);
|
|
EXPECT_EQ(&s, memory::default_resource());
|
|
}
|
|
|
|
TEST_F(Memory, stackResourceDoesNotUseHeapAtAll) {
|
|
memory::MonotonicStackResource< 1024 > mr;
|
|
std::pmr::string{"to large to fit in small buffer optimization", &mr};
|
|
|
|
EXPECT_EQ(alloc.count, 0);
|
|
}
|
|
|
|
TEST_F(Memory, heapResourceWillAllocate1kWhenAsk) {
|
|
memory::set_default_resource(&alloc);
|
|
memory::StrictMonotonic_1k_HeapResource mr;
|
|
std::pmr::string{"to large to fit in small buffer optimization", &mr};
|
|
|
|
EXPECT_EQ(alloc.count, 1);
|
|
EXPECT_EQ(alloc.bytes, 1024);
|
|
}
|
|
|
|
TEST_F(Memory, heapResourceWillAllocate8kWhenAsk) {
|
|
memory::StrictMonotonic_8k_HeapResource mr;
|
|
std::pmr::string{"to large to fit in small buffer optimization", &mr};
|
|
|
|
EXPECT_EQ(alloc.count, 1);
|
|
EXPECT_EQ(alloc.bytes, 8 * 1024);
|
|
}
|