|
1 | 1 | /** |
2 | 2 | *@license Apache-2.0 |
3 | 3 | * |
4 | | -* Copyright (c)2018 The Stdlib Authors. |
| 4 | +* Copyright (c)2023 The Stdlib Authors. |
5 | 5 | * |
6 | 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | 7 | * you may not use this file except in compliance with the License. |
|
21 | 21 | // MODULES // |
22 | 22 |
|
23 | 23 | vartape=require('tape'); |
24 | | -varproxyquire=require('proxyquire'); |
25 | | -varhasSharedArrayBufferSupport=require('@stdlib/assert-has-sharedarraybuffer-support');// eslint-disable-line id-length |
26 | | -varisFunction=require('@stdlib/assert-is-function'); |
27 | | -varinstanceOf=require('@stdlib/assert-instance-of'); |
28 | | -varMAX_SAFE_INTEGER=require('@stdlib/constants-float64-max-safe-integer'); |
29 | | -varhasOwnProp=require('@stdlib/assert-has-own-property'); |
30 | | -varhasProp=require('@stdlib/assert-has-property'); |
31 | | -varpolyfill=require('./../../dist/polyfill.js'); |
32 | | -varCtor=require('./../../dist'); |
33 | | - |
34 | | - |
35 | | -// VARIABLES // |
36 | | - |
37 | | -varopts={ |
38 | | -'skip':!hasSharedArrayBufferSupport() |
39 | | -}; |
| 24 | +varmain=require('./../../dist'); |
40 | 25 |
|
41 | 26 |
|
42 | 27 | // TESTS // |
43 | 28 |
|
44 | | -tape('main export isa function',functiontest(t){ |
| 29 | +tape('main export isdefined',functiontest(t){ |
45 | 30 | t.ok(true,__filename); |
46 | | -t.strictEqual(typeofCtor,'function','main export is a function'); |
47 | | -t.end(); |
48 | | -}); |
49 | | - |
50 | | -tape('if an environment supports `SharedArrayBuffer`, the export is an alias for `SharedArrayBuffer`',functiontest(t){ |
51 | | -varFoo=proxyquire('./../dist',{ |
52 | | -'@stdlib/assert-has-sharedarraybuffer-support':isTrue, |
53 | | -'./main.js':Mock |
54 | | -}); |
55 | | -t.strictEqual(Foo,Mock,'returns built-in'); |
56 | | - |
57 | | -if(!opts.skip){ |
58 | | -t.strictEqual(Ctor,SharedArrayBuffer,'is alias');// eslint-disable-line stdlib/require-globals, no-undef |
59 | | -} |
60 | | - |
61 | | -t.end(); |
62 | | - |
63 | | -functionMock(){ |
64 | | -returnthis; |
65 | | -} |
66 | | - |
67 | | -functionisTrue(){ |
68 | | -returntrue; |
69 | | -} |
70 | | -}); |
71 | | - |
72 | | -tape('if an environment does not support `SharedArrayBuffer`, the export is a polyfill',functiontest(t){ |
73 | | -varFoo=proxyquire('./../dist',{ |
74 | | -'@stdlib/assert-has-sharedarraybuffer-support':isFalse |
75 | | -}); |
76 | | - |
77 | | -t.strictEqual(Foo,polyfill,'returns polyfill'); |
78 | | -t.end(); |
79 | | - |
80 | | -functionisFalse(){ |
81 | | -returnfalse; |
82 | | -} |
83 | | -}); |
84 | | - |
85 | | -tape('the main export is a constructor',opts,functiontest(t){ |
86 | | -varbuf=newCtor(10); |
87 | | -t.strictEqual(instanceOf(buf,Ctor),true,'returns an instance'); |
88 | | -t.end(); |
89 | | -}); |
90 | | - |
91 | | -tape('the constructor length is equal to `1`',opts,functiontest(t){ |
92 | | -t.strictEqual(Ctor.length,1,'returns expected value'); |
93 | | -t.end(); |
94 | | -}); |
95 | | - |
96 | | -tape('the constructor throws an error if provided a value exceeding `2^53-1`',opts,functiontest(t){ |
97 | | -t.throws(badValue,RangeError,'throws an error'); |
98 | | -t.end(); |
99 | | - |
100 | | -functionbadValue(){ |
101 | | -returnnewCtor(MAX_SAFE_INTEGER+1); |
102 | | -} |
103 | | -}); |
104 | | - |
105 | | -tape('the constructor returns a `SharedArrayBuffer` instance having a `byteLength` property, which returns the number of bytes in a `SharedArrayBuffer`',opts,functiontest(t){ |
106 | | -varbuf; |
107 | | - |
108 | | -t.strictEqual(hasOwnProp(Ctor.prototype,'byteLength'),true,'has prototype property'); |
109 | | - |
110 | | -buf=newCtor(10); |
111 | | -t.strictEqual(hasOwnProp(buf,'byteLength'),false,'does not have own property'); |
112 | | -t.strictEqual(hasProp(buf,'byteLength'),true,'has property'); |
113 | | -t.strictEqual(buf.byteLength,10,'returns expected value'); |
114 | | - |
115 | | -t.end(); |
116 | | -}); |
117 | | - |
118 | | -tape('the constructor returns a `SharedArrayBuffer` instance having a `slice` method, which copies the bytes of a `SharedArrayBuffer` to a new `SharedArrayBuffer`',opts,functiontest(t){ |
119 | | -varb1; |
120 | | -varb2; |
121 | | - |
122 | | -t.strictEqual(hasOwnProp(Ctor.prototype,'slice'),true,'has prototype property'); |
123 | | -t.strictEqual(isFunction(Ctor.prototype.slice),true,'has method'); |
124 | | - |
125 | | -b1=newCtor(10); |
126 | | -t.strictEqual(hasOwnProp(b1,'slice'),false,'does not have own property'); |
127 | | -t.strictEqual(hasProp(b1,'slice'),true,'has property'); |
128 | | - |
129 | | -b2=b1.slice(); |
130 | | -t.strictEqual(instanceOf(b2,Ctor),true,'returns an instance'); |
131 | | -t.notEqual(b2,b1,'returns a new reference'); |
132 | | -t.strictEqual(b2.byteLength,b1.byteLength,'has same number of bytes'); |
133 | | - |
134 | | -b2=b1.slice(2); |
135 | | -t.strictEqual(instanceOf(b2,Ctor),true,'returns an instance'); |
136 | | -t.notEqual(b2,b1,'returns a new reference'); |
137 | | -t.strictEqual(b2.byteLength,8,'has expected number of bytes'); |
138 | | - |
139 | | -b2=b1.slice(b1.byteLength+10); |
140 | | -t.strictEqual(instanceOf(b2,Ctor),true,'returns an instance'); |
141 | | -t.notEqual(b2,b1,'returns a new reference'); |
142 | | -t.strictEqual(b2.byteLength,0,'has expected number of bytes'); |
143 | | - |
144 | | -b2=b1.slice(-2); |
145 | | -t.strictEqual(instanceOf(b2,Ctor),true,'returns an instance'); |
146 | | -t.notEqual(b2,b1,'returns a new reference'); |
147 | | -t.strictEqual(b2.byteLength,2,'has expected number of bytes'); |
148 | | - |
149 | | -b2=b1.slice(-100); |
150 | | -t.strictEqual(instanceOf(b2,Ctor),true,'returns an instance'); |
151 | | -t.notEqual(b2,b1,'returns a new reference'); |
152 | | -t.strictEqual(b2.byteLength,10,'has expected number of bytes'); |
153 | | - |
154 | | -b2=b1.slice(0,6); |
155 | | -t.strictEqual(instanceOf(b2,Ctor),true,'returns an instance'); |
156 | | -t.notEqual(b2,b1,'returns a new reference'); |
157 | | -t.strictEqual(b2.byteLength,6,'has expected number of bytes'); |
158 | | - |
159 | | -b2=b1.slice(2,6); |
160 | | -t.strictEqual(instanceOf(b2,Ctor),true,'returns an instance'); |
161 | | -t.notEqual(b2,b1,'returns a new reference'); |
162 | | -t.strictEqual(b2.byteLength,4,'has expected number of bytes'); |
163 | | - |
164 | | -b2=b1.slice(0,-2); |
165 | | -t.strictEqual(instanceOf(b2,Ctor),true,'returns an instance'); |
166 | | -t.notEqual(b2,b1,'returns a new reference'); |
167 | | -t.strictEqual(b2.byteLength,8,'has expected number of bytes'); |
168 | | - |
169 | | -b2=b1.slice(0,-100); |
170 | | -t.strictEqual(instanceOf(b2,Ctor),true,'returns an instance'); |
171 | | -t.notEqual(b2,b1,'returns a new reference'); |
172 | | -t.strictEqual(b2.byteLength,0,'has expected number of bytes'); |
173 | | - |
174 | | -b2=b1.slice(-4,-2); |
175 | | -t.strictEqual(instanceOf(b2,Ctor),true,'returns an instance'); |
176 | | -t.notEqual(b2,b1,'returns a new reference'); |
177 | | -t.strictEqual(b2.byteLength,2,'has expected number of bytes'); |
178 | | - |
| 31 | +t.strictEqual(main!==void0,true,'main export is defined'); |
179 | 32 | t.end(); |
180 | 33 | }); |
181 | | - |
182 | | -// TODO: add tests testing shared memory semantics/behavior |