/
int-uint.t
130 lines (114 loc) 路 4.32 KB
/
int-uint.t
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
use v6;
use Test;
use lib $?FILE.IO.parent(2).add: 'packages/Test-Helpers';
use Test::Util;
# L<S09/Sized types/Sized low-level types are named most generally by appending the number of bits to a generic low-level type name>
my @inttypes = <int uint> X~ <1 2 4 8 16 32 64>;
push @inttypes, "byte";
@inttypes .= grep: {
use MONKEY-SEE-NO-EVAL;
try EVAL "my $_ \$var = 1; \$var"
};
# nothing to test, we're done
unless @inttypes {
plan 1;
pass "No native types to test yet";
exit;
}
plan 11 * @inttypes + 4;
for @inttypes -> $type {
my ($minval,$maxval) = ::($type).Range.int-bounds;
# TODO: merge this if/else into one test once the fundge isn't needed
if $type eq "uint64" {
#?rakudo.jvm todo 'getting -1 instead of 18446744073709551615'
is EVAL("my $type \$var = $maxval; \$var"), $maxval,
"$type can be $maxval";
} else {
is EVAL("my $type \$var = $maxval; \$var"), $maxval,
"$type can be $maxval";
}
is EVAL("my $type \$var = $minval; \$var"), $minval,
"$type can be $minval";
if $type eq "uint64" {
is EVAL("my $type \$var = $maxval; \$var++; \$var"), $minval,
"$type overflows to $minval";
} elsif $type eq "int64" {
is EVAL("my $type \$var = $maxval; \$var++; \$var"), $minval,
"$type overflows to $minval";
} else {
#?rakudo.jvm todo 'max overflow to min'
is EVAL("my $type \$var = $maxval; \$var++; \$var"), $minval,
"$type overflows to $minval";
}
if $type eq "uint64" {
is EVAL("my $type \$var = $minval; \$var--; \$var"), $maxval,
"$type underflows to $maxval";
} elsif $type eq "int64" {
is EVAL("my $type \$var = $minval; \$var--; \$var"), $maxval,
"$type underflows to $maxval";
} else {
#?rakudo.jvm todo 'underflow to max'
is EVAL("my $type \$var = $minval; \$var--; \$var"), $maxval,
"$type underflows to $maxval";
}
if $type eq "uint64" {
#?rakudo.jvm todo 'setting to more than max'
throws-like { EVAL "my $type \$var = {$maxval+1}" },
Exception,
"setting $type to more than $maxval throws";
} else {
# XXX TODO: merge this if/else into one test once the fudge isn't needed
if $type eq 'int64' {
#?rakudo.jvm todo 'setting more than max throws'
throws-like { EVAL "my $type \$var = {$maxval+1}" },
Exception,
"setting $type to more than $maxval throws";
}
else {
#?rakudo todo 'setting more than max throws'
throws-like { EVAL "my $type \$var = {$maxval+1}" },
Exception,
"setting $type to more than $maxval throws";
}
}
# XXX TODO: merge this if/else into one test once the fudge isn't needed
if $type eq 'int64' {
#?rakudo.jvm todo 'setting less than min throws'
throws-like { EVAL "my $type \$var = {$minval-1}" },
Exception,
"setting $type to less than $minval throws";
}
else {
#?rakudo todo 'setting less than min throws'
throws-like { EVAL "my $type \$var = {$minval-1}" },
Exception,
"setting $type to less than $minval throws";
}
throws-like { EVAL "my $type \$var = 'foo'" },
Exception,
"$type cannot be a string";
throws-like { EVAL "my $type \$var = 42.1" },
Exception,
"$type cannot be non-integer";
throws-like { EVAL "my $type \$var = NaN" },
Exception,
"$type cannot be NaN";
is(EVAL("my $type \$var = 0; \$var++; \$var"), 1, "$type \$var++ works");
is(EVAL("my $type \$var = 1; \$var--; \$var"), 0, "$type \$var-- works");
}
# https://github.com/Raku/old-issue-tracker/issues/5022
#?rakudo.js.browser skip "CUnion doesn't work in the browser we don't have proper NativeCall there"
{
class Overlap is repr('CUnion') {
has uint32 $.u32;
has uint16 $.u16;
has uint8 $.u8;
}
my $overlap = Overlap.new(u32 => 1234567);
is $overlap.u32, 1234567, "uint32 in union is unsigned";
is $overlap.u16, 54919, "uint16 in union is unsigned";
is $overlap.u8, 135, "uint8 in union is unsigned";
}
# https://github.com/Raku/old-issue-tracker/issues/6332
is-eqv byte.Range.int-bounds, (0, 255), "byte.Range works";
# vim: expandtab shiftwidth=4