1 /* -*- Mode: js; js-indent-level: 2; -*- */
3 * Copyright 2011 Mozilla Foundation and contributors
4 * Licensed under the New BSD license. See LICENSE or:
5 * http://opensource.org/licenses/BSD-3-Clause
7 if (typeof define !== 'function') {
8 var define = require('amdefine')(module, require);
10 define(function (require, exports, module) {
12 var ArraySet = require('../../lib/source-map/array-set').ArraySet;
14 function makeTestSet() {
15 var set = new ArraySet();
16 for (var i = 0; i < 100; i++) {
22 exports['test .has() membership'] = function (assert, util) {
23 var set = makeTestSet();
24 for (var i = 0; i < 100; i++) {
25 assert.ok(set.has(String(i)));
29 exports['test .indexOf() elements'] = function (assert, util) {
30 var set = makeTestSet();
31 for (var i = 0; i < 100; i++) {
32 assert.strictEqual(set.indexOf(String(i)), i);
36 exports['test .at() indexing'] = function (assert, util) {
37 var set = makeTestSet();
38 for (var i = 0; i < 100; i++) {
39 assert.strictEqual(set.at(i), String(i));
43 exports['test creating from an array'] = function (assert, util) {
44 var set = ArraySet.fromArray(['foo', 'bar', 'baz', 'quux', 'hasOwnProperty']);
46 assert.ok(set.has('foo'));
47 assert.ok(set.has('bar'));
48 assert.ok(set.has('baz'));
49 assert.ok(set.has('quux'));
50 assert.ok(set.has('hasOwnProperty'));
52 assert.strictEqual(set.indexOf('foo'), 0);
53 assert.strictEqual(set.indexOf('bar'), 1);
54 assert.strictEqual(set.indexOf('baz'), 2);
55 assert.strictEqual(set.indexOf('quux'), 3);
57 assert.strictEqual(set.at(0), 'foo');
58 assert.strictEqual(set.at(1), 'bar');
59 assert.strictEqual(set.at(2), 'baz');
60 assert.strictEqual(set.at(3), 'quux');
63 exports['test that you can add __proto__; see github issue #30'] = function (assert, util) {
64 var set = new ArraySet();
66 assert.ok(set.has('__proto__'));
67 assert.strictEqual(set.at(0), '__proto__');
68 assert.strictEqual(set.indexOf('__proto__'), 0);
71 exports['test .fromArray() with duplicates'] = function (assert, util) {
72 var set = ArraySet.fromArray(['foo', 'foo']);
73 assert.ok(set.has('foo'));
74 assert.strictEqual(set.at(0), 'foo');
75 assert.strictEqual(set.indexOf('foo'), 0);
76 assert.strictEqual(set.toArray().length, 1);
78 set = ArraySet.fromArray(['foo', 'foo'], true);
79 assert.ok(set.has('foo'));
80 assert.strictEqual(set.at(0), 'foo');
81 assert.strictEqual(set.at(1), 'foo');
82 assert.strictEqual(set.indexOf('foo'), 0);
83 assert.strictEqual(set.toArray().length, 2);
86 exports['test .add() with duplicates'] = function (assert, util) {
87 var set = new ArraySet();
91 assert.ok(set.has('foo'));
92 assert.strictEqual(set.at(0), 'foo');
93 assert.strictEqual(set.indexOf('foo'), 0);
94 assert.strictEqual(set.toArray().length, 1);
97 assert.ok(set.has('foo'));
98 assert.strictEqual(set.at(0), 'foo');
99 assert.strictEqual(set.at(1), 'foo');
100 assert.strictEqual(set.indexOf('foo'), 0);
101 assert.strictEqual(set.toArray().length, 2);