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
|
// |reftest| skip-if(!this.hasOwnProperty("TypedObject"))
var BUGNUMBER = 578700;
var summary = 'TypedObjects numeric types';
var actual = '';
var expect = '';
var ArrayType = TypedObject.ArrayType;
var StructType = TypedObject.StructType;
var uint8 = TypedObject.uint8;
var uint16 = TypedObject.uint16;
var uint32 = TypedObject.uint32;
var uint8Clamped = TypedObject.uint8Clamped;
var int8 = TypedObject.int8;
var int16 = TypedObject.int16;
var int32 = TypedObject.int32;
var float32 = TypedObject.float32;
var float64 = TypedObject.float64;
function runTests()
{
printBugNumber(BUGNUMBER);
printStatus(summary);
var TestPassCount = 0;
var TestFailCount = 0;
var TestTodoCount = 0;
var TODO = 1;
function check(fun, todo) {
var thrown = null;
var success = false;
try {
success = fun();
} catch (x) {
thrown = x;
}
if (thrown)
success = false;
if (todo) {
TestTodoCount++;
if (success) {
var ex = new Error;
print ("=== TODO but PASSED? ===");
print (ex.stack);
print ("========================");
}
return;
}
if (success) {
TestPassCount++;
} else {
TestFailCount++;
var ex = new Error;
print ("=== FAILED ===");
print (ex.stack);
if (thrown) {
print (" threw exception:");
print (thrown);
}
print ("==============");
}
}
function checkThrows(fun, todo) {
var thrown = false;
try {
fun();
} catch (x) {
thrown = true;
}
check(() => thrown, todo);
}
var types = [uint8, uint16, uint32, int8, int16, int32];
var strings = ["uint8", "uint16", "uint32", "int8", "int16", "int32"];
for (var i = 0; i < types.length; i++) {
var type = types[i];
check(() => type(true) === 1);
check(() => type(false) === 0);
check(() => type(+Infinity) === 0);
check(() => type(-Infinity) === 0);
check(() => type(NaN) === 0);
check(() => type.toSource() === strings[i]);
check(() => type(null) == 0);
check(() => type(undefined) == 0);
check(() => type([]) == 0);
check(() => type({}) == 0);
check(() => type(/abcd/) == 0);
checkThrows(() => new type());
checkThrows(() => type());
}
var floatTypes = [float32, float64];
var floatStrings = ["float32", "float64"];
for (var i = 0; i < floatTypes.length; i++) {
var type = floatTypes[i];
check(() => type(true) === 1);
check(() => type(false) === 0);
check(() => type(+Infinity) === Infinity);
check(() => type(-Infinity) === -Infinity);
check(() => Number.isNaN(type(NaN)));
check(() => type.toSource() === floatStrings[i]);
check(() => type(null) == 0);
check(() => Number.isNaN(type(undefined)));
check(() => type([]) == 0);
check(() => Number.isNaN(type({})));
check(() => Number.isNaN(type(/abcd/)));
checkThrows(() => new type());
checkThrows(() => type());
}
///// test ranges and creation
/// uint8
// valid
check(() => uint8(0) == 0);
check(() => uint8(-0) == 0);
check(() => uint8(129) == 129);
check(() => uint8(255) == 255);
// overflow is allowed for explicit conversions
check(() => uint8(-1) == 255);
check(() => uint8(-255) == 1);
check(() => uint8(256) == 0);
check(() => uint8(2345678) == 206);
check(() => uint8(3.14) == 3);
check(() => uint8(342.56) == 86);
check(() => uint8(-342.56) == 170);
/// uint8clamped
// valid
check(() => uint8Clamped(0) == 0);
check(() => uint8Clamped(-0) == 0);
check(() => uint8Clamped(129) == 129);
check(() => uint8Clamped(-30) == 0);
check(() => uint8Clamped(254.5) == 254);
check(() => uint8Clamped(257) == 255);
check(() => uint8Clamped(513) == 255);
check(() => uint8Clamped(60000) == 255);
// strings
check(() => uint8("0") == 0);
check(() => uint8("255") == 255);
check(() => uint8("256") == 0);
check(() => uint8("0x0f") == 15);
check(() => uint8("0x00") == 0);
check(() => uint8("0xff") == 255);
check(() => uint8("0x1ff") == 255);
// in JS, string literals with leading zeroes are interpreted as decimal
check(() => uint8("-0777") == 247);
check(() => uint8("-0xff") == 0);
/// uint16
// valid
check(() => uint16(65535) == 65535);
// overflow is allowed for explicit conversions
check(() => uint16(-1) == 65535);
check(() => uint16(-65535) == 1);
check(() => uint16(-65536) == 0);
check(() => uint16(65536) == 0);
// strings
check(() => uint16("0x1234") == 0x1234);
check(() => uint16("0x00") == 0);
check(() => uint16("0xffff") == 65535);
check(() => uint16("-0xffff") == 0);
check(() => uint16("0xffffff") == 0xffff);
// wrong types
check(() => uint16(3.14) == 3); // c-like casts in explicit conversion
print("done");
reportCompare(0, TestFailCount, "TypedObjects numeric type tests");
}
runTests();
|