129 lines
3.5 KiB
C
129 lines
3.5 KiB
C
|
|
/*
|
||
|
|
* String -> number wrappers: aksl_atoi / atol / atoll / atof.
|
||
|
|
*
|
||
|
|
* TODO.md section 1.4. This file covers the parts of the contract that are
|
||
|
|
* stable today: the happy paths (including negatives and leading whitespace)
|
||
|
|
* and the NULL guards.
|
||
|
|
*
|
||
|
|
* It deliberately says nothing about non-numeric input, trailing junk or
|
||
|
|
* overflow. Those all return *success* today (TODO.md 2.1.5) and the correct
|
||
|
|
* behaviour is asserted by tests/test_convert_strict.c, which is registered as
|
||
|
|
* a known failure. Pinning the current lax behaviour here as well would mean
|
||
|
|
* this file starts failing on the day the defect is fixed.
|
||
|
|
*/
|
||
|
|
|
||
|
|
#include "aksl_capture.h"
|
||
|
|
|
||
|
|
static int test_atoi_converts_positive(void)
|
||
|
|
{
|
||
|
|
int out = -1;
|
||
|
|
|
||
|
|
AKSL_CHECK_OK(aksl_atoi("1234", &out));
|
||
|
|
AKSL_CHECK(out == 1234);
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static int test_atoi_converts_negative(void)
|
||
|
|
{
|
||
|
|
int out = 0;
|
||
|
|
|
||
|
|
AKSL_CHECK_OK(aksl_atoi("-42", &out));
|
||
|
|
AKSL_CHECK(out == -42);
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static int test_atoi_skips_leading_whitespace(void)
|
||
|
|
{
|
||
|
|
int out = 0;
|
||
|
|
|
||
|
|
AKSL_CHECK_OK(aksl_atoi(" \t 7", &out));
|
||
|
|
AKSL_CHECK(out == 7);
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static int test_atoi_rejects_null_arguments(void)
|
||
|
|
{
|
||
|
|
int out = 0;
|
||
|
|
|
||
|
|
AKSL_CHECK_STATUS_MSG_CONTAINS(aksl_atoi(NULL, &out),
|
||
|
|
AKERR_NULLPOINTER, "nptr=");
|
||
|
|
AKSL_CHECK_STATUS_MSG_CONTAINS(aksl_atoi("1", NULL),
|
||
|
|
AKERR_NULLPOINTER, "dest=");
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static int test_atol_converts_and_rejects_null(void)
|
||
|
|
{
|
||
|
|
long out = 0;
|
||
|
|
|
||
|
|
AKSL_CHECK_OK(aksl_atol("2147483648", &out));
|
||
|
|
AKSL_CHECK(out == 2147483648L);
|
||
|
|
AKSL_CHECK_OK(aksl_atol("-2147483648", &out));
|
||
|
|
AKSL_CHECK(out == -2147483648L);
|
||
|
|
|
||
|
|
AKSL_CHECK_STATUS(aksl_atol(NULL, &out), AKERR_NULLPOINTER);
|
||
|
|
AKSL_CHECK_STATUS(aksl_atol("1", NULL), AKERR_NULLPOINTER);
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static int test_atoll_converts_and_rejects_null(void)
|
||
|
|
{
|
||
|
|
long long out = 0;
|
||
|
|
|
||
|
|
AKSL_CHECK_OK(aksl_atoll("9007199254740993", &out));
|
||
|
|
AKSL_CHECK(out == 9007199254740993LL);
|
||
|
|
AKSL_CHECK_OK(aksl_atoll("-9007199254740993", &out));
|
||
|
|
AKSL_CHECK(out == -9007199254740993LL);
|
||
|
|
|
||
|
|
AKSL_CHECK_STATUS(aksl_atoll(NULL, &out), AKERR_NULLPOINTER);
|
||
|
|
AKSL_CHECK_STATUS(aksl_atoll("1", NULL), AKERR_NULLPOINTER);
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static int test_atof_converts_and_rejects_null(void)
|
||
|
|
{
|
||
|
|
double out = 0.0;
|
||
|
|
|
||
|
|
AKSL_CHECK_OK(aksl_atof("2.5", &out));
|
||
|
|
AKSL_CHECK(out == 2.5);
|
||
|
|
AKSL_CHECK_OK(aksl_atof(" -0.125", &out));
|
||
|
|
AKSL_CHECK(out == -0.125);
|
||
|
|
|
||
|
|
AKSL_CHECK_STATUS(aksl_atof(NULL, &out), AKERR_NULLPOINTER);
|
||
|
|
AKSL_CHECK_STATUS(aksl_atof("1", NULL), AKERR_NULLPOINTER);
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
/* The wrappers hold no state: the same input converts the same way twice. */
|
||
|
|
static int test_conversions_are_repeatable(void)
|
||
|
|
{
|
||
|
|
int first = 0;
|
||
|
|
int second = 0;
|
||
|
|
|
||
|
|
AKSL_CHECK_OK(aksl_atoi("321", &first));
|
||
|
|
AKSL_CHECK_OK(aksl_atoi("321", &second));
|
||
|
|
AKSL_CHECK(first == second);
|
||
|
|
AKSL_CHECK(first == 321);
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
int main(void)
|
||
|
|
{
|
||
|
|
int failures = 0;
|
||
|
|
|
||
|
|
akerr_init();
|
||
|
|
|
||
|
|
AKSL_RUN(failures, test_atoi_converts_positive);
|
||
|
|
AKSL_RUN(failures, test_atoi_converts_negative);
|
||
|
|
AKSL_RUN(failures, test_atoi_skips_leading_whitespace);
|
||
|
|
AKSL_RUN(failures, test_atoi_rejects_null_arguments);
|
||
|
|
|
||
|
|
AKSL_RUN(failures, test_atol_converts_and_rejects_null);
|
||
|
|
AKSL_RUN(failures, test_atoll_converts_and_rejects_null);
|
||
|
|
AKSL_RUN(failures, test_atof_converts_and_rejects_null);
|
||
|
|
|
||
|
|
AKSL_RUN(failures, test_conversions_are_repeatable);
|
||
|
|
|
||
|
|
AKSL_REPORT(failures);
|
||
|
|
}
|