Remove ntoa, units

This commit is contained in:
Alex Kotov 2022-12-25 13:38:06 +04:00
parent 9f3fa6497a
commit f76a6630d6
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
17 changed files with 1 additions and 1557 deletions

View File

@ -91,14 +91,12 @@ jobs:
packages:
- without: 'all'
- without: 'io'
- without: 'ntoa'
dependencies: '--without-units'
steps:
- uses: actions/checkout@v2
- name: autogen
run: ./autogen.sh
- name: configure
run: ./configure --enable-checks --without-${{matrix.packages.without}} ${{matrix.packages.dependencies}}
run: ./configure --enable-checks --without-${{matrix.packages.without}}
- name: make
run: make
- name: check

View File

@ -79,12 +79,6 @@ endif
if WITH_MBR
libkernaux_la_SOURCES += src/mbr.c
endif
if WITH_NTOA
libkernaux_la_SOURCES += src/ntoa.c
endif
if WITH_PFA
libkernaux_la_SOURCES += src/pfa.c
endif
if WITH_UNITS
libkernaux_la_SOURCES += src/units.c
endif

View File

@ -48,10 +48,6 @@ zero). Work-in-progress APIs can change at any time.
* Data formats
* [ELF](/include/kernaux/elf.h) (*work in progress*)
* [MBR](/include/kernaux/mbr.h) (*work in progress*)
* Utilities
* [Measurement units utils](/include/kernaux/units.h) (*work in progress*)
* Usual functions
* [itoa/ftoa replacement](/include/kernaux/ntoa.h) (*non-breaking since* **0.4.0**)
* libc replacement (*work in progress*)
* [ctype.h](/libc/include/ctype.h)
* [errno.h](/libc/include/errno.h)
@ -134,7 +130,6 @@ explicitly included, use `--without-all`.
* `--with[out]-arch-riscv64` - architecture riscv64
* `--with[out]-arch-x86-64` - architecture x86-64
* `--with[out]-asm` - kernel assembler helpers
* `--with[out]-ntoa` - itoa/ftoa

View File

@ -68,9 +68,7 @@ AC_ARG_WITH( [arch-x86-64], AS_HELP_STRING([--without-arch-x86-64], [wit
AC_ARG_WITH( [asm], AS_HELP_STRING([--without-asm], [without kernel assembler helpers]))
AC_ARG_WITH( [elf], AS_HELP_STRING([--without-elf], [without ELF utils]))
AC_ARG_WITH( [mbr], AS_HELP_STRING([--without-mbr], [without MBR utils]))
AC_ARG_WITH( [ntoa], AS_HELP_STRING([--without-ntoa], [without itoa/ftoa]))
AC_ARG_WITH( [pfa], AS_HELP_STRING([--without-pfa], [without Page Frame Allocator]))
AC_ARG_WITH( [units], AS_HELP_STRING([--without-units], [without measurement units utils]))
dnl Packages (disabled by default)
AC_ARG_WITH( [libc], AS_HELP_STRING([--with-libc], [with libc replacement]))
@ -104,9 +102,7 @@ if test -z "$with_arch_x86_64"; then with_arch_x86_64=no; fi
if test -z "$with_asm"; then with_asm=no; fi
if test -z "$with_elf"; then with_elf=no; fi
if test -z "$with_mbr"; then with_mbr=no; fi
if test -z "$with_ntoa"; then with_ntoa=no; fi
if test -z "$with_pfa"; then with_pfa=no; fi
if test -z "$with_units"; then with_units=no; fi
])
AS_IF([test "$with_all" = no], do_without_all)
@ -141,9 +137,7 @@ AS_IF([test "$with_arch_x86_64" = no ], [with_arch_x86_64=no], [wit
AS_IF([test "$with_asm" = no ], [with_asm=no], [with_asm=yes])
AS_IF([test "$with_elf" = no ], [with_elf=no], [with_elf=yes])
AS_IF([test "$with_mbr" = no ], [with_mbr=no], [with_mbr=yes])
AS_IF([test "$with_ntoa" = no ], [with_ntoa=no], [with_ntoa=yes])
AS_IF([test "$with_pfa" = no ], [with_pfa=no], [with_pfa=yes])
AS_IF([test "$with_units" = no ], [with_units=no], [with_units=yes])
dnl Packages (disabled by default)
AS_IF([test "$with_libc" = yes], [with_libc=yes], [with_libc=no])
@ -157,8 +151,6 @@ AS_IF([test "$with_libc" = yes], [with_libc=yes], [wit
AS_IF([test "$enable_checks" = yes -a "$enable_freestanding" = yes], AC_MSG_ERROR([can not build freestanding tests]))
AS_IF([test "$enable_checks" = yes -a "$with_libc" = yes], AC_MSG_ERROR([can not use package `libc' with tests]))
AS_IF([test "$with_units" = yes -a "$with_ntoa" = no], AC_MSG_ERROR([package `units' requires package `ntoa']))
#########################
@ -194,9 +186,7 @@ AM_CONDITIONAL([WITH_ARCH_X86_64], [test "$with_arch_x86_64" = yes])
AM_CONDITIONAL([WITH_ASM], [test "$with_asm" = yes])
AM_CONDITIONAL([WITH_ELF], [test "$with_elf" = yes])
AM_CONDITIONAL([WITH_MBR], [test "$with_mbr" = yes])
AM_CONDITIONAL([WITH_NTOA], [test "$with_ntoa" = yes])
AM_CONDITIONAL([WITH_PFA], [test "$with_pfa" = yes])
AM_CONDITIONAL([WITH_UNITS], [test "$with_units" = yes])
dnl Packages (disabled by default)
AM_CONDITIONAL([WITH_LIBC], [test "$with_libc" = yes])
@ -237,9 +227,7 @@ AS_IF([test "$with_arch_x86_64" = yes], [AC_DEFINE([WITH_ARCH_X86_64],
AS_IF([test "$with_asm" = yes], [AC_DEFINE([WITH_ASM], [1], [with kernel assembler helpers])])
AS_IF([test "$with_elf" = yes], [AC_DEFINE([WITH_ELF], [1], [with ELF utils])])
AS_IF([test "$with_mbr" = yes], [AC_DEFINE([WITH_MBR], [1], [with MBR utils])])
AS_IF([test "$with_ntoa" = yes], [AC_DEFINE([WITH_NTOA], [1], [with ntoa])])
AS_IF([test "$with_pfa" = yes], [AC_DEFINE([WITH_PFA], [1], [with Page Frame Allocator])])
AS_IF([test "$with_units", = yes], [AC_DEFINE([WITH_UNITS], [1], [with measurement units utils])])
dnl Packages (disabled by default)
AS_IF([test "$with_libc" = yes], [AC_DEFINE([WITH_LIBC], [1], [with libc replacement])])
@ -263,9 +251,7 @@ AS_IF([test "$with_arch_riscv64" = no], [AC_SUBST([comment_line_arch_riscv64], [
AS_IF([test "$with_arch_x86_64" = no], [AC_SUBST([comment_line_arch_x86_64], [//])])
AS_IF([test "$with_elf" = no], [AC_SUBST([comment_line_elf], [//])])
AS_IF([test "$with_mbr" = no], [AC_SUBST([comment_line_mbr], [//])])
AS_IF([test "$with_ntoa" = no], [AC_SUBST([comment_line_ntoa], [//])])
AS_IF([test "$with_pfa" = no], [AC_SUBST([comment_line_pfa], [//])])
AS_IF([test "$with_units" = no], [AC_SUBST([comment_line_units], [//])])

View File

@ -14,8 +14,6 @@ nobase_include_HEADERS = \
kernaux/macro/packing_end.run \
kernaux/macro/packing_start.run \
kernaux/mbr.h \
kernaux/ntoa.h \
kernaux/pfa.h \
kernaux/runtime.h \
kernaux/units.h \
kernaux/version.h

View File

@ -9,8 +9,6 @@
#include <kernaux/elf.h>
#include <kernaux/macro.h>
#include <kernaux/mbr.h>
#include <kernaux/ntoa.h>
#include <kernaux/pfa.h>
#include <kernaux/runtime.h>
#include <kernaux/units.h>
#include <kernaux/version.h>

View File

@ -1,70 +0,0 @@
#ifndef KERNAUX_INCLUDED_NTOA
#define KERNAUX_INCLUDED_NTOA
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#define KERNAUX_NTOA_MAX_PREFIX_LEN 100
#define KERNAUX_NTOA_DEFAULT_PREFIX_2 "0b"
#define KERNAUX_NTOA_DEFAULT_PREFIX_8 "0o"
#define KERNAUX_NTOA_DEFAULT_PREFIX_16 "0x"
// "1111111111111111111111111111111111111111111111111111111111111111"
#define KERNAUX_UTOA_MIN_BUFFER_SIZE (64 + 1)
// "111111111111111111111111111111111111111111111111111111111111111"
// "-1000000000000000000000000000000000000000000000000000000000000000"
#define KERNAUX_ITOA_MIN_BUFFER_SIZE (65 + 1)
// "0b1111111111111111111111111111111111111111111111111111111111111111"
#define KERNAUX_UTOA2_BUFFER_SIZE (64 + 2 + 1)
// "0b111111111111111111111111111111111111111111111111111111111111111"
// "-0b1000000000000000000000000000000000000000000000000000000000000000"
#define KERNAUX_ITOA2_BUFFER_SIZE (65 + 2 + 1)
// "0o1777777777777777777777"
#define KERNAUX_UTOA8_BUFFER_SIZE (21 + 2 + 1)
// "0o777777777777777777777"
// "-0o1000000000000000000000"
#define KERNAUX_ITOA8_BUFFER_SIZE (22 + 2 + 1)
// "18446744073709551615"
#define KERNAUX_UTOA10_BUFFER_SIZE (20 + 1)
// "9223372036854775807"
// "-9223372036854775808"
#define KERNAUX_ITOA10_BUFFER_SIZE (20 + 1)
// "0xffffffffffffffff"
#define KERNAUX_UTOA16_BUFFER_SIZE (16 + 2 + 1)
// "0x7fffffffffffffff"
// "-0x8000000000000000"
#define KERNAUX_ITOA16_BUFFER_SIZE (17 + 2 + 1)
char *kernaux_utoa(uint64_t value, char *buffer, int base, const char *prefix);
char *kernaux_itoa(int64_t value, char *buffer, int base, const char *prefix);
char *kernaux_utoa2(uint64_t value, char *buffer);
char *kernaux_itoa2(int64_t value, char *buffer);
char *kernaux_utoa8(uint64_t value, char *buffer);
char *kernaux_itoa8(int64_t value, char *buffer);
char *kernaux_utoa10(uint64_t value, char *buffer);
char *kernaux_itoa10(int64_t value, char *buffer);
char *kernaux_utoa16(uint64_t value, char *buffer);
char *kernaux_itoa16(int64_t value, char *buffer);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,42 +0,0 @@
#ifndef KERNAUX_INCLUDED_UNITS
#define KERNAUX_INCLUDED_UNITS
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
enum KernAux_Unit {
KERNAUX_UNIT_BIT,
KERNAUX_UNIT_BYTE,
};
enum KernAux_UnitPrefixDec {
KERNAUX_UNITPFX_KILO,
KERNAUX_UNITPFX_MEGA,
KERNAUX_UNITPFX_GIGA,
};
enum KernAux_UnitPrefixBin {
KERNAUX_UNITPFX_KIBI,
KERNAUX_UNITPFX_MEBI,
KERNAUX_UNITPFX_GIBI,
};
bool kernaux_units_human_raw(uint64_t value, enum KernAux_Unit unit,
char *buffer, size_t buffer_size);
bool kernaux_units_human_dec(uint64_t value, enum KernAux_Unit unit,
enum KernAux_UnitPrefixDec prefix,
char *buffer, size_t buffer_size);
bool kernaux_units_human_bin(uint64_t value, enum KernAux_Unit unit,
enum KernAux_UnitPrefixBin prefix,
char *buffer, size_t buffer_size);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -3,8 +3,6 @@
@comment_line_elf@#define KERNAUX_VERSION_WITH_ELF
@comment_line_mbr@#define KERNAUX_VERSION_WITH_MBR
@comment_line_ntoa@#define KERNAUX_VERSION_WITH_NTOA
@comment_line_pfa@#define KERNAUX_VERSION_WITH_PFA
@comment_line_units@#define KERNAUX_VERSION_WITH_UNITS
#endif

View File

@ -13,10 +13,8 @@ include/kernaux/macro.h
include/kernaux/macro/packing_end.run
include/kernaux/macro/packing_start.run
include/kernaux/mbr.h
include/kernaux/ntoa.h
include/kernaux/pfa.h
include/kernaux/runtime.h
include/kernaux/units.h
include/kernaux/version.h
lib/libkernaux.a
lib/libkernaux.so

View File

@ -1,119 +0,0 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "assert.h"
#include <kernaux/ntoa.h>
#include <stddef.h>
char *kernaux_utoa(uint64_t value, char *buffer, int base, const char *prefix)
{
KERNAUX_NOTNULL(buffer);
// Protect caller from invalid state in case of future assertions
// cppcheck-suppress ctunullpointer
*buffer = '\0';
switch (base) {
case 'b': case 'B': base = 2; break;
case 'o': case 'O': base = 8; break;
case 'd': case 'D': base = 10; break;
case 'h': case 'x': base = 16; break;
case 'H': case 'X': base = -16; break;
}
// Uppercase
char alpha = 'a';
if (base < 0) {
alpha = 'A';
base = -base;
}
KERNAUX_ASSERT(base >= 2 && base <= 36);
// Write prefix
if (prefix) {
for (size_t prefix_len = 1; *prefix; ++prefix_len) {
if (prefix_len > KERNAUX_NTOA_MAX_PREFIX_LEN) {
// Protect caller from invalid state
*buffer = '\0';
KERNAUX_PANIC("prefix is too long");
}
*(buffer++) = *(prefix++);
}
}
// Write number
char *pos = buffer;
if (value == 0) *(pos++) = '0';
while (value > 0) {
// cppcheck-suppress zerodivcond
const char mod = value % base;
*(pos++) = mod < 10 ? mod + '0' : mod - 10 + alpha;
// cppcheck-suppress zerodivcond
value /= base;
}
char *const result = pos;
*(pos--) = '\0';
// Reverse number
while (buffer < pos) {
const char tmp = *buffer;
*(buffer++) = *pos;
*(pos--) = tmp;
}
return result;
}
char *kernaux_itoa(int64_t value, char *buffer, int base, const char *const prefix)
{
if (value >= 0) {
return kernaux_utoa(value, buffer, base, prefix);
} else {
*(buffer++) = '-';
return kernaux_utoa(-value, buffer, base, prefix);
}
}
char *kernaux_utoa2(uint64_t value, char *buffer)
{
return kernaux_utoa(value, buffer, 'b', KERNAUX_NTOA_DEFAULT_PREFIX_2);
}
char *kernaux_itoa2(int64_t value, char *buffer)
{
return kernaux_itoa(value, buffer, 'b', KERNAUX_NTOA_DEFAULT_PREFIX_2);
}
char *kernaux_utoa8(uint64_t value, char *buffer)
{
return kernaux_utoa(value, buffer, 'o', KERNAUX_NTOA_DEFAULT_PREFIX_8);
}
char *kernaux_itoa8(int64_t value, char *buffer)
{
return kernaux_itoa(value, buffer, 'o', KERNAUX_NTOA_DEFAULT_PREFIX_8);
}
char *kernaux_utoa10(uint64_t value, char *buffer)
{
return kernaux_utoa(value, buffer, 'd', NULL);
}
char *kernaux_itoa10(int64_t value, char *buffer)
{
return kernaux_itoa(value, buffer, 'd', NULL);
}
char *kernaux_utoa16(uint64_t value, char *buffer)
{
return kernaux_utoa(value, buffer, 'x', KERNAUX_NTOA_DEFAULT_PREFIX_16);
}
char *kernaux_itoa16(int64_t value, char *buffer)
{
return kernaux_itoa(value, buffer, 'x', KERNAUX_NTOA_DEFAULT_PREFIX_16);
}

View File

@ -1,149 +0,0 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "assert.h"
#include <kernaux/ntoa.h>
#include <kernaux/units.h>
#include <string.h>
#define TMP_BUFFER_SIZE (64)
bool kernaux_units_human_raw(
const uint64_t value,
const enum KernAux_Unit unit,
char *const buffer,
const size_t buffer_size
) {
KERNAUX_NOTNULL(buffer);
KERNAUX_ASSERT(buffer_size > 0);
char tmp_buffer[TMP_BUFFER_SIZE];
char *tmp = tmp_buffer;
kernaux_utoa10(value, tmp);
while (*tmp != '\0') ++tmp;
*(tmp++) = ' ';
switch (unit) {
case KERNAUX_UNIT_BIT:
*(tmp++) = 'b';
*(tmp++) = 'i';
*(tmp++) = 't';
break;
case KERNAUX_UNIT_BYTE:
*(tmp++) = 'B';
*(tmp++) = 'y';
*(tmp++) = 't';
*(tmp++) = 'e';
break;
}
*(tmp++) = '\0';
const size_t tmp_size = strlen(tmp_buffer) + 1;
if (tmp_size > buffer_size) return false;
strcpy(buffer, tmp_buffer);
return true;
}
bool kernaux_units_human_dec(
const uint64_t value,
const enum KernAux_Unit unit,
const enum KernAux_UnitPrefixDec prefix,
char *const buffer,
const size_t buffer_size
) {
KERNAUX_NOTNULL(buffer);
KERNAUX_ASSERT(buffer_size > 0);
char tmp_buffer[TMP_BUFFER_SIZE];
char *tmp = tmp_buffer;
kernaux_utoa10(value, tmp);
while (*tmp != '\0') ++tmp;
*(tmp++) = ' ';
switch (prefix) {
case KERNAUX_UNITPFX_KILO:
*(tmp++) = 'k';
break;
case KERNAUX_UNITPFX_MEGA:
*(tmp++) = 'M';
break;
case KERNAUX_UNITPFX_GIGA:
*(tmp++) = 'G';
break;
}
switch (unit) {
case KERNAUX_UNIT_BIT:
*(tmp++) = 'b';
*(tmp++) = 'i';
*(tmp++) = 't';
break;
case KERNAUX_UNIT_BYTE:
*(tmp++) = 'B';
break;
}
*(tmp++) = '\0';
const size_t tmp_size = strlen(tmp_buffer) + 1;
if (tmp_size > buffer_size) return false;
strcpy(buffer, tmp_buffer);
return true;
}
bool kernaux_units_human_bin(
const uint64_t value,
const enum KernAux_Unit unit,
const enum KernAux_UnitPrefixBin prefix,
char *const buffer,
const size_t buffer_size
) {
char tmp_buffer[TMP_BUFFER_SIZE];
char *tmp = tmp_buffer;
kernaux_utoa10(value, tmp);
while (*tmp != '\0') ++tmp;
*(tmp++) = ' ';
switch (prefix) {
case KERNAUX_UNITPFX_KIBI:
*(tmp++) = 'K';
*(tmp++) = 'i';
break;
case KERNAUX_UNITPFX_MEBI:
*(tmp++) = 'M';
*(tmp++) = 'i';
break;
case KERNAUX_UNITPFX_GIBI:
*(tmp++) = 'G';
*(tmp++) = 'i';
break;
}
switch (unit) {
case KERNAUX_UNIT_BIT:
*(tmp++) = 'b';
*(tmp++) = 'i';
*(tmp++) = 't';
break;
case KERNAUX_UNIT_BYTE:
*(tmp++) = 'B';
break;
}
*(tmp++) = '\0';
const size_t tmp_size = strlen(tmp_buffer) + 1;
if (tmp_size > buffer_size) return false;
strcpy(buffer, tmp_buffer);
return true;
}

3
tests/.gitignore vendored
View File

@ -1,8 +1,5 @@
/test_arch_i386
/test_elf
/test_mbr
/test_ntoa
/test_ntoa_assert
/test_pfa
/test_pfa_assert
/test_units_human

View File

@ -40,32 +40,6 @@ test_mbr_SOURCES = \
test_mbr.c
endif
#############
# test_ntoa #
#############
if WITH_NTOA
TESTS += test_ntoa
test_ntoa_LDADD = $(top_builddir)/libkernaux.la
test_ntoa_SOURCES = \
main.c \
test_ntoa.c
endif
####################
# test_ntoa_assert #
####################
if ENABLE_ASSERT
if WITH_NTOA
TESTS += test_ntoa_assert
test_ntoa_assert_LDADD = $(top_builddir)/libkernaux.la
test_ntoa_assert_SOURCES = \
main.c \
test_ntoa_assert.c
endif
endif
############
# test_pfa #
############
@ -91,15 +65,3 @@ test_pfa_assert_SOURCES = \
test_pfa_assert.c
endif
endif
####################
# test_units_human #
####################
if WITH_UNITS
TESTS += test_units_human
test_units_human_LDADD = $(top_builddir)/libkernaux.la
test_units_human_SOURCES = \
main.c \
test_units_human.c
endif

View File

@ -1,777 +0,0 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <kernaux/ntoa.h>
#include <assert.h>
#include <stddef.h>
#include <string.h>
#define VALID_LONG_PREFIX "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
#define TOO_LONG_PREFIX "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
static const struct {
const char *result;
int base;
uint64_t value;
} utoa_cases[] = {
{ "0", 2, 0 }, { "0", -2, 0 }, { "0", 'b', 0 }, { "0", 'B', 0 },
{ "0", 3, 0 }, { "0", -3, 0 },
{ "0", 4, 0 }, { "0", -4, 0 },
{ "0", 5, 0 }, { "0", -5, 0 },
{ "0", 6, 0 }, { "0", -6, 0 },
{ "0", 7, 0 }, { "0", -7, 0 },
{ "0", 8, 0 }, { "0", -8, 0 }, { "0", 'o', 0 }, { "0", 'O', 0 },
{ "0", 9, 0 }, { "0", -9, 0 },
{ "0", 10, 0 }, { "0", -10, 0 }, { "0", 'd', 0 }, { "0", 'D', 0 },
{ "0", 11, 0 }, { "0", -11, 0 },
{ "0", 12, 0 }, { "0", -12, 0 },
{ "0", 13, 0 }, { "0", -13, 0 },
{ "0", 14, 0 }, { "0", -14, 0 },
{ "0", 15, 0 }, { "0", -15, 0 },
{ "0", 16, 0 }, { "0", -16, 0 },
{ "0", 'h', 0 }, { "0", 'H', 0 }, { "0", 'x', 0 }, { "0", 'X', 0 },
{ "0", 17, 0 }, { "0", -17, 0 },
{ "0", 18, 0 }, { "0", -18, 0 },
{ "0", 19, 0 }, { "0", -19, 0 },
{ "0", 20, 0 }, { "0", -20, 0 },
{ "0", 21, 0 }, { "0", -21, 0 },
{ "0", 22, 0 }, { "0", -22, 0 },
{ "0", 23, 0 }, { "0", -23, 0 },
{ "0", 24, 0 }, { "0", -24, 0 },
{ "0", 25, 0 }, { "0", -25, 0 },
{ "0", 26, 0 }, { "0", -26, 0 },
{ "0", 27, 0 }, { "0", -27, 0 },
{ "0", 28, 0 }, { "0", -28, 0 },
{ "0", 29, 0 }, { "0", -29, 0 },
{ "0", 30, 0 }, { "0", -30, 0 },
{ "0", 31, 0 }, { "0", -31, 0 },
{ "0", 32, 0 }, { "0", -32, 0 },
{ "0", 33, 0 }, { "0", -33, 0 },
{ "0", 34, 0 }, { "0", -34, 0 },
{ "0", 35, 0 }, { "0", -35, 0 },
{ "0", 36, 0 }, { "0", -36, 0 },
{ "1111011", 2, 123 }, { "1111011", -2, 123 },
{ "1111011", 'b', 123 }, { "1111011", 'B', 123 },
{ "11120", 3, 123 }, { "11120", -3, 123 },
{ "1323", 4, 123 }, { "1323", -4, 123 },
{ "443", 5, 123 }, { "443", -5, 123 },
{ "323", 6, 123 }, { "323", -6, 123 },
{ "234", 7, 123 }, { "234", -7, 123 },
{ "173", 8, 123 }, { "173", -8, 123 },
{ "173", 'o', 123 }, { "173", 'O', 123 },
{ "146", 9, 123 }, { "146", -9, 123 },
{ "123", 10, 123 }, { "123", -10, 123 },
{ "123", 'd', 123 }, { "123", 'D', 123 },
{ "102", 11, 123 }, { "102", -11, 123 },
{ "a3", 12, 123 }, { "A3", -12, 123 },
{ "96", 13, 123 }, { "96", -13, 123 },
{ "8b", 14, 123 }, { "8B", -14, 123 },
{ "83", 15, 123 }, { "83", -15, 123 },
{ "7b", 16, 123 }, { "7B", -16, 123 },
{ "7b", 'h', 123 }, { "7B", 'H', 123 },
{ "7b", 'x', 123 }, { "7B", 'X', 123 },
{ "74", 17, 123 }, { "74", -17, 123 },
{ "6f", 18, 123 }, { "6F", -18, 123 },
{ "69", 19, 123 }, { "69", -19, 123 },
{ "63", 20, 123 }, { "63", -20, 123 },
{ "5i", 21, 123 }, { "5I", -21, 123 },
{ "5d", 22, 123 }, { "5D", -22, 123 },
{ "58", 23, 123 }, { "58", -23, 123 },
{ "53", 24, 123 }, { "53", -24, 123 },
{ "4n", 25, 123 }, { "4N", -25, 123 },
{ "4j", 26, 123 }, { "4J", -26, 123 },
{ "4f", 27, 123 }, { "4F", -27, 123 },
{ "4b", 28, 123 }, { "4B", -28, 123 },
{ "47", 29, 123 }, { "47", -29, 123 },
{ "43", 30, 123 }, { "43", -30, 123 },
{ "3u", 31, 123 }, { "3U", -31, 123 },
{ "3r", 32, 123 }, { "3R", -32, 123 },
{ "3o", 33, 123 }, { "3O", -33, 123 },
{ "3l", 34, 123 }, { "3L", -34, 123 },
{ "3i", 35, 123 }, { "3I", -35, 123 },
{ "3f", 36, 123 }, { "3F", -36, 123 },
{ "11000000111001", 2, 12345 }, { "11000000111001", -2, 12345 },
{ "121221020", 3, 12345 }, { "121221020", -3, 12345 },
{ "3000321", 4, 12345 }, { "3000321", -4, 12345 },
{ "343340", 5, 12345 }, { "343340", -5, 12345 },
{ "133053", 6, 12345 }, { "133053", -6, 12345 },
{ "50664", 7, 12345 }, { "50664", -7, 12345 },
{ "30071", 8, 12345 }, { "30071", -8, 12345 },
{ "17836", 9, 12345 }, { "17836", -9, 12345 },
{ "12345", 10, 12345 }, { "12345", -10, 12345 },
{ "9303", 11, 12345 }, { "9303", -11, 12345 },
{ "7189", 12, 12345 }, { "7189", -12, 12345 },
{ "5808", 13, 12345 }, { "5808", -13, 12345 },
{ "46db", 14, 12345 }, { "46DB", -14, 12345 },
{ "39d0", 15, 12345 }, { "39D0", -15, 12345 },
{ "3039", 16, 12345 }, { "3039", -16, 12345 },
{ "28c3", 17, 12345 }, { "28C3", -17, 12345 },
{ "221f", 18, 12345 }, { "221F", -18, 12345 },
{ "1f3e", 19, 12345 }, { "1F3E", -19, 12345 },
{ "1ah5", 20, 12345 }, { "1AH5", -20, 12345 },
{ "16ki", 21, 12345 }, { "16KI", -21, 12345 },
{ "13b3", 22, 12345 }, { "13B3", -22, 12345 },
{ "107h", 23, 12345 }, { "107H", -23, 12345 },
{ "la9", 24, 12345 }, { "LA9", -24, 12345 },
{ "jik", 25, 12345 }, { "JIK", -25, 12345 },
{ "i6l", 26, 12345 }, { "I6L", -26, 12345 },
{ "gp6", 27, 12345 }, { "GP6", -27, 12345 },
{ "fkp", 28, 12345 }, { "FKP", -28, 12345 },
{ "ejk", 29, 12345 }, { "EJK", -29, 12345 },
{ "dlf", 30, 12345 }, { "DLF", -30, 12345 },
{ "cq7", 31, 12345 }, { "CQ7", -31, 12345 },
{ "c1p", 32, 12345 }, { "C1P", -32, 12345 },
{ "bb3", 33, 12345 }, { "BB3", -33, 12345 },
{ "an3", 34, 12345 }, { "AN3", -34, 12345 },
{ "a2p", 35, 12345 }, { "A2P", -35, 12345 },
{ "9ix", 36, 12345 }, { "9IX", -36, 12345 },
};
static const struct {
uint64_t value;
const char *result;
} utoa2_cases[] = {
{ 0, "0" },
{ 1, "1" },
{ 2, "10" },
{ 3, "11" },
{ 4, "100" },
{ 5, "101" },
{ 6, "110" },
{ 7, "111" },
{ 8, "1000" },
{ UINT16_MAX, "1111111111111111" },
{ UINT16_MAX + 1, "10000000000000000" },
{ UINT32_MAX, "11111111111111111111111111111111" },
{ (uint64_t)UINT32_MAX + 1, "100000000000000000000000000000000" },
{ UINT64_MAX - 6, "1111111111111111111111111111111111111111111111111111111111111001" },
{ UINT64_MAX - 5, "1111111111111111111111111111111111111111111111111111111111111010" },
{ UINT64_MAX - 1, "1111111111111111111111111111111111111111111111111111111111111110" },
{ UINT64_MAX, "1111111111111111111111111111111111111111111111111111111111111111" },
};
static const struct {
int64_t value;
const char *result;
} itoa2_cases[] = {
{ 0, "0" },
{ 1, "1" },
{ -1, "-1" },
{ 2, "10" },
{ -2, "-10" },
{ 3, "11" },
{ -3, "-11" },
{ 4, "100" },
{ -4, "-100" },
{ 5, "101" },
{ -5, "-101" },
{ 6, "110" },
{ -6, "-110" },
{ 7, "111" },
{ -7, "-111" },
{ 8, "1000" },
{ -8, "-1000" },
{ UINT16_MAX, "1111111111111111" },
{ UINT16_MAX + 1, "10000000000000000" },
{ UINT32_MAX, "11111111111111111111111111111111" },
{ (int64_t)UINT32_MAX + 1, "100000000000000000000000000000000" },
{ INT64_MAX - 6, "111111111111111111111111111111111111111111111111111111111111001" },
{ INT64_MIN + 7, "-111111111111111111111111111111111111111111111111111111111111001" },
{ INT64_MAX - 5, "111111111111111111111111111111111111111111111111111111111111010" },
{ INT64_MIN + 6, "-111111111111111111111111111111111111111111111111111111111111010" },
{ INT64_MAX - 1, "111111111111111111111111111111111111111111111111111111111111110" },
{ INT64_MIN + 2, "-111111111111111111111111111111111111111111111111111111111111110" },
{ INT64_MAX, "111111111111111111111111111111111111111111111111111111111111111" },
{ INT64_MIN + 1, "-111111111111111111111111111111111111111111111111111111111111111" },
{ INT64_MIN, "-1000000000000000000000000000000000000000000000000000000000000000" },
};
static const struct {
uint64_t value;
const char *result;
} utoa8_cases[] = {
{ 00, "0" },
{ 01, "1" },
{ 02, "2" },
{ 07, "7" },
{ 010, "10" },
{ 011, "11" },
{ 012, "12" },
{ 017, "17" },
{ 020, "20" },
{ 021, "21" },
{ 077, "77" },
{ 0100, "100" },
{ 0101, "101" },
{ 0177, "177" },
{ 0200, "200" },
{ 0201, "201" },
{ 0777, "777" },
{ 01000, "1000" },
{ 01001, "1001" },
{ 01777, "1777" },
{ 02000, "2000" },
{ 02001, "2001" },
{ 07777, "7777" },
{ 010000, "10000" },
{ 010001, "10001" },
{ UINT16_MAX, "177777" },
{ UINT16_MAX + 1, "200000" },
{ UINT32_MAX, "37777777777" },
{ (uint64_t)UINT32_MAX + 1, "40000000000" },
{ UINT64_MAX - 6, "1777777777777777777771" },
{ UINT64_MAX - 5, "1777777777777777777772" },
{ UINT64_MAX - 1, "1777777777777777777776" },
{ UINT64_MAX, "1777777777777777777777" },
};
static const struct {
int64_t value;
const char *result;
} itoa8_cases[] = {
{ 00, "0" },
{ 01, "1" },
{ -01, "-1" },
{ 02, "2" },
{ -02, "-2" },
{ 07, "7" },
{ -07, "-7" },
{ 010, "10" },
{ -010, "-10" },
{ 011, "11" },
{ -011, "-11" },
{ 012, "12" },
{ 017, "17" },
{ 020, "20" },
{ 021, "21" },
{ 077, "77" },
{ 0100, "100" },
{ 0101, "101" },
{ 0177, "177" },
{ 0200, "200" },
{ 0201, "201" },
{ 0777, "777" },
{ 01000, "1000" },
{ 01001, "1001" },
{ 01777, "1777" },
{ 02000, "2000" },
{ 02001, "2001" },
{ 07777, "7777" },
{ 010000, "10000" },
{ 010001, "10001" },
{ UINT16_MAX, "177777" },
{ UINT16_MAX + 1, "200000" },
{ UINT32_MAX, "37777777777" },
{ (uint64_t)UINT32_MAX + 1, "40000000000" },
{ INT64_MAX - 6, "777777777777777777771" },
{ INT64_MIN + 7, "-777777777777777777771" },
{ INT64_MAX - 5, "777777777777777777772" },
{ INT64_MIN + 6, "-777777777777777777772" },
{ INT64_MAX - 1, "777777777777777777776" },
{ INT64_MIN + 2, "-777777777777777777776" },
{ INT64_MAX, "777777777777777777777" },
{ INT64_MIN + 1, "-777777777777777777777" },
{ INT64_MIN, "-1000000000000000000000" },
};
static const struct {
uint64_t value;
const char *result;
} utoa10_cases[] = {
{ 0, "0" },
{ 1, "1" },
{ 2, "2" },
{ 9, "9" },
{ 10, "10" },
{ 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" },
{ (uint64_t)UINT32_MAX + 1, "4294967296" },
{ UINT64_MAX - 6, "18446744073709551609" },
{ UINT64_MAX - 5, "18446744073709551610" },
{ UINT64_MAX - 1, "18446744073709551614" },
{ 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" },
};
static const struct {
uint64_t value;
const char *result;
} utoa16_cases[] = {
{ 0x0, "0" }, { 0x1, "1" },
{ 0x2, "2" }, { 0x9, "9" },
{ 0xa, "a" }, { 0xb, "b" },
{ 0xc, "c" }, { 0xd, "d" },
{ 0xe, "e" }, { 0xf, "f" },
{ 0x10, "10" }, { 0x11, "11" },
{ 0x12, "12" }, { 0x19, "19" },
{ 0x1a, "1a" }, { 0x1b, "1b" },
{ 0x1c, "1c" }, { 0x1d, "1d" },
{ 0x1e, "1e" }, { 0x1f, "1f" },
{ 0x20, "20" }, { 0x21, "21" },
{ 0x22, "22" }, { 0x29, "29" },
{ 0x2a, "2a" }, { 0x2b, "2b" },
{ 0x2c, "2c" }, { 0x2d, "2d" },
{ 0x2e, "2e" }, { 0x2f, "2f" },
{ 0x90, "90" }, { 0x91, "91" },
{ 0x92, "92" }, { 0x99, "99" },
{ 0x9a, "9a" }, { 0x9b, "9b" },
{ 0x9c, "9c" }, { 0x9d, "9d" },
{ 0x9e, "9e" }, { 0x9f, "9f" },
{ 0xa0, "a0" }, { 0xa1, "a1" },
{ 0xa2, "a2" }, { 0xa9, "a9" },
{ 0xaa, "aa" }, { 0xab, "ab" },
{ 0xac, "ac" }, { 0xad, "ad" },
{ 0xae, "ae" }, { 0xaf, "af" },
{ 0xb0, "b0" }, { 0xb1, "b1" },
{ 0xb2, "b2" }, { 0xb9, "b9" },
{ 0xba, "ba" }, { 0xbb, "bb" },
{ 0xbc, "bc" }, { 0xbd, "bd" },
{ 0xbe, "be" }, { 0xbf, "bf" },
{ 0xc0, "c0" }, { 0xc1, "c1" },
{ 0xc2, "c2" }, { 0xc9, "c9" },
{ 0xca, "ca" }, { 0xcb, "cb" },
{ 0xcc, "cc" }, { 0xcd, "cd" },
{ 0xce, "ce" }, { 0xcf, "cf" },
{ 0xd0, "d0" }, { 0xd1, "d1" },
{ 0xd2, "d2" }, { 0xd9, "d9" },
{ 0xda, "da" }, { 0xdb, "db" },
{ 0xdc, "dc" }, { 0xdd, "dd" },
{ 0xde, "de" }, { 0xdf, "df" },
{ 0xe0, "e0" }, { 0xe1, "e1" },
{ 0xe2, "e2" }, { 0xe9, "e9" },
{ 0xea, "ea" }, { 0xeb, "eb" },
{ 0xec, "ec" }, { 0xed, "ed" },
{ 0xee, "ee" }, { 0xef, "ef" },
{ 0xf0, "f0" }, { 0xf1, "f1" },
{ 0xf2, "f2" }, { 0xf9, "f9" },
{ 0xfa, "fa" }, { 0xfb, "fb" },
{ 0xfc, "fc" }, { 0xfd, "fd" },
{ 0xfe, "fe" }, { 0xff, "ff" },
{ 0x100, "100" }, { 0x101, "101" },
{ 0x109, "109" }, { 0x10a, "10a" },
{ 0x10f, "10f" },
{ 0x110, "110" }, { 0x111, "111" },
{ 0x119, "119" }, { 0x11a, "11a" },
{ 0x11f, "11f" }, { 0x120, "120" },
{ 0x1ff, "1ff" }, { 0x200, "200" },
{ 0xfff, "fff" }, { 0x1000, "1000" },
{ UINT16_MAX, "ffff" },
{ UINT16_MAX + 1, "10000" },
{ UINT32_MAX, "ffffffff" },
{ (uint64_t)UINT32_MAX + 1, "100000000" },
{ UINT64_MAX - 6, "fffffffffffffff9" },
{ UINT64_MAX - 5, "fffffffffffffffa" },
{ UINT64_MAX - 1, "fffffffffffffffe" },
{ UINT64_MAX, "ffffffffffffffff" },
};
static const struct {
int64_t value;
const char *result;
} itoa16_cases[] = {
{ 0x0, "0" }, { 0x1, "1" },
{ 0x2, "2" }, { 0x9, "9" },
{ 0xa, "a" }, { 0xb, "b" },
{ 0xc, "c" }, { 0xd, "d" },
{ 0xe, "e" }, { 0xf, "f" },
{ 0x10, "10" }, { 0x11, "11" },
{ 0x12, "12" }, { 0x19, "19" },
{ 0x1a, "1a" }, { 0x1b, "1b" },
{ 0x1c, "1c" }, { 0x1d, "1d" },
{ 0x1e, "1e" }, { 0x1f, "1f" },
{ 0x20, "20" }, { 0x21, "21" },
{ 0x22, "22" }, { 0x29, "29" },
{ 0x2a, "2a" }, { 0x2b, "2b" },
{ 0x2c, "2c" }, { 0x2d, "2d" },
{ 0x2e, "2e" }, { 0x2f, "2f" },
{ 0x90, "90" }, { 0x91, "91" },
{ 0x92, "92" }, { 0x99, "99" },
{ 0x9a, "9a" }, { 0x9b, "9b" },
{ 0x9c, "9c" }, { 0x9d, "9d" },
{ 0x9e, "9e" }, { 0x9f, "9f" },
{ 0xa0, "a0" }, { 0xa1, "a1" },
{ 0xa2, "a2" }, { 0xa9, "a9" },
{ 0xaa, "aa" }, { 0xab, "ab" },
{ 0xac, "ac" }, { 0xad, "ad" },
{ 0xae, "ae" }, { 0xaf, "af" },
{ 0xb0, "b0" }, { 0xb1, "b1" },
{ 0xb2, "b2" }, { 0xb9, "b9" },
{ 0xba, "ba" }, { 0xbb, "bb" },
{ 0xbc, "bc" }, { 0xbd, "bd" },
{ 0xbe, "be" }, { 0xbf, "bf" },
{ 0xc0, "c0" }, { 0xc1, "c1" },
{ 0xc2, "c2" }, { 0xc9, "c9" },
{ 0xca, "ca" }, { 0xcb, "cb" },
{ 0xcc, "cc" }, { 0xcd, "cd" },
{ 0xce, "ce" }, { 0xcf, "cf" },
{ 0xd0, "d0" }, { 0xd1, "d1" },
{ 0xd2, "d2" }, { 0xd9, "d9" },
{ 0xda, "da" }, { 0xdb, "db" },
{ 0xdc, "dc" }, { 0xdd, "dd" },
{ 0xde, "de" }, { 0xdf, "df" },
{ 0xe0, "e0" }, { 0xe1, "e1" },
{ 0xe2, "e2" }, { 0xe9, "e9" },
{ 0xea, "ea" }, { 0xeb, "eb" },
{ 0xec, "ec" }, { 0xed, "ed" },
{ 0xee, "ee" }, { 0xef, "ef" },
{ 0xf0, "f0" }, { 0xf1, "f1" },
{ 0xf2, "f2" }, { 0xf9, "f9" },
{ 0xfa, "fa" }, { 0xfb, "fb" },
{ 0xfc, "fc" }, { 0xfd, "fd" },
{ 0xfe, "fe" }, { 0xff, "ff" },
{ 0x100, "100" }, { 0x101, "101" },
{ 0x109, "109" }, { 0x10a, "10a" },
{ 0x10f, "10f" },
{ 0x110, "110" }, { 0x111, "111" },
{ 0x119, "119" }, { 0x11a, "11a" },
{ 0x11f, "11f" }, { 0x120, "120" },
{ 0x1ff, "1ff" }, { 0x200, "200" },
{ 0xfff, "fff" }, { 0x1000, "1000" },
{ UINT16_MAX, "ffff" },
{ UINT16_MAX + 1, "10000" },
{ UINT32_MAX, "ffffffff" },
{ (uint64_t)UINT32_MAX + 1, "100000000" },
{ INT64_MIN + 7, "-7ffffffffffffff9" },
{ INT64_MAX - 6, "7ffffffffffffff9" },
{ INT64_MIN + 6, "-7ffffffffffffffa" },
{ INT64_MAX - 5, "7ffffffffffffffa" },
{ INT64_MIN + 2, "-7ffffffffffffffe" },
{ INT64_MAX - 1, "7ffffffffffffffe" },
{ INT64_MIN + 1, "-7fffffffffffffff" },
{ INT64_MAX, "7fffffffffffffff" },
{ INT64_MIN, "-8000000000000000" },
};
static const char *str_end(const char *str)
{
for (;; ++str) if (*str == '\0') return str;
}
void test_main()
{
{
char buffer[KERNAUX_UTOA_MIN_BUFFER_SIZE + 3];
for (
size_t index = 0;
index < sizeof(utoa_cases) / sizeof(utoa_cases[0]);
++index
) {
const char *const end1 = kernaux_utoa(
utoa_cases[index].value,
buffer,
utoa_cases[index].base,
NULL
);
assert(strcmp(buffer, utoa_cases[index].result) == 0);
assert(end1 == str_end(buffer));
const char *const end2 = kernaux_utoa(
utoa_cases[index].value,
buffer,
utoa_cases[index].base,
""
);
assert(strcmp(buffer, utoa_cases[index].result) == 0);
assert(end2 == str_end(buffer));
const char *const end3 = kernaux_utoa(
utoa_cases[index].value,
buffer,
utoa_cases[index].base,
"foo"
);
assert(strncmp(buffer, "foo", 3) == 0);
assert(strcmp(&buffer[3], utoa_cases[index].result) == 0);
assert(end3 == str_end(buffer));
}
}
{
char buffer[KERNAUX_ITOA_MIN_BUFFER_SIZE + 3];
for (
size_t index = 0;
index < sizeof(utoa_cases) / sizeof(utoa_cases[0]);
++index
) {
if (utoa_cases[index].value > (uint64_t)INT64_MAX) continue;
const int64_t value = utoa_cases[index].value;
const int base = utoa_cases[index].base;
const char *const end1 = kernaux_itoa(value, buffer, base, NULL);
assert(strcmp(buffer, utoa_cases[index].result) == 0);
assert(end1 == str_end(buffer));
const char *const end2 = kernaux_itoa(value, buffer, base, "");
assert(strcmp(buffer, utoa_cases[index].result) == 0);
assert(end2 == str_end(buffer));
const char *const end3 = kernaux_itoa(value, buffer, base, "foo");
assert(strncmp(buffer, "foo", 3) == 0);
assert(strcmp(&buffer[3], utoa_cases[index].result) == 0);
assert(end3 == str_end(buffer));
if (value <= 0 || base < 2 || base > 36) continue;
const char *const end4 = kernaux_itoa(-value, buffer, base, NULL);
assert(buffer[0] == '-');
assert(strcmp(&buffer[1], utoa_cases[index].result) == 0);
assert(end4 == str_end(buffer));
const char *const end5 = kernaux_itoa(-value, buffer, base, "");
assert(buffer[0] == '-');
assert(strcmp(&buffer[1], utoa_cases[index].result) == 0);
assert(end5 == str_end(buffer));
const char *const end6 = kernaux_itoa(-value, buffer, base, "foo");
assert(strncmp(buffer, "-foo", 4) == 0);
assert(strcmp(&buffer[4], utoa_cases[index].result) == 0);
assert(end6 == str_end(buffer));
}
}
{
char buffer[KERNAUX_UTOA2_BUFFER_SIZE];
for (
size_t index = 0;
index < sizeof(utoa2_cases) / sizeof(utoa2_cases[0]);
++index
) {
const char *const end1 =
kernaux_utoa2(utoa2_cases[index].value, buffer);
assert(strncmp(buffer, "0b", 2) == 0);
assert(strcmp(&buffer[2], utoa2_cases[index].result) == 0);
assert(end1 == str_end(buffer));
}
}
{
char buffer[KERNAUX_ITOA2_BUFFER_SIZE];
for (
size_t index = 0;
index < sizeof(itoa2_cases) / sizeof(itoa2_cases[0]);
++index
) {
const int64_t value = itoa2_cases[index].value;
const char *const end1 = kernaux_itoa2(value, buffer);
if (value >= 0) {
assert(strncmp(buffer, "0b", 2) == 0);
assert(strcmp(&buffer[2], itoa2_cases[index].result) == 0);
} else {
assert(strncmp(buffer, "-0b", 3) == 0);
assert(strcmp(&buffer[3], &itoa2_cases[index].result[1]) == 0);
}
assert(end1 == str_end(buffer));
if (value <= 0) continue;
const char *const end2 = kernaux_itoa2(-value, buffer);
assert(strncmp(buffer, "-0b", 3) == 0);
assert(strcmp(&buffer[3], itoa2_cases[index].result) == 0);
assert(end2 == str_end(buffer));
}
}
{
char buffer[KERNAUX_UTOA8_BUFFER_SIZE];
for (
size_t index = 0;
index < sizeof(utoa8_cases) / sizeof(utoa8_cases[0]);
++index
) {
const char *const end1 =
kernaux_utoa8(utoa8_cases[index].value, buffer);
assert(strncmp(buffer, "0o", 2) == 0);
assert(strcmp(&buffer[2], utoa8_cases[index].result) == 0);
assert(end1 == str_end(buffer));
}
}
{
char buffer[KERNAUX_ITOA8_BUFFER_SIZE];
for (
size_t index = 0;
index < sizeof(itoa8_cases) / sizeof(itoa8_cases[0]);
++index
) {
const int64_t value = itoa8_cases[index].value;
const char *const end1 = kernaux_itoa8(value, buffer);
if (value >= 0) {
assert(strncmp(buffer, "0o", 2) == 0);
assert(strcmp(&buffer[2], itoa8_cases[index].result) == 0);
} else {
assert(strncmp(buffer, "-0o", 3) == 0);
assert(strcmp(&buffer[3], &itoa8_cases[index].result[1]) == 0);
}
assert(end1 == str_end(buffer));
if (value <= 0) continue;
const char *const end2 = kernaux_itoa8(-value, buffer);
assert(strncmp(buffer, "-0o", 3) == 0);
assert(strcmp(&buffer[3], itoa8_cases[index].result) == 0);
assert(end2 == str_end(buffer));
}
}
{
char buffer[KERNAUX_UTOA10_BUFFER_SIZE];
for (
size_t index = 0;
index < sizeof(utoa10_cases) / sizeof(utoa10_cases[0]);
++index
) {
const char *const end1 =
kernaux_utoa10(utoa10_cases[index].value, buffer);
assert(strcmp(buffer, utoa10_cases[index].result) == 0);
assert(end1 == str_end(buffer));
}
}
{
char buffer[KERNAUX_ITOA10_BUFFER_SIZE];
for (
size_t index = 0;
index < sizeof(itoa10_cases) / sizeof(itoa10_cases[0]);
++index
) {
const int64_t value = itoa10_cases[index].value;
const char *const end1 = kernaux_itoa10(value, buffer);
assert(strcmp(buffer, itoa10_cases[index].result) == 0);
assert(end1 == str_end(buffer));
if (value <= 0) continue;
const char *const end2 = kernaux_itoa10(-value, buffer);
assert(buffer[0] == '-');
assert(strcmp(&buffer[1], itoa10_cases[index].result) == 0);
assert(end2 == str_end(buffer));
}
}
{
char buffer[KERNAUX_UTOA16_BUFFER_SIZE];
for (
size_t index = 0;
index < sizeof(utoa16_cases) / sizeof(utoa16_cases[0]);
++index
) {
const char *const end1 =
kernaux_utoa16(utoa16_cases[index].value, buffer);
assert(strncmp(buffer, "0x", 2) == 0);
assert(strcmp(&buffer[2], utoa16_cases[index].result) == 0);
assert(end1 == str_end(buffer));
}
}
{
char buffer[KERNAUX_ITOA16_BUFFER_SIZE];
for (
size_t index = 0;
index < sizeof(itoa16_cases) / sizeof(itoa16_cases[0]);
++index
) {
const int64_t value = itoa16_cases[index].value;
const char *const end1 = kernaux_itoa16(value, buffer);
if (value >= 0) {
assert(strncmp(buffer, "0x", 2) == 0);
assert(strcmp(&buffer[2], itoa16_cases[index].result) == 0);
} else {
assert(strncmp(buffer, "-0x", 3) == 0);
assert(strcmp(&buffer[3], &itoa16_cases[index].result[1]) == 0);
}
assert(end1 == str_end(buffer));
if (value <= 0) continue;
const char *const end2 = kernaux_itoa16(-value, buffer);
assert(strncmp(buffer, "-0x", 3) == 0);
assert(strcmp(&buffer[3], itoa16_cases[index].result) == 0);
assert(end2 == str_end(buffer));
}
}
}

View File

@ -1,154 +0,0 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <kernaux/ntoa.h>
#include <kernaux/runtime.h>
#include <assert.h>
#include <setjmp.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#define VALID_LONG_PREFIX "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
#define TOO_LONG_PREFIX "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
static jmp_buf jmpbuf;
static unsigned int assert_count_exp = 0;
static unsigned int assert_count_ctr = 0;
static const char *assert_last_file = NULL;
static int assert_last_line = 0;
static const char *assert_last_msg = NULL;
static void assert_cb(
const char *const file,
const int line,
const char *const msg
) {
++assert_count_ctr;
assert_last_file = file;
assert_last_line = line;
assert_last_msg = msg;
longjmp(jmpbuf, 1);
}
static void test_utoa_assert(char *const buffer, const int base)
{
if (setjmp(jmpbuf) == 0) {
kernaux_utoa(0, buffer, base, NULL);
} else {
// cppcheck-suppress assignmentInAssert
assert(assert_count_ctr == ++assert_count_exp);
assert(strstr(assert_last_file, "src/ntoa.c") != NULL);
assert(assert_last_line != 0);
assert(assert_last_msg != NULL);
assert_last_file = NULL;
assert_last_line = 0;
assert_last_msg = NULL;
}
}
static void test_itoa_assert(char *const buffer, const int base)
{
if (setjmp(jmpbuf) == 0) {
kernaux_itoa(0, buffer, base, NULL);
} else {
// cppcheck-suppress assignmentInAssert
assert(assert_count_ctr == ++assert_count_exp);
assert(strstr(assert_last_file, "src/ntoa.c") != NULL);
assert(assert_last_line != 0);
assert(assert_last_msg != NULL);
assert_last_file = NULL;
assert_last_line = 0;
assert_last_msg = NULL;
}
}
static const char *str_end(const char *str)
{
for (;; ++str) if (*str == '\0') return str;
}
void test_main()
{
kernaux_assert_cb = assert_cb;
if (setjmp(jmpbuf) != 0) abort();
{
char buffer[KERNAUX_UTOA_MIN_BUFFER_SIZE + KERNAUX_NTOA_MAX_PREFIX_LEN];
const char *const end1 =
kernaux_utoa(123, buffer, 'd', VALID_LONG_PREFIX);
assert(strcmp(buffer, VALID_LONG_PREFIX"123") == 0);
assert(end1 == str_end(buffer));
assert(assert_count_ctr == assert_count_exp);
assert(assert_last_file == NULL);
assert(assert_last_line == 0);
assert(assert_last_msg == NULL);
if (setjmp(jmpbuf) == 0) {
kernaux_utoa(123, buffer, 'd', TOO_LONG_PREFIX);
} else {
assert(strcmp(buffer, VALID_LONG_PREFIX) == 0);
// cppcheck-suppress assignmentInAssert
assert(assert_count_ctr == ++assert_count_exp);
assert(strstr(assert_last_file, "src/ntoa.c") != NULL);
assert(assert_last_line != 0);
assert(strcmp(assert_last_msg, "prefix is too long") == 0);
assert_last_file = NULL;
assert_last_line = 0;
assert_last_msg = NULL;
}
test_utoa_assert(NULL, 'd');
test_utoa_assert(buffer, 0);
test_utoa_assert(buffer, 1);
test_utoa_assert(buffer, -1);
test_utoa_assert(buffer, 37);
test_utoa_assert(buffer, -37);
}
if (setjmp(jmpbuf) != 0) abort();
{
char buffer[KERNAUX_ITOA_MIN_BUFFER_SIZE + KERNAUX_NTOA_MAX_PREFIX_LEN];
const char *const end1 =
kernaux_itoa(123, buffer, 'd', VALID_LONG_PREFIX);
assert(strcmp(buffer, VALID_LONG_PREFIX"123") == 0);
assert(end1 == str_end(buffer));
assert(assert_count_ctr == assert_count_exp);
assert(assert_last_file == NULL);
assert(assert_last_line == 0);
assert(assert_last_msg == NULL);
if (setjmp(jmpbuf) == 0) {
kernaux_itoa(123, buffer, 'd', TOO_LONG_PREFIX);
} else {
assert(strcmp(buffer, VALID_LONG_PREFIX) == 0);
// cppcheck-suppress assignmentInAssert
assert(assert_count_ctr == ++assert_count_exp);
assert(strstr(assert_last_file, "src/ntoa.c") != NULL);
assert(assert_last_line != 0);
assert(strcmp(assert_last_msg, "prefix is too long") == 0);
assert_last_file = NULL;
assert_last_line = 0;
assert_last_msg = NULL;
}
test_itoa_assert(NULL, 'd');
test_itoa_assert(buffer, 0);
test_itoa_assert(buffer, 1);
test_itoa_assert(buffer, -1);
test_itoa_assert(buffer, 37);
test_itoa_assert(buffer, -37);
}
}

View File

@ -1,169 +0,0 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <kernaux/units.h>
#include <assert.h>
#include <stdint.h>
#include <string.h>
static void test_raw(
unsigned long long value,
enum KernAux_Unit unit,
const char *result
);
static void test_dec(
unsigned long long value,
enum KernAux_Unit unit,
enum KernAux_UnitPrefixDec prefix,
const char *result
);
static void test_bin(
unsigned long long value,
enum KernAux_Unit unit,
enum KernAux_UnitPrefixBin prefix,
const char *result
);
void test_main()
{
test_raw(0, KERNAUX_UNIT_BIT, "0 bit");
test_raw(0, KERNAUX_UNIT_BYTE, "0 Byte");
test_raw(1, KERNAUX_UNIT_BIT, "1 bit");
test_raw(1, KERNAUX_UNIT_BYTE, "1 Byte");
test_raw(2, KERNAUX_UNIT_BIT, "2 bit");
test_raw(2, KERNAUX_UNIT_BYTE, "2 Byte");
test_raw(10, KERNAUX_UNIT_BIT, "10 bit");
test_raw(10, KERNAUX_UNIT_BYTE, "10 Byte");
test_raw(UINT8_MAX, KERNAUX_UNIT_BIT, "255 bit");
test_raw(UINT8_MAX, KERNAUX_UNIT_BYTE, "255 Byte");
test_raw(UINT16_MAX, KERNAUX_UNIT_BIT, "65535 bit");
test_raw(UINT16_MAX, KERNAUX_UNIT_BYTE, "65535 Byte");
test_raw(UINT32_MAX, KERNAUX_UNIT_BIT, "4294967295 bit");
test_raw(UINT32_MAX, KERNAUX_UNIT_BYTE, "4294967295 Byte");
test_raw(UINT64_MAX, KERNAUX_UNIT_BIT, "18446744073709551615 bit");
test_raw(UINT64_MAX, KERNAUX_UNIT_BYTE, "18446744073709551615 Byte");
test_dec(0, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_KILO, "0 kbit");
test_dec(0, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_KILO, "0 kB");
test_dec(1, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_KILO, "1 kbit");
test_dec(1, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_KILO, "1 kB");
test_dec(2, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_KILO, "2 kbit");
test_dec(2, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_KILO, "2 kB");
test_dec(UINT8_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_KILO, "255 kbit");
test_dec(UINT8_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_KILO, "255 kB");
test_dec(UINT16_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_KILO, "65535 kbit");
test_dec(UINT16_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_KILO, "65535 kB");
test_dec(UINT32_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_KILO, "4294967295 kbit");
test_dec(UINT32_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_KILO, "4294967295 kB");
test_dec(UINT64_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_KILO, "18446744073709551615 kbit");
test_dec(UINT64_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_KILO, "18446744073709551615 kB");
test_dec(0, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_MEGA, "0 Mbit");
test_dec(0, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_MEGA, "0 MB");
test_dec(1, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_MEGA, "1 Mbit");
test_dec(1, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_MEGA, "1 MB");
test_dec(2, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_MEGA, "2 Mbit");
test_dec(2, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_MEGA, "2 MB");
test_dec(UINT8_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_MEGA, "255 Mbit");
test_dec(UINT8_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_MEGA, "255 MB");
test_dec(UINT16_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_MEGA, "65535 Mbit");
test_dec(UINT16_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_MEGA, "65535 MB");
test_dec(UINT32_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_MEGA, "4294967295 Mbit");
test_dec(UINT32_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_MEGA, "4294967295 MB");
test_dec(UINT64_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_MEGA, "18446744073709551615 Mbit");
test_dec(UINT64_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_MEGA, "18446744073709551615 MB");
test_dec(0, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_GIGA, "0 Gbit");
test_dec(0, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_GIGA, "0 GB");
test_dec(1, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_GIGA, "1 Gbit");
test_dec(1, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_GIGA, "1 GB");
test_dec(2, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_GIGA, "2 Gbit");
test_dec(2, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_GIGA, "2 GB");
test_dec(UINT8_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_GIGA, "255 Gbit");
test_dec(UINT8_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_GIGA, "255 GB");
test_dec(UINT16_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_GIGA, "65535 Gbit");
test_dec(UINT16_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_GIGA, "65535 GB");
test_dec(UINT32_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_GIGA, "4294967295 Gbit");
test_dec(UINT32_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_GIGA, "4294967295 GB");
test_dec(UINT64_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_GIGA, "18446744073709551615 Gbit");
test_dec(UINT64_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_GIGA, "18446744073709551615 GB");
test_bin(0, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_KIBI, "0 Kibit");
test_bin(0, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_KIBI, "0 KiB");
test_bin(1, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_KIBI, "1 Kibit");
test_bin(1, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_KIBI, "1 KiB");
test_bin(2, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_KIBI, "2 Kibit");
test_bin(2, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_KIBI, "2 KiB");
test_bin(UINT8_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_KIBI, "255 Kibit");
test_bin(UINT8_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_KIBI, "255 KiB");
test_bin(UINT16_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_KIBI, "65535 Kibit");
test_bin(UINT16_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_KIBI, "65535 KiB");
test_bin(UINT32_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_KIBI, "4294967295 Kibit");
test_bin(UINT32_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_KIBI, "4294967295 KiB");
test_bin(UINT64_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_KIBI, "18446744073709551615 Kibit");
test_bin(UINT64_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_KIBI, "18446744073709551615 KiB");
test_bin(0, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_MEBI, "0 Mibit");
test_bin(0, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_MEBI, "0 MiB");
test_bin(1, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_MEBI, "1 Mibit");
test_bin(1, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_MEBI, "1 MiB");
test_bin(2, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_MEBI, "2 Mibit");
test_bin(2, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_MEBI, "2 MiB");
test_bin(UINT8_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_MEBI, "255 Mibit");
test_bin(UINT8_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_MEBI, "255 MiB");
test_bin(UINT16_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_MEBI, "65535 Mibit");
test_bin(UINT16_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_MEBI, "65535 MiB");
test_bin(UINT32_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_MEBI, "4294967295 Mibit");
test_bin(UINT32_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_MEBI, "4294967295 MiB");
test_bin(UINT64_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_MEBI, "18446744073709551615 Mibit");
test_bin(UINT64_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_MEBI, "18446744073709551615 MiB");
test_bin(0, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_GIBI, "0 Gibit");
test_bin(0, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_GIBI, "0 GiB");
test_bin(1, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_GIBI, "1 Gibit");
test_bin(1, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_GIBI, "1 GiB");
test_bin(2, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_GIBI, "2 Gibit");
test_bin(2, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_GIBI, "2 GiB");
test_bin(UINT8_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_GIBI, "255 Gibit");
test_bin(UINT8_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_GIBI, "255 GiB");
test_bin(UINT16_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_GIBI, "65535 Gibit");
test_bin(UINT16_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_GIBI, "65535 GiB");
test_bin(UINT32_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_GIBI, "4294967295 Gibit");
test_bin(UINT32_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_GIBI, "4294967295 GiB");
test_bin(UINT64_MAX, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_GIBI, "18446744073709551615 Gibit");
test_bin(UINT64_MAX, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_GIBI, "18446744073709551615 GiB");
}
void test_raw(
const unsigned long long value,
const enum KernAux_Unit unit,
const char *const result
) {
char buffer[64];
assert(kernaux_units_human_raw(value, unit, buffer, sizeof(buffer)));
assert(strcmp(buffer, result) == 0);
}
void test_dec(
const unsigned long long value,
const enum KernAux_Unit unit,
const enum KernAux_UnitPrefixDec prefix,
const char *const result
) {
char buffer[64];
assert(kernaux_units_human_dec(value, unit, prefix, buffer, sizeof(buffer)));
assert(strcmp(buffer, result) == 0);
}
void test_bin(
const unsigned long long value,
const enum KernAux_Unit unit,
const enum KernAux_UnitPrefixBin prefix,
const char *const result
) {
char buffer[64];
assert(kernaux_units_human_bin(value, unit, prefix, buffer, sizeof(buffer)));
assert(strcmp(buffer, result) == 0);
}