t-get_str.c revision 1.1.1.1
1/* Test mpq_get_str. 2 3Copyright 2001 Free Software Foundation, Inc. 4 5This file is part of the GNU MP Library. 6 7The GNU MP Library is free software; you can redistribute it and/or modify 8it under the terms of the GNU Lesser General Public License as published by 9the Free Software Foundation; either version 3 of the License, or (at your 10option) any later version. 11 12The GNU MP Library is distributed in the hope that it will be useful, but 13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 14or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 15License for more details. 16 17You should have received a copy of the GNU Lesser General Public License 18along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */ 19 20#include <stdio.h> 21#include <stdlib.h> 22#include <string.h> 23#include "gmp.h" 24#include "gmp-impl.h" 25#include "tests.h" 26 27 28void 29check_one (mpq_srcptr q, int base, const char *want) 30{ 31 char *str, *ret; 32 size_t str_alloc; 33 34 MPQ_CHECK_FORMAT (q); 35 mp_trace_base = base; 36 37 str_alloc = 38 mpz_sizeinbase (mpq_numref(q), ABS(base)) + 39 mpz_sizeinbase (mpq_denref(q), ABS(base)) + 3; 40 41 str = mpq_get_str (NULL, base, q); 42 if (strlen(str)+1 > str_alloc) 43 { 44 printf ("mpq_get_str size bigger than should be (passing NULL)\n"); 45 printf (" base %d\n", base); 46 printf (" got size %lu \"%s\"\n", (unsigned long) strlen(str)+1, str); 47 printf (" want size %lu\n", (unsigned long) str_alloc); 48 abort (); 49 } 50 if (strcmp (str, want) != 0) 51 { 52 printf ("mpq_get_str wrong (passing NULL)\n"); 53 printf (" base %d\n", base); 54 printf (" got \"%s\"\n", str); 55 printf (" want \"%s\"\n", want); 56 mpq_trace (" q", q); 57 abort (); 58 } 59 (*__gmp_free_func) (str, strlen (str) + 1); 60 61 str = (char *) (*__gmp_allocate_func) (str_alloc); 62 63 ret = mpq_get_str (str, base, q); 64 if (str != ret) 65 { 66 printf ("mpq_get_str wrong return value (passing non-NULL)\n"); 67 printf (" base %d\n", base); 68 printf (" got %p\n", ret); 69 printf (" want %p\n", want); 70 abort (); 71 } 72 if (strcmp (str, want) != 0) 73 { 74 printf ("mpq_get_str wrong (passing non-NULL)\n"); 75 printf (" base %d\n", base); 76 printf (" got \"%s\"\n", str); 77 printf (" want \"%s\"\n", want); 78 abort (); 79 } 80 (*__gmp_free_func) (str, str_alloc); 81} 82 83 84void 85check_all (mpq_srcptr q, int base, const char *want) 86{ 87 char *s; 88 89 check_one (q, base, want); 90 91 s = __gmp_allocate_strdup (want); 92 strtoupper (s); 93 check_one (q, -base, s); 94 (*__gmp_free_func) (s, strlen(s)+1); 95} 96 97void 98check_data (void) 99{ 100 static const struct { 101 int base; 102 const char *num; 103 const char *den; 104 const char *want; 105 } data[] = { 106 { 10, "0", "1", "0" }, 107 { 10, "1", "1", "1" }, 108 109 { 16, "ffffffff", "1", "ffffffff" }, 110 { 16, "ffffffffffffffff", "1", "ffffffffffffffff" }, 111 112 { 16, "1", "ffffffff", "1/ffffffff" }, 113 { 16, "1", "ffffffffffffffff", "1/ffffffffffffffff" }, 114 { 16, "1", "10000000000000003", "1/10000000000000003" }, 115 116 { 10, "12345678901234567890", "9876543210987654323", 117 "12345678901234567890/9876543210987654323" }, 118 }; 119 120 mpq_t q; 121 int i; 122 123 mpq_init (q); 124 for (i = 0; i < numberof (data); i++) 125 { 126 mpz_set_str_or_abort (mpq_numref(q), data[i].num, data[i].base); 127 mpz_set_str_or_abort (mpq_denref(q), data[i].den, data[i].base); 128 check_all (q, data[i].base, data[i].want); 129 } 130 mpq_clear (q); 131} 132 133 134int 135main (void) 136{ 137 tests_start (); 138 139 check_data (); 140 141 tests_end (); 142 exit (0); 143} 144