nbuf: make clones zero-copy
This commit is contained in:
parent
cf4bd2ea5b
commit
ac7172c841
13 changed files with 429 additions and 114 deletions
|
@ -29,6 +29,30 @@ nbuf_t *nbuf_create(usize size, error *err);
|
|||
*/
|
||||
nbuf_t *nbuf_from(const void *restrict data, usize size, error *err);
|
||||
|
||||
/**
|
||||
* Create a new buffer of fixed size and copy a string into it.
|
||||
*
|
||||
* The original string is neither modified not deallocated.
|
||||
* If allocation fails or `s` is `nil`, an error is yeeted.
|
||||
*
|
||||
* @param s: String to fill the buffer with
|
||||
* @param err: Error pointer
|
||||
* @returns The buffer, except if an error occurred
|
||||
*/
|
||||
nbuf_t *nbuf_from_str(const char *restrict s, error *err);
|
||||
|
||||
/**
|
||||
* Create a new buffer of fixed size and copy a neo string into it.
|
||||
*
|
||||
* The original string is unmodified.
|
||||
* If allocation fails or `s` is `nil`, an error is yeeted.
|
||||
*
|
||||
* @param s: Neo string to fill the buffer with
|
||||
* @param err: Error pointer
|
||||
* @returns The buffer, except if an error occurred
|
||||
*/
|
||||
nbuf_t *nbuf_from_nstr(nstr_t *s, error *err);
|
||||
|
||||
/**
|
||||
* Return a new copy of `buf`.
|
||||
*
|
||||
|
@ -38,7 +62,7 @@ nbuf_t *nbuf_from(const void *restrict data, usize size, error *err);
|
|||
* @param err: Error pointer
|
||||
* @returns A copy of `buf`, unless an error occurred
|
||||
*/
|
||||
nbuf_t *nbuf_clone(const nbuf_t *buf, error *err);
|
||||
nbuf_t *nbuf_clone(nbuf_t *buf, error *err);
|
||||
|
||||
/**
|
||||
* Get the byte at the specified index.
|
||||
|
|
|
@ -188,7 +188,7 @@ int nstrcmp(const nstr_t *s1, const nstr_t *s2, error *err);
|
|||
* @param err: Error pointer
|
||||
* @returns The new padded string
|
||||
*/
|
||||
nstr_t *leftpad(const nstr_t *s, usize length, nchar fill, error *err);
|
||||
nstr_t *leftpad(nstr_t *s, usize length, nchar fill, error *err);
|
||||
|
||||
/**
|
||||
* Iterate over each character in a string.
|
||||
|
|
|
@ -67,7 +67,12 @@ typedef struct _neo_nref nref_t;
|
|||
struct _neo_nbuf {
|
||||
NREF_FIELD;
|
||||
NLEN_FIELD(_size);
|
||||
u8 _data[0];
|
||||
/**
|
||||
* If this buffer was cloned or converted from a string, this points to
|
||||
* the original structure's refcounter. Otherwise, it is nil.
|
||||
*/
|
||||
nref_t *_borrow;
|
||||
const byte *_data;
|
||||
};
|
||||
/**
|
||||
* A statically sized, refcounted buffer.
|
||||
|
|
66
src/nbuf.c
66
src/nbuf.c
|
@ -12,6 +12,9 @@
|
|||
|
||||
static void nbuf_destroy(struct _neo_nbuf *buf)
|
||||
{
|
||||
if (buf->_borrow != nil)
|
||||
_neo_nput(buf->_borrow);
|
||||
|
||||
nfree(buf);
|
||||
}
|
||||
|
||||
|
@ -31,11 +34,13 @@ nbuf_t *nbuf_create(usize size, error *err)
|
|||
return nil;
|
||||
}
|
||||
|
||||
buf->_size = size;
|
||||
|
||||
byte *data = (byte *)buf + sizeof(*buf);
|
||||
for (unsigned int i = 0; i < 4; i++)
|
||||
buf->_data[size + i] = 0;
|
||||
data[size + i] = 0;
|
||||
|
||||
buf->_size = size;
|
||||
buf->_data = data;
|
||||
buf->_borrow = nil;
|
||||
nref_init(buf, nbuf_destroy);
|
||||
neat(err);
|
||||
return buf;
|
||||
|
@ -53,18 +58,65 @@ nbuf_t *nbuf_from(const void *data, usize len, error *err)
|
|||
return nil;
|
||||
}
|
||||
|
||||
memcpy(&buf->_data[0], data, len);
|
||||
byte *buf_data = (byte *)buf + sizeof(*buf);
|
||||
memcpy(buf_data, data, len);
|
||||
/* _data field already filled by nbuf_create */
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
nbuf_t *nbuf_clone(const nbuf_t *buf, error *err)
|
||||
nbuf_t *nbuf_from_str(const char *s, error *err)
|
||||
{
|
||||
if (s == nil) {
|
||||
yeet(err, EFAULT, "String is nil");
|
||||
return nil;
|
||||
}
|
||||
|
||||
usize size = strlen(s) + 1;
|
||||
return nbuf_from(s, size, err);
|
||||
}
|
||||
|
||||
nbuf_t *nbuf_from_nstr(nstr_t *s, error *err)
|
||||
{
|
||||
if (s == nil) {
|
||||
yeet(err, EFAULT, "String is nil");
|
||||
return nil;
|
||||
}
|
||||
|
||||
nbuf_t *buf = nalloc(sizeof(*buf), err);
|
||||
catch(err) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
nget(s);
|
||||
buf->_size = s->_size;
|
||||
buf->_borrow = &s->__neo_nref;
|
||||
buf->_data = (const byte *)s->_data;
|
||||
nref_init(buf, nbuf_destroy);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
nbuf_t *nbuf_clone(nbuf_t *buf, error *err)
|
||||
{
|
||||
if (buf == nil) {
|
||||
yeet(err, EFAULT, "Source buffer is nil");
|
||||
return nil;
|
||||
}
|
||||
|
||||
return nbuf_from(&buf->_data[0], nlen(buf), err);
|
||||
nbuf_t *clone = nalloc(sizeof(*clone), err);
|
||||
catch(err) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
nget(buf);
|
||||
|
||||
clone->_size = buf->_size;
|
||||
clone->_borrow = &buf->__neo_nref;
|
||||
clone->_data = buf->_data;
|
||||
nref_init(clone, nbuf_destroy);
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
int nbuf_cmp(const nbuf_t *buf1, const nbuf_t *buf2, error *err)
|
||||
|
@ -83,7 +135,7 @@ int nbuf_cmp(const nbuf_t *buf1, const nbuf_t *buf2, error *err)
|
|||
|
||||
neat(err);
|
||||
|
||||
if (buf1 == buf2)
|
||||
if (buf1->_data == buf2->_data)
|
||||
return 0;
|
||||
|
||||
/*
|
||||
|
|
|
@ -47,7 +47,7 @@ static inline nstr_t *leftpad_unsafe(const nstr_t *s, usize len, nchar fillchr,
|
|||
return padded;
|
||||
}
|
||||
|
||||
nstr_t *leftpad(const nstr_t *s, usize len, nchar fillchr, error *err)
|
||||
nstr_t *leftpad(nstr_t *s, usize len, nchar fillchr, error *err)
|
||||
{
|
||||
if (s == nil) {
|
||||
yeet(err, EFAULT, "String is nil");
|
||||
|
|
|
@ -16,11 +16,11 @@ list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/contrib)
|
|||
include(Catch)
|
||||
|
||||
add_executable(neo_test neo_test.cpp)
|
||||
include(nbuf/nbuf.cmake)
|
||||
include(string/string.cmake)
|
||||
|
||||
target_sources(neo_test PRIVATE
|
||||
list.cpp
|
||||
nbuf.cpp
|
||||
nref.cpp
|
||||
)
|
||||
|
||||
|
|
21
test/nbuf/nbuf.cmake
Normal file
21
test/nbuf/nbuf.cmake
Normal file
|
@ -0,0 +1,21 @@
|
|||
# See the end of this file for copyright and license terms.
|
||||
|
||||
target_sources(neo_test PRIVATE
|
||||
nbuf/nbuf_clone.cpp
|
||||
nbuf/nbuf_cmp.cpp
|
||||
nbuf/nbuf_create.cpp
|
||||
nbuf/nbuf_from.cpp
|
||||
nbuf/nbuf_from_nstr.cpp
|
||||
nbuf/nbuf_from_str.cpp
|
||||
)
|
||||
|
||||
# This file is part of libneo.
|
||||
# Copyright (c) 2021 Fefie <owo@fef.moe>.
|
||||
#
|
||||
# libneo is non-violent software: you may only use, redistribute,
|
||||
# and/or modify it under the terms of the CNPLv6+ as found in
|
||||
# the LICENSE file in the source code root directory or at
|
||||
# <https://git.pixie.town/thufie/CNPL>.
|
||||
#
|
||||
# libneo comes with ABSOLUTELY NO WARRANTY, to the extent
|
||||
# permitted by applicable law. See the CNPLv6+ for details.
|
78
test/nbuf/nbuf_clone.cpp
Normal file
78
test/nbuf/nbuf_clone.cpp
Normal file
|
@ -0,0 +1,78 @@
|
|||
/** See the end of this file for copyright and license terms. */
|
||||
|
||||
#include <catch2/catch.hpp>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <neo.h>
|
||||
|
||||
SCENARIO( "nbuf_clone: Zero-copy clone", "[src/nbuf.c]" )
|
||||
{
|
||||
const char *data = "i'm gay,,,";
|
||||
usize size = strlen(data);
|
||||
|
||||
GIVEN( "a populated buffer" )
|
||||
{
|
||||
nbuf_t *original = nbuf_from(data, size, nil);
|
||||
|
||||
WHEN( "the buffer is cloned" )
|
||||
{
|
||||
error err;
|
||||
nbuf_t *clone = nbuf_clone(original, &err);
|
||||
REQUIRE( clone != nil );
|
||||
REQUIRE( errnum(&err) == 0 );
|
||||
|
||||
THEN( "the clone's data pointer is equal to the original" )
|
||||
{
|
||||
REQUIRE( clone->_data == original->_data );
|
||||
}
|
||||
|
||||
WHEN( "the original buffer is put" )
|
||||
{
|
||||
nput(original);
|
||||
THEN( "the original is not deallocated" )
|
||||
{
|
||||
REQUIRE( original != nil );
|
||||
}
|
||||
}
|
||||
|
||||
WHEN( "the cloned buffer is put" )
|
||||
{
|
||||
nput(clone);
|
||||
THEN( "the original is not deallocated" )
|
||||
{
|
||||
REQUIRE( clone == nil );
|
||||
REQUIRE( original->__neo_nref._count == 1 );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE( "nbuf_clone: Error if original buffer is nil", "[src/nbuf.c]" )
|
||||
{
|
||||
error err;
|
||||
nbuf_t *buf = nbuf_clone(nil, &err);
|
||||
|
||||
nstr_t *expected_msg = nstr("Source buffer is nil", nil);
|
||||
|
||||
REQUIRE( buf == nil );
|
||||
REQUIRE( errnum(&err) == EFAULT );
|
||||
REQUIRE( nstreq(expected_msg, errmsg(&err), nil) );
|
||||
|
||||
errput(&err);
|
||||
nput(expected_msg);
|
||||
}
|
||||
|
||||
/*
|
||||
* This file is part of libneo.
|
||||
* Copyright (c) 2021 Fefie <owo@fef.moe>.
|
||||
*
|
||||
* libneo is non-violent software: you may only use, redistribute,
|
||||
* and/or modify it under the terms of the CNPLv6+ as found in
|
||||
* the LICENSE file in the source code root directory or at
|
||||
* <https://git.pixie.town/thufie/CNPL>.
|
||||
*
|
||||
* libneo comes with ABSOLUTELY NO WARRANTY, to the extent
|
||||
* permitted by applicable law. See the CNPLv6+ for details.
|
||||
*/
|
|
@ -6,108 +6,6 @@
|
|||
|
||||
#include <neo.h>
|
||||
|
||||
TEST_CASE( "nbuf_create: Create regular buffer", "[src/nbuf.c]" )
|
||||
{
|
||||
error err;
|
||||
nbuf_t *buf = nbuf_create(32, &err);
|
||||
|
||||
REQUIRE( buf != nil );
|
||||
REQUIRE( nlen(buf) == 32 );
|
||||
REQUIRE( errnum(&err) == 0 );
|
||||
}
|
||||
|
||||
TEST_CASE( "nbuf_create: Error if size is 0", "[src/nbuf.c]" )
|
||||
{
|
||||
error err;
|
||||
nbuf_t *buf = nbuf_create(0, &err);
|
||||
nstr_t *expected_msg = nstr("Cannot create zero-size buffer", nil);
|
||||
|
||||
REQUIRE( buf == nil );
|
||||
REQUIRE( errnum(&err) == ERANGE );
|
||||
REQUIRE( nstreq(errmsg(&err), expected_msg, nil) );
|
||||
errput(&err);
|
||||
nput(expected_msg);
|
||||
}
|
||||
|
||||
TEST_CASE( "nbuf_from: Copy data", "[src/nbuf.c]" )
|
||||
{
|
||||
error err;
|
||||
const char *data = "i'm gay,,,";
|
||||
usize size = strlen(data);
|
||||
nbuf_t *buf = nbuf_from(data, size, &err);
|
||||
|
||||
REQUIRE( buf != nil );
|
||||
REQUIRE( nlen(buf) == size );
|
||||
REQUIRE( errnum(&err) == 0 );
|
||||
REQUIRE( memcmp(data, &buf->_data[0], size) == 0 );
|
||||
|
||||
nput(buf);
|
||||
}
|
||||
|
||||
TEST_CASE( "nbuf_from: Error if data is nil", "[src/nbuf.c]" )
|
||||
{
|
||||
error err;
|
||||
nbuf_t *buf = nbuf_from(nil, 1, &err);
|
||||
|
||||
nstr_t *expected_msg = nstr("Data is nil", nil);
|
||||
|
||||
REQUIRE( buf == nil );
|
||||
REQUIRE( errnum(&err) == EFAULT );
|
||||
REQUIRE( nstreq(expected_msg, errmsg(&err), nil) );
|
||||
|
||||
errput(&err);
|
||||
nput(expected_msg);
|
||||
}
|
||||
|
||||
TEST_CASE( "nbuf_from: Error if size is 0", "[src/nbuf.c]" )
|
||||
{
|
||||
error err;
|
||||
const char *data = "i'm gay,,,";
|
||||
nbuf_t *buf = nbuf_from(data, 0, &err);
|
||||
|
||||
nstr_t *expected_msg = nstr("Cannot create zero-size buffer", nil);
|
||||
|
||||
REQUIRE( buf == nil );
|
||||
REQUIRE( errnum(&err) == ERANGE );
|
||||
REQUIRE( nstreq(expected_msg, errmsg(&err), nil) );
|
||||
|
||||
errput(&err);
|
||||
nput(expected_msg);
|
||||
}
|
||||
|
||||
TEST_CASE( "nbuf_clone: Copy data", "[src/nbuf.c]" )
|
||||
{
|
||||
error err;
|
||||
const char *data = "i'm gay,,,";
|
||||
usize size = strlen(data);
|
||||
|
||||
nbuf_t *original = nbuf_from(data, size, nil);
|
||||
nbuf_t *clone = nbuf_clone(original, &err);
|
||||
|
||||
REQUIRE( clone != nil );
|
||||
REQUIRE( nlen(clone) == nlen(original) );
|
||||
REQUIRE( errnum(&err) == 0 );
|
||||
REQUIRE( memcmp(data, &clone->_data[0], size) == 0 );
|
||||
|
||||
nput(original);
|
||||
nput(clone);
|
||||
}
|
||||
|
||||
TEST_CASE( "nbuf_clone: Error if original buffer is nil", "[src/nbuf.c]" )
|
||||
{
|
||||
error err;
|
||||
nbuf_t *buf = nbuf_clone(nil, &err);
|
||||
|
||||
nstr_t *expected_msg = nstr("Source buffer is nil", nil);
|
||||
|
||||
REQUIRE( buf == nil );
|
||||
REQUIRE( errnum(&err) == EFAULT );
|
||||
REQUIRE( nstreq(expected_msg, errmsg(&err), nil) );
|
||||
|
||||
errput(&err);
|
||||
nput(expected_msg);
|
||||
}
|
||||
|
||||
TEST_CASE( "nbuf_cmp: Return 0 if buffers are equal", "[src/nbuf.c]" )
|
||||
{
|
||||
error err;
|
43
test/nbuf/nbuf_create.cpp
Normal file
43
test/nbuf/nbuf_create.cpp
Normal file
|
@ -0,0 +1,43 @@
|
|||
/** See the end of this file for copyright and license terms. */
|
||||
|
||||
#include <catch2/catch.hpp>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <neo.h>
|
||||
|
||||
TEST_CASE( "nbuf_create: Create regular buffer", "[src/nbuf.c]" )
|
||||
{
|
||||
error err;
|
||||
nbuf_t *buf = nbuf_create(32, &err);
|
||||
|
||||
REQUIRE( buf != nil );
|
||||
REQUIRE( nlen(buf) == 32 );
|
||||
REQUIRE( errnum(&err) == 0 );
|
||||
}
|
||||
|
||||
TEST_CASE( "nbuf_create: Error if size is 0", "[src/nbuf.c]" )
|
||||
{
|
||||
error err;
|
||||
nbuf_t *buf = nbuf_create(0, &err);
|
||||
nstr_t *expected_msg = nstr("Cannot create zero-size buffer", nil);
|
||||
|
||||
REQUIRE( buf == nil );
|
||||
REQUIRE( errnum(&err) == ERANGE );
|
||||
REQUIRE( nstreq(errmsg(&err), expected_msg, nil) );
|
||||
errput(&err);
|
||||
nput(expected_msg);
|
||||
}
|
||||
|
||||
/*
|
||||
* This file is part of libneo.
|
||||
* Copyright (c) 2021 Fefie <owo@fef.moe>.
|
||||
*
|
||||
* libneo is non-violent software: you may only use, redistribute,
|
||||
* and/or modify it under the terms of the CNPLv6+ as found in
|
||||
* the LICENSE file in the source code root directory or at
|
||||
* <https://git.pixie.town/thufie/CNPL>.
|
||||
*
|
||||
* libneo comes with ABSOLUTELY NO WARRANTY, to the extent
|
||||
* permitted by applicable law. See the CNPLv6+ for details.
|
||||
*/
|
66
test/nbuf/nbuf_from.cpp
Normal file
66
test/nbuf/nbuf_from.cpp
Normal file
|
@ -0,0 +1,66 @@
|
|||
/** See the end of this file for copyright and license terms. */
|
||||
|
||||
#include <catch2/catch.hpp>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <neo.h>
|
||||
|
||||
TEST_CASE( "nbuf_from: Copy data", "[src/nbuf.c]" )
|
||||
{
|
||||
error err;
|
||||
const char *data = "i'm gay,,,";
|
||||
usize size = strlen(data);
|
||||
nbuf_t *buf = nbuf_from(data, size, &err);
|
||||
|
||||
REQUIRE( buf != nil );
|
||||
REQUIRE( nlen(buf) == size );
|
||||
REQUIRE( errnum(&err) == 0 );
|
||||
REQUIRE( memcmp(data, &buf->_data[0], size) == 0 );
|
||||
|
||||
nput(buf);
|
||||
}
|
||||
|
||||
TEST_CASE( "nbuf_from: Error if data is nil", "[src/nbuf.c]" )
|
||||
{
|
||||
error err;
|
||||
nbuf_t *buf = nbuf_from(nil, 1, &err);
|
||||
|
||||
nstr_t *expected_msg = nstr("Data is nil", nil);
|
||||
|
||||
REQUIRE( buf == nil );
|
||||
REQUIRE( errnum(&err) == EFAULT );
|
||||
REQUIRE( nstreq(expected_msg, errmsg(&err), nil) );
|
||||
|
||||
errput(&err);
|
||||
nput(expected_msg);
|
||||
}
|
||||
|
||||
TEST_CASE( "nbuf_from: Error if size is 0", "[src/nbuf.c]" )
|
||||
{
|
||||
error err;
|
||||
const char *data = "i'm gay,,,";
|
||||
nbuf_t *buf = nbuf_from(data, 0, &err);
|
||||
|
||||
nstr_t *expected_msg = nstr("Cannot create zero-size buffer", nil);
|
||||
|
||||
REQUIRE( buf == nil );
|
||||
REQUIRE( errnum(&err) == ERANGE );
|
||||
REQUIRE( nstreq(expected_msg, errmsg(&err), nil) );
|
||||
|
||||
errput(&err);
|
||||
nput(expected_msg);
|
||||
}
|
||||
|
||||
/*
|
||||
* This file is part of libneo.
|
||||
* Copyright (c) 2021 Fefie <owo@fef.moe>.
|
||||
*
|
||||
* libneo is non-violent software: you may only use, redistribute,
|
||||
* and/or modify it under the terms of the CNPLv6+ as found in
|
||||
* the LICENSE file in the source code root directory or at
|
||||
* <https://git.pixie.town/thufie/CNPL>.
|
||||
*
|
||||
* libneo comes with ABSOLUTELY NO WARRANTY, to the extent
|
||||
* permitted by applicable law. See the CNPLv6+ for details.
|
||||
*/
|
78
test/nbuf/nbuf_from_nstr.cpp
Normal file
78
test/nbuf/nbuf_from_nstr.cpp
Normal file
|
@ -0,0 +1,78 @@
|
|||
/** See the end of this file for copyright and license terms. */
|
||||
|
||||
#include <catch2/catch.hpp>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <neo.h>
|
||||
|
||||
SCENARIO( "nbuf_from_nstr: Zero-copy from neo string", "[src/nbuf.c]" )
|
||||
{
|
||||
const char *data = "i'm gay,,,";
|
||||
usize size = strlen(data);
|
||||
|
||||
GIVEN( "a neo string" )
|
||||
{
|
||||
nbuf_t *original = nbuf_from(data, size, nil);
|
||||
|
||||
WHEN( "a buffer is created from it" )
|
||||
{
|
||||
error err;
|
||||
nbuf_t *clone = nbuf_clone(original, &err);
|
||||
REQUIRE( clone != nil );
|
||||
REQUIRE( errnum(&err) == 0 );
|
||||
|
||||
THEN( "the buffer's data pointer is equal to the string's" )
|
||||
{
|
||||
REQUIRE( clone->_data == original->_data );
|
||||
}
|
||||
|
||||
WHEN( "the string is put" )
|
||||
{
|
||||
nput(original);
|
||||
THEN( "the original is not deallocated" )
|
||||
{
|
||||
REQUIRE( original != nil );
|
||||
}
|
||||
}
|
||||
|
||||
WHEN( "the buffer is put" )
|
||||
{
|
||||
nput(clone);
|
||||
THEN( "the string is not deallocated" )
|
||||
{
|
||||
REQUIRE( clone == nil );
|
||||
REQUIRE( original->__neo_nref._count == 1 );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE( "nbuf_from_nstr: Error if string is nil", "[src/nbuf.c]" )
|
||||
{
|
||||
error err;
|
||||
nbuf_t *buf = nbuf_from_nstr(nil, &err);
|
||||
|
||||
nstr_t *expected_msg = nstr("String is nil", nil);
|
||||
|
||||
REQUIRE( buf == nil );
|
||||
REQUIRE( errnum(&err) == EFAULT );
|
||||
REQUIRE( nstreq(expected_msg, errmsg(&err), nil) );
|
||||
|
||||
errput(&err);
|
||||
nput(expected_msg);
|
||||
}
|
||||
|
||||
/*
|
||||
* This file is part of libneo.
|
||||
* Copyright (c) 2021 Fefie <owo@fef.moe>.
|
||||
*
|
||||
* libneo is non-violent software: you may only use, redistribute,
|
||||
* and/or modify it under the terms of the CNPLv6+ as found in
|
||||
* the LICENSE file in the source code root directory or at
|
||||
* <https://git.pixie.town/thufie/CNPL>.
|
||||
*
|
||||
* libneo comes with ABSOLUTELY NO WARRANTY, to the extent
|
||||
* permitted by applicable law. See the CNPLv6+ for details.
|
||||
*/
|
50
test/nbuf/nbuf_from_str.cpp
Normal file
50
test/nbuf/nbuf_from_str.cpp
Normal file
|
@ -0,0 +1,50 @@
|
|||
/** See the end of this file for copyright and license terms. */
|
||||
|
||||
#include <catch2/catch.hpp>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <neo.h>
|
||||
|
||||
TEST_CASE( "nbuf_from_str: Copy data", "[src/nbuf.c]" )
|
||||
{
|
||||
error err;
|
||||
const char *s = "i'm gay,,,";
|
||||
usize size = strlen(s) + 1;
|
||||
nbuf_t *buf = nbuf_from_str(s, &err);
|
||||
|
||||
REQUIRE( buf != nil );
|
||||
REQUIRE( nlen(buf) == size );
|
||||
REQUIRE( errnum(&err) == 0 );
|
||||
REQUIRE( memcmp(s, &buf->_data[0], size) == 0 );
|
||||
|
||||
nput(buf);
|
||||
}
|
||||
|
||||
TEST_CASE( "nbuf_from_str: Error if string is nil", "[src/nbuf.c]" )
|
||||
{
|
||||
error err;
|
||||
nbuf_t *buf = nbuf_from_str(nil, &err);
|
||||
|
||||
nstr_t *expected_msg = nstr("String is nil", nil);
|
||||
|
||||
REQUIRE( buf == nil );
|
||||
REQUIRE( errnum(&err) == EFAULT );
|
||||
REQUIRE( nstreq(expected_msg, errmsg(&err), nil) );
|
||||
|
||||
errput(&err);
|
||||
nput(expected_msg);
|
||||
}
|
||||
|
||||
/*
|
||||
* This file is part of libneo.
|
||||
* Copyright (c) 2021 Fefie <owo@fef.moe>.
|
||||
*
|
||||
* libneo is non-violent software: you may only use, redistribute,
|
||||
* and/or modify it under the terms of the CNPLv6+ as found in
|
||||
* the LICENSE file in the source code root directory or at
|
||||
* <https://git.pixie.town/thufie/CNPL>.
|
||||
*
|
||||
* libneo comes with ABSOLUTELY NO WARRANTY, to the extent
|
||||
* permitted by applicable law. See the CNPLv6+ for details.
|
||||
*/
|
Loading…
Reference in a new issue