[18190] | 1 | /* Test mpz_import. |
---|
| 2 | |
---|
[22253] | 3 | Copyright 2002, 2004 Free Software Foundation, Inc. |
---|
[18190] | 4 | |
---|
| 5 | This file is part of the GNU MP Library. |
---|
| 6 | |
---|
| 7 | The GNU MP Library is free software; you can redistribute it and/or modify |
---|
| 8 | it under the terms of the GNU Lesser General Public License as published by |
---|
| 9 | the Free Software Foundation; either version 2.1 of the License, or (at your |
---|
| 10 | option) any later version. |
---|
| 11 | |
---|
| 12 | The GNU MP Library is distributed in the hope that it will be useful, but |
---|
| 13 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
| 14 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
---|
| 15 | License for more details. |
---|
| 16 | |
---|
| 17 | You should have received a copy of the GNU Lesser General Public License |
---|
| 18 | along with the GNU MP Library; see the file COPYING.LIB. If not, write to |
---|
| 19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
---|
| 20 | MA 02111-1307, USA. */ |
---|
| 21 | |
---|
| 22 | #include <stdio.h> |
---|
| 23 | #include <stdlib.h> |
---|
[22253] | 24 | #include <string.h> |
---|
[18190] | 25 | #include "gmp.h" |
---|
| 26 | #include "gmp-impl.h" |
---|
| 27 | #include "tests.h" |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | void |
---|
| 31 | check_data (void) |
---|
| 32 | { |
---|
| 33 | static const struct { |
---|
| 34 | const char *want; |
---|
| 35 | size_t count; |
---|
| 36 | int order; |
---|
| 37 | size_t size; |
---|
| 38 | int endian; |
---|
| 39 | int nail; |
---|
| 40 | char src[64]; |
---|
| 41 | |
---|
| 42 | } data[] = { |
---|
| 43 | |
---|
| 44 | { "0", 0,1, 1,1, 0 }, |
---|
| 45 | { "0", 1,1, 0,1, 0 }, |
---|
| 46 | |
---|
| 47 | { "0x12345678", 4,1, 1,1, 0, { 0x12, 0x34, 0x56, 0x78 } }, |
---|
| 48 | { "0x12345678", 1,1, 4,1, 0, { 0x12, 0x34, 0x56, 0x78 } }, |
---|
| 49 | { "0x12345678", 1,-1, 4,1, 0, { 0x12, 0x34, 0x56, 0x78 } }, |
---|
| 50 | |
---|
| 51 | { "0x12345678", 4,-1, 1,-1, 0, { 0x78, 0x56, 0x34, 0x12 } }, |
---|
| 52 | { "0x12345678", 1,1, 4,-1, 0, { 0x78, 0x56, 0x34, 0x12 } }, |
---|
| 53 | { "0x12345678", 1,-1, 4,-1, 0, { 0x78, 0x56, 0x34, 0x12 } }, |
---|
| 54 | |
---|
| 55 | { "0", 5,1, 1,1, 7, { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE } }, |
---|
| 56 | { "0", 5,-1, 1,1, 7, { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE } }, |
---|
| 57 | { "0x15", 5,1, 1,1, 7, { 0xFF, 0xFE, 0xFF, 0xFE, 0xFF } }, |
---|
| 58 | |
---|
| 59 | { "0", 3,1, 2,1, 1, { 0x80,0x00, 0x80,0x00, 0x80,0x00 } }, |
---|
| 60 | { "0", 3,1, 2,-1, 1, { 0x00,0x80, 0x00,0x80, 0x00,0x80 } }, |
---|
| 61 | { "0", 3,1, 2,1, 15, { 0xFF,0xFE, 0xFF,0xFE, 0xFF,0xFE } }, |
---|
| 62 | |
---|
| 63 | { "0x2A", 3,1, 2,1, 14, { 0xFF,0xFE, 0xFF,0xFE, 0xFF,0xFE } }, |
---|
| 64 | { "0x06", 3,1, 2,1, 14, { 0xFF,0xFC, 0xFF,0xFD, 0xFF,0xFE } }, |
---|
| 65 | { "0x24", 3,-1, 2,1, 14, { 0xFF,0xFC, 0xFF,0xFD, 0xFF,0xFE } }, |
---|
| 66 | |
---|
| 67 | { "0x123456789ABC", 3,1, 2,1, 0, { 0x12,0x34, 0x56,0x78, 0x9A,0xBC } }, |
---|
| 68 | { "0x123456789ABC", 3,-1, 2,1, 0, { 0x9A,0xBC, 0x56,0x78, 0x12,0x34 } }, |
---|
| 69 | { "0x123456789ABC", 3,1, 2,-1, 0, { 0x34,0x12, 0x78,0x56, 0xBC,0x9A } }, |
---|
| 70 | { "0x123456789ABC", 3,-1, 2,-1, 0, { 0xBC,0x9A, 0x78,0x56, 0x34,0x12 } }, |
---|
| 71 | |
---|
| 72 | { "0x112233445566778899AABBCC", 3,1, 4,1, 0, |
---|
| 73 | { 0x11,0x22,0x33,0x44, 0x55,0x66,0x77,0x88, 0x99,0xAA,0xBB,0xCC } }, |
---|
| 74 | { "0x112233445566778899AABBCC", 3,-1, 4,1, 0, |
---|
| 75 | { 0x99,0xAA,0xBB,0xCC, 0x55,0x66,0x77,0x88, 0x11,0x22,0x33,0x44 } }, |
---|
| 76 | { "0x112233445566778899AABBCC", 3,1, 4,-1, 0, |
---|
| 77 | { 0x44,0x33,0x22,0x11, 0x88,0x77,0x66,0x55, 0xCC,0xBB,0xAA,0x99 } }, |
---|
| 78 | { "0x112233445566778899AABBCC", 3,-1, 4,-1, 0, |
---|
| 79 | { 0xCC,0xBB,0xAA,0x99, 0x88,0x77,0x66,0x55, 0x44,0x33,0x22,0x11 } }, |
---|
| 80 | |
---|
| 81 | { "0x100120023003400450056006700780089009A00AB00BC00C", 3,1, 8,1, 0, |
---|
| 82 | { 0x10,0x01,0x20,0x02,0x30,0x03,0x40,0x04, |
---|
| 83 | 0x50,0x05,0x60,0x06,0x70,0x07,0x80,0x08, |
---|
| 84 | 0x90,0x09,0xA0,0x0A,0xB0,0x0B,0xC0,0x0C } }, |
---|
| 85 | { "0x100120023003400450056006700780089009A00AB00BC00C", 3,-1, 8,1, 0, |
---|
| 86 | { 0x90,0x09,0xA0,0x0A,0xB0,0x0B,0xC0,0x0C, |
---|
| 87 | 0x50,0x05,0x60,0x06,0x70,0x07,0x80,0x08, |
---|
| 88 | 0x10,0x01,0x20,0x02,0x30,0x03,0x40,0x04 } }, |
---|
| 89 | { "0x100120023003400450056006700780089009A00AB00BC00C", 3,1, 8,-1, 0, |
---|
| 90 | { 0x04,0x40,0x03,0x30,0x02,0x20,0x01,0x10, |
---|
| 91 | 0x08,0x80,0x07,0x70,0x06,0x60,0x05,0x50, |
---|
| 92 | 0x0C,0xC0,0x0B,0xB0,0x0A,0xA0,0x09,0x90 } }, |
---|
| 93 | { "0x100120023003400450056006700780089009A00AB00BC00C", 3,-1, 8,-1, 0, |
---|
| 94 | { 0x0C,0xC0,0x0B,0xB0,0x0A,0xA0,0x09,0x90, |
---|
| 95 | 0x08,0x80,0x07,0x70,0x06,0x60,0x05,0x50, |
---|
| 96 | 0x04,0x40,0x03,0x30,0x02,0x20,0x01,0x10 } }, |
---|
| 97 | |
---|
| 98 | { "0x155555555555555555555555", 3,1, 4,1, 1, |
---|
| 99 | { 0xD5,0x55,0x55,0x55, 0xAA,0xAA,0xAA,0xAA, 0xD5,0x55,0x55,0x55 } }, |
---|
| 100 | { "0x155555555555555555555555", 3,-1, 4,1, 1, |
---|
| 101 | { 0xD5,0x55,0x55,0x55, 0xAA,0xAA,0xAA,0xAA, 0xD5,0x55,0x55,0x55 } }, |
---|
| 102 | { "0x155555555555555555555555", 3,1, 4,-1, 1, |
---|
| 103 | { 0x55,0x55,0x55,0xD5, 0xAA,0xAA,0xAA,0xAA, 0x55,0x55,0x55,0xD5 } }, |
---|
| 104 | { "0x155555555555555555555555", 3,-1, 4,-1, 1, |
---|
| 105 | { 0x55,0x55,0x55,0xD5, 0xAA,0xAA,0xAA,0xAA, 0x55,0x55,0x55,0xD5 } }, |
---|
| 106 | }; |
---|
| 107 | |
---|
| 108 | char buf[sizeof(data[0].src) + sizeof (mp_limb_t)]; |
---|
| 109 | char *src; |
---|
| 110 | size_t align; |
---|
| 111 | int i; |
---|
| 112 | mpz_t got, want; |
---|
| 113 | |
---|
| 114 | mpz_init (got); |
---|
| 115 | mpz_init (want); |
---|
| 116 | |
---|
| 117 | for (i = 0; i < numberof (data); i++) |
---|
| 118 | { |
---|
| 119 | for (align = 0; align < sizeof (mp_limb_t); align++) |
---|
| 120 | { |
---|
| 121 | mpz_set_str_or_abort (want, data[i].want, 0); |
---|
| 122 | src = buf + align; |
---|
| 123 | memcpy (src, data[i].src, data[i].count * data[i].size); |
---|
| 124 | |
---|
| 125 | mpz_set_ui (got, 0L); |
---|
| 126 | mpz_import (got, data[i].count, data[i].order, |
---|
| 127 | data[i].size, data[i].endian, data[i].nail, src); |
---|
| 128 | |
---|
| 129 | MPZ_CHECK_FORMAT (got); |
---|
| 130 | if (mpz_cmp (got, want) != 0) |
---|
| 131 | { |
---|
| 132 | printf ("wrong at data[%d]\n", i); |
---|
| 133 | printf (" count=%u order=%d size=%u endian=%d nail=%u align=%u\n", |
---|
| 134 | data[i].count, data[i].order, |
---|
| 135 | data[i].size, data[i].endian, data[i].nail, |
---|
| 136 | align); |
---|
| 137 | mpz_trace (" got ", got); |
---|
| 138 | mpz_trace (" want", want); |
---|
| 139 | abort (); |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | mpz_clear (got); |
---|
| 144 | mpz_clear (want); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | int |
---|
| 149 | main (void) |
---|
| 150 | { |
---|
| 151 | tests_start (); |
---|
| 152 | |
---|
| 153 | mp_trace_base = -16; |
---|
| 154 | check_data (); |
---|
| 155 | |
---|
| 156 | tests_end (); |
---|
| 157 | exit (0); |
---|
| 158 | } |
---|