You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
3.4 KiB
C++

/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>
#include <neo.h>
TEST_CASE( "nstrtrim: Trim leading whitespace", "[string/nstrtrim.c]" )
{
error err;
nstr_t *s = nstr(" \v\v aaaaa", nil);
nstr_t *expected = nstr("aaaaa", nil);
nstr_t *actual = nstrtrim(s, &err);
REQUIRE( actual != nil );
REQUIRE( nlen(actual) == 5 );
REQUIRE( nstreq(expected, actual, nil) );
REQUIRE( errnum(&err) == 0 );
nput(s);
nput(expected);
nput(actual);
}
TEST_CASE( "nstrtrim: Trim trailing whitespace", "[string/nstrtrim.c]" )
{
error err;
nstr_t *s = nstr("aaaaa \t \r\n \f \n", nil);
nstr_t *expected = nstr("aaaaa", nil);
nstr_t *actual = nstrtrim(s, &err);
REQUIRE( actual != nil );
REQUIRE( nlen(actual) == 5 );
REQUIRE( nstreq(expected, actual, nil) );
REQUIRE( errnum(&err) == 0 );
nput(s);
nput(expected);
nput(actual);
}
TEST_CASE( "nstrtrim: Trim both leading and trailing whitespace", "[string/nstrtrim.c]" )
{
error err;
nstr_t *s = nstr(" \v\v aaaaa \t \r\n \f \n", nil);
nstr_t *expected = nstr("aaaaa", nil);
nstr_t *actual = nstrtrim(s, &err);
REQUIRE( actual != nil );
REQUIRE( nlen(actual) == 5 );
REQUIRE( nstreq(expected, actual, nil) );
REQUIRE( errnum(&err) == 0 );
nput(s);
nput(expected);
nput(actual);
}
TEST_CASE( "nstrtrim: Don't modify empty string", "[string/nstrtrim.c]" )
{
error err;
nstr_t *s = nstr("", nil);
nstr_t *expected = nstr("", nil);
nstr_t *actual = nstrtrim(s, &err);
REQUIRE( actual != nil );
REQUIRE( nlen(actual) == 0 );
REQUIRE( nstreq(expected, actual, nil) );
REQUIRE( errnum(&err) == 0 );
nput(s);
nput(expected);
nput(actual);
}
TEST_CASE( "nstrtrim: Empty string if input is whitespace only", "[string/nstrtrim.c]" )
{
error err;
nstr_t *s = nstr(" ", nil);
nstr_t *expected = nstr("", nil);
nstr_t *actual = nstrtrim(s, &err);
REQUIRE( actual != nil );
REQUIRE( nlen(actual) == 0 );
REQUIRE( nstreq(expected, actual, nil) );
REQUIRE( errnum(&err) == 0 );
nput(s);
nput(expected);
nput(actual);
}
TEST_CASE( "nstrtrim: Error if string is nil", "[string/nstrtim.c]" )
{
error err;
nstr_t *tr = nstrtrim(nil, &err);
nstr_t *expected_msg = nstr("String is nil", nil);
REQUIRE( tr == nil );
REQUIRE( errnum(&err) == EFAULT );
REQUIRE( nstreq(expected_msg, errmsg(&err), nil) );
errput(&err);
}
TEST_CASE( "nstrtrim_put: Decrement refcount", "[string/nstrtrim.c]" )
{
error err;
nstr_t *s = nstr(" aaaaa ", nil);
nstr_t *expected = nstr("aaaaa", nil);
nget(s);
nstr_t *actual = nstrtrim_put(s, &err);
REQUIRE( nref_count(s) == 1 );
REQUIRE( actual != nil );
REQUIRE( nstreq(expected, actual, nil) );
REQUIRE( errnum(&err) == 0 );
nput(s);
nput(expected);
nput(actual);
}
TEST_CASE( "nstrtrim_put: Error if string is nil", "[string/nstrtim.c]" )
{
error err;
nstr_t *tr = nstrtrim_put(nil, &err);
nstr_t *expected_msg = nstr("String is nil", nil);
REQUIRE( tr == nil );
REQUIRE( errnum(&err) == EFAULT );
REQUIRE( nstreq(expected_msg, errmsg(&err), nil) );
errput(&err);
}
/*
* 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.
*/