File size: 2,185 Bytes
78d2150 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
#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>
/* parse_integer is defined in dd.c and this file is included after it. */
static void assert_parse_ok(const char *s, intmax_t expected)
{
strtol_error err = (strtol_error)0xDEADBEEF; /* sentinel */
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) {
/* no-op */
}
void tearDown(void) {
/* no-op */
}
void test_parse_integer_basic_and_B_suffix(void)
{
assert_parse_ok("0", 0);
assert_parse_ok("123", 123);
/* Trailing 'B' is accepted as a flag (not a multiplier) */
assert_parse_ok("10B", 10);
/* 'BB' is invalid: only a single trailing B is allowed */
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);
/* Missing RHS after 'x' should be invalid */
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)
{
/* Common suffixes parsed by xstrtoumax according to dd semantics */
assert_parse_ok("1c", 1); /* c = 1 */
assert_parse_ok("1w", 2); /* w = 2 */
assert_parse_ok("1b", 512); /* b = 512 */
assert_parse_ok("1K", 1024); /* K = 1024 */
assert_parse_ok("1kB", 1000); /* kB = 1000 */
}
void test_parse_integer_overflow_simple(void)
{
/* Build a string representing INTMAX_MAX + 1 to trigger overflow */
char buf[128];
uintmax_t big = (uintmax_t)INTMAX_MAX + 1;
snprintf(buf, sizeof buf, "% |