[project @ 1998-06-05 14:38:07 by simonm]
[ghc-hetmet.git] / ghc / rts / gmp / mpz / tests / io-binary.c
1 /* Test mpz_inp_binary and mpz_out_binary.
2
3    We write and read back some test strings, and both compare
4    the numerical result, and make sure the pattern on file is
5    what we expect.  The latter is important for compatibility
6    between machines with different word sizes.  */
7
8 #include <stdio.h>
9 #include "gmp.h"
10
11 FILE *file;
12
13 test (str, binary_len, binary_str)
14      char *str;
15      int binary_len;
16      char *binary_str;
17 {
18   mpz_t x, y;
19   int n_written;
20   char buf[100];
21
22   mpz_init_set_str (x, str, 0);
23   mpz_init (y);
24
25   fseek (file, 0, SEEK_SET);
26   mpz_out_binary (file, x);
27   n_written = ftell (file);
28   if (n_written != binary_len)
29     abort ();
30
31   fseek (file, 0, SEEK_SET);
32   mpz_inp_binary (y, file);
33   if (n_written != ftell (file))
34     abort ();
35   if (mpz_cmp (x, y) != 0)
36     abort ();
37
38   fseek (file, 0, SEEK_SET);
39   fread (buf, n_written, 1, file);
40   if (memcmp (buf, binary_str, binary_len) != 0)
41     abort ();
42
43   mpz_clear (x);
44 }
45
46 main ()
47 {
48   file = fopen ("xtmpfile", "w+");
49
50   test ("0", 4,
51         "\000\000\000\000");
52
53   test ("1", 5,
54         "\000\000\000\001\001");
55   test ("0x123", 6,
56         "\000\000\000\002\001\043");
57   test ("0xdeadbeef", 8,
58         "\000\000\000\004\336\255\276\357");
59   test ("0xbabefaced", 9,
60         "\000\000\000\005\013\253\357\254\355");
61   test ("0x123456789facade0", 12,
62         "\000\000\000\010\022\064\126\170\237\254\255\340");
63
64   test ("-1", 5,
65         "\377\377\377\377\001");
66   test ("-0x123", 6,
67         "\377\377\377\376\001\043");
68   test ("-0xdeadbeef", 8,
69         "\377\377\377\374\336\255\276\357");
70   test ("-0xbabefaced", 9,
71         "\377\377\377\373\013\253\357\254\355");
72   test ("-0x123456789facade0", 12,
73         "\377\377\377\370\022\064\126\170\237\254\255\340");
74
75   exit (0);
76 }