0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
|
// test_strtou.c
// String conversion unit test for libutil
// Copyright (C) 2021, Jakob Wakeling
// MIT Licence
#include "../strconv.h"
#include "unit.h"
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
int main(void) {
/* STRTOU8 */
{
char i[] = "0";
ASSERT("T000 STRTOU8 ZERO BASE 0",
strtou8(i, NULL, 0) == 0);
ASSERT("T001 STRTOU8 ZERO BASE 8",
strtou8(i, NULL, 8) == 0);
}
{
char i[] = "64";
ASSERT("T002 STRTOU8 NORMAL BASE 0",
strtou8(i, NULL, 0) == 64);
ASSERT("T003 STRTOU8 NORMAL BASE 8",
strtou8(i, NULL, 8) == 52);
ASSERT("T004 STRTOU8 NORMAL BASE 10",
strtou8(i, NULL, 10) == 64);
ASSERT("T005 STRTOU8 NORMAL BASE 16",
strtou8(i, NULL, 16) == 100);
}
{
char i[] = "3M";
ASSERT("T006 STRTOU8 NORMAL BASE 36",
strtou8(i, NULL, 36) == 130u);
}
{
char i8[] = "0377";
char i10[] = "255";
char i16[] = "0xFF";
errno = 0;
ASSERT("T007 STRTOU8 MAX BASE 0",
strtou8(i8, NULL, 0) == UINT8_MAX && errno != ERANGE);
errno = 0;
ASSERT("T008 STRTOU8 MAX BASE 8",
strtou8(i8, NULL, 8) == UINT8_MAX && errno != ERANGE);
errno = 0;
ASSERT("T009 STRTOU8 MAX BASE 10",
strtou8(i10, NULL, 10) == UINT8_MAX && errno != ERANGE);
errno = 0;
ASSERT("T010 STRTOU8 MAX BASE 16",
strtou8(i16, NULL, 16) == UINT8_MAX && errno != ERANGE);
}
{
char i[] = "-153";
errno = 0;
ASSERT("T011 STRTOU8 NEGATIVE",
!strtou8(i, NULL, 10) && errno == EINVAL);
errno = 0;
ASSERT("T012 STRTOU8 BASE 1",
!strtou8(i, NULL, 1) && errno == EINVAL);
errno = 0;
ASSERT("T013 STRTOU8 BASE -1",
!strtou8(i, NULL, -1) && errno == EINVAL);
errno = 0;
ASSERT("T014 STRTOU8 BASE 37",
!strtou8(i, NULL, 37) && errno == EINVAL);
}
{
char i[] = "257";
errno = 0;
ASSERT("T015 STRTOU8 OVERFLOW",
strtou8(i, NULL, 10) == UINT8_MAX && errno == ERANGE);
}
/* STRTOU16 */
{
char i[] = "0";
ASSERT("T100 STRTOU16 ZERO BASE 0",
strtou16(i, NULL, 0) == 0);
ASSERT("T101 STRTOU16 ZERO BASE 8",
strtou16(i, NULL, 8) == 0);
}
{
char i[] = "64";
ASSERT("T102 STRTOU16 NORMAL BASE 0",
strtou16(i, NULL, 0) == 64);
ASSERT("T103 STRTOU16 NORMAL BASE 8",
strtou16(i, NULL, 8) == 52);
ASSERT("T104 STRTOU16 NORMAL BASE 10",
strtou16(i, NULL, 10) == 64);
ASSERT("T105 STRTOU16 NORMAL BASE 16",
strtou16(i, NULL, 16) == 100);
}
{
char i[] = "AZ1";
ASSERT("T106 STRTOU16 NORMAL BASE 36",
strtou16(i, NULL, 36) == 14221u);
}
{
char i8[] = "0177777";
char i10[] = "65535";
char i16[] = "0xFFFF";
errno = 0;
ASSERT("T107 STRTOU16 MAX BASE 0",
strtou16(i8, NULL, 0) == UINT16_MAX && errno != ERANGE);
errno = 0;
ASSERT("T108 STRTOU16 MAX BASE 8",
strtou16(i8, NULL, 8) == UINT16_MAX && errno != ERANGE);
errno = 0;
ASSERT("T109 STRTOU16 MAX BASE 10",
strtou16(i10, NULL, 10) == UINT16_MAX && errno != ERANGE);
errno = 0;
ASSERT("T110 STRTOU16 MAX BASE 16",
strtou16(i16, NULL, 16) == UINT16_MAX && errno != ERANGE);
}
{
char i[] = "-153";
errno = 0;
ASSERT("T111 STRTOU16 NEGATIVE",
!strtou16(i, NULL, 10) && errno == EINVAL);
errno = 0;
ASSERT("T112 STRTOU16 BASE 1",
!strtou16(i, NULL, 1) && errno == EINVAL);
errno = 0;
ASSERT("T113 STRTOU16 BASE -1",
!strtou16(i, NULL, -1) && errno == EINVAL);
errno = 0;
ASSERT("T114 STRTOU16 BASE 37",
!strtou16(i, NULL, 37) && errno == EINVAL);
}
{
char i[] = "65537";
errno = 0;
ASSERT("T115 STRTOU16 OVERFLOW",
strtou16(i, NULL, 10) == UINT16_MAX && errno == ERANGE);
}
/* STRTOU32 */
{
char i[] = "0";
ASSERT("T200 STRTOU32 ZERO BASE 0",
strtou32(i, NULL, 0) == 0);
ASSERT("T201 STRTOU32 ZERO BASE 8",
strtou32(i, NULL, 8) == 0);
}
{
char i[] = "64";
ASSERT("T202 STRTOU32 NORMAL BASE 0",
strtou32(i, NULL, 0) == 64);
ASSERT("T203 STRTOU32 NORMAL BASE 8",
strtou32(i, NULL, 8) == 52);
ASSERT("T204 STRTOU32 NORMAL BASE 10",
strtou32(i, NULL, 10) == 64);
ASSERT("T205 STRTOU32 NORMAL BASE 16",
strtou32(i, NULL, 16) == 100);
}
{
char i[] = "T7A7J";
ASSERT("T206 STRTOU32 NORMAL BASE 36",
strtou32(i, NULL, 36) == 49048687u);
}
{
char i8[] = "037777777777";
char i10[] = "4294967295";
char i16[] = "0xFFFFFFFF";
errno = 0;
ASSERT("T207 STRTOU32 MAX BASE 0",
strtou32(i8, NULL, 0) == UINT32_MAX && errno != ERANGE);
errno = 0;
ASSERT("T208 STRTOU32 MAX BASE 8",
strtou32(i8, NULL, 8) == UINT32_MAX && errno != ERANGE);
errno = 0;
ASSERT("T209 STRTOU32 MAX BASE 10",
strtou32(i10, NULL, 10) == UINT32_MAX && errno != ERANGE);
errno = 0;
ASSERT("T210 STRTOU32 MAX BASE 16",
strtou32(i16, NULL, 16) == UINT32_MAX && errno != ERANGE);
}
{
char i[] = "-153";
errno = 0;
ASSERT("T211 STRTOU32 NEGATIVE",
!strtou32(i, NULL, 10) && errno == EINVAL);
errno = 0;
ASSERT("T212 STRTOU32 BASE 1",
!strtou32(i, NULL, 1) && errno == EINVAL);
errno = 0;
ASSERT("T213 STRTOU32 BASE -1",
!strtou32(i, NULL, -1) && errno == EINVAL);
errno = 0;
ASSERT("T214 STRTOU32 BASE 37",
!strtou32(i, NULL, 37) && errno == EINVAL);
}
{
char i[] = "4294967297";
errno = 0;
ASSERT("T215 STRTOU32 OVERFLOW",
strtou32(i, NULL, 10) == UINT32_MAX && errno == ERANGE);
}
/* STRTOU64 */
{
char i[] = "0";
ASSERT("T300 STRTOU64 ZERO BASE 0",
strtou64(i, NULL, 0) == 0);
ASSERT("T301 STRTOU64 ZERO BASE 8",
strtou64(i, NULL, 8) == 0);
}
{
char i[] = "64";
ASSERT("T302 STRTOU64 NORMAL BASE 0",
strtou64(i, NULL, 0) == 64);
ASSERT("T303 STRTOU64 NORMAL BASE 8",
strtou64(i, NULL, 8) == 52);
ASSERT("T304 STRTOU64 NORMAL BASE 10",
strtou64(i, NULL, 10) == 64);
ASSERT("T305 STRTOU64 NORMAL BASE 16",
strtou64(i, NULL, 16) == 100);
}
{
char i[] = "3579ADGJMPSVZ";
ASSERT("T306 STRTOU64 NORMAL BASE 36",
strtou64(i, NULL, 36) == 14899788948715380863u);
}
{
char i8[] = "01777777777777777777777";
char i10[] = "18446744073709551615";
char i16[] = "0xFFFFFFFFFFFFFFFF";
errno = 0;
ASSERT("T307 STRTOU64 MAX BASE 0",
strtou64(i8, NULL, 0) == UINT64_MAX && errno != ERANGE);
errno = 0;
ASSERT("T308 STRTOU64 MAX BASE 8",
strtou64(i8, NULL, 8) == UINT64_MAX && errno != ERANGE);
errno = 0;
ASSERT("T309 STRTOU64 MAX BASE 10",
strtou64(i10, NULL, 10) == UINT64_MAX && errno != ERANGE);
errno = 0;
ASSERT("T310 STRTOU64 MAX BASE 16",
strtou64(i16, NULL, 16) == UINT64_MAX && errno != ERANGE);
}
{
char i[] = "-153";
errno = 0;
ASSERT("T311 STRTOU64 NEGATIVE",
!strtou64(i, NULL, 10) && errno == EINVAL);
errno = 0;
ASSERT("T312 STRTOU64 BASE 1",
!strtou64(i, NULL, 1) && errno == EINVAL);
errno = 0;
ASSERT("T313 STRTOU64 BASE -1",
!strtou64(i, NULL, -1) && errno == EINVAL);
errno = 0;
ASSERT("T314 STRTOU64 BASE 37",
!strtou64(i, NULL, 37) && errno == EINVAL);
}
{
char i[] = "18446744073709551616";
errno = 0;
ASSERT("T315 STRTOU64 OVERFLOW",
strtou64(i, NULL, 10) == UINT64_MAX && errno == ERANGE);
}
printf("%d of %d tests passed\n", testspassed, testsrun);
return testsfailed;
}
|