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.

216 lines
5.0 KiB
C++

/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>
#include <neo.h>
extern "C" struct nstr2u_positive_case {
const char *s;
u64 u;
int radix;
};
TEST_CASE( "nstr2u: Parse numbers", "[string/nstr2x.c]" )
{
struct nstr2u_positive_case positives[] = {
{"0", 0, 2 },
{"0", 0, 36 },
{"3w5e11264sgsf", 0xffffffffffffffff, 36 },
{"3W5E11264SGSF", 0xffffffffffffffff, 36 },
{"0xffffffffffffffff", 0xffffffffffffffff, 0 },
{"69420", 69420, 0 },
{"+69420", 69420, 0 },
{"0xff", 0xff, 0 },
{"0XFF", 0xff, 0 },
{"+0xff", 0xff, 0 },
{"+0XFF", 0xff, 0 },
{"0b11111111", 0xff, 0 },
{"0B11111111", 0xff, 0 },
{"0o77777777", 077777777, 0 },
{"0O77777777", 077777777, 0 },
{"069420", 69420, 0 }
};
struct nstr2u_positive_case *current_case = &positives[0];
do {
error err;
nstr_t *s = nstr(current_case->s, nil);
u64 u = nstr2u(s, current_case->radix, &err);
REQUIRE( u == current_case->u );
REQUIRE( errnum(&err) == 0 );
} while (++current_case < &positives[0] + sizeof(positives) / sizeof(positives[0]));
}
TEST_CASE( "nstr2u: Error if number too high", "[string/nstr2x.c]" )
{
error err;
nstr_t *s = nstr("10000000000000000", nil);
u64 u = nstr2u(s, 16, &err);
nstr_t *expected_msg = nstr("Number out of range", nil);
nstr_t *actual_msg = errmsg(&err);
REQUIRE( u == 0xffffffffffffffff );
REQUIRE( errnum(&err) == ERANGE );
REQUIRE( nstreq(expected_msg, actual_msg, nil) );
nput(expected_msg);
nput(actual_msg);
nput(s);
}
TEST_CASE( "nstr2u: Error if base too low", "[string/nstr2x.c]" )
{
error err;
nstr_t *s = nstr("420", nil);
u64 u = nstr2u(s, -1, &err);
nstr_t *expected_msg = nstr("Numerical base out of range", nil);
nstr_t *actual_msg = errmsg(&err);
REQUIRE( u == 0 );
REQUIRE( nstreq(expected_msg, actual_msg, nil) );
REQUIRE( errnum(&err) == EINVAL );
nput(expected_msg);
nput(actual_msg);
nput(s);
errput(&err);
}
TEST_CASE( "nstr2u: Error if base is 1", "[string/nstr2x.c]" )
{
error err;
nstr_t *s = nstr("420", nil);
u64 u = nstr2u(s, 1, &err);
nstr_t *expected_msg = nstr("Numerical base out of range", nil);
nstr_t *actual_msg = errmsg(&err);
REQUIRE( u == 0 );
REQUIRE( nstreq(expected_msg, actual_msg, nil) );
REQUIRE( errnum(&err) == EINVAL );
nput(expected_msg);
nput(actual_msg);
nput(s);
errput(&err);
}
TEST_CASE( "nstr2u: Error if base too high", "[string/nstr2x.c]" )
{
error err;
nstr_t *s = nstr("420", nil);
u64 u = nstr2u(s, 37, &err);
nstr_t *expected_msg = nstr("Numerical base out of range", nil);
nstr_t *actual_msg = errmsg(&err);
REQUIRE( u == 0 );
REQUIRE( nstreq(expected_msg, actual_msg, nil) );
REQUIRE( errnum(&err) == EINVAL );
nput(expected_msg);
nput(actual_msg);
nput(s);
errput(&err);
}
TEST_CASE( "nstr2u: Error if character not part of numerical system", "[string/nstr2x.c]" )
{
error err;
nstr_t *s = nstr("012", nil);
u64 u = nstr2u(s, 2, &err);
nstr_t *expected_msg = nstr("'2' is not a base 2 digit", nil);
nstr_t *actual_msg = errmsg(&err);
REQUIRE( u == 0 );
REQUIRE( nstreq(expected_msg, actual_msg, nil) );
REQUIRE( errnum(&err) == EINVAL );
nput(expected_msg);
nput(actual_msg);
nput(s);
errput(&err);
}
TEST_CASE( "nstr2u: Error if non-digit character", "[string/nstr2x.c]" )
{
error err;
nstr_t *s = nstr("0,12", nil);
u64 u = nstr2u(s, 2, &err);
nstr_t *expected_msg = nstr("',' is not a base 2 digit", nil);
nstr_t *actual_msg = errmsg(&err);
REQUIRE( u == 0 );
REQUIRE( nstreq(expected_msg, actual_msg, nil) );
REQUIRE( errnum(&err) == EINVAL );
nput(expected_msg);
nput(actual_msg);
nput(s);
errput(&err);
}
TEST_CASE( "nstr2u: Error if string is nil", "[string/nstr2x.c]" )
{
error err;
u64 u = nstr2u(nil, 2, &err);
nstr_t *expected_msg = nstr("String is nil", nil);
nstr_t *actual_msg = errmsg(&err);
REQUIRE( u == 0 );
REQUIRE( nstreq(expected_msg, actual_msg, nil) );
REQUIRE( errnum(&err) == EFAULT );
nput(expected_msg);
errput(&err);
}
TEST_CASE( "nstr2u_put: Decrement refcount", "[string/nstr2x.c]" )
{
error err;
nstr_t *s = nstr("420", nil);
nget(s);
u64 u = nstr2u_put(s, 10, &err);
REQUIRE( u == 420 );
REQUIRE( nref_count(s) == 1 );
nput(s);
}
TEST_CASE( "nstr2u_put: Error if string is nil", "[string/nstr2x.c]" )
{
error err;
u64 u = nstr2u_put(nil, 2, &err);
nstr_t *expected_msg = nstr("String is nil", nil);
nstr_t *actual_msg = errmsg(&err);
REQUIRE( u == 0 );
REQUIRE( nstreq(expected_msg, actual_msg, nil) );
REQUIRE( errnum(&err) == EFAULT );
nput(expected_msg);
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.
*/