Compare commits
3 Commits
3040b09669
...
ac7172c841
Author | SHA1 | Date |
---|---|---|
anna | ac7172c841 | 3 years ago |
anna | cf4bd2ea5b | 3 years ago |
anna | d504786bc7 | 3 years ago |
@ -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.
|
@ -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.
|
||||
*/
|
@ -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.
|
||||
*/
|
@ -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.
|
||||
*/
|
@ -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.
|
||||
*/
|
@ -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 New Issue