4472f7588845d666fc4a91a0e92b52d02d241d7e
[debian/amanda] / installcheck / bigint.pl
1 # Copyright (c) 2005-2008 Zmanda Inc.  All Rights Reserved.
2 #
3 # This program is free software; you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License version 2 as published
5 # by the Free Software Foundation.
6 #
7 # This program is distributed in the hope that it will be useful, but
8 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
10 # for more details.
11 #
12 # You should have received a copy of the GNU General Public License along
13 # with this program; if not, write to the Free Software Foundation, Inc.,
14 # 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 #
16 # Contact information: Zmanda Inc, 465 S Mathlida Ave, Suite 300
17 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
18
19 use Test::More tests => 74;
20
21 use lib "@amperldir@";
22 use Amanda::Tests;
23 use Math::BigInt;
24 use Amanda::BigIntCompat;
25
26 # define some constants; Perl doesn't have native 64-bit numbers, so
27 # none are tested 
28 $G_MAXUINT64_bigint = Math::BigInt->new('18446744073709551615');
29 $G_MAXINT64_bigint = Math::BigInt->new('9223372036854775807');
30 $G_MININT64_bigint = Math::BigInt->new('-9223372036854775808');
31
32 $G_MAXUINT32_native = 2 ** 32 - 1;
33 $G_MAXUINT32_double = 2.0 ** 32 - 1;
34 $G_MAXUINT32_bigint = Math::BigInt->new('4294967295');
35 $G_MAXINT32_native = 2 ** 31 - 1;
36 $G_MAXINT32_double = 2.0 ** 31 - 1;
37 $G_MAXINT32_bigint = Math::BigInt->new('2147483647');
38 $G_MININT32_native = - 2 ** 31;
39 $G_MININT32_double = - 2.0 ** 31;
40 $G_MININT32_bigint = Math::BigInt->new('-2147483648');
41
42 $G_MAXUINT16_native = 2 ** 16 - 1;
43 $G_MAXUINT16_double = 2.0 ** 16 - 1;
44 $G_MAXUINT16_bigint = Math::BigInt->new('65535');
45 $G_MAXINT16_native = 2 ** 15 - 1;
46 $G_MAXINT16_double = 2.0 ** 15 - 1;
47 $G_MAXINT16_bigint = Math::BigInt->new('32767');
48 $G_MININT16_native = - 2 ** 15;
49 $G_MININT16_double = - 2.0 ** 15;
50 $G_MININT16_bigint = Math::BigInt->new('-32768');
51
52 $G_MAXUINT8_native = 2 ** 8 - 1;
53 $G_MAXUINT8_double = 2.0 ** 8 - 1;
54 $G_MAXUINT8_bigint = Math::BigInt->new('255');
55 $G_MAXINT8_native = 2 ** 7 - 1;
56 $G_MAXINT8_double = 2.0 ** 7 - 1;
57 $G_MAXINT8_bigint = Math::BigInt->new('127');
58 $G_MININT8_native = - 2 ** 7;
59 $G_MININT8_double = - 2.0 ** 7;
60 $G_MININT8_bigint = Math::BigInt->new('-128');
61
62 # first test "taking" integers -- Perl -> C
63
64 is(Amanda::Tests::take_guint64(0), "ZERO", 
65     "Perl->C guint64 0");
66 is(Amanda::Tests::take_guint64($G_MAXUINT64_bigint), "MAX", 
67     "Perl->C guint64 bigint MAX ($G_MAXUINT64_bigint)");
68 is(Amanda::Tests::take_gint64(0), "ZERO", 
69     "Perl->C gint64 0");
70 is(Amanda::Tests::take_gint64($G_MAXINT64_bigint), "MAX", 
71     "Perl->C gint64 bigint MAX ($G_MAXINT64_bigint)");
72 is(Amanda::Tests::take_gint64($G_MININT64_bigint), "MIN", 
73     "Perl->C gint64 bigint MIN ($G_MININT64_bigint)");
74
75 is(Amanda::Tests::take_guint32(0), "ZERO", 
76     "Perl->C guint32 0");
77 is(Amanda::Tests::take_guint32($G_MAXUINT32_bigint), "MAX", 
78     "Perl->C guint32 bigint MAX ($G_MAXUINT32_bigint)");
79 is(Amanda::Tests::take_guint32($G_MAXUINT32_native), "MAX", 
80     "Perl->C guint32 native MAX ($G_MAXUINT32_native)");
81 is(Amanda::Tests::take_guint32($G_MAXUINT32_double), "MAX", 
82     "Perl->C guint32 double MAX ($G_MAXUINT32_double)");
83 is(Amanda::Tests::take_gint32(0), "ZERO", 
84     "Perl->C gint32 0");
85 is(Amanda::Tests::take_gint32($G_MAXINT32_bigint), "MAX", 
86     "Perl->C gint32 bigint MAX ($G_MAXINT32_bigint)");
87 is(Amanda::Tests::take_gint32($G_MAXINT32_native), "MAX", 
88     "Perl->C gint32 native MAX ($G_MAXINT32_native)");
89 is(Amanda::Tests::take_gint32($G_MAXINT32_double), "MAX", 
90     "Perl->C gint32 double MAX ($G_MAXINT32_double)");
91 is(Amanda::Tests::take_gint32($G_MININT32_bigint), "MIN", 
92     "Perl->C gint32 bigint MIN ($G_MININT32_bigint)");
93 is(Amanda::Tests::take_gint32($G_MININT32_native), "MIN", 
94     "Perl->C gint32 native MIN ($G_MININT32_native)");
95 is(Amanda::Tests::take_gint32($G_MININT32_double), "MIN", 
96     "Perl->C gint32 double MIN ($G_MININT32_double)");
97
98 is(Amanda::Tests::take_guint16(0), "ZERO", 
99     "Perl->C guint16 0");
100 is(Amanda::Tests::take_guint16($G_MAXUINT16_bigint), "MAX", 
101     "Perl->C guint16 bigint MAX ($G_MAXUINT16_bigint)");
102 is(Amanda::Tests::take_guint16($G_MAXUINT16_native), "MAX", 
103     "Perl->C guint16 native MAX ($G_MAXUINT16_native)");
104 is(Amanda::Tests::take_guint16($G_MAXUINT16_double), "MAX", 
105     "Perl->C guint16 double MAX ($G_MAXUINT16_double)");
106 is(Amanda::Tests::take_gint16(0), "ZERO", 
107     "Perl->C gint16 0");
108 is(Amanda::Tests::take_gint16($G_MAXINT16_bigint), "MAX", 
109     "Perl->C gint16 bigint MAX ($G_MAXINT16_bigint)");
110 is(Amanda::Tests::take_gint16($G_MAXINT16_native), "MAX", 
111     "Perl->C gint16 native MAX ($G_MAXINT16_native)");
112 is(Amanda::Tests::take_gint16($G_MAXINT16_double), "MAX", 
113     "Perl->C gint16 double MAX ($G_MAXINT16_double)");
114 is(Amanda::Tests::take_gint16($G_MININT16_bigint), "MIN", 
115     "Perl->C gint16 bigint MIN ($G_MININT16_bigint)");
116 is(Amanda::Tests::take_gint16($G_MININT16_native), "MIN", 
117     "Perl->C gint16 native MIN ($G_MININT16_native)");
118 is(Amanda::Tests::take_gint16($G_MININT16_double), "MIN", 
119     "Perl->C gint16 double MIN ($G_MININT16_double)");
120
121 is(Amanda::Tests::take_guint8(0), "ZERO", 
122     "Perl->C guint8 0");
123 is(Amanda::Tests::take_guint8($G_MAXUINT8_bigint), "MAX", 
124     "Perl->C guint8 bigint MAX ($G_MAXUINT8_bigint)");
125 is(Amanda::Tests::take_guint8($G_MAXUINT8_native), "MAX", 
126     "Perl->C guint8 native MAX ($G_MAXUINT8_native)");
127 is(Amanda::Tests::take_guint8($G_MAXUINT8_double), "MAX", 
128     "Perl->C guint8 double MAX ($G_MAXUINT8_double)");
129 is(Amanda::Tests::take_gint8(0), "ZERO", 
130     "Perl->C gint8 0");
131 is(Amanda::Tests::take_gint8($G_MAXINT8_bigint), "MAX", 
132     "Perl->C gint8 bigint MAX ($G_MAXINT8_bigint)");
133 is(Amanda::Tests::take_gint8($G_MAXINT8_native), "MAX", 
134     "Perl->C gint8 native MAX ($G_MAXINT8_native)");
135 is(Amanda::Tests::take_gint8($G_MAXINT8_double), "MAX", 
136     "Perl->C gint8 double MAX ($G_MAXINT8_double)");
137 is(Amanda::Tests::take_gint8($G_MININT8_bigint), "MIN", 
138     "Perl->C gint8 bigint MIN ($G_MININT8_bigint)");
139 is(Amanda::Tests::take_gint8($G_MININT8_native), "MIN", 
140     "Perl->C gint8 native MIN ($G_MININT8_native)");
141 is(Amanda::Tests::take_gint8($G_MININT8_double), "MIN", 
142     "Perl->C gint8 double MIN ($G_MININT8_double)");
143
144 # now test giving integers -- C -> Perl
145
146 is(Amanda::Tests::give_guint64("0"), 0, "C -> Perl guint64 0");
147 is(Amanda::Tests::give_guint64("+"), $G_MAXUINT64_bigint, "C -> Perl guint64 MAX (always bigint)");
148 is(Amanda::Tests::give_gint64("0"), 0, "C -> Perl gint64 0");
149 is(Amanda::Tests::give_gint64("+"), $G_MAXINT64_bigint, "C -> Perl gint64 MAX (always bigint)");
150 is(Amanda::Tests::give_gint64("-"), $G_MININT64_bigint, "C -> Perl gint64 MIN (always bigint)");
151
152 is(Amanda::Tests::give_guint32("0"), 0, "C -> Perl guint32 0");
153 is(Amanda::Tests::give_guint32("+"), $G_MAXUINT32_bigint, "C -> Perl guint32 MAX (always bigint)");
154 is(Amanda::Tests::give_gint32("0"), 0, "C -> Perl gint32 0");
155 is(Amanda::Tests::give_gint32("+"), $G_MAXINT32_bigint, "C -> Perl gint32 MAX (always bigint)");
156 is(Amanda::Tests::give_gint32("-"), $G_MININT32_bigint, "C -> Perl gint32 MIN (always bigint)");
157
158 is(Amanda::Tests::give_guint16("0"), 0, "C -> Perl guint16 0");
159 is(Amanda::Tests::give_guint16("+"), $G_MAXUINT16_bigint, "C -> Perl guint16 MAX (always bigint)");
160 is(Amanda::Tests::give_gint16("0"), 0, "C -> Perl gint16 0");
161 is(Amanda::Tests::give_gint16("+"), $G_MAXINT16_bigint, "C -> Perl gint16 MAX (always bigint)");
162 is(Amanda::Tests::give_gint16("-"), $G_MININT16_bigint, "C -> Perl gint16 MIN (always bigint)");
163
164 is(Amanda::Tests::give_guint8("0"), 0, "C -> Perl guint8 0");
165 is(Amanda::Tests::give_guint8("+"), $G_MAXUINT8_bigint, "C -> Perl guint8 MAX (always bigint)");
166 is(Amanda::Tests::give_gint8("0"), 0, "C -> Perl gint8 0");
167 is(Amanda::Tests::give_gint8("+"), $G_MAXINT8_bigint, "C -> Perl gint8 MAX (always bigint)");
168 is(Amanda::Tests::give_gint8("-"), $G_MININT8_bigint, "C -> Perl gint8 MIN (always bigint)");
169
170 # finally, test overflows in Perl -> C conversions; these all croak(), so we capture the errors
171 # with an eval {}
172
173 eval { Amanda::Tests::take_gint64($G_MAXINT64_bigint+1); };
174 like($@, qr/Expected a signed 64-bit value or smaller/,
175     "gint64 rejects numbers greater than max");
176 eval { Amanda::Tests::take_gint64($G_MININT64_bigint-1); };
177 like($@, qr/Expected a signed 64-bit value or smaller/,
178     "gint64 rejects numbers less than min");
179 eval { Amanda::Tests::take_guint64($G_MAXUINT64_bigint+1); };
180 like($@, qr/Expected an unsigned 64-bit value or smaller/, 
181     "guint64 rejects numbers greater than max");
182 eval { Amanda::Tests::take_guint64(-1); };
183 like($@, qr/Expected an unsigned value, got a negative integer/, 
184     "guint64 rejects numbers less than zero");
185
186 eval { Amanda::Tests::take_gint32($G_MAXINT32_native+1); };
187 like($@, qr/Expected a 32-bit integer; value out of range/, 
188     "gint32 rejects numbers greater than max");
189 eval { Amanda::Tests::take_gint32($G_MININT32_native-1); };
190 like($@, qr/Expected a 32-bit integer; value out of range/, 
191     "gint32 rejects numbers less than min");
192 eval { Amanda::Tests::take_guint32($G_MAXUINT32_native+1); };
193 like($@, qr/Expected a 32-bit unsigned integer/, 
194     "guint32 rejects numbers greater than max");
195 eval { Amanda::Tests::take_guint32(-1); };
196 like($@, qr/Expected an unsigned value, got a negative integer/, 
197     "guint32 rejects numbers less than zero");
198
199 eval { Amanda::Tests::take_gint16($G_MAXINT16_native+1); };
200 like($@, qr/Expected a 16-bit integer; value out of range/, 
201     "gint16 rejects numbers greater than max");
202 eval { Amanda::Tests::take_gint16($G_MININT16_native-1); };
203 like($@, qr/Expected a 16-bit integer; value out of range/, 
204     "gint16 rejects numbers less than min");
205 eval { Amanda::Tests::take_guint16($G_MAXUINT16_native+1); };
206 like($@, qr/Expected a 16-bit unsigned integer/,
207     "guint16 rejects numbers greater than max");
208 eval { Amanda::Tests::take_guint16(-1); };
209 like($@, qr/Expected an unsigned value, got a negative integer/, 
210     "guint16 rejects numbers less than zero");
211
212 eval { Amanda::Tests::take_gint8($G_MAXINT8_native+1); };
213 like($@, qr/Expected a 8-bit integer; value out of range/, 
214     "gint8 rejects numbers greater than max");
215 eval { Amanda::Tests::take_gint8($G_MININT8_native-1); };
216 like($@, qr/Expected a 8-bit integer; value out of range/, 
217     "gint8 rejects numbers less than min");
218 eval { Amanda::Tests::take_guint8($G_MAXUINT8_native+1); };
219 like($@, qr/Expected a 8-bit unsigned integer/,
220     "guint8 rejects numbers greater than max");
221 eval { Amanda::Tests::take_guint8(-1); };
222 like($@, qr/Expected an unsigned value, got a negative integer/, 
223     "guint8 rejects numbers less than zero");