]> git.r.bdr.sh - rbdr/dotfiles/blob
b5797edd540c3636adac27862013abc8279b610e
[rbdr/dotfiles] /
1 /* -*- Mode: js; js-indent-level: 2; -*- */
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
6 */
7 if (typeof define !== 'function') {
8 var define = require('amdefine')(module, require);
9 }
10 define(function (require, exports, module) {
11
12 var ArraySet = require('../../lib/source-map/array-set').ArraySet;
13
14 function makeTestSet() {
15 var set = new ArraySet();
16 for (var i = 0; i < 100; i++) {
17 set.add(String(i));
18 }
19 return set;
20 }
21
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)));
26 }
27 };
28
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);
33 }
34 };
35
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));
40 }
41 };
42
43 exports['test creating from an array'] = function (assert, util) {
44 var set = ArraySet.fromArray(['foo', 'bar', 'baz', 'quux', 'hasOwnProperty']);
45
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'));
51
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);
56
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');
61 };
62
63 exports['test that you can add __proto__; see github issue #30'] = function (assert, util) {
64 var set = new ArraySet();
65 set.add('__proto__');
66 assert.ok(set.has('__proto__'));
67 assert.strictEqual(set.at(0), '__proto__');
68 assert.strictEqual(set.indexOf('__proto__'), 0);
69 };
70
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);
77
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);
84 };
85
86 exports['test .add() with duplicates'] = function (assert, util) {
87 var set = new ArraySet();
88 set.add('foo');
89
90 set.add('foo');
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);
95
96 set.add('foo', true);
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);
102 };
103
104 });