|
|
#include "../../unity/unity.h" |
|
|
|
|
|
#include <stdlib.h> |
|
|
#include <string.h> |
|
|
#include <limits.h> |
|
|
#include <inttypes.h> |
|
|
#include <stdint.h> |
|
|
#include <stdio.h> |
|
|
#include <errno.h> |
|
|
|
|
|
|
|
|
|
|
|
static void assert_parse_ok(const char *s, intmax_t expected) |
|
|
{ |
|
|
strtol_error err = (strtol_error)0xDEADBEEF; |
|
|
intmax_t r = parse_integer(s, &err); |
|
|
TEST_ASSERT_EQUAL_INT_MESSAGE(LONGINT_OK, err, "Expected LONGINT_OK"); |
|
|
TEST_ASSERT_TRUE_MESSAGE(r == expected, "Returned value mismatch"); |
|
|
} |
|
|
|
|
|
static void assert_parse_err_bits(const char *s, int err_mask) |
|
|
{ |
|
|
strtol_error err = LONGINT_OK; |
|
|
(void)parse_integer(s, &err); |
|
|
TEST_ASSERT_TRUE_MESSAGE((err & err_mask) != 0, "Expected error bits not set"); |
|
|
} |
|
|
|
|
|
void setUp(void) { |
|
|
|
|
|
} |
|
|
|
|
|
void tearDown(void) { |
|
|
|
|
|
} |
|
|
|
|
|
void test_parse_integer_basic_and_B_suffix(void) |
|
|
{ |
|
|
assert_parse_ok("0", 0); |
|
|
assert_parse_ok("123", 123); |
|
|
|
|
|
|
|
|
assert_parse_ok("10B", 10); |
|
|
|
|
|
|
|
|
strtol_error err = LONGINT_OK; |
|
|
intmax_t r = parse_integer("1BB", &err); |
|
|
TEST_ASSERT_TRUE(r == 1); |
|
|
TEST_ASSERT_TRUE((err & LONGINT_INVALID_SUFFIX_CHAR) != 0); |
|
|
} |
|
|
|
|
|
void test_parse_integer_multiplicative_x(void) |
|
|
{ |
|
|
assert_parse_ok("2x3", 6); |
|
|
|
|
|
|
|
|
assert_parse_err_bits("2x", LONGINT_INVALID); |
|
|
} |
|
|
|
|
|
void test_parse_integer_chain_multiplication(void) |
|
|
{ |
|
|
assert_parse_ok("2x3x4", 24); |
|
|
} |
|
|
|
|
|
void test_parse_integer_suffix_variants(void) |
|
|
{ |
|
|
|
|
|
assert_parse_ok("1c", 1); |
|
|
assert_parse_ok("1w", 2); |
|
|
assert_parse_ok("1b", 512); |
|
|
assert_parse_ok("1K", 1024); |
|
|
assert_parse_ok("1kB", 1000); |
|
|
} |
|
|
|
|
|
void test_parse_integer_overflow_simple(void) |
|
|
{ |
|
|
|
|
|
char buf[128]; |
|
|
uintmax_t big = (uintmax_t)INTMAX_MAX + 1; |
|
|
snprintf(buf, sizeof buf, "% |