Skip to content

Commit

Permalink
C++ test improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
amunra committed Jan 29, 2024
1 parent d89e932 commit 7a4265c
Show file tree
Hide file tree
Showing 6 changed files with 7,211 additions and 89 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ jobs:
command: clippy
args: --all-targets --all-features -- -D warnings

- name: Compile FFI tests
run: |
cd questdb-confstr-ffi
cd cpp_test
./compile
- name: Run FFI tests
run: |
cd questdb-confstr-ffi
Expand Down
3 changes: 3 additions & 0 deletions questdb-confstr-ffi/cpp_test/compile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rm -fR ./build > /dev/null 2>&1 && \
cmake -B ./build -S . && \
cmake --build ./build
7,106 changes: 7,106 additions & 0 deletions questdb-confstr-ffi/cpp_test/doctest.hpp

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions questdb-confstr-ffi/cpp_test/run
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
rm -fR ./build > /dev/null 2>&1 && \
cmake -B ./build -S . && \
cmake --build ./build && \
./build/test
105 changes: 19 additions & 86 deletions questdb-confstr-ffi/cpp_test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,104 +23,37 @@
******************************************************************************/

#include <iostream>
#include <optional>
#include <string_view>

#include <questdb/conf_str.h>
#include <questdb/conf_str.hpp>

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.hpp"

class parse_err {
public:
parse_err(questdb_conf_str_parse_err* impl) : _impl(impl) {}

std::string_view msg() const noexcept {
return { _impl->msg, _impl->msg_len };
}

size_t pos() const noexcept {
return _impl->pos;
}

~parse_err() noexcept {
questdb_conf_str_parse_err_free(_impl);
}

private:
questdb_conf_str_parse_err* _impl;
};


class conf_str {
public:
static conf_str parse(std::string_view str) {
questdb_conf_str_parse_err* err = nullptr;
auto res = ::questdb_conf_str_parse(str.data(), str.size(), &err);
if (res != nullptr) {
return conf_str{res};
} else {
throw parse_err(err);
}
}

std::string_view service() const noexcept {
size_t service_len = 0;
auto str = ::questdb_conf_str_service(_impl, &service_len);
return { str, service_len };
}

std::optional<std::string_view> get(std::string_view key) const noexcept {
size_t val_len = 0;
auto str = ::questdb_conf_str_get(_impl, key.data(), key.size(), &val_len);
if (str != nullptr) {
return { { str, val_len } };
}
return {};
}

~conf_str() noexcept {
::questdb_conf_str_free(_impl);
}

private:
conf_str(::questdb_conf_str* impl) : _impl(impl) {}
::questdb_conf_str* _impl;
};


static void t1() {
TEST_CASE("basic no params")
{
const auto c1 = conf_str::parse("http");
assert(c1.service() == "http");
CHECK(c1.service() == "http");
CHECK(c1.get("host") == std::nullopt);
}

static void t2() {
TEST_CASE("basic with params")
{
const auto c1 = conf_str::parse("http::host=localhost;port=9000;");
assert(c1.service() == "http");
assert(c1.get("host") == "localhost");
assert(c1.get("port") == "9000");
CHECK(c1.service() == "http");
CHECK(c1.get("host") == "localhost");
CHECK(c1.get("port") == "9000");
}

static void t3() {
TEST_CASE("parse error")
{
auto str = "http;port=9000";
REQUIRE_THROWS_AS(conf_str::parse(str), parse_err);
try {
const auto c1 = conf_str::parse("http;port=9000");
abort();
conf_str::parse(str);
} catch (const parse_err& e) {
assert(e.msg() == "bad separator, expected ':' got ';' at position 4");
assert(e.pos() == 4);
CHECK(e.msg() == "bad separator, expected ':' got ';' at position 4");
CHECK(e.pos() == 4);
}
}

int main() {
std::cerr << "Running tests" << std::endl;

std::cerr << "t1" << std::endl;
t1();

std::cerr << "t2" << std::endl;
t2();

std::cerr << "t3" << std::endl;
t3();

std::cerr << "All tests passed" << std::endl;
return 0;
}
77 changes: 77 additions & 0 deletions questdb-confstr-ffi/include/questdb/conf_str.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#pragma once

#include "conf_str.h"

#include <optional>
#include <string_view>


class parse_err
{
public:
parse_err(questdb_conf_str_parse_err* impl) : _impl(impl) {}

std::string_view msg() const noexcept
{
return { _impl->msg, _impl->msg_len };
}

size_t pos() const noexcept
{
return _impl->pos;
}

~parse_err() noexcept
{
questdb_conf_str_parse_err_free(_impl);
}

private:
questdb_conf_str_parse_err* _impl;
};


class conf_str
{
public:
static conf_str parse(std::string_view str)
{
questdb_conf_str_parse_err* err = nullptr;
auto res = ::questdb_conf_str_parse(str.data(), str.size(), &err);
if (res != nullptr)
{
return conf_str{res};
}
else
{
throw parse_err(err);
}
}

std::string_view service() const noexcept
{
size_t service_len = 0;
auto str = ::questdb_conf_str_service(_impl, &service_len);
return { str, service_len };
}

std::optional<std::string_view> get(std::string_view key) const noexcept
{
size_t val_len = 0;
auto str = ::questdb_conf_str_get(_impl, key.data(), key.size(), &val_len);
if (str != nullptr)
{
return { { str, val_len } };
}
return {};
}

~conf_str() noexcept
{
::questdb_conf_str_free(_impl);
}

private:
conf_str(::questdb_conf_str* impl) : _impl(impl) {}
::questdb_conf_str* _impl;
};

0 comments on commit 7a4265c

Please sign in to comment.