|
|
#include "../../unity/unity.h" |
|
|
#include <string.h> |
|
|
#include <stdlib.h> |
|
|
|
|
|
|
|
|
void setUp(void) { } |
|
|
void tearDown(void) { } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void assert_mpz_equals_str(mpz_t actual, const char *dec_str) |
|
|
{ |
|
|
mpz_t expected; |
|
|
int rc; |
|
|
mpz_init(expected); |
|
|
rc = mpz_init_set_str(expected, dec_str, 10); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0, rc); |
|
|
TEST_ASSERT_EQUAL_INT(0, mpz_cmp(expected, actual)); |
|
|
mpz_clear(expected); |
|
|
} |
|
|
|
|
|
|
|
|
static void assert_toarith_fails_preserves_string(const char *s) |
|
|
{ |
|
|
VALUE *v = str_value(s); |
|
|
char *orig_ptr = v->u.s; |
|
|
|
|
|
char *copy = strdup(orig_ptr); |
|
|
TEST_ASSERT_NOT_NULL(copy); |
|
|
|
|
|
bool ok = toarith(v); |
|
|
TEST_ASSERT_FALSE(ok); |
|
|
TEST_ASSERT_EQUAL_INT(string, v->type); |
|
|
TEST_ASSERT_TRUE(orig_ptr == v->u.s); |
|
|
TEST_ASSERT_EQUAL_INT(0, strcmp(copy, v->u.s)); |
|
|
|
|
|
free(copy); |
|
|
freev(v); |
|
|
} |
|
|
|
|
|
|
|
|
static void assert_toarith_succeeds_to(const char *input, const char *expected_dec) |
|
|
{ |
|
|
VALUE *v = str_value(input); |
|
|
bool ok = toarith(v); |
|
|
TEST_ASSERT_TRUE(ok); |
|
|
TEST_ASSERT_EQUAL_INT(integer, v->type); |
|
|
assert_mpz_equals_str(v->u.i, expected_dec); |
|
|
freev(v); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void test_toarith_integer_nochange(void) |
|
|
{ |
|
|
VALUE *v = int_value(123UL); |
|
|
|
|
|
mpz_t before; |
|
|
mpz_init_set(before, v->u.i); |
|
|
|
|
|
bool ok = toarith(v); |
|
|
TEST_ASSERT_TRUE(ok); |
|
|
TEST_ASSERT_EQUAL_INT(integer, v->type); |
|
|
TEST_ASSERT_EQUAL_INT(0, mpz_cmp(before, v->u.i)); |
|
|
|
|
|
mpz_clear(before); |
|
|
freev(v); |
|
|
} |
|
|
|
|
|
static void test_toarith_integer_zero_nochange(void) |
|
|
{ |
|
|
VALUE *v = int_value(0UL); |
|
|
mpz_t before; |
|
|
mpz_init_set(before, v->u.i); |
|
|
|
|
|
bool ok = toarith(v); |
|
|
TEST_ASSERT_TRUE(ok); |
|
|
TEST_ASSERT_EQUAL_INT(integer, v->type); |
|
|
TEST_ASSERT_EQUAL_INT(0, mpz_cmp(before, v->u.i)); |
|
|
|
|
|
mpz_clear(before); |
|
|
freev(v); |
|
|
} |
|
|
|
|
|
static void test_toarith_valid_decimal_string_positive(void) |
|
|
{ |
|
|
assert_toarith_succeeds_to("12345", "12345"); |
|
|
} |
|
|
|
|
|
static void test_toarith_valid_decimal_string_negative(void) |
|
|
{ |
|
|
assert_toarith_succeeds_to("-987654321", "-987654321"); |
|
|
} |
|
|
|
|
|
static void test_toarith_valid_decimal_string_leading_zeros(void) |
|
|
{ |
|
|
assert_toarith_succeeds_to("000123", "123"); |
|
|
} |
|
|
|
|
|
static void test_toarith_valid_decimal_string_minus_zero(void) |
|
|
{ |
|
|
assert_toarith_succeeds_to("-0", "0"); |
|
|
} |
|
|
|
|
|
static void test_toarith_valid_very_large_integer(void) |
|
|
{ |
|
|
|
|
|
const char *bigneg = |
|
|
"-123456789012345678901234567890123456789012345678901234567890"; |
|
|
assert_toarith_succeeds_to(bigneg, bigneg); |
|
|
} |
|
|
|
|
|
static void test_toarith_invalid_empty_string(void) |
|
|
{ |
|
|
assert_toarith_fails_preserves_string(""); |
|
|
} |
|
|
|
|
|
static void test_toarith_invalid_just_minus(void) |
|
|
{ |
|
|
assert_toarith_fails_preserves_string("-"); |
|
|
} |
|
|
|
|
|
static void test_toarith_invalid_plus_sign(void) |
|
|
{ |
|
|
assert_toarith_fails_preserves_string("+1"); |
|
|
} |
|
|
|
|
|
static void test_toarith_invalid_embedded_non_digit(void) |
|
|
{ |
|
|
assert_toarith_fails_preserves_string("12x"); |
|
|
} |
|
|
|
|
|
static void test_toarith_invalid_leading_space(void) |
|
|
{ |
|
|
assert_toarith_fails_preserves_string(" 1"); |
|
|
} |
|
|
|
|
|
static void test_toarith_invalid_trailing_space(void) |
|
|
{ |
|
|
assert_toarith_fails_preserves_string("1 "); |
|
|
} |
|
|
|
|
|
static void test_toarith_invalid_double_minus(void) |
|
|
{ |
|
|
assert_toarith_fails_preserves_string("--1"); |
|
|
} |
|
|
|
|
|
int main(void) |
|
|
{ |
|
|
UNITY_BEGIN(); |
|
|
|
|
|
RUN_TEST(test_toarith_integer_nochange); |
|
|
RUN_TEST(test_toarith_integer_zero_nochange); |
|
|
|
|
|
RUN_TEST(test_toarith_valid_decimal_string_positive); |
|
|
RUN_TEST(test_toarith_valid_decimal_string_negative); |
|
|
RUN_TEST(test_toarith_valid_decimal_string_leading_zeros); |
|
|
RUN_TEST(test_toarith_valid_decimal_string_minus_zero); |
|
|
RUN_TEST(test_toarith_valid_very_large_integer); |
|
|
|
|
|
RUN_TEST(test_toarith_invalid_empty_string); |
|
|
RUN_TEST(test_toarith_invalid_just_minus); |
|
|
RUN_TEST(test_toarith_invalid_plus_sign); |
|
|
RUN_TEST(test_toarith_invalid_embedded_non_digit); |
|
|
RUN_TEST(test_toarith_invalid_leading_space); |
|
|
RUN_TEST(test_toarith_invalid_trailing_space); |
|
|
RUN_TEST(test_toarith_invalid_double_minus); |
|
|
|
|
|
return UNITY_END(); |
|
|
} |