1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2025-02-17 15:45:32 -05:00

Add func kernaux_itoa10

This commit is contained in:
Alex Kotov 2022-01-18 13:12:54 +05:00
parent 861efcd159
commit 196eeff016
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
3 changed files with 69 additions and 0 deletions

View file

@ -8,9 +8,11 @@ extern "C" {
#include <stdint.h>
// uint64_t: "18446744073709551615"
// int64_t: "-9223372036854775808"
#define KERNAUX_ITOA_BUFFER_SIZE 21
void kernaux_utoa10(uint64_t value, char *buffer);
void kernaux_itoa10(int64_t value, char *buffer);
#ifdef __cplusplus
}

View file

@ -23,3 +23,13 @@ void kernaux_utoa10(uint64_t value, char *buffer)
*(pos--) = tmp;
}
}
void kernaux_itoa10(int64_t value, char *buffer)
{
if (value >= 0) {
kernaux_utoa10(value, buffer);
} else {
*(buffer++) = '-';
kernaux_utoa10(-value, buffer);
}
}

View file

@ -46,6 +46,54 @@ static const struct {
{ UINT64_MAX, "18446744073709551615" },
};
static const struct {
int64_t value;
const char *result;
} itoa10_cases[] = {
{ 0, "0" },
{ 1, "1" },
{ -1, "-1" },
{ 2, "2" },
{ -2, "-2" },
{ 9, "9" },
{ -9, "-9" },
{ 10, "10" },
{ -10, "-10" },
{ 11, "11" },
{ -11, "-11" },
{ 12, "12" },
{ 19, "19" },
{ 20, "20" },
{ 21, "21" },
{ 99, "99" },
{ 100, "100" },
{ 101, "101" },
{ 199, "199" },
{ 200, "200" },
{ 201, "201" },
{ 999, "999" },
{ 1000, "1000" },
{ 1001, "1001" },
{ 1999, "1999" },
{ 2000, "2000" },
{ 2001, "2001" },
{ 9999, "9999" },
{ 10000, "10000" },
{ 10001, "10001" },
{ UINT16_MAX, "65535" },
{ UINT16_MAX + 1, "65536" },
{ UINT32_MAX, "4294967295" },
{ (int64_t)UINT32_MAX + 1, "4294967296" },
{ INT64_MAX - 8, "9223372036854775799" },
{ INT64_MIN + 9, "-9223372036854775799" },
{ INT64_MAX - 7, "9223372036854775800" },
{ INT64_MIN + 8, "-9223372036854775800" },
{ INT64_MAX - 1, "9223372036854775806" },
{ INT64_MIN + 1, "-9223372036854775807" },
{ INT64_MAX, "9223372036854775807" },
{ INT64_MIN, "-9223372036854775808" },
};
int main()
{
char buffer[KERNAUX_ITOA_BUFFER_SIZE];
@ -59,5 +107,14 @@ int main()
assert(strcmp(buffer, utoa10_cases[index].result) == 0);
}
for (
size_t index = 0;
index < sizeof(itoa10_cases) / sizeof(itoa10_cases[0]);
++index
) {
kernaux_itoa10(itoa10_cases[index].value, buffer);
assert(strcmp(buffer, itoa10_cases[index].result) == 0);
}
return 0;
}