Skip to content

Commit

Permalink
[QUALITY]: reduce codacy warnings (mostly duplications)
Browse files Browse the repository at this point in the history
  • Loading branch information
SzigetiJ committed Dec 9, 2023
1 parent c2e7c03 commit 7d1278d
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 90 deletions.
10 changes: 5 additions & 5 deletions examples/exprtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@

// expression evaluation is an explicit sequence of function evaluations
BigUInt128 fun_2a_plus_b_funseq(const BigUInt128 *a, const BigUInt128 *b) {
BigUInt128 tmp=biguint128_shl(a,2);
BigUInt128 tmp=biguint128_shl(a,1);
BigUInt128 res=biguint128_add(&tmp, b);
return res;
}

BigUInt128 fun_2a_plus_b_paramtree(const BigUInt128 *a, const BigUInt128 *b) {
BigUInt128 tmp=biguint128_ctor_default();
return biguint128_add(
biguint128_shl_or(&tmp,a,2),
biguint128_shl_or(&tmp,a,1),
b
);
}
Expand All @@ -35,8 +35,8 @@ int main() {
const char b_str[]="10000000000000000000000000"; // 10^25
char sep_str[BUFLEN+1];
char res_str[BUFLEN];
BigUInt128 a = biguint128_ctor_deccstream(a_str, strlen(a_str));
BigUInt128 b = biguint128_ctor_deccstream(b_str, strlen(b_str));
BigUInt128 a = biguint128_ctor_deccstream(a_str, sizeof(a_str)/sizeof(a_str[0])-1);
BigUInt128 b = biguint128_ctor_deccstream(b_str, sizeof(b_str)/sizeof(a_str[0])-1);
memset(sep_str,'-',BUFLEN);
sep_str[BUFLEN]=0;

Expand All @@ -49,4 +49,4 @@ int main() {
BUFLEN, res_str
);
return 0;
}
}
85 changes: 41 additions & 44 deletions tests/bigdecimal128_add_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@
#define BIGDECCAP ((BIGUINT_BITS / 10 + 1) * 3 + 1)
#define BUFLEN (BIGDECCAP + 3)

// INTERNAL TYPES

typedef struct {
CStr num1;
CStr num2;
} PrecTestInputType;
} TestInputType;

typedef struct {
CStr num;
} PrecTestOutputType;
} TestOutputType;

typedef BigDecimal128 (*BigDecimalFun)(const BigDecimal128 *a, const BigDecimal128 *b);


const PrecTestInputType any_in[] = {
// TEST DATA
const TestInputType any_in[] = {
{STR("0"),STR("0")},
{STR("1"),STR("0.1")},
{STR("10"),STR("0.01")},
Expand All @@ -30,7 +36,7 @@ const PrecTestInputType any_in[] = {
{STR("3.55"),STR("3.45")}
};

const PrecTestOutputType add_out[] = {
const TestOutputType add_out[] = {
{STR("0")},
{STR("1.1")},
{STR("10.01")},
Expand All @@ -41,7 +47,7 @@ const PrecTestOutputType add_out[] = {
{STR("7.00")}
};

const PrecTestOutputType sub_out[] = {
const TestOutputType sub_out[] = {
{STR("0")},
{STR("0.9")},
{STR("9.99")},
Expand All @@ -54,58 +60,49 @@ const PrecTestOutputType sub_out[] = {

int input_len = sizeof(any_in) / sizeof(any_in[0]);

bool test_add0() {
bool fail = false;
// INTERNAL FUNCTIONS
/**
* @return true: test failed, false: test passed.
*/
static inline bool eval_testcase_(const TestInputType *tin, const TestOutputType *expected, BigDecimalFun fun, char funchr) {
if (BIGDECCAP < tin->num1.len || BIGDECCAP < tin->num2.len) {
return false;
}

char buffer[BUFLEN + 1];
for (int i = 0; i < input_len; ++i) {
const PrecTestInputType *ti = &any_in[i];
const PrecTestOutputType *expected = &add_out[i];
if (BIGDECCAP < ti->num1.len || BIGDECCAP < ti->num2.len)
continue;
BigDecimal128 a = bigdecimal128_ctor_cstream(tin->num1.str, tin->num1.len);
BigDecimal128 b = bigdecimal128_ctor_cstream(tin->num2.str, tin->num2.len);

BigDecimal128 a = bigdecimal128_ctor_cstream(ti->num1.str, ti->num1.len);
BigDecimal128 b = bigdecimal128_ctor_cstream(ti->num2.str, ti->num2.len);
BigDecimal128 result = fun(&a, &b);

BigDecimal128 sum = bigdecimal128_add(&a, &b);
buint_size_t len = bigdecimal128_print(&result, buffer, sizeof(buffer) / sizeof(char) - 1);
buffer[len] = 0;

buint_size_t len = bigdecimal128_print(&sum, buffer, sizeof(buffer) / sizeof(char) - 1);
buffer[len] = 0;
int testresult = strcmp(expected->num.str, buffer);

int result = strcmp(expected->num.str, buffer);
if (testresult != 0) {
fprintf(stderr, "input: (%s %c %s); expected output: [%s], actual [%s]\n",
tin->num1.str, funchr, tin->num2.str, expected->num.str, buffer);
return true;
}
return false;
}

if (result != 0) {
fprintf(stderr, "input: (%s + %s); expected output: [%s], actual [%s]\n",
ti->num1.str, ti->num2.str, expected->num.str, buffer);
fail = true;
}
// TEST FUNCTIONS
bool test_add0() {
bool fail = false;
for (int i = 0; i < input_len; ++i) {
bool test_failed = eval_testcase_(&any_in[i], &add_out[i], bigdecimal128_add, '+');
fail|=test_failed;
}
return !fail;
}

bool test_sub0() {
bool fail = false;
char buffer[BUFLEN + 1];
for (int i = 0; i < input_len; ++i) {
const PrecTestInputType *ti = &any_in[i];
const PrecTestOutputType *expected = &sub_out[i];
if (BIGDECCAP < ti->num1.len || BIGDECCAP < ti->num2.len)
continue;

BigDecimal128 a = bigdecimal128_ctor_cstream(ti->num1.str, ti->num1.len);
BigDecimal128 b = bigdecimal128_ctor_cstream(ti->num2.str, ti->num2.len);

BigDecimal128 diff = bigdecimal128_sub(&a, &b);

buint_size_t len = bigdecimal128_print(&diff, buffer, sizeof(buffer) / sizeof(char) - 1);
buffer[len] = 0;

int result = strcmp(expected->num.str, buffer);

if (result != 0) {
fprintf(stderr, "input: (%s + %s); expected output: [%s], actual [%s]\n",
ti->num1.str, ti->num2.str, expected->num.str, buffer);
fail = true;
}
bool test_failed = eval_testcase_(&any_in[i], &sub_out[i], bigdecimal128_sub, '-');
fail|=test_failed;
}
return !fail;
}
Expand Down
89 changes: 50 additions & 39 deletions tests/bigdecimal128_mul_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@
#define BIGDECCAP ((BIGUINT_BITS / 10 + 1) * 3 + 1)
#define BUFLEN (BIGDECCAP + 3)

// INTERNAL TYPES

typedef struct {
CStr num1;
CStr num2;
UInt resprec;
} PrecTestInputType;
} TestInputType;

typedef struct {
CStr num;
} PrecTestOutputType;
} TestOutputType;

typedef BigDecimal128 (*BigDecimalFun)(const BigDecimal128 *a, const BigDecimal128 *b, UInt prec);

const PrecTestInputType any_in[] = {
// TEST DATA
const TestInputType any_in[] = {
{STR("20"),STR("0.5"),0},
{STR("0.2"),STR("0.6"),3},
{STR("10"),STR("30"),5},
Expand All @@ -30,7 +35,7 @@ const PrecTestInputType any_in[] = {
{STR("0.000"),STR("10.00"),4}
};

const PrecTestOutputType mul_out[] = {
const TestOutputType mul_out[] = {
{STR("10.0")},
{STR("0.12")},
{STR("300")},
Expand All @@ -40,7 +45,7 @@ const PrecTestOutputType mul_out[] = {
{STR("0.00000")}
};

const PrecTestOutputType div_out[] = {
const TestOutputType div_out[] = {
{STR("40")},
{STR("0.333")},
{STR("0.33333")},
Expand All @@ -52,12 +57,39 @@ const PrecTestOutputType div_out[] = {

int input_len = sizeof(any_in) / sizeof(any_in[0]);

// INTERNAL FUNCTIONS
/**
* @return true: test failed, false: test passed.
*/
static inline bool eval_divtestcase_(const TestInputType *tin, const TestOutputType *expected, BigDecimalFun fun, char *funstr) {
char buffer[BUFLEN + 1];
if (BIGDECCAP < tin->num1.len || BIGDECCAP < tin->num2.len)
return false;

BigDecimal128 a = bigdecimal128_ctor_cstream(tin->num1.str, tin->num1.len);
BigDecimal128 b = bigdecimal128_ctor_cstream(tin->num2.str, tin->num2.len);

BigDecimal128 quotient = fun(&a, &b, tin->resprec);
buint_size_t len = bigdecimal128_print(&quotient, buffer, sizeof(buffer) / sizeof(char) - 1);
buffer[len] = 0;

int result = strcmp(expected->num.str, buffer);

if (result != 0) {
fprintf(stderr, "input: %s(%s, %s , %"PRIuint"); expected output: [%s], actual [%s]\n",
funstr, tin->num1.str, tin->num2.str, tin->resprec, expected->num.str, buffer);
return true;
}
return false;
}

// TEST FUNCTIONS
bool test_mul0() {
bool fail = false;
char buffer[BUFLEN + 1];
for (int i = 0; i < input_len; ++i) {
const PrecTestInputType *ti = &any_in[i];
const PrecTestOutputType *expected = &mul_out[i];
const TestInputType *ti = &any_in[i];
const TestOutputType *expected = &mul_out[i];
if (BIGDECCAP < ti->num1.len || BIGDECCAP < ti->num2.len)
continue;

Expand All @@ -82,40 +114,18 @@ bool test_mul0() {

bool test_div0() {
bool fail = false;
char buffer[BUFLEN + 1];
char buffer_fast[BUFLEN + 1];
for (int i = 0; i < input_len; ++i) {
const PrecTestInputType *ti = &any_in[i];
const PrecTestOutputType *expected = &div_out[i];
if (BIGDECCAP < ti->num1.len || BIGDECCAP < ti->num2.len)
continue;

BigDecimal128 a = bigdecimal128_ctor_cstream(ti->num1.str, ti->num1.len);
BigDecimal128 b = bigdecimal128_ctor_cstream(ti->num2.str, ti->num2.len);

BigDecimal128 quotient = bigdecimal128_div(&a, &b, ti->resprec);
buint_size_t len = bigdecimal128_print(&quotient, buffer, sizeof(buffer) / sizeof(char) - 1);
buffer[len] = 0;

BigDecimal128 quotient_fast = bigdecimal128_div_fast(&a, &b, ti->resprec);
buint_size_t len_fast = bigdecimal128_print(&quotient_fast, buffer_fast, sizeof(buffer_fast) / sizeof(char) - 1);
buffer_fast[len_fast] = 0;

int result = strcmp(expected->num.str, buffer);
int result_fast = strcmp(expected->num.str, buffer_fast);

if (result != 0) {
fprintf(stderr, "input: (%s / %s , %"PRIuint"); expected output: [%s], actual [%s]\n",
ti->num1.str, ti->num2.str, ti->resprec, expected->num.str, buffer);
fail = true;
}

if (result_fast != 0) {
fprintf(stderr, "input: (%s / %s , %"PRIuint" (fast)); expected output: [%s], actual [%s]\n",
ti->num1.str, ti->num2.str, ti->resprec, expected->num.str, buffer_fast);
fail = true;
}
bool testfailed = eval_divtestcase_(&any_in[i], &div_out[i], bigdecimal128_div, "div");
fail|=testfailed;
}
return !fail;
}

bool test_div_fast0() {
bool fail = false;
for (int i = 0; i < input_len; ++i) {
bool testfailed = eval_divtestcase_(&any_in[i], &div_out[i], bigdecimal128_div_fast, "div");
fail|=testfailed;
}
return !fail;
}
Expand All @@ -124,6 +134,7 @@ int main(int argc, char **argv) {

assert(test_mul0());
assert(test_div0());
assert(test_div_fast0());

return 0;
}
Expand Down
7 changes: 5 additions & 2 deletions tests/performance/print_perf.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ int main() {
buf[biguint128_print_dec(&b0, buf, BUFLEN)] = 0;
// just do something with the printed string
for (unsigned int i = 0; i < BUFLEN; ++i) {
sum+=buf[i];
sum+= buf[i];
}
}
t1 = clock();
print_result(t0, t1, "print_dec", LOOPS);
}
fprintf(stderr,"sum: %u\n", sum);

// #2: hexadecimal print
{
Expand All @@ -45,12 +46,14 @@ int main() {
buf[biguint128_print_hex(&b0, buf, BUFLEN)] = 0;
// just do something with the printed string
for (unsigned int i = 0; i < BUFLEN; ++i) {
sum+=buf[i];
sum+= buf[i];
}
}
t1 = clock();
print_result(t0, t1, "print_hex", LOOPS);
}
// do something with sum
fprintf(stderr,"sum: %u\n", sum);

return 0;
}

0 comments on commit 7d1278d

Please sign in to comment.