#
source:
trunk/third/gmp/tests/mpz/t-fits.c
@
18191

Revision 18191, 5.1 KB checked in by ghudson, 22 years ago (diff) |
---|

Line | |
---|---|

1 | /* Test mpz_fits_*_p */ |

2 | |

3 | /* |

4 | Copyright 2001 Free Software Foundation, Inc. |

5 | |

6 | This file is part of the GNU MP Library. |

7 | |

8 | The GNU MP Library is free software; you can redistribute it and/or modify |

9 | it under the terms of the GNU Lesser General Public License as published by |

10 | the Free Software Foundation; either version 2.1 of the License, or (at your |

11 | option) any later version. |

12 | |

13 | The GNU MP Library is distributed in the hope that it will be useful, but |

14 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |

15 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |

16 | License for more details. |

17 | |

18 | You should have received a copy of the GNU Lesser General Public License |

19 | along with the GNU MP Library; see the file COPYING.LIB. If not, write to |

20 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |

21 | MA 02111-1307, USA. |

22 | */ |

23 | |

24 | #include <stdio.h> |

25 | #include <stdlib.h> |

26 | #include "gmp.h" |

27 | #include "gmp-impl.h" |

28 | #include "tests.h" |

29 | |

30 | |

31 | /* Nothing sophisticated here, just exercise mpz_fits_*_p on a small amount |

32 | of data. */ |

33 | |

34 | #define EXPECT_S(fun,name,answer) \ |

35 | got = fun (z); \ |

36 | if (got != answer) \ |

37 | { \ |

38 | printf ("%s (%s) got %d want %d\n", name, expr, got, answer); \ |

39 | printf (" z size %d\n", SIZ(z)); \ |

40 | printf (" z dec "); mpz_out_str (stdout, 10, z); printf ("\n"); \ |

41 | printf (" z hex "); mpz_out_str (stdout, 16, z); printf ("\n"); \ |

42 | error = 1; \ |

43 | } |

44 | |

45 | #if HAVE_STRINGIZE |

46 | #define EXPECT(fun,answer) EXPECT_S(fun,#fun,answer) |

47 | #else |

48 | #define EXPECT(fun,answer) EXPECT_S(fun,"fun",answer) |

49 | #endif |

50 | |

51 | int |

52 | main (void) |

53 | { |

54 | mpz_t z; |

55 | int got; |

56 | const char *expr; |

57 | int error = 0; |

58 | |

59 | tests_start (); |

60 | mpz_init (z); |

61 | |

62 | mpz_set_ui (z, 0L); |

63 | expr = "0"; |

64 | EXPECT (mpz_fits_ulong_p, 1); |

65 | EXPECT (mpz_fits_uint_p, 1); |

66 | EXPECT (mpz_fits_ushort_p, 1); |

67 | EXPECT (mpz_fits_slong_p, 1); |

68 | EXPECT (mpz_fits_sint_p, 1); |

69 | EXPECT (mpz_fits_sshort_p, 1); |

70 | |

71 | mpz_set_ui (z, 1L); |

72 | expr = "1"; |

73 | EXPECT (mpz_fits_ulong_p, 1); |

74 | EXPECT (mpz_fits_uint_p, 1); |

75 | EXPECT (mpz_fits_ushort_p, 1); |

76 | EXPECT (mpz_fits_slong_p, 1); |

77 | EXPECT (mpz_fits_sint_p, 1); |

78 | EXPECT (mpz_fits_sshort_p, 1); |

79 | |

80 | mpz_set_si (z, -1L); |

81 | expr = "-1"; |

82 | EXPECT (mpz_fits_ulong_p, 0); |

83 | EXPECT (mpz_fits_uint_p, 0); |

84 | EXPECT (mpz_fits_ushort_p, 0); |

85 | EXPECT (mpz_fits_slong_p, 1); |

86 | EXPECT (mpz_fits_sint_p, 1); |

87 | EXPECT (mpz_fits_sshort_p, 1); |

88 | |

89 | mpz_set_ui (z, 1L); |

90 | mpz_mul_2exp (z, z, 5L*BITS_PER_MP_LIMB); |

91 | expr = "2^(5*BPML)"; |

92 | EXPECT (mpz_fits_ulong_p, 0); |

93 | EXPECT (mpz_fits_uint_p, 0); |

94 | EXPECT (mpz_fits_ushort_p, 0); |

95 | EXPECT (mpz_fits_slong_p, 0); |

96 | EXPECT (mpz_fits_sint_p, 0); |

97 | EXPECT (mpz_fits_sshort_p, 0); |

98 | |

99 | |

100 | mpz_set_ui (z, (unsigned long) USHRT_MAX); |

101 | expr = "USHRT_MAX"; |

102 | EXPECT (mpz_fits_ulong_p, 1); |

103 | EXPECT (mpz_fits_uint_p, 1); |

104 | EXPECT (mpz_fits_ushort_p, 1); |

105 | |

106 | mpz_set_ui (z, (unsigned long) USHRT_MAX); |

107 | mpz_add_ui (z, z, 1L); |

108 | expr = "USHRT_MAX + 1"; |

109 | EXPECT (mpz_fits_ushort_p, 0); |

110 | |

111 | |

112 | mpz_set_ui (z, (unsigned long) UINT_MAX); |

113 | expr = "UINT_MAX"; |

114 | EXPECT (mpz_fits_ulong_p, 1); |

115 | EXPECT (mpz_fits_uint_p, 1); |

116 | |

117 | mpz_set_ui (z, (unsigned long) UINT_MAX); |

118 | mpz_add_ui (z, z, 1L); |

119 | expr = "UINT_MAX + 1"; |

120 | EXPECT (mpz_fits_uint_p, 0); |

121 | |

122 | |

123 | mpz_set_ui (z, ULONG_MAX); |

124 | expr = "ULONG_MAX"; |

125 | EXPECT (mpz_fits_ulong_p, 1); |

126 | |

127 | mpz_set_ui (z, ULONG_MAX); |

128 | mpz_add_ui (z, z, 1L); |

129 | expr = "ULONG_MAX + 1"; |

130 | EXPECT (mpz_fits_ulong_p, 0); |

131 | |

132 | |

133 | mpz_set_si (z, (long) SHRT_MAX); |

134 | expr = "SHRT_MAX"; |

135 | EXPECT (mpz_fits_slong_p, 1); |

136 | EXPECT (mpz_fits_sint_p, 1); |

137 | EXPECT (mpz_fits_sshort_p, 1); |

138 | |

139 | mpz_set_si (z, (long) SHRT_MAX); |

140 | mpz_add_ui (z, z, 1L); |

141 | expr = "SHRT_MAX + 1"; |

142 | EXPECT (mpz_fits_sshort_p, 0); |

143 | |

144 | |

145 | mpz_set_si (z, (long) INT_MAX); |

146 | expr = "INT_MAX"; |

147 | EXPECT (mpz_fits_slong_p, 1); |

148 | EXPECT (mpz_fits_sint_p, 1); |

149 | |

150 | mpz_set_si (z, (long) INT_MAX); |

151 | mpz_add_ui (z, z, 1L); |

152 | expr = "INT_MAX + 1"; |

153 | EXPECT (mpz_fits_sint_p, 0); |

154 | |

155 | |

156 | mpz_set_si (z, LONG_MAX); |

157 | expr = "LONG_MAX"; |

158 | EXPECT (mpz_fits_slong_p, 1); |

159 | |

160 | mpz_set_si (z, LONG_MAX); |

161 | mpz_add_ui (z, z, 1L); |

162 | expr = "LONG_MAX + 1"; |

163 | EXPECT (mpz_fits_slong_p, 0); |

164 | |

165 | |

166 | mpz_set_si (z, (long) SHRT_MIN); |

167 | expr = "SHRT_MIN"; |

168 | EXPECT (mpz_fits_slong_p, 1); |

169 | EXPECT (mpz_fits_sint_p, 1); |

170 | EXPECT (mpz_fits_sshort_p, 1); |

171 | |

172 | mpz_set_si (z, (long) SHRT_MIN); |

173 | mpz_sub_ui (z, z, 1L); |

174 | expr = "SHRT_MIN + 1"; |

175 | EXPECT (mpz_fits_sshort_p, 0); |

176 | |

177 | |

178 | mpz_set_si (z, (long) INT_MIN); |

179 | expr = "INT_MIN"; |

180 | EXPECT (mpz_fits_slong_p, 1); |

181 | EXPECT (mpz_fits_sint_p, 1); |

182 | |

183 | mpz_set_si (z, (long) INT_MIN); |

184 | mpz_sub_ui (z, z, 1L); |

185 | expr = "INT_MIN + 1"; |

186 | EXPECT (mpz_fits_sint_p, 0); |

187 | |

188 | |

189 | mpz_set_si (z, LONG_MIN); |

190 | expr = "LONG_MIN"; |

191 | EXPECT (mpz_fits_slong_p, 1); |

192 | |

193 | mpz_set_si (z, LONG_MIN); |

194 | mpz_sub_ui (z, z, 1L); |

195 | expr = "LONG_MIN + 1"; |

196 | EXPECT (mpz_fits_slong_p, 0); |

197 | |

198 | |

199 | if (error) |

200 | abort (); |

201 | |

202 | mpz_clear (z); |

203 | tests_end (); |

204 | exit (0); |

205 | } |

**Note:**See TracBrowser for help on using the repository browser.