From 5db48d36043ca55ba2ba1986bbdd4699f4941778 Mon Sep 17 00:00:00 2001 From: yangfl Date: Thu, 4 Apr 2019 11:38:56 +0800 Subject: [PATCH] stb_divide: use return value to indicate errors --- stb_divide.h | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/stb_divide.h b/stb_divide.h index 84874d5..6662629 100644 --- a/stb_divide.h +++ b/stb_divide.h @@ -275,24 +275,32 @@ int stb_mod_eucl(int v1, int v2) #include int show=0; +int err=0; void stbdiv_check(int q, int r, int a, int b, char *type, int dir) { - if ((dir > 0 && r < 0) || (dir < 0 && r > 0)) + if ((dir > 0 && r < 0) || (dir < 0 && r > 0)) { fprintf(stderr, "FAILED: %s(%d,%d) remainder %d in wrong direction\n", type,a,b,r); - else + err++; + } else if (b != INT_MIN) // can't compute abs(), but if b==INT_MIN all remainders are valid - if (r <= -abs(b) || r >= abs(b)) + if (r <= -abs(b) || r >= abs(b)) { fprintf(stderr, "FAILED: %s(%d,%d) remainder %d out of range\n", type,a,b,r); + err++; + } #ifdef STB_DIVIDE_TEST_64 { STB_DIVIDE_TEST_64 q64 = q, r64=r, a64=a, b64=b; - if (q64*b64+r64 != a64) + if (q64*b64+r64 != a64) { fprintf(stderr, "FAILED: %s(%d,%d) remainder %d doesn't match quotient %d\n", type,a,b,r,q); + err++; + } } #else - if (q*b+r != a) + if (q*b+r != a) { fprintf(stderr, "FAILED: %s(%d,%d) remainder %d doesn't match quotient %d\n", type,a,b,r,q); + err++; + } #endif } @@ -372,7 +380,7 @@ int main(int argc, char **argv) testh(INT_MIN , INT_MAX); testh(INT_MIN+1, INT_MAX); - return 0; + return err > 0 ? 1 : 0; } #endif // STB_DIVIDE_TEST #endif // STB_DIVIDE_IMPLEMENTATION