From e3af84d7d594abf88ccd6c4d28067bd6cfb3bca0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se>
Date: Fri, 15 Nov 2002 09:08:42 +0100
Subject: [PATCH] (test_bignum): Use nettle_mpz_init_set_str_256_s.
 (test_size): New function. (test_main): Test size computation and formatting
 of negative numbers.

Rev: src/nettle/testsuite/bignum-test.c:1.3
---
 testsuite/bignum-test.c | 52 ++++++++++++++++++++++++++++++++++++++---
 1 file changed, 49 insertions(+), 3 deletions(-)

diff --git a/testsuite/bignum-test.c b/testsuite/bignum-test.c
index bfdcddb0..b86108fc 100644
--- a/testsuite/bignum-test.c
+++ b/testsuite/bignum-test.c
@@ -18,7 +18,7 @@ test_bignum(const char *hex, unsigned length, const uint8_t *base256)
   uint8_t *buf;
   
   mpz_init_set_str(a, hex, 16);
-  nettle_mpz_init_set_str_256(b, length, base256);
+  nettle_mpz_init_set_str_256_s(b, length, base256);
 
   if (mpz_cmp(a, b))
     FAIL();
@@ -35,14 +35,60 @@ test_bignum(const char *hex, unsigned length, const uint8_t *base256)
 
   mpz_clear(a); mpz_clear(b);
 }
+
+static void
+test_size(long x, unsigned size)
+{
+  mpz_t t;
+
+  mpz_init_set_si(t, x);
+  ASSERT(nettle_mpz_sizeinbase_256_s(t) == size);
+  mpz_clear(t);
+}
 #endif /* HAVE_LIBGMP */
 
+
 int
 test_main(void)
 {
 #if HAVE_LIBGMP
-  test_bignum("0", 0, "");
-  test_bignum("010203040506", 7, H("00010203040506"));
+  test_size(0, 1);
+  test_size(1, 1);
+  test_size(0x7f, 1);
+  test_size(0x80, 2);
+  test_size(0x81, 2);
+  test_size(0xff, 2);
+  test_size(0x100, 2);
+  test_size(0x101, 2);
+  test_size(0x1111, 2);
+  test_size(0x7fff, 2);
+  test_size(0x8000, 3);
+  test_size(0x8001, 3);
+
+  test_size(-      1, 1); /*     ff */
+  test_size(-   0x7f, 1); /*     81 */
+  test_size(-   0x80, 1); /*     80 */
+  test_size(-   0x81, 2); /*   ff7f */
+  test_size(-   0xff, 2); /*   ff01 */
+  test_size(-  0x100, 2); /*   ff00 */
+  test_size(-  0x101, 2); /*   feff */
+  test_size(- 0x1111, 2); /*   eeef */
+  test_size(- 0x7fff, 2); /*   8001 */
+  test_size(- 0x8000, 2); /*   8000 */
+  test_size(- 0x8001, 3); /* ff7fff */
+
+  test_bignum("0", HL("00"));
+  test_bignum("010203040506", HL("010203040506"));
+  test_bignum("80010203040506", HL("0080010203040506"));
+
+  test_bignum(   "-1", HL(    "ff"));
+  test_bignum(  "-7f", HL(    "81"));
+  test_bignum(  "-80", HL(    "80"));
+  test_bignum(  "-81", HL(  "ff7f"));
+  test_bignum("-7fff", HL(  "8001"));
+  test_bignum("-8000", HL(  "8000"));
+  test_bignum("-8001", HL("ff7fff"));
+  
   SUCCESS();
 #else /* !HAVE_LIBGMP */
   SKIP();
-- 
GitLab