summaryrefslogtreecommitdiffstats
path: root/contrib/libgmp/mpz/tests/io-binary.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/libgmp/mpz/tests/io-binary.c')
-rw-r--r--contrib/libgmp/mpz/tests/io-binary.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/contrib/libgmp/mpz/tests/io-binary.c b/contrib/libgmp/mpz/tests/io-binary.c
new file mode 100644
index 0000000..c28f2a5
--- /dev/null
+++ b/contrib/libgmp/mpz/tests/io-binary.c
@@ -0,0 +1,76 @@
+/* Test mpz_inp_binary and mpz_out_binary.
+
+ We write and read back some test strings, and both compare
+ the numerical result, and make sure the pattern on file is
+ what we expect. The latter is important for compatibility
+ between machines with different word sizes. */
+
+#include <stdio.h>
+#include "gmp.h"
+
+FILE *file;
+
+test (str, binary_len, binary_str)
+ char *str;
+ int binary_len;
+ char *binary_str;
+{
+ mpz_t x, y;
+ int n_written;
+ char buf[100];
+
+ mpz_init_set_str (x, str, 0);
+ mpz_init (y);
+
+ fseek (file, 0, SEEK_SET);
+ mpz_out_binary (file, x);
+ n_written = ftell (file);
+ if (n_written != binary_len)
+ abort ();
+
+ fseek (file, 0, SEEK_SET);
+ mpz_inp_binary (y, file);
+ if (n_written != ftell (file))
+ abort ();
+ if (mpz_cmp (x, y) != 0)
+ abort ();
+
+ fseek (file, 0, SEEK_SET);
+ fread (buf, n_written, 1, file);
+ if (memcmp (buf, binary_str, binary_len) != 0)
+ abort ();
+
+ mpz_clear (x);
+}
+
+main ()
+{
+ file = fopen ("xtmpfile", "w+");
+
+ test ("0", 4,
+ "\000\000\000\000");
+
+ test ("1", 5,
+ "\000\000\000\001\001");
+ test ("0x123", 6,
+ "\000\000\000\002\001\043");
+ test ("0xdeadbeef", 8,
+ "\000\000\000\004\336\255\276\357");
+ test ("0xbabefaced", 9,
+ "\000\000\000\005\013\253\357\254\355");
+ test ("0x123456789facade0", 12,
+ "\000\000\000\010\022\064\126\170\237\254\255\340");
+
+ test ("-1", 5,
+ "\377\377\377\377\001");
+ test ("-0x123", 6,
+ "\377\377\377\376\001\043");
+ test ("-0xdeadbeef", 8,
+ "\377\377\377\374\336\255\276\357");
+ test ("-0xbabefaced", 9,
+ "\377\377\377\373\013\253\357\254\355");
+ test ("-0x123456789facade0", 12,
+ "\377\377\377\370\022\064\126\170\237\254\255\340");
+
+ exit (0);
+}
OpenPOWER on IntegriCloud