From 35a114acd539fdd223a6e4ce236a193943ab6c53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mattias=20Andr=C3=A9e?= Date: Fri, 25 Mar 2016 00:18:03 +0100 Subject: [PATCH] test: add support for big integers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- test.c | 51 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/test.c b/test.c index 508e3a6..d5f167a 100644 --- a/test.c +++ b/test.c @@ -1,11 +1,48 @@ /* See LICENSE file for copyright and license details. */ #include #include +#include #include #include "util.h" -#define STOI(s) enstrtonum(2, s, LLONG_MIN, LLONG_MAX) +static int +intcmp(char *a, char *b) +{ + char *s; + int asign = *a == '-' ? -1 : 1; + int bsign = *b == '-' ? -1 : 1; + int ret; + + if (*a == '-' || *a == '+') a += 1; + if (*b == '-' || *b == '+') b += 1; + + if (!*a || !*b) + goto noint; + for (s = a; *s; s++) + if (!isdigit(*s)) + goto noint; + for (s = b; *s; s++) + if (!isdigit(*s)) + goto noint; + + while (*a == '0') a++; + while (*b == '0') b++; + asign *= !!*a; + bsign *= !!*b; + + if (asign != bsign) + return asign < bsign ? -1 : 1; + else if (strlen(a) != strlen(b)) + return asign * (strlen(a) < strlen(b) ? -1 : 1); + else + return asign * strcmp(a, b); + +noint: + enprintf(2, "expected integer operands\n"); + + return 0; /* not reached */ +} static int mtimecmp(struct stat *buf1, struct stat *buf2) @@ -44,12 +81,12 @@ static int unary_t(char *s) { int fd = enstrtonum(2, s, 0, INT_MAX); return isat static int binary_se(char *s1, char *s2) { return !strcmp(s1, s2); } static int binary_sn(char *s1, char *s2) { return strcmp(s1, s2); } -static int binary_eq(char *s1, char *s2) { long long a = STOI(s1), b = STOI(s2); return a == b; } -static int binary_ne(char *s1, char *s2) { long long a = STOI(s1), b = STOI(s2); return a != b; } -static int binary_gt(char *s1, char *s2) { long long a = STOI(s1), b = STOI(s2); return a > b; } -static int binary_ge(char *s1, char *s2) { long long a = STOI(s1), b = STOI(s2); return a >= b; } -static int binary_lt(char *s1, char *s2) { long long a = STOI(s1), b = STOI(s2); return a < b; } -static int binary_le(char *s1, char *s2) { long long a = STOI(s1), b = STOI(s2); return a <= b; } +static int binary_eq(char *s1, char *s2) { return intcmp(s1, s2) == 0; } +static int binary_ne(char *s1, char *s2) { return intcmp(s1, s2) != 0; } +static int binary_gt(char *s1, char *s2) { return intcmp(s1, s2) > 0; } +static int binary_ge(char *s1, char *s2) { return intcmp(s1, s2) >= 0; } +static int binary_lt(char *s1, char *s2) { return intcmp(s1, s2) < 0; } +static int binary_le(char *s1, char *s2) { return intcmp(s1, s2) <= 0; } static int binary_ef(char *s1, char *s2)