Skip to content

Commit

Permalink
[ISSUE-0060]: additional tests; bigdecimal print fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
SzigetiJ committed Jan 3, 2024
1 parent 19a036e commit 2789568
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/bigdecimal128.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,9 @@ BigDecimal128 bigdecimal128_ctor_cstream(const char *dec_digits, buint_size_t le

buint_size_t bigdecimal128_print(const BigDecimal128 *a, char *buf, buint_size_t buf_len) {
buint_size_t aepos = bigint128_print_dec(&a->val, buf, buf_len);
buint_size_t abpos = (buf_len && buf[0] == MINUS_SIGN) ? 1 : 0;
buint_size_t abpos = (aepos && buf[0] == MINUS_SIGN) ? 1 : 0;
buint_size_t alen = aepos - abpos;
if (alen == 0) return 0;
if (a->prec != 0) { // decimal dot must be inserted
if (alen <= a->prec) { // leading 0s must be inserted
buint_size_t ashift = a->prec - alen + 1;
Expand Down
25 changes: 25 additions & 0 deletions tests/bigdecimal128_io_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,34 @@ bool test_io_dec0() {
return !fail;
}

bool test_io_lowbuf() {
bool pass = true;
char buffer[BIGDECLEN_HI + 1];

for (int i = 0; i < dec_input_len; ++i) {
const CStr *input = &dec_input[i];
size_t exp_len = dec_printed[i].len;
if (DEC_BIGUINTLEN_LO < input->len)
continue;
BigDecimal128 a = bigdecimal128_ctor_cstream(input->str, input->len);
for (int len_dec = 1; len_dec <= 2; ++len_dec) {
if (exp_len < len_dec) continue;
buint_size_t len = bigdecimal128_print(&a, buffer, exp_len - len_dec);
if (len != 0) {
fprintf(stderr, "bigdecimal128_print() error on low buffer size. Params: (%s,buf,%"PRIbuint_size_t") expected: [0], actual: [%"PRIbuint_size_t"] (len_dec: %d)\n", input->str, exp_len - len_dec, len, len_dec);
pass = false;
}
}
}


return pass;
}

int main(int argc, char **argv) {

assert(test_io_dec0());
assert(test_io_lowbuf());

return 0;
}
Expand Down
10 changes: 8 additions & 2 deletions tests/intio_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ bool test_get_digit() {
'A','B','C','D','E','F',
'g','n','o','v','w',
'G','N','O','v','W',
'.','?','!','/','\0'
'.','!','/','\0', // below numbers
':','<','=','>','?','@', // between numbers and UC
'[','\\',']','^','_','`', // between UC and LC
'{','|','}','~' // over LC
};
const unsigned int samples_len = sizeof(samples)/sizeof(char);
const unsigned char sample_values[] = {
Expand All @@ -22,7 +25,10 @@ bool test_get_digit() {
10,11,12,13,14,15,
16,23,24,31,32,
16,23,24,31,32,
255,255,255,255,255
255,255,255,255,
255,255,255,255,255,255,
255,255,255,255,255,255,
255,255,255,255
};

bool fail = false;
Expand Down

0 comments on commit 2789568

Please sign in to comment.