Skip to content

Commit

Permalink
imrat: fix an inconsistency in reading string values (#62)
Browse files Browse the repository at this point in the history
Make mp_rat_read_cstring more consistent with mp_int_read_cstring.
Add some tests for that.

Updates #57
  • Loading branch information
creachadair authored Jan 25, 2025
1 parent 2a5176f commit 2a071f1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ DFLAGSY=-g -DDEBUG=1

# --- end of configuration section ---

TARGETS=bintest bug-swap imtest imtimer rtest
TARGETS=bintest bug-swap bug-qread imtest imtimer rtest
HDRS=imath.h imrat.h iprime.h imdrover.h rsamath.h gmp_compat.h
SRCS=$(HDRS:.h=.c) $(TARGETS:=.c)
OBJS=$(SRCS:.c=.o)
Expand Down Expand Up @@ -79,7 +79,7 @@ objs: $(OBJS)
check: test gmp-compat-test
@ echo "Completed running imath and gmp-compat unit tests"

test: imtest pi bug-swap doc.md
test: imtest pi bug-swap bug-qread doc.md
@ echo ""
@ echo "Running tests, you should not see any 'FAILED' lines here."
@ echo "If you do, please see doc.txt for how to report a bug."
Expand All @@ -100,7 +100,7 @@ docker-test: docker-image
$(EXAMPLES):%: imath.o imrat.o iprime.o %.o
$(CC) $(CFLAGS) -o $@ $^ $(LIBS)

$(TARGETS):%: imath.o %.o
$(TARGETS):%: imath.o imrat.o %.o
$(CC) $(CFLAGS) -o $@ $^ $(LIBS)

examples: $(EXAMPLES)
Expand Down
8 changes: 5 additions & 3 deletions imrat.c
Original file line number Diff line number Diff line change
Expand Up @@ -669,16 +669,18 @@ mp_result mp_rat_read_cstring(mp_rat r, mp_size radix, const char *str,
}

/* Skip whitespace between numerator and (possible) separator */
while (isspace((unsigned char)*endp)) {
++endp;
char *sp = endp;
while (isspace((unsigned char)*sp)) {
++sp;
}

/* If there is no separator, we will stop reading at this point. */
if (*endp != '/') {
if (*sp != '/') {
mp_int_set_value(MP_DENOM_P(r), 1);
if (end != NULL) *end = endp;
return res;
}
endp = sp;

++endp; /* skip separator */
if ((res = mp_int_read_cstring(MP_DENOM_P(r), radix, endp, end)) != MP_OK) {
Expand Down
38 changes: 38 additions & 0 deletions tests/bug-qread.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <stdio.h>

#include "imrat.h"

struct test {
char* input;
int radix;
mp_result want;
};

int main(int arch, char* argv[]) {
struct test tests[] = {
{"123", 10, 0},
{" 1ca", 16, 0},
{"1010", 2, 0},
{"123 ", 10, -5}, /* MP_TRUNC */
{"123/456", 8, 0},
{"123 / 456", 8, 0},
{"123 /", 8, -4}, /* MP_UNDEF */
{" -5/-3", 10, 0},
{NULL, 10, 0},
};

int errs = 0;
for (int i = 0; tests[i].input != NULL; i++) {
mpq_t value;
mp_rat_init(&value);

mp_result got = mp_rat_read_cstring(&value, tests[i].radix, tests[i].input, NULL);
if (got != tests[i].want) {
printf("ERROR: test %d: input \"%s\": got \"%s\", want \"%s\"\n",
i+1, tests[i].input, mp_error_string(got), mp_error_string(tests[i].want));
}
}
printf("REGRESSION: mp_rat_read_cstring inconsistent space handling: %s\n",
errs ? "FAILED" : "OK");
return !errs;
}
2 changes: 1 addition & 1 deletion tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pitest 4096 10 pi4096-10.txt
echo ""
echo "-- Running regression tests"

for bug in bug-swap ; do
for bug in bug-qread bug-swap ; do
../${bug}
done

Expand Down

0 comments on commit 2a071f1

Please sign in to comment.