Skip to content

Commit ed8bd4b

Browse files
test: add tests for method
1 parent 8a4a396 commit ed8bd4b

File tree

1 file changed

+226
-0
lines changed

1 file changed

+226
-0
lines changed
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var tape = require( 'tape' );
24+
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
25+
var isFunction = require( '@stdlib/assert/is-function' );
26+
var factory = require( './../lib' );
27+
28+
29+
// TESTS //
30+
31+
tape( 'main export is a function', function test( t ) {
32+
t.ok( true, __filename );
33+
t.strictEqual( typeof factory, 'function', 'main export is a function' );
34+
t.end();
35+
});
36+
37+
tape( 'the function returns a function', function test( t ) {
38+
var ctor = factory( 'float64' );
39+
t.strictEqual( isFunction( ctor ), true, 'returns expected value' );
40+
t.end();
41+
});
42+
43+
tape( 'attached to the prototype of the returned function is an `includes` method', function test( t ) {
44+
var ctor = factory( 'float64' );
45+
t.strictEqual( hasOwnProp( ctor.prototype, 'includes' ), true, 'returns expected value' );
46+
t.strictEqual( isFunction( ctor.prototype.includes ), true, 'returns expected value' );
47+
t.end();
48+
});
49+
50+
tape( 'the method throws an error if invoked with a `this` context which is not a typed array instance', function test( t ) {
51+
var values;
52+
var ctor;
53+
var arr;
54+
var i;
55+
56+
ctor = factory( 'float64' );
57+
arr = new ctor( 'little-endian', 5 );
58+
59+
values = [
60+
'5',
61+
5,
62+
NaN,
63+
true,
64+
false,
65+
null,
66+
void 0,
67+
{},
68+
[],
69+
function noop() {}
70+
];
71+
for ( i = 0; i < values.length; i++ ) {
72+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
73+
}
74+
t.end();
75+
76+
function badValue( value ) {
77+
return function badValue() {
78+
return arr.includes.call( value, 0 );
79+
};
80+
}
81+
});
82+
83+
tape( 'the method throws an error if provided a second argument which is not an integer', function test( t ) {
84+
var values;
85+
var ctor;
86+
var arr;
87+
var i;
88+
89+
ctor = factory( 'float64' );
90+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0 ] );
91+
92+
values = [
93+
'5',
94+
3.14,
95+
NaN,
96+
true,
97+
false,
98+
null,
99+
void 0,
100+
{},
101+
[],
102+
function noop() {}
103+
];
104+
for ( i = 0; i < values.length; i++ ) {
105+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
106+
}
107+
t.end();
108+
109+
function badValue( value ) {
110+
return function badValue() {
111+
return arr.includes( 1.0, value );
112+
};
113+
}
114+
});
115+
116+
tape( 'the method returns `false` if provided a second argument which exceeds array dimensions', function test( t ) {
117+
var ctor;
118+
var arr;
119+
var v;
120+
var i;
121+
122+
ctor = factory( 'float64' );
123+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] );
124+
125+
for ( i = arr.length; i < arr.length+5; i++ ) {
126+
v = arr.includes( 1.0, i );
127+
t.strictEqual( v, false, 'returns expected value' );
128+
}
129+
t.end();
130+
});
131+
132+
tape( 'the method returns `false` if operating on an empty array', function test( t ) {
133+
var ctor;
134+
var arr;
135+
var v;
136+
137+
ctor = factory( 'float64' );
138+
arr = new ctor( 'little-endian', [] );
139+
140+
v = arr.includes( 1.0 );
141+
t.strictEqual( v, false, 'returns expected value' );
142+
143+
t.end();
144+
});
145+
146+
tape( 'the method returns `false` if a search element is not found', function test( t ) {
147+
var ctor;
148+
var arr;
149+
var v;
150+
151+
ctor = factory( 'float64' );
152+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
153+
154+
v = arr.includes( 10.0 );
155+
t.strictEqual( v, false, 'returns expected value' );
156+
157+
t.end();
158+
});
159+
160+
tape( 'the method returns `true` if an element is found', function test( t ) {
161+
var ctor;
162+
var arr;
163+
var v;
164+
165+
ctor = factory( 'float64' );
166+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] );
167+
168+
v = arr.includes( 2.0 );
169+
t.strictEqual( v, true, 'returns expected value' );
170+
171+
t.end();
172+
});
173+
174+
tape( 'the method supports specifying a starting search index', function test( t ) {
175+
var ctor;
176+
var arr;
177+
var v;
178+
179+
ctor = factory( 'float64' );
180+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] );
181+
182+
v = arr.includes( 2.0, 0 );
183+
t.strictEqual( v, true, 'returns expected value' );
184+
185+
v = arr.includes( 2.0, 2 );
186+
t.strictEqual( v, true, 'returns expected value' );
187+
188+
v = arr.includes( 1.0, 4 );
189+
t.strictEqual( v, false, 'returns expected value' );
190+
191+
t.end();
192+
});
193+
194+
tape( 'the method supports specifying a starting search index (negative)', function test( t ) {
195+
var ctor;
196+
var arr;
197+
var v;
198+
199+
ctor = factory( 'float64' );
200+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] );
201+
202+
v = arr.includes( 2.0, -3 );
203+
t.strictEqual( v, true, 'returns expected value' );
204+
205+
v = arr.includes( 1.0, -3 );
206+
t.strictEqual( v, false, 'returns expected value' );
207+
208+
t.end();
209+
});
210+
211+
tape( 'when provided a starting index which resolves to an index less than zero, the method searches from the first array element', function test( t ) {
212+
var ctor;
213+
var arr;
214+
var v;
215+
216+
ctor = factory( 'float64' );
217+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 2.0 ] );
218+
219+
v = arr.includes( 2.0, -10 );
220+
t.strictEqual( v, true, 'returns expected value' );
221+
222+
v = arr.includes( 1.0, -10 );
223+
t.strictEqual( v, true, 'returns expected value' );
224+
225+
t.end();
226+
});

0 commit comments

Comments
 (0)