File: Math-UInt128.t

package info (click to toggle)
libmath-int128-perl 0.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 604 kB
  • sloc: perl: 2,614; ansic: 340; makefile: 3
file content (185 lines) | stat: -rwxr-xr-x 3,136 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/perl

use strict;
use warnings;

use Test::More 0.88;

use Math::Int128 qw(uint128 uint128_to_number
                    net_to_uint128 uint128_to_net
                    native_to_uint128 uint128_to_native);

my $i = uint128('1234567890123456789');
my $j = $i + 1;
my $k = (uint128(1) << 60) + 255;

# 1
ok($i == '1234567890123456789');

ok($j - 1 == '1234567890123456789');

ok (($k & 127) == 127);

ok (($k & 256) == 0);

# 5
ok ($i * 2 == $j + $j - 2);

ok ($i * $i * $i * $i == ($j * $j - 2 * $j + 1) * ($j * $j - 2 * $j + 1));

ok (($i / $j) == 0);

ok ($j / $i == 1);

ok ($i % $j == $i);

# 10
ok ($j % $i == 1);

ok (($j += 1) == $i + 2);

ok ($j == $i + 2);

ok (($j -= 3) == $i - 1);

ok ($j == $i - 1);

$j = $i;
# 15
ok (($j *= 2) == $i << 1);

ok (($j >> 1) == $i);

ok (($j / 2) == $i);

$j = $i + 2;

ok (($j %= $i) == 2);

ok ($j == 2);

# 20
ok (($j <=> $i) < 0);

ok (($i <=> $j) > 0);

ok (($i <=> $i) == 0);

ok (($j <=> 2) == 0);

ok ($j < $i);

# 25
ok ($j <= $i);

ok (!($i < $j));

ok (!($i <= $j));

ok ($i <= $i);

ok ($j >= $j);

# 30
ok ($i > $j);

ok ($i >= $j);

ok (!($j > $i));

ok (!($j >= $i));

ok (int(log(uint128(1)<<50)/log(2)+0.001) == 50);

# 35

my $l = uint128("127131031961723452345");

is ("$l", "127131031961723452345", "string to/from int128 conversion");

ok (native_to_uint128(uint128_to_native(1)) == 1);

ok (native_to_uint128(uint128_to_native(0)) == 0);

ok (native_to_uint128(uint128_to_native(12343)) == 12343);

ok (native_to_uint128(uint128_to_native($l)) == $l);

# 40

ok (native_to_uint128(uint128_to_native($j)) == $j);

ok (native_to_uint128(uint128_to_native($i)) == $i);

ok (net_to_uint128(uint128_to_net(1)) == 1);

ok (net_to_uint128(uint128_to_net(0)) == 0);

ok (net_to_uint128(uint128_to_net(12343)) == 12343);

# 45

ok (net_to_uint128(uint128_to_net($l)) == $l);

ok (net_to_uint128(uint128_to_net($j)) == $j);

ok (net_to_uint128(uint128_to_net($i)) == $i);

{
    use integer;
    my $int = uint128(255);
    ok($int == 255);
    $int <<= 32;
    $int |= 4294967295;
    ok($int == '1099511627775');
}

my $two  = uint128(2);
my $four = uint128(4);
is ($two  ** -1, 0, "signed pow 2**-1");
is ($four ** -1, 0, "signed pow 4**-1");

sub slow_pow_uint128 {
    my ($a, $b) = @_;
    my $acu = uint128(1);
    $acu *= $a for 1..$b;
    $acu;
}

sub slow_pow_nv {
    my ($base, $exp) = @_;
    my $r = 1;
    $r *= $base for 1..$exp;
    $r
}

my $max = (((uint128(2) ** 127) - 1) * 2) + 1;
for my $j (0..127) {
    my $one = uint128(1);

    is($two  ** $j, $one <<     $j, "signed pow 2**$j");
    is($four ** $j, $one << 2 * $j, "signed pow 4**$j") if $j < 64;

    is($one << $j, $two ** $j, "$one << $j");

    $one <<= $j;
    is($one, $two ** $j, "$one <<= $j");

    next unless $j;

    my $pow2j = slow_pow_nv(2, $j);

    is($max >> $j, $max / $pow2j, "max uint128 >> $j");

    my $copy = uint128($max);
    $copy >>= $j;
    is($copy, $max / $pow2j, "max uint128 >>= $j");
}

for my $i (5..9) {
    for my $j (0..40) { # 9**40 < 2**127
        is(uint128($i) ** $j, slow_pow_uint128($i, $j), "signed pow $i ** $j");
    }
}

done_testing();