diff --git a/include/neo/_string.h b/include/neo/_string.h index 1d3f3d6..33b4104 100644 --- a/include/neo/_string.h +++ b/include/neo/_string.h @@ -72,7 +72,8 @@ string *u2nstr(u64 u, int radix, error *err); /** * Duplicate a string. * - * If `s` is `nil` or allocation fails, an error is yeeted. + * This should never be neccessary because strings in libneo are refcounted + * and immutable. If `s` is `nil` or allocation fails, an error is yeeted. * * @param s: String to duplicate * @param err: Error pointer diff --git a/src/string/leftpad.c b/src/string/leftpad.c index 75bc32c..5f5047a 100644 --- a/src/string/leftpad.c +++ b/src/string/leftpad.c @@ -50,7 +50,7 @@ static inline string *leftpad_unsafe(const string *s, usize len, nchar fillchr, string *leftpad(const string *s, usize len, nchar fillchr, error *err) { if (s == nil) { - yeet(err, EFAULT, "String must not be nil"); + yeet(err, EFAULT, "String is nil"); return 0; } diff --git a/src/string/nstrdup.c b/src/string/nstrdup.c index d634157..fd1bfd9 100644 --- a/src/string/nstrdup.c +++ b/src/string/nstrdup.c @@ -1,10 +1,18 @@ /** See the end of this file for copyright and license terms. */ +#include + +#include "neo/_error.h" #include "neo/_string.h" #include "neo/_types.h" string *nstrdup(const string *s, error *err) { + if (s == nil) { + yeet(err, EFAULT, "String is nil"); + return nil; + } + return nstr(s->_data, err); } diff --git a/test/string/i2nstr.cpp b/test/string/i2nstr.cpp new file mode 100644 index 0000000..ada4343 --- /dev/null +++ b/test/string/i2nstr.cpp @@ -0,0 +1,171 @@ +/** See the end of this file for copyright and license terms. */ + +#include +#include + +#include + +TEST_CASE( "i2nstr: Convert 0 to base 2", "[string/x2nstr.c]" ) +{ + error err; + + string *expected = nstr("0", nil); + string *actual = i2nstr(0, 2, &err); + + REQUIRE( actual != nil ); + REQUIRE( nstreq(expected, actual, nil) ); + REQUIRE( errnum(&err) == 0 ); + + nput(expected); + nput(actual); +} + +TEST_CASE( "i2nstr: Convert 0 to base 36", "[string/x2nstr.c]" ) +{ + error err; + + string *expected = nstr("0", nil); + string *actual = i2nstr(0, 36, &err); + + REQUIRE( actual != nil ); + REQUIRE( nstreq(expected, actual, nil) ); + REQUIRE( errnum(&err) == 0 ); + + nput(expected); + nput(actual); +} + +TEST_CASE( "i2nstr: Convert 255 to base 2", "[string/x2nstr.c]" ) +{ + error err; + + string *expected = nstr("11111111", nil); + string *actual = i2nstr(255, 2, &err); + + REQUIRE( actual != nil ); + REQUIRE( nstreq(expected, actual, nil) ); + REQUIRE( errnum(&err) == 0 ); + + nput(expected); + nput(actual); +} + +TEST_CASE( "i2nstr: Convert -255 to base 2", "[string/x2nstr.c]" ) +{ + error err; + + string *expected = nstr("-11111111", nil); + string *actual = i2nstr(-255, 2, &err); + + REQUIRE( actual != nil ); + REQUIRE( nstreq(expected, actual, nil) ); + REQUIRE( errnum(&err) == 0 ); + + nput(expected); + nput(actual); +} + +TEST_CASE( "i2nstr: Convert 1679615 to base 36", "[string/x2nstr.c]" ) +{ + error err; + + string *expected = nstr("zzzz", nil); + string *actual = i2nstr(1679615, 36, &err); + + REQUIRE( actual != nil ); + REQUIRE( nstreq(expected, actual, nil) ); + REQUIRE( errnum(&err) == 0 ); + + nput(expected); + nput(actual); +} + +TEST_CASE( "i2nstr: Convert -1679615 to base 36", "[string/x2nstr.c]" ) +{ + error err; + + string *expected = nstr("-zzzz", nil); + string *actual = i2nstr(-1679615, 36, &err); + + REQUIRE( actual != nil ); + REQUIRE( nstreq(expected, actual, nil) ); + REQUIRE( errnum(&err) == 0 ); + + nput(expected); + nput(actual); +} + +TEST_CASE( "i2nstr: Convert 2^63-1 to base 16", "[string/x2nstr.c]" ) +{ + error err; + + string *expected = nstr("7fffffffffffffff", nil); + string *actual = i2nstr(0x7fffffffffffffff, 16, &err); + + REQUIRE( actual != nil ); + REQUIRE( nstreq(expected, actual, nil) ); + REQUIRE( errnum(&err) == 0 ); + + nput(expected); + nput(actual); +} + +TEST_CASE( "i2nstr: Convert -2^63 to base 16", "[string/x2nstr.c]" ) +{ + error err; + + string *expected = nstr("-8000000000000000", nil); + string *actual = i2nstr(-0x8000000000000000, 16, &err); + + REQUIRE( actual != nil ); + REQUIRE( nstreq(expected, actual, nil) ); + REQUIRE( errnum(&err) == 0 ); + + nput(expected); + nput(actual); +} + +TEST_CASE( "i2nstr: Error if base too low", "[string/x2nstr.c]" ) +{ + error err; + string *s = i2nstr(420, 1, &err); + + string *expected_msg = nstr("Numerical base out of range", nil); + string *actual_msg = errmsg(&err); + + REQUIRE( s == nil ); + REQUIRE( nstreq(expected_msg, actual_msg, nil) ); + REQUIRE( errnum(&err) == EINVAL ); + + nput(expected_msg); + errput(&err); +} + +TEST_CASE( "i2nstr: Error if base too high", "[string/x2nstr.c]" ) +{ + error err; + string *s = i2nstr(420, 37, &err); + + string *expected_msg = nstr("Numerical base out of range", nil); + string *actual_msg = errmsg(&err); + + REQUIRE( s == nil ); + REQUIRE( nstreq(expected_msg, actual_msg, nil) ); + REQUIRE( errnum(&err) == EINVAL ); + + nput(expected_msg); + errput(&err); +} + +/* + * This file is part of libneo. + * Copyright (c) 2021 Fefie . + * + * 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 + * . + * + * libneo comes with ABSOLUTELY NO WARRANTY, to the extent + * permitted by applicable law. See the CNPLv6+ for details. + */ diff --git a/test/string/leftpad.cpp b/test/string/leftpad.cpp new file mode 100644 index 0000000..027cabb --- /dev/null +++ b/test/string/leftpad.cpp @@ -0,0 +1,103 @@ +/** See the end of this file for copyright and license terms. */ + +#include +#include + +#include + +TEST_CASE( "leftpad: Pad a string with ASCII character", "[string/leftpad.c]" ) +{ + error err; + string *s = nstr("i'm gay,,,", nil); + + string *expected = nstr(",,,i'm gay,,,", nil); + string *actual = leftpad(s, 13, ',', &err); + + REQUIRE( actual != nil ); + REQUIRE( nlen(actual) == 13 ); + REQUIRE( nstreq(expected, actual, nil) ); + REQUIRE( errnum(&err) == 0 ); + + nput(s); + nput(expected); + nput(actual); +} + +TEST_CASE( "leftpad: Pad a string with UTF-8 character", "[string/leftpad.c]" ) +{ + error err; + string *s = nstr("i'm gay,,,", nil); + + string *expected = nstr("\xf0\x9f\xa5\xba\xf0\x9f\xa5\xbai'm gay,,,", nil); + string *actual = leftpad(s, 12, 0x01f97a, &err); + + REQUIRE( actual != nil ); + REQUIRE( nlen(actual) == 12 ); + REQUIRE( nstreq(expected, actual, nil) ); + REQUIRE( errnum(&err) == 0 ); + + nput(s); + nput(expected); + nput(actual); +} + +TEST_CASE( "leftpad: Duplicate if string is expected length", "[string/leftpad.c]" ) +{ + error err; + string *s = nstr("i'm gay,,,", nil); + + string *padded = leftpad(s, 10, ',', &err); + + REQUIRE( padded != nil ); + REQUIRE( nlen(padded) == 10 ); + REQUIRE( nstreq(s, padded, nil) ); + REQUIRE( errnum(&err) == 0 ); + + nput(s); + nput(padded); +} + +TEST_CASE( "leftpad: Error if string is nil", "[string/leftpad.c]" ) +{ + error err; + string *s = leftpad(nil, 10, ',', &err); + + string *expected_msg = nstr("String is nil", nil); + + REQUIRE( s == nil ); + REQUIRE( errnum(&err) == EFAULT ); + REQUIRE( nstreq(expected_msg, errmsg(&err), nil) ); + + nput(expected_msg); + errput(&err); +} + +TEST_CASE( "leftpad: Error if string is too long", "[string/leftpad.c]" ) +{ + error err; + string *s = nstr("i'm gay,,,", nil); + string *padded = leftpad(s, 9, ',', &err); + + string *expected_msg = nstr("String is longer than requested length", nil); + + REQUIRE( padded == nil ); + REQUIRE( errnum(&err) == ERANGE ); + REQUIRE( nstreq(expected_msg, errmsg(&err), nil) ); + + nput(s); + nput(expected_msg); + errput(&err); +} + +/* + * This file is part of libneo. + * Copyright (c) 2021 Fefie . + * + * 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 + * . + * + * libneo comes with ABSOLUTELY NO WARRANTY, to the extent + * permitted by applicable law. See the CNPLv6+ for details. + */ diff --git a/test/string/nstr.cpp b/test/string/nstr.cpp new file mode 100644 index 0000000..07da6ad --- /dev/null +++ b/test/string/nstr.cpp @@ -0,0 +1,74 @@ +/** See the end of this file for copyright and license terms. */ + +#include +#include + +#include + +TEST_CASE( "nstr: Create a string", "[string/nstr.c]" ) +{ + error err; + string *s = nstr("i'm gay,,,", &err); + + REQUIRE( s != nil ); + REQUIRE( nlen(s) == 10 ); + REQUIRE( s->_size == 14 ); + REQUIRE( s->_data[10] == '\0' ); + REQUIRE( errnum(&err) == 0 ); + nput(s); +} + +TEST_CASE( "nstr: Error if raw strig is nil", "[string/nstr,c]") +{ + error err; + string *s = nstr(nil, &err); + + string *expected = nstr("String is nil", nil); + string *actual = errmsg(&err); + + REQUIRE( s == nil ); + REQUIRE( errnum(&err) == EFAULT ); + REQUIRE( nstreq(expected, actual, nil) ); + errput(&err); +} + +TEST_CASE( "nnstr: Create a string", "[string/nstr.c]" ) +{ + error err; + /* 10 character string, must be truncated */ + string *s = nnstr("i'm gay,,,", 8, &err); + + REQUIRE( s != nil ); + REQUIRE( nlen(s) == 8 ); + REQUIRE( s->_size == 12 ); + REQUIRE( s->_data[8] == '\0' ); + REQUIRE( errnum(&err) == 0 ); + nput(s); +} + +TEST_CASE( "nnstr: Error if raw strig is nil", "[string/nstr,c]") +{ + error err; + string *s = nnstr(nil, 10, &err); + + string *expected = nstr("String is nil", nil); + string *actual = errmsg(&err); + + REQUIRE( s == nil ); + REQUIRE( errnum(&err) == EFAULT ); + REQUIRE( nstreq(expected, actual, nil) ); + errput(&err); +} + +/* + * This file is part of libneo. + * Copyright (c) 2021 Fefie . + * + * 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 + * . + * + * libneo comes with ABSOLUTELY NO WARRANTY, to the extent + * permitted by applicable law. See the CNPLv6+ for details. + */ diff --git a/test/string/nstrcat.cpp b/test/string/nstrcat.cpp new file mode 100644 index 0000000..79b8791 --- /dev/null +++ b/test/string/nstrcat.cpp @@ -0,0 +1,112 @@ +/** See the end of this file for copyright and license terms. */ + +#include +#include + +#include + +TEST_CASE( "nstrcat: Concatenate two strings", "[string/nstrcat.c]" ) +{ + error err; + string *s1 = nstr("i'm ", nil); + string *s2 = nstr("gay,,,", nil); + string *expected = nstr("i'm gay,,,", nil); + string *actual = nstrcat(s1, s2, &err); + + REQUIRE( actual != nil ); + REQUIRE( nlen(actual) == 10 ); + REQUIRE( nstreq(expected, actual, nil) ); + REQUIRE( errnum(&err) == 0 ); + + nput(s1); + nput(s2); + nput(expected); + nput(actual); +} + +TEST_CASE( "nstrcat: Concatenate two UTF-8 strings", "[string/nstrcat.c]" ) +{ + error err; + string *s1 = nstr("i'm ", nil); + string *s2 = nstr("gay\xf0\x9f\xa5\xba,,,", nil); + string *expected = nstr("i'm gay\xf0\x9f\xa5\xba,,,", nil); + string *actual = nstrcat(s1, s2, &err); + + REQUIRE( actual != nil ); + REQUIRE( nlen(actual) == 11 ); + REQUIRE( nstreq(expected, actual, nil) ); + REQUIRE( errnum(&err) == 0 ); + + nput(s1); + nput(s2); + nput(expected); + nput(actual); +} + +TEST_CASE( "nstrcat: Concatenate two empty strings", "[string/nstrcat.c]" ) +{ + error err; + string *s1 = nstr("", nil); + string *s2 = nstr("", nil); + string *expected = nstr("", nil); + string *actual = nstrcat(s1, s2, &err); + + REQUIRE( actual != nil ); + REQUIRE( nlen(actual) == 0 ); + REQUIRE( nstreq(expected, actual, nil) ); + REQUIRE( errnum(&err) == 0 ); + + nput(s1); + nput(s2); + nput(expected); + nput(actual); +} + +TEST_CASE( "nstrcat: Error if first string is nil", "[string/nstrcat.c]" ) +{ + error err; + string *s2 = nstr("", nil); + string *cat = nstrcat(nil, s2, &err); + + string *expected_msg = nstr("First string is nil", nil); + string *actual_msg = errmsg(&err); + + REQUIRE( cat == nil ); + REQUIRE( errnum(&err) == EFAULT ); + REQUIRE( nstreq(expected_msg, actual_msg, nil) ); + + nput(s2); + nput(expected_msg); + errput(&err); +} + +TEST_CASE( "nstrcat: Error if second string is nil", "[string/nstrcat.c]" ) +{ + error err; + string *s1 = nstr("", nil); + string *cat = nstrcat(s1, nil, &err); + + string *expected_msg = nstr("Second string is nil", nil); + string *actual_msg = errmsg(&err); + + REQUIRE( cat == nil ); + REQUIRE( errnum(&err) == EFAULT ); + REQUIRE( nstreq(expected_msg, actual_msg, nil) ); + + nput(s1); + nput(expected_msg); + errput(&err); +} + +/* + * This file is part of libneo. + * Copyright (c) 2021 Fefie . + * + * 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 + * . + * + * libneo comes with ABSOLUTELY NO WARRANTY, to the extent + * permitted by applicable law. See the CNPLv6+ for details. + */ diff --git a/test/string/nstrcmp.cpp b/test/string/nstrcmp.cpp new file mode 100644 index 0000000..af85b14 --- /dev/null +++ b/test/string/nstrcmp.cpp @@ -0,0 +1,154 @@ +/** See the end of this file for copyright and license terms. */ + +#include +#include + +#include + +TEST_CASE( "nstrcmp: Return 0 if strings are equal", "[string/nstrcat.c]" ) +{ + error err; + string *s1 = nstr("aaaaa", nil); + string *s2 = nstr("aaaaa", nil); + + int diff = nstrcmp(s1, s2, &err); + + REQUIRE( diff == 0 ); + REQUIRE( errnum(&err) == 0 ); + + nput(s1); + nput(s2); +} + +TEST_CASE( "nstrcmp: Return negative if first string is empty", "[string/nstrcat.c]" ) +{ + error err; + string *s1 = nstr("", nil); + string *s2 = nstr("aaaaa", nil); + + int diff = nstrcmp(s1, s2, &err); + + REQUIRE( diff < 0 ); + REQUIRE( errnum(&err) == 0 ); + + nput(s1); + nput(s2); +} + +TEST_CASE( "nstrcmp: Return positive if second string is empty", "[string/nstrcat.c]" ) +{ + error err; + string *s1 = nstr("aaaaa", nil); + string *s2 = nstr("", nil); + + int diff = nstrcmp(s1, s2, &err); + + REQUIRE( diff > 0 ); + REQUIRE( errnum(&err) == 0 ); + + nput(s1); + nput(s2); +} + +TEST_CASE( "nstrcmp: Return 0 if both strings are empty", "[string/nstrcat.c]" ) +{ + error err; + string *s1 = nstr("", nil); + string *s2 = nstr("", nil); + + int diff = nstrcmp(s1, s2, &err); + + REQUIRE( diff == 0 ); + REQUIRE( errnum(&err) == 0 ); + + nput(s1); + nput(s2); +} + +TEST_CASE( "nstrcmp: Return positive if first string is larger", "[string/nstrcat.c]" ) +{ + error err; + /* 'a' has a higher ASCII value than 'A' */ + string *s1 = nstr("aaaaa", nil); + string *s2 = nstr("aaaAa", nil); + + int diff = nstrcmp(s1, s2, &err); + + REQUIRE( diff > 0 ); + REQUIRE( errnum(&err) == 0 ); + + nput(s1); + nput(s2); +} + +TEST_CASE( "nstrcmp: Return negative if second string is larger", "[string/nstrcat.c]" ) +{ + error err; + /* 'a' has a higher ASCII value than 'A' */ + string *s1 = nstr("aaaAa", nil); + string *s2 = nstr("aaaaa", nil); + + int diff = nstrcmp(s1, s2, &err); + + REQUIRE( diff < 0 ); + + REQUIRE( errnum(&err) == 0 ); + + nput(s1); + nput(s2); +} + +TEST_CASE( "nstrcmp: Error if first string is nil", "[string/nstrcat.c]" ) +{ + error err; + string *s2 = nstr("aaaaa", nil); + usize diff = nstrcmp(nil, s2, &err); + + string *expected_msg = nstr("First string is nil", nil); + string *actual_msg = errmsg(&err); + + REQUIRE( diff > 0 ); + REQUIRE( errnum(&err) == EFAULT ); + REQUIRE( nstreq(expected_msg, actual_msg, nil) ); + + nput(s2); + nput(expected_msg); + errput(&err); +} + +TEST_CASE( "nstrcmp: Error if second string is nil", "[string/nstrcat.c]" ) +{ + error err; + string *s1 = nstr("aaaaa", nil); + usize diff = nstrcmp(s1, nil, &err); + + string *expected_msg = nstr("Second string is nil", nil); + string *actual_msg = errmsg(&err); + + REQUIRE( diff > 0 ); + REQUIRE( errnum(&err) == EFAULT ); + REQUIRE( nstreq(expected_msg, actual_msg, nil) ); + + nput(s1); + nput(expected_msg); + errput(&err); +} + +/* + * We don't need to test for Unicode sequences because Unicode sequences are + * designed to be able to be treated like ASCII strings in comparison operations + * and still produce a correct result. + */ + +/* + * This file is part of libneo. + * Copyright (c) 2021 Fefie . + * + * 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 + * . + * + * libneo comes with ABSOLUTELY NO WARRANTY, to the extent + * permitted by applicable law. See the CNPLv6+ for details. + */ diff --git a/test/string/nstrdup.cpp b/test/string/nstrdup.cpp new file mode 100644 index 0000000..023d965 --- /dev/null +++ b/test/string/nstrdup.cpp @@ -0,0 +1,48 @@ +/** See the end of this file for copyright and license terms. */ + +#include +#include + +#include + +TEST_CASE( "nstrdup: Duplicate a string", "[string/nstrcat.c]" ) +{ + error err; + string *s = nstr("aaaaa", nil); + string *dup = nstrdup(s, &err); + + REQUIRE( dup != nil ); + REQUIRE( dup != s ); + REQUIRE( nstreq(s, dup, nil) ); + REQUIRE( errnum(&err) == 0 ); + + nput(s); + nput(dup); +} + +TEST_CASE( "nstrdup: Error if string is nil", "[string/nstrcat.c]" ) +{ + error err; + string *dup = nstrdup(nil, &err); + + string *expected_msg = nstr("String is nil", nil); + + REQUIRE( dup == nil ); + REQUIRE( errnum(&err) == EFAULT ); + REQUIRE( nstreq(expected_msg, errmsg(&err), nil) ); + + errput(&err); +} + +/* + * This file is part of libneo. + * Copyright (c) 2021 Fefie . + * + * 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 + * . + * + * libneo comes with ABSOLUTELY NO WARRANTY, to the extent + * permitted by applicable law. See the CNPLv6+ for details. + */ diff --git a/test/string/nstrmul.cpp b/test/string/nstrmul.cpp new file mode 100644 index 0000000..136f378 --- /dev/null +++ b/test/string/nstrmul.cpp @@ -0,0 +1,96 @@ +/** See the end of this file for copyright and license terms. */ + +#include +#include + +#include + +TEST_CASE( "nstrmul: Repeat a string", "[string/nstrcat.c]" ) +{ + error err; + string *s = nstr("aaaaa", nil); + + string *expected = nstr("aaaaaaaaaaaaaaa", nil); + string *actual = nstrmul(s, 3, &err); + + REQUIRE( actual != nil ); + REQUIRE( nlen(actual) == 5 * 3 ); + REQUIRE( nstreq(expected, actual, nil) ); + REQUIRE( errnum(&err) == 0 ); + + nput(s); + nput(expected); + nput(actual); +} + +TEST_CASE( "nstrmul: Duplicate a string if count is 1", "[string/nstrcat.c]" ) +{ + error err; + string *s = nstr("aaaaa", nil); + + string *mul = nstrmul(s, 1, &err); + + REQUIRE( nul != nil ); + REQUIRE( nlen(mul) == 5 ); + REQUIRE( nstreq(s, mul, nil) ); + REQUIRE( errnum(&err) == 0 ); + + nput(s); + nput(mul); +} + +TEST_CASE( "nstrmul: Error if string is nil", "[string/nstrcat.c]" ) +{ + error err; + string *mul = nstrmul(nil, 1, &err); + + string *expected_msg = nstr("String is nil", nil); + + REQUIRE( mul == nil ); + REQUIRE( errnum(&err) == EFAULT ); + REQUIRE( nstreq(expected_msg, errmsg(&err), nil) ); + + errput(&err); +} + +TEST_CASE( "nstrmul: Return empty string if count is 0", "[string/nstrcat.c]" ) +{ + error err; + string *s = nstr("aaaaa", nil); + string *mul = nstrmul(s, 0, &err); + + string *expected_msg = nstr("String is nil", nil); + + REQUIRE( mul == nil ); + REQUIRE( errnum(&err) == EFAULT ); + REQUIRE( nstreq(expected_msg, errmsg(&err), nil) ); + + errput(&err); +} + +TEST_CASE( "nstrmul: Error if string is nil", "[string/nstrcat.c]" ) +{ + error err; + string *mul = nstrmul(nil, 3, &err); + + string *expected_msg = nstr("String is nil", nil); + + REQUIRE( mul == nil ); + REQUIRE( errnum(&err) == EFAULT ); + REQUIRE( nstreq(expected_msg, errmsg(&err), nil) ); + + errput(&err); +} + +/* + * This file is part of libneo. + * Copyright (c) 2021 Fefie . + * + * 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 + * . + * + * libneo comes with ABSOLUTELY NO WARRANTY, to the extent + * permitted by applicable law. See the CNPLv6+ for details. + */ diff --git a/test/string/string.cmake b/test/string/string.cmake index 9359e1e..0d3720d 100644 --- a/test/string/string.cmake +++ b/test/string/string.cmake @@ -2,6 +2,16 @@ include(string/utf/utf.cmake) +target_sources(neo_test PRIVATE + string/i2nstr.cpp + string/leftpad.cpp + string/nstr.cpp + string/nstrcat.cpp + string/nstrcmp.cpp + string/nstrdup.cpp + string/u2nstr.cpp +) + # This file is part of libneo. # Copyright (c) 2021 Fefie . # diff --git a/test/string/u2nstr.cpp b/test/string/u2nstr.cpp new file mode 100644 index 0000000..6708df2 --- /dev/null +++ b/test/string/u2nstr.cpp @@ -0,0 +1,126 @@ +/** See the end of this file for copyright and license terms. */ + +#include +#include + +#include + +TEST_CASE( "u2nstr: Convert 0 to base 2", "[string/x2nstr.c]" ) +{ + error err; + + string *expected = nstr("0", nil); + string *actual = u2nstr(0, 2, &err); + + REQUIRE( actual != nil ); + REQUIRE( nstreq(expected, actual, nil) ); + REQUIRE( errnum(&err) == 0 ); + + nput(expected); + nput(actual); +} + +TEST_CASE( "u2nstr: Convert 0 to base 36", "[string/x2nstr.c]" ) +{ + error err; + + string *expected = nstr("0", nil); + string *actual = u2nstr(0, 36, &err); + + REQUIRE( actual != nil ); + REQUIRE( nstreq(expected, actual, nil) ); + REQUIRE( errnum(&err) == 0 ); + + nput(expected); + nput(actual); +} + +TEST_CASE( "u2nstr: Convert 255 to base 2", "[string/x2nstr.c]" ) +{ + error err; + + string *expected = nstr("11111111", nil); + string *actual = u2nstr(255, 2, &err); + + REQUIRE( actual != nil ); + REQUIRE( nstreq(expected, actual, nil) ); + REQUIRE( errnum(&err) == 0 ); + + nput(expected); + nput(actual); +} + +TEST_CASE( "u2nstr: Convert 1679615 to base 36", "[string/x2nstr.c]" ) +{ + error err; + + string *expected = nstr("zzzz", nil); + string *actual = u2nstr(1679615, 36, &err); + + REQUIRE( actual != nil ); + REQUIRE( nstreq(expected, actual, nil) ); + REQUIRE( errnum(&err) == 0 ); + + nput(expected); + nput(actual); +} + +TEST_CASE( "u2nstr: Convert 2^64-1 to base 16", "[string/x2nstr.c]" ) +{ + error err; + + string *expected = nstr("ffffffffffffffff", nil); + string *actual = u2nstr(0xffffffffffffffff, 16, &err); + + REQUIRE( actual != nil ); + REQUIRE( nstreq(expected, actual, nil) ); + REQUIRE( errnum(&err) == 0 ); + + nput(expected); + nput(actual); +} + +TEST_CASE( "u2nstr: Error if base too low", "[string/x2nstr.c]" ) +{ + error err; + string *s = u2nstr(420, 1, &err); + + string *expected_msg = nstr("Numerical base out of range", nil); + string *actual_msg = errmsg(&err); + + REQUIRE( s == nil ); + REQUIRE( nstreq(expected_msg, actual_msg, nil) ); + REQUIRE( errnum(&err) == EINVAL ); + + nput(expected_msg); + errput(&err); +} + +TEST_CASE( "u2nstr: Error if base too high", "[string/x2nstr.c]" ) +{ + error err; + string *s = u2nstr(420, 37, &err); + + string *expected_msg = nstr("Numerical base out of range", nil); + string *actual_msg = errmsg(&err); + + REQUIRE( s == nil ); + REQUIRE( nstreq(expected_msg, actual_msg, nil) ); + REQUIRE( errnum(&err) == EINVAL ); + + nput(expected_msg); + errput(&err); +} + +/* + * This file is part of libneo. + * Copyright (c) 2021 Fefie . + * + * 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 + * . + * + * libneo comes with ABSOLUTELY NO WARRANTY, to the extent + * permitted by applicable law. See the CNPLv6+ for details. + */