Imported Upstream version 3.2.0
[debian/amanda] / installcheck / bigint.pl
1 # Copyright (c) 2008, 2010 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. Mathilda Ave., Suite 300
17 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
18
19 use Test::More tests => 74;
20 use strict;
21 use warnings;
22
23 use lib "@amperldir@";
24 use Amanda::Tests;
25 use Math::BigInt;
26 use Amanda::BigIntCompat;
27
28 # define some constants; Perl doesn't have native 64-bit numbers, so
29 # none are tested 
30 my $G_MAXUINT64_bigint = Math::BigInt->new('18446744073709551615');
31 my $G_MAXINT64_bigint = Math::BigInt->new('9223372036854775807');
32 my $G_MININT64_bigint = Math::BigInt->new('-9223372036854775808');
33
34 my $G_MAXUINT32_native = 2 ** 32 - 1;
35 my $G_MAXUINT32_double = 2.0 ** 32 - 1;
36 my $G_MAXUINT32_bigint = Math::BigInt->new('4294967295');
37 my $G_MAXINT32_native = 2 ** 31 - 1;
38 my $G_MAXINT32_double = 2.0 ** 31 - 1;
39 my $G_MAXINT32_bigint = Math::BigInt->new('2147483647');
40 my $G_MININT32_native = - 2 ** 31;
41 my $G_MININT32_double = - 2.0 ** 31;
42 my $G_MININT32_bigint = Math::BigInt->new('-2147483648');
43
44 my $G_MAXUINT16_native = 2 ** 16 - 1;
45 my $G_MAXUINT16_double = 2.0 ** 16 - 1;
46 my $G_MAXUINT16_bigint = Math::BigInt->new('65535');
47 my $G_MAXINT16_native = 2 ** 15 - 1;
48 my $G_MAXINT16_double = 2.0 ** 15 - 1;
49 my $G_MAXINT16_bigint = Math::BigInt->new('32767');
50 my $G_MININT16_native = - 2 ** 15;
51 my $G_MININT16_double = - 2.0 ** 15;
52 my $G_MININT16_bigint = Math::BigInt->new('-32768');
53
54 my $G_MAXUINT8_native = 2 ** 8 - 1;
55 my $G_MAXUINT8_double = 2.0 ** 8 - 1;
56 my $G_MAXUINT8_bigint = Math::BigInt->new('255');
57 my $G_MAXINT8_native = 2 ** 7 - 1;
58 my $G_MAXINT8_double = 2.0 ** 7 - 1;
59 my $G_MAXINT8_bigint = Math::BigInt->new('127');
60 my $G_MININT8_native = - 2 ** 7;
61 my $G_MININT8_double = - 2.0 ** 7;
62 my $G_MININT8_bigint = Math::BigInt->new('-128');
63
64 # first test "taking" integers -- Perl -> C
65
66 is(Amanda::Tests::take_guint64(0), "ZERO", 
67     "Perl->C guint64 0");
68 is(Amanda::Tests::take_guint64($G_MAXUINT64_bigint), "MAX", 
69     "Perl->C guint64 bigint MAX ($G_MAXUINT64_bigint)");
70 is(Amanda::Tests::take_gint64(0), "ZERO", 
71     "Perl->C gint64 0");
72 is(Amanda::Tests::take_gint64($G_MAXINT64_bigint), "MAX", 
73     "Perl->C gint64 bigint MAX ($G_MAXINT64_bigint)");
74 is(Amanda::Tests::take_gint64($G_MININT64_bigint), "MIN", 
75     "Perl->C gint64 bigint MIN ($G_MININT64_bigint)");
76
77 is(Amanda::Tests::take_guint32(0), "ZERO", 
78     "Perl->C guint32 0");
79 is(Amanda::Tests::take_guint32($G_MAXUINT32_bigint), "MAX", 
80     "Perl->C guint32 bigint MAX ($G_MAXUINT32_bigint)");
81 is(Amanda::Tests::take_guint32($G_MAXUINT32_native), "MAX", 
82     "Perl->C guint32 native MAX ($G_MAXUINT32_native)");
83 is(Amanda::Tests::take_guint32($G_MAXUINT32_double), "MAX", 
84     "Perl->C guint32 double MAX ($G_MAXUINT32_double)");
85 is(Amanda::Tests::take_gint32(0), "ZERO", 
86     "Perl->C gint32 0");
87 is(Amanda::Tests::take_gint32($G_MAXINT32_bigint), "MAX", 
88     "Perl->C gint32 bigint MAX ($G_MAXINT32_bigint)");
89 is(Amanda::Tests::take_gint32($G_MAXINT32_native), "MAX", 
90     "Perl->C gint32 native MAX ($G_MAXINT32_native)");
91 is(Amanda::Tests::take_gint32($G_MAXINT32_double), "MAX", 
92     "Perl->C gint32 double MAX ($G_MAXINT32_double)");
93 is(Amanda::Tests::take_gint32($G_MININT32_bigint), "MIN", 
94     "Perl->C gint32 bigint MIN ($G_MININT32_bigint)");
95 is(Amanda::Tests::take_gint32($G_MININT32_native), "MIN", 
96     "Perl->C gint32 native MIN ($G_MININT32_native)");
97 is(Amanda::Tests::take_gint32($G_MININT32_double), "MIN", 
98     "Perl->C gint32 double MIN ($G_MININT32_double)");
99
100 is(Amanda::Tests::take_guint16(0), "ZERO", 
101     "Perl->C guint16 0");
102 is(Amanda::Tests::take_guint16($G_MAXUINT16_bigint), "MAX", 
103     "Perl->C guint16 bigint MAX ($G_MAXUINT16_bigint)");
104 is(Amanda::Tests::take_guint16($G_MAXUINT16_native), "MAX", 
105     "Perl->C guint16 native MAX ($G_MAXUINT16_native)");
106 is(Amanda::Tests::take_guint16($G_MAXUINT16_double), "MAX", 
107     "Perl->C guint16 double MAX ($G_MAXUINT16_double)");
108 is(Amanda::Tests::take_gint16(0), "ZERO", 
109     "Perl->C gint16 0");
110 is(Amanda::Tests::take_gint16($G_MAXINT16_bigint), "MAX", 
111     "Perl->C gint16 bigint MAX ($G_MAXINT16_bigint)");
112 is(Amanda::Tests::take_gint16($G_MAXINT16_native), "MAX", 
113     "Perl->C gint16 native MAX ($G_MAXINT16_native)");
114 is(Amanda::Tests::take_gint16($G_MAXINT16_double), "MAX", 
115     "Perl->C gint16 double MAX ($G_MAXINT16_double)");
116 is(Amanda::Tests::take_gint16($G_MININT16_bigint), "MIN", 
117     "Perl->C gint16 bigint MIN ($G_MININT16_bigint)");
118 is(Amanda::Tests::take_gint16($G_MININT16_native), "MIN", 
119     "Perl->C gint16 native MIN ($G_MININT16_native)");
120 is(Amanda::Tests::take_gint16($G_MININT16_double), "MIN", 
121     "Perl->C gint16 double MIN ($G_MININT16_double)");
122
123 is(Amanda::Tests::take_guint8(0), "ZERO", 
124     "Perl->C guint8 0");
125 is(Amanda::Tests::take_guint8($G_MAXUINT8_bigint), "MAX", 
126     "Perl->C guint8 bigint MAX ($G_MAXUINT8_bigint)");
127 is(Amanda::Tests::take_guint8($G_MAXUINT8_native), "MAX", 
128     "Perl->C guint8 native MAX ($G_MAXUINT8_native)");
129 is(Amanda::Tests::take_guint8($G_MAXUINT8_double), "MAX", 
130     "Perl->C guint8 double MAX ($G_MAXUINT8_double)");
131 is(Amanda::Tests::take_gint8(0), "ZERO", 
132     "Perl->C gint8 0");
133 is(Amanda::Tests::take_gint8($G_MAXINT8_bigint), "MAX", 
134     "Perl->C gint8 bigint MAX ($G_MAXINT8_bigint)");
135 is(Amanda::Tests::take_gint8($G_MAXINT8_native), "MAX", 
136     "Perl->C gint8 native MAX ($G_MAXINT8_native)");
137 is(Amanda::Tests::take_gint8($G_MAXINT8_double), "MAX", 
138     "Perl->C gint8 double MAX ($G_MAXINT8_double)");
139 is(Amanda::Tests::take_gint8($G_MININT8_bigint), "MIN", 
140     "Perl->C gint8 bigint MIN ($G_MININT8_bigint)");
141 is(Amanda::Tests::take_gint8($G_MININT8_native), "MIN", 
142     "Perl->C gint8 native MIN ($G_MININT8_native)");
143 is(Amanda::Tests::take_gint8($G_MININT8_double), "MIN", 
144     "Perl->C gint8 double MIN ($G_MININT8_double)");
145
146 # now test giving integers -- C -> Perl
147
148 is(Amanda::Tests::give_guint64("0"), 0, "C -> Perl guint64 0");
149 is(Amanda::Tests::give_guint64("+"), $G_MAXUINT64_bigint, "C -> Perl guint64 MAX (always bigint)");
150 is(Amanda::Tests::give_gint64("0"), 0, "C -> Perl gint64 0");
151 is(Amanda::Tests::give_gint64("+"), $G_MAXINT64_bigint, "C -> Perl gint64 MAX (always bigint)");
152 is(Amanda::Tests::give_gint64("-"), $G_MININT64_bigint, "C -> Perl gint64 MIN (always bigint)");
153
154 is(Amanda::Tests::give_guint32("0"), 0, "C -> Perl guint32 0");
155 is(Amanda::Tests::give_guint32("+"), $G_MAXUINT32_bigint, "C -> Perl guint32 MAX (always bigint)");
156 is(Amanda::Tests::give_gint32("0"), 0, "C -> Perl gint32 0");
157 is(Amanda::Tests::give_gint32("+"), $G_MAXINT32_bigint, "C -> Perl gint32 MAX (always bigint)");
158 is(Amanda::Tests::give_gint32("-"), $G_MININT32_bigint, "C -> Perl gint32 MIN (always bigint)");
159
160 is(Amanda::Tests::give_guint16("0"), 0, "C -> Perl guint16 0");
161 is(Amanda::Tests::give_guint16("+"), $G_MAXUINT16_bigint, "C -> Perl guint16 MAX (always bigint)");
162 is(Amanda::Tests::give_gint16("0"), 0, "C -> Perl gint16 0");
163 is(Amanda::Tests::give_gint16("+"), $G_MAXINT16_bigint, "C -> Perl gint16 MAX (always bigint)");
164 is(Amanda::Tests::give_gint16("-"), $G_MININT16_bigint, "C -> Perl gint16 MIN (always bigint)");
165
166 is(Amanda::Tests::give_guint8("0"), 0, "C -> Perl guint8 0");
167 is(Amanda::Tests::give_guint8("+"), $G_MAXUINT8_bigint, "C -> Perl guint8 MAX (always bigint)");
168 is(Amanda::Tests::give_gint8("0"), 0, "C -> Perl gint8 0");
169 is(Amanda::Tests::give_gint8("+"), $G_MAXINT8_bigint, "C -> Perl gint8 MAX (always bigint)");
170 is(Amanda::Tests::give_gint8("-"), $G_MININT8_bigint, "C -> Perl gint8 MIN (always bigint)");
171
172 # finally, test overflows in Perl -> C conversions; these all croak(), so we capture the errors
173 # with an eval {}
174
175 eval { Amanda::Tests::take_gint64($G_MAXINT64_bigint+1); };
176 like($@, qr/Expected a signed 64-bit value or smaller/,
177     "gint64 rejects numbers greater than max");
178 eval { Amanda::Tests::take_gint64($G_MININT64_bigint-1); };
179 like($@, qr/Expected a signed 64-bit value or smaller/,
180     "gint64 rejects numbers less than min");
181 eval { Amanda::Tests::take_guint64($G_MAXUINT64_bigint+1); };
182 like($@, qr/Expected an unsigned 64-bit value or smaller/, 
183     "guint64 rejects numbers greater than max");
184 eval { Amanda::Tests::take_guint64(-1); };
185 like($@, qr/Expected an unsigned value, got a negative integer/, 
186     "guint64 rejects numbers less than zero");
187
188 eval { Amanda::Tests::take_gint32($G_MAXINT32_native+1); };
189 like($@, qr/Expected a 32-bit integer; value out of range/, 
190     "gint32 rejects numbers greater than max");
191 eval { Amanda::Tests::take_gint32($G_MININT32_native-1); };
192 like($@, qr/Expected a 32-bit integer; value out of range/, 
193     "gint32 rejects numbers less than min");
194 eval { Amanda::Tests::take_guint32($G_MAXUINT32_native+1); };
195 like($@, qr/Expected a 32-bit unsigned integer/, 
196     "guint32 rejects numbers greater than max");
197 eval { Amanda::Tests::take_guint32(-1); };
198 like($@, qr/Expected an unsigned value, got a negative integer/, 
199     "guint32 rejects numbers less than zero");
200
201 eval { Amanda::Tests::take_gint16($G_MAXINT16_native+1); };
202 like($@, qr/Expected a 16-bit integer; value out of range/, 
203     "gint16 rejects numbers greater than max");
204 eval { Amanda::Tests::take_gint16($G_MININT16_native-1); };
205 like($@, qr/Expected a 16-bit integer; value out of range/, 
206     "gint16 rejects numbers less than min");
207 eval { Amanda::Tests::take_guint16($G_MAXUINT16_native+1); };
208 like($@, qr/Expected a 16-bit unsigned integer/,
209     "guint16 rejects numbers greater than max");
210 eval { Amanda::Tests::take_guint16(-1); };
211 like($@, qr/Expected an unsigned value, got a negative integer/, 
212     "guint16 rejects numbers less than zero");
213
214 eval { Amanda::Tests::take_gint8($G_MAXINT8_native+1); };
215 like($@, qr/Expected a 8-bit integer; value out of range/, 
216     "gint8 rejects numbers greater than max");
217 eval { Amanda::Tests::take_gint8($G_MININT8_native-1); };
218 like($@, qr/Expected a 8-bit integer; value out of range/, 
219     "gint8 rejects numbers less than min");
220 eval { Amanda::Tests::take_guint8($G_MAXUINT8_native+1); };
221 like($@, qr/Expected a 8-bit unsigned integer/,
222     "guint8 rejects numbers greater than max");
223 eval { Amanda::Tests::take_guint8(-1); };
224 like($@, qr/Expected an unsigned value, got a negative integer/, 
225     "guint8 rejects numbers less than zero");