Commit e9a9a2ff authored by tqcq's avatar tqcq
Browse files

feat add string_view

parent 3ff33234
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -191,6 +191,8 @@ if(SLED_BUILD_TESTS)
                src/sled/debugging/symbolize_test.cc NO_MAIN)
  sled_add_test(NAME sled_sigslot_test SRCS src/sled/sigslot_test.cc)
  sled_add_test(NAME sled_rpc_test SRCS src/sled/network/rpc_test.cc)
  sled_add_test(NAME sled_string_view_test SRCS
                src/sled/nonstd/string_view_test.cc)
endif(SLED_BUILD_TESTS)

if(SLED_BUILD_FUZZ)
+1713 −0

File added.

Preview size limit exceeded, changes collapsed.

+59 −0
Original line number Diff line number Diff line
#include <sled/nonstd/string_view.h>

using namespace sled;
using size_type = size_t;

TEST_SUITE("string_view")
{
    TEST_CASE("string_view: Allows to default construct an empty string_view")
    {
        string_view sv;

        // use parenthesis with data() to prevent lest from using sv.data() as C-string:

        CHECK(sv.size() == size_type(0));
        CHECK((sv.data() == nssv_nullptr));
    }

    TEST_CASE("string_view: Allows to construct from pointer and size")
    {
        string_view sv("hello world", 5);

        CHECK(sv.size() == size_type(5));
        CHECK(*(sv.data() + 0) == 'h');
        CHECK(*(sv.data() + 4) == 'o');
    }

    TEST_CASE("string_view: Allows to construct from C-string")
    {
        string_view sv("hello world");

        CHECK(sv.size() == size_type(11));
        CHECK(*(sv.data() + 0) == 'h');
        CHECK(*(sv.data() + 10) == 'd');
    }

    TEST_CASE("string_view: Allows to copy-construct from empty string_view")
    {
        string_view sv1;

        string_view sv2(sv1);

        // use parenthesis with data() to prevent lest from using sv.data() as C-string:

        CHECK(sv2.size() == size_type(0));
        CHECK((sv2.data() == nssv_nullptr));
    }

    TEST_CASE("string_view: Allows to copy-construct from non-empty string_view")
    {
        string_view sv1("hello world", 5);

        string_view sv2(sv1);

        CHECK(sv2.size() == sv1.size());
        CHECK((sv2.data() == sv1.data()));
        CHECK(*(sv2.data() + 0) == 'h');
        CHECK(*(sv2.data() + 4) == 'o');
    }
}
+3 −2
Original line number Diff line number Diff line
@@ -3,9 +3,10 @@
#define SLED_SLED_H

// thrid_party
#include "async/async.h"
#include "inja.hpp"
#include "rx.h"
#include "sled/async/async.h"
#include "sled/nonstd/string_view.h"
#include "toml.hpp"

// experimental
@@ -22,7 +23,7 @@
// #include "sled/futures/promise.h"

// lang
#include "lang/attributes.h"
#include "sled/lang/attributes.h"

// log
#include "sled/log/log.h"