home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / BIGTEST.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  5KB  |  182 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /* BIGTEST.C
  4. **
  5. ** Routines to test Big Number Arithmetic functions found in BIGNUM.C
  6. **
  7. */
  8.  
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <conio.h>
  12. #include <time.h>
  13. #if defined(_MSC_VER)
  14.  #include <memory.h>
  15. #elif defined(__TURBOC__)
  16.  #include <mem.h>
  17. #else
  18.  #include <string.h>
  19. #endif
  20.  
  21. #include "bignum.h"    /* Typedef for BigNum type */
  22.  
  23. ULong Big2Long(BigNum * b)
  24. {
  25.       /* Converts the big number in b to an unsigned long. This is a support
  26.       ** routine for test purposes only. The length of b should be 3 or less
  27.       ** to avoid overflowing the long.
  28.       */
  29.  
  30.       int i;
  31.       ULong res = 0L;
  32.  
  33.       for (i = 0; i < b->nlen; i++)
  34.       {
  35.             res *= (ULong) MODULUS;
  36.             res += (ULong) b->n[i];
  37.       }
  38.       return res;
  39. }
  40.  
  41. void BigNumRand(BigNum **b)
  42. {
  43.       /* This routine fills the big number pointed to by *b to a random long.
  44.       ** It is a support routine for test purposes only.
  45.       */
  46.  
  47.       int  i = (*b)->nlen - 1;
  48.  
  49.       memset((*b)->n, 0, (*b)->nlen * sizeof(UShort));
  50.  
  51.       if (i < 0)
  52.             return;
  53.       else if (i == 0)
  54.             (*b)->n[0] = rand() % MODULUS;
  55.       else if (i == 1)
  56.       {
  57.             (*b)->n[0] = rand() % MODULUS;
  58.             (*b)->n[1] = rand() % MODULUS;
  59.       }
  60.       else
  61.       {
  62.             (*b)->n[i--] = rand() % MODULUS;
  63.             (*b)->n[i--] = rand() % MODULUS;
  64.             (*b)->n[i] = rand() % 21;
  65.       }
  66. }
  67.  
  68. int main(void)
  69. {
  70.       /* Test the big number arithmetic routines using random longs. Runs
  71.       ** until an error is encountered or a key pressed.
  72.       ** Gives examples of how to call the routines.
  73.       */
  74.  
  75.       BigNum *a, *b, *c, *d = NULL, *p, *s;
  76.       ULong al, bl, cl, dl, sl, cnt = 0L;
  77.  
  78.       /* 'Randomly' seed the rand() generator */
  79.  
  80.       srand((unsigned int) time(NULL));
  81.  
  82.       /* Create two big numbers */
  83.  
  84.       a = BigNumAlloc(3);
  85.       b = BigNumAlloc(3);
  86.  
  87.       do    /* Loop until a key is pressed... */
  88.       {
  89.             if ((++cnt % 1000L) == 0)     /* Show every 1000 iterations */
  90.                   printf("%ld\n", cnt);
  91.             if (kbhit())            /* If a key pressed--we're outta here! */
  92.             {
  93.                   getch();
  94.                   break;
  95.             }
  96.  
  97.             /* Make the two big numbers random... */
  98.  
  99.             BigNumRand(&a);
  100.             BigNumRand(&b);
  101.  
  102.             /* Get their 'long' values... */
  103.  
  104.             al = Big2Long(a);
  105.             bl = Big2Long(b);
  106.  
  107.             c = BigNumDiv(a, b, &d);        /* Divide a by b */
  108.             if (c == NULL)
  109.             {
  110.                   printf("Error: Divide did not return quotient\n");
  111.                   break;
  112.             }
  113.             if (d == NULL)
  114.             {
  115.                   printf("Error: Divide did not return remainder\n");
  116.                   break;
  117.             }
  118.             cl = Big2Long(c);        /* Get quotient as a long */
  119.             dl = Big2Long(d);        /* Get remainder as a long */
  120.  
  121.             if ((al / bl) != cl)
  122.             {
  123.                   printf("Error: Quotient %lu / %lu != %lu\n", al, bl, cl);
  124.                   printf("a = %d%04d%04d   b = %d%04d\n",
  125.                         a->n[0], a->n[1], a->n[2], b->n[0], b->n[1]);
  126.                   break;
  127.             }
  128.             if ((al % bl) != dl)
  129.             {
  130.                   printf("Error: Remainder %lu %c %lu != %lu\n",
  131.                         al, '%', bl, dl);
  132.                   printf("a = %d%04d%04d   b = %d%04d\n",
  133.                         a->n[0], a->n[1], a->n[2], b->n[0], b->n[1]);
  134.                   break;
  135.             }
  136.  
  137.             /* Reconstruct BigNum a by multiplying the quotient by b and
  138.             ** adding the remainder.
  139.             */
  140.  
  141.             p = BigNumMul(c, b);
  142.             s = BigNumAdd(p, d);
  143.             sl = Big2Long(s);
  144.             if (sl != al)
  145.             {
  146.                   printf("Error: Couldn't reconstruct original "
  147.                         "divisor: %lu != %lu\n", al, sl);
  148.                   break;
  149.             }
  150.             BigNumFree(s);    /* To use s again, must free it first! */
  151.             s = BigNumSub(a, b);
  152.             if (al >= bl)     /* For BigNumSub(), a must be > than b */
  153.             {
  154.                   sl = Big2Long(s);
  155.                   if (sl != (al - bl))
  156.                   {
  157.                         printf("Error: Subtraction error %lu - %lu != %lu\n",
  158.                               al, bl, sl);
  159.                         break;
  160.                   }
  161.             }
  162.             else if (s != NULL)
  163.             {
  164.                   printf("Invalid subtraction %lu - %lu\n", al, bl);
  165.                   break;
  166.             }
  167.  
  168.             /* Free all the memory allocated by the arithmetic calls */
  169.  
  170.             BigNumFree(c);
  171.             BigNumFree(d);
  172.             BigNumFree(p);
  173.             BigNumFree(s);
  174.  
  175.       } while (1);
  176.  
  177.       BigNumFree(a);
  178.       BigNumFree(b);
  179.  
  180.       return 0;
  181. }
  182.