summaryrefslogtreecommitdiff
path: root/Zend/tests/type_declarations/scalar_return_basic_64bit.phpt
blob: e49b9f5b2ad3648ba7c5c3491fe9367e697e33e8 (plain)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
--TEST--
Return scalar type basics
--SKIPIF--
<?php if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only"); ?>
--FILE--
<?php

$errnames = [
    E_NOTICE => 'E_NOTICE',
    E_WARNING => 'E_WARNING',
];
set_error_handler(function (int $errno, string $errmsg, string $file, int $line) use ($errnames) {
    echo "$errnames[$errno]: $errmsg on line $line\n";
    return true;
});

$functions = [
    'int' => function ($i): int { return $i; },
    'float' => function ($f): float { return $f; },
    'string' => function ($s): string { return $s; },
    'bool' => function ($b): bool { return $b; }
];

class StringCapable {
    public function __toString() {
        return "foobar";
    }
}

$values = [
    1,
    "1",
    1.0,
    1.5,
    "1a",
    "a",
    "",
    PHP_INT_MAX,
    NAN,
    TRUE,
    FALSE,
    NULL,
    [],
    new StdClass,
    new StringCapable,
    fopen("data:text/plain,foobar", "r")
];

foreach ($functions as $type => $function) {
    echo PHP_EOL, "Testing '$type' type:", PHP_EOL;
    foreach ($values as $value) {
        echo "*** Trying ";
        var_dump($value);
        try {
            var_dump($function($value));
        } catch (TypeError $e) {
            echo "*** Caught ", $e->getMessage(), " in ", $e->getFile(), " on line ", $e->getLine(), PHP_EOL;
        }
    }
}

echo PHP_EOL . "Done";
?>
--EXPECTF--
Testing 'int' type:
*** Trying int(1)
int(1)
*** Trying string(1) "1"
int(1)
*** Trying float(1)
int(1)
*** Trying float(1.5)
int(1)
*** Trying string(2) "1a"
*** Caught {closure}(): Return value must be of type int, string returned in %s on line %d
*** Trying string(1) "a"
*** Caught {closure}(): Return value must be of type int, string returned in %s on line %d
*** Trying string(0) ""
*** Caught {closure}(): Return value must be of type int, string returned in %s on line %d
*** Trying int(9223372036854775807)
int(9223372036854775807)
*** Trying float(NAN)
*** Caught {closure}(): Return value must be of type int, float returned in %s on line %d
*** Trying bool(true)
int(1)
*** Trying bool(false)
int(0)
*** Trying NULL
*** Caught {closure}(): Return value must be of type int, null returned in %s on line %d
*** Trying array(0) {
}
*** Caught {closure}(): Return value must be of type int, array returned in %s on line %d
*** Trying object(stdClass)#6 (0) {
}
*** Caught {closure}(): Return value must be of type int, stdClass returned in %s on line %d
*** Trying object(StringCapable)#7 (0) {
}
*** Caught {closure}(): Return value must be of type int, StringCapable returned in %s on line %d
*** Trying resource(5) of type (stream)
*** Caught {closure}(): Return value must be of type int, resource returned in %s on line %d

Testing 'float' type:
*** Trying int(1)
float(1)
*** Trying string(1) "1"
float(1)
*** Trying float(1)
float(1)
*** Trying float(1.5)
float(1.5)
*** Trying string(2) "1a"
*** Caught {closure}(): Return value must be of type float, string returned in %s on line %d
*** Trying string(1) "a"
*** Caught {closure}(): Return value must be of type float, string returned in %s on line %d
*** Trying string(0) ""
*** Caught {closure}(): Return value must be of type float, string returned in %s on line %d
*** Trying int(9223372036854775807)
float(9.223372036854776E+18)
*** Trying float(NAN)
float(NAN)
*** Trying bool(true)
float(1)
*** Trying bool(false)
float(0)
*** Trying NULL
*** Caught {closure}(): Return value must be of type float, null returned in %s on line %d
*** Trying array(0) {
}
*** Caught {closure}(): Return value must be of type float, array returned in %s on line %d
*** Trying object(stdClass)#6 (0) {
}
*** Caught {closure}(): Return value must be of type float, stdClass returned in %s on line %d
*** Trying object(StringCapable)#7 (0) {
}
*** Caught {closure}(): Return value must be of type float, StringCapable returned in %s on line %d
*** Trying resource(5) of type (stream)
*** Caught {closure}(): Return value must be of type float, resource returned in %s on line %d

Testing 'string' type:
*** Trying int(1)
string(1) "1"
*** Trying string(1) "1"
string(1) "1"
*** Trying float(1)
string(1) "1"
*** Trying float(1.5)
string(3) "1.5"
*** Trying string(2) "1a"
string(2) "1a"
*** Trying string(1) "a"
string(1) "a"
*** Trying string(0) ""
string(0) ""
*** Trying int(9223372036854775807)
string(19) "9223372036854775807"
*** Trying float(NAN)
string(3) "NAN"
*** Trying bool(true)
string(1) "1"
*** Trying bool(false)
string(0) ""
*** Trying NULL
*** Caught {closure}(): Return value must be of type string, null returned in %s on line %d
*** Trying array(0) {
}
*** Caught {closure}(): Return value must be of type string, array returned in %s on line %d
*** Trying object(stdClass)#6 (0) {
}
*** Caught {closure}(): Return value must be of type string, stdClass returned in %s on line %d
*** Trying object(StringCapable)#7 (0) {
}
string(6) "foobar"
*** Trying resource(5) of type (stream)
*** Caught {closure}(): Return value must be of type string, resource returned in %s on line %d

Testing 'bool' type:
*** Trying int(1)
bool(true)
*** Trying string(1) "1"
bool(true)
*** Trying float(1)
bool(true)
*** Trying float(1.5)
bool(true)
*** Trying string(2) "1a"
bool(true)
*** Trying string(1) "a"
bool(true)
*** Trying string(0) ""
bool(false)
*** Trying int(9223372036854775807)
bool(true)
*** Trying float(NAN)
bool(true)
*** Trying bool(true)
bool(true)
*** Trying bool(false)
bool(false)
*** Trying NULL
*** Caught {closure}(): Return value must be of type bool, null returned in %s on line %d
*** Trying array(0) {
}
*** Caught {closure}(): Return value must be of type bool, array returned in %s on line %d
*** Trying object(stdClass)#6 (0) {
}
*** Caught {closure}(): Return value must be of type bool, stdClass returned in %s on line %d
*** Trying object(StringCapable)#7 (0) {
}
*** Caught {closure}(): Return value must be of type bool, StringCapable returned in %s on line %d
*** Trying resource(5) of type (stream)
*** Caught {closure}(): Return value must be of type bool, resource returned in %s on line %d

Done