Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Tests, README and package.json

  • Loading branch information...
commit 1360f0798e280e2e7251d6bc0010576c01bbfca5 1 parent ffd898b
@jez0990 authored
View
18 lib/simplesets.js
@@ -336,12 +336,12 @@ exports.StringSet.prototype = StringSetPrototype;
var ObjectSetPrototype = {
// Does this set contain an element x with id x.id? Returns true or false.
has: function(x) {
- return this._items.hasOwnProperty(x.id);
+ return x && this._items.hasOwnProperty(x.id);
},
// Add an element x to this set, and return this set.
add: function(x) {
- if (!this.has(x)) {
+ if (x && !this.has(x)) {
this._items[x.id] = x;
this._size++;
}
@@ -362,8 +362,8 @@ var ObjectSetPrototype = {
// the other set, or both.
union: function(other) {
var result = new exports.ObjectSet();
- for (var x in this._items) result.add(this._items[x.id]);
- for (x in other._items) result.add(other._items[x.id]);
+ for (var x in this._items) result.add(this._items[x]);
+ for (x in other._items) result.add(other._items[x]);
return result;
},
@@ -372,8 +372,8 @@ var ObjectSetPrototype = {
intersection: function(other) {
var result = new exports.ObjectSet();
for (var x in other._items)
- if (this._items.hasOwnProperty(x.id))
- result.add(this._items[x.id]);
+ if (this._items.hasOwnProperty(x))
+ result.add(this._items[x]);
return result;
},
@@ -382,7 +382,7 @@ var ObjectSetPrototype = {
difference: function(other) {
var result = new exports.ObjectSet();
for (var x in this._items)
- if (!other._items.hasOwnProperty(x.id))
+ if (!other._items.hasOwnProperty(x))
result.add(this._items[x]);
return result;
},
@@ -397,8 +397,8 @@ var ObjectSetPrototype = {
// Return true if every element of this set is in the other set.
issubset: function(other) {
for (var x in this._items)
- if (!other._items.hasOwnProperty(x.id))
- return false;
+ if (!other._items.hasOwnProperty(x))
+ return false;
return true;
},
View
7 package.json
@@ -1,11 +1,12 @@
-{ "name": "simplesets",
- "version": "1.2.0",
+{
+ "name": "simplesets",
+ "version": "1.3.0",
"description": "Simple set data type, with API similar to Python's sets module.",
"author": "Peter Scott <pjscott@iastate.edu>",
"main": "./lib/simplesets.js",
"repository": {"type": "git", "url": "http://github.com/PeterScott/simplesets-nodejs.git"},
"homepage": "http://github.com/PeterScott/simplesets-nodejs",
"scripts": {
- "test": "node test/testsets.js"
+ "test": "node test/testsets.js"
}
}
View
91 test/testsets.js
@@ -176,5 +176,94 @@ set_test_each(new sets.StringSet([1, 2, 3, "11", "22", "33", "foo"]));
set_test_each(new sets.StringSet());
set_test_each(new sets.StringSet({foo: 42}));
+
+///////////////////////
+// sets.ObjectSet tests
+///////////////////////
+
+// Test basic set construction, add, remove, size, copy, equals, and has.
+var s1 = new sets.ObjectSet([{id:1,value:1},{id:2,value:2},{id:3,value:3}]);
+var s2 = new sets.ObjectSet();
+assert.equal(s1.size(), 3);
+assert.equal(s2.size(), 0);
+assert.equal(s1.has({id:2,value:2}), true);
+assert.equal(s1.has({id:4,value:4}), false);
+assert.equal(s2.has({id:1,value:1}), false);
+s1.remove({id:2,value:2});
+s1.add({id:42,value:42});
+s2.add({id:3,value:3}); s2.add({id:4,value:4});
+s2.remove({id:3,value:3});
+assert.equal(s1.has({id:2,value:2}), false);
+assert.equal(s2.has({id:3,value:3}), false);
+assert.equal(s2.has({id:4,value:4}), true);
+assert.ok(s1.equals(new sets.ObjectSet([{id:42,value:42},{id:1,value:1},{id:3,value:3}])));
+assert.ok(s2.equals(s2));
+assert.ok(s1.equals(s1.copy()));
+assert.ok(s1.copy() !== s1);
+assert.ok(!s1.equals(s2));
+assert.ok(typeof s1.array() == typeof [{id:1,value:1},{id:2,value:2},{id:3,value:3}]);
+
+// Test two-set operations, using mixed number and string keys.
+var s3 = new sets.ObjectSet([{id:1,value:1},{id:2,value:2},{id:3,value:3},{id:"11",value:"11"},{id:"22",value:"22"},{id:"33",value:"33"}]);
+var s4 = new sets.ObjectSet([{id:3,value:3},{id:"11",value:"11"},{id:"foo",value:"foo"}]);
+var nullset = new sets.ObjectSet();
+var s3sub = new sets.ObjectSet([{id:1,value:1},{id:"33",value:"33"},{id:2,value:2}]);
+assert.ok(s3.issuperset(s3sub));
+assert.ok(!s3sub.issuperset(s3));
+assert.ok(s3sub.issubset(s3));
+assert.ok(!s4.issubset(s3));
+assert.ok(s3.intersection(s4).equals(new sets.ObjectSet([{id:"11",value:"11"},{id:3,value:3}])));
+assert.ok(s3.difference(s4).equals(new sets.ObjectSet([{id:1,value:1},{id:2,value:2},{id:"22",value:"22"},{id:"33",value:"33"}])));
+assert.ok(s3.symmetric_difference(s4).equals(new sets.ObjectSet([{id:1,value:1},{id:2,value:2},{id:"22",value:"22"},{id:"33",value:"33"},{id:"foo",value:"foo"}])));
+assert.ok(nullset.union(nullset.copy()).size() === 0);
+
+// Test the pop operation
+var sa = new sets.ObjectSet([{id:3,value:3},{id:1,value:1}, {id:4,value:4},{id:1,value:1}, {id:5,value:5},{id:9,value:9}]);
+var sb = new sets.ObjectSet();
+
+for (var i = 0; i < 200; i++) {
+ var sa_copy = sa.copy();
+ sb.add(sa_copy.pop());
+ assert.ok(sa_copy.size() === sa.size() - 1);
+}
+
+assert.ok(sb.issubset(sa));
+
+// Test the pick operation, in much the same way.
+var sa = new sets.ObjectSet([{id:3,value:3},{id:1,value:1}, {id:4,value:4},{id:1,value:1}, {id:5,value:5},{id:9,value:9}]);
+var sb = new sets.ObjectSet();
+
+for (var i = 0; i < 200; i++) {
+ var sa_old_size = sa.size();
+ sb.add(sa.pick());
+ assert.ok(sa.size() === sa_old_size);
+}
+
+assert.ok(sb.issubset(sa));
+assert.ok(nullset.pick() === null);
+assert.ok(nullset.pop() === null);
+assert.ok(nullset.issubset(nullset));
+
+// Make sure add and remove return the set, for chaining.
+var s5 = new sets.ObjectSet();
+s5.add({id:3,value:3}).add({id:4,value:4}).remove({id:3,value:3});
+assert.ok(s5.equals(new sets.StringSet([{id:4,value:4}])));
+
+// Test each() function
+function stringset_test_each(s) {
+ n = new sets.ObjectSet();
+ s.each(function(x) {
+ assert.ok(!n.has(x));
+ n.add(x);
+ });
+ assert.ok(n.equals(s));
+ assert.ok(s.equals(n));
+}
+set_test_each(new sets.ObjectSet([{id:3,value:3},{id:1,value:1}, {id:4,value:4},{id:1,value:1}, {id:5,value:5},{id:9,value:9},{id:2,value:2},{id:6,value:6}, {id:5,value:5},{id:3,value:3}, {id:5,value:5},{id:8,value:8},{id:9,value:9}, {id:7,value:7},{id:9,value:9}]));
+set_test_each(new sets.ObjectSet([{id:1,value:1},{id:2,value:2},{id:"22",value:"22"},{id:"33",value:"33"},{id:"foo",value:"foo"}]));
+set_test_each(new sets.ObjectSet());
+set_test_each(new sets.ObjectSet({id:"foo",value:{foo: 42}}));
+
+
// If we got to here, then...
-console.log('All tests passed!');
+console.log('All tests passed!');
View
269 test/testsets.js~
@@ -0,0 +1,269 @@
+var assert = require('assert');
+var sets = require('../lib/simplesets');
+
+////////////////////
+// sets.Set tests
+////////////////////
+
+// Test basic set construction, add, remove, size, copy, equals, and has.
+var s1 = new sets.Set([1, 2, 3]);
+var s2 = new sets.Set();
+assert.equal(s1.size(), 3);
+assert.equal(s2.size(), 0);
+assert.equal(s1.has(2), true);
+assert.equal(s1.has(4), false);
+assert.equal(s2.has(1), false);
+s1.remove(2);
+s1.add(42);
+s2.add(3); s2.add(4);
+s2.remove(3);
+assert.equal(s1.has(2), false);
+assert.equal(s2.has(3), false);
+assert.equal(s2.has(4), true);
+assert.ok(s1.equals(new sets.Set([42, 1, 3])));
+assert.ok(s2.equals(s2));
+assert.ok(s1.equals(s1.copy()));
+assert.ok(s1.copy() !== s1);
+assert.ok(!s1.equals(s2));
+assert.ok(typeof s1.array() == typeof [1, 2, 3]);
+
+// Test two-set operations, using mixed number and string keys.
+var s3 = new sets.Set([1, 2, 3, "1", "2", "3"]);
+var s4 = new sets.Set([3, "1", "foo"]);
+var nullset = new sets.Set();
+var s3sub = new sets.Set([1, "3", 2]);
+assert.ok(s3.issuperset(s3sub));
+assert.ok(!s3sub.issuperset(s3));
+assert.ok(s3sub.issubset(s3));
+assert.ok(!s4.issubset(s3));
+assert.ok(s3.union(s4).equals(new sets.Set([1, 2, 3, "1", "2", "3", "foo"])));
+assert.ok(s3.intersection(s4).equals(new sets.Set(["1", 3])));
+assert.ok(s3.difference(s4).equals(new sets.Set([1, 2, "2", "3"])));
+assert.ok(s3.symmetric_difference(s4).equals(new sets.Set([1, 2, "2", "3", "foo"])));
+assert.ok(nullset.union(nullset.copy()).size() === 0);
+
+// Test the pop operation
+var sa = new sets.Set([3, 1, 4, 1, 5, 9]);
+var sb = new sets.Set();
+
+for (var i = 0; i < 200; i++) {
+ var sa_copy = sa.copy();
+ sb.add(sa_copy.pop());
+ assert.ok(sa_copy.size() === sa.size() - 1);
+}
+
+assert.ok(sb.issubset(sa));
+
+// Test the pick operation, in much the same way.
+var sa = new sets.Set([3, 1, 4, 1, 5, 9]);
+var sb = new sets.Set();
+
+for (var i = 0; i < 200; i++) {
+ var sa_old_size = sa.size();
+ sb.add(sa.pick());
+ assert.ok(sa.size() === sa_old_size);
+}
+
+assert.ok(sb.issubset(sa));
+assert.ok(nullset.pick() === null);
+assert.ok(nullset.pop() === null);
+assert.ok(nullset.issubset(nullset));
+
+// Make sure add and remove return the set, for chaining.
+var s5 = new sets.Set();
+s5.add(3).add(4).remove(3);
+assert.ok(s5.equals(new sets.Set([4])));
+
+// Test each() function
+function set_test_each(s) {
+ n = new sets.Set();
+ s.each(function(x) {
+ assert.ok(!n.has(x));
+ n.add(x);
+ });
+ assert.ok(n.equals(s));
+ assert.ok(s.equals(n));
+}
+set_test_each(new sets.Set([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]));
+set_test_each(new sets.Set([1, 2, 3, "1", "2", "3", "foo"]));
+set_test_each(new sets.Set());
+set_test_each(new sets.Set({foo: 42}));
+
+///////////////////////
+// sets.StringSet tests
+///////////////////////
+
+// Test basic set construction, add, remove, size, copy, equals, and has.
+var s1 = new sets.StringSet([1, 2, 3]);
+var s2 = new sets.StringSet();
+assert.equal(s1.size(), 3);
+assert.equal(s2.size(), 0);
+assert.equal(s1.has(2), true);
+assert.equal(s1.has(4), false);
+assert.equal(s2.has(1), false);
+s1.remove(2);
+s1.add(42);
+s2.add(3); s2.add(4);
+s2.remove(3);
+assert.equal(s1.has(2), false);
+assert.equal(s2.has(3), false);
+assert.equal(s2.has(4), true);
+assert.ok(s1.equals(new sets.StringSet([42, 1, 3])));
+assert.ok(s2.equals(s2));
+assert.ok(s1.equals(s1.copy()));
+assert.ok(s1.copy() !== s1);
+assert.ok(!s1.equals(s2));
+assert.ok(typeof s1.array() == typeof [1, 2, 3]);
+
+// Test two-set operations, using mixed number and string keys.
+var s3 = new sets.StringSet([1, 2, 3, "11", "22", "33"]);
+var s4 = new sets.StringSet([3, "11", "foo"]);
+var nullset = new sets.StringSet();
+var s3sub = new sets.StringSet([1, "33", 2]);
+assert.ok(s3.issuperset(s3sub));
+assert.ok(!s3sub.issuperset(s3));
+assert.ok(s3sub.issubset(s3));
+assert.ok(!s4.issubset(s3));
+assert.ok(s3.intersection(s4).equals(new sets.StringSet(["11", 3])));
+assert.ok(s3.difference(s4).equals(new sets.StringSet([1, 2, "22", "33"])));
+assert.ok(s3.symmetric_difference(s4).equals(new sets.StringSet([1, 2, "22", "33", "foo"])));
+assert.ok(nullset.union(nullset.copy()).size() === 0);
+
+// Test the pop operation
+var sa = new sets.StringSet([3, 1, 4, 1, 5, 9]);
+var sb = new sets.StringSet();
+
+for (var i = 0; i < 200; i++) {
+ var sa_copy = sa.copy();
+ sb.add(sa_copy.pop());
+ assert.ok(sa_copy.size() === sa.size() - 1);
+}
+
+assert.ok(sb.issubset(sa));
+
+// Test the pick operation, in much the same way.
+var sa = new sets.StringSet([3, 1, 4, 1, 5, 9]);
+var sb = new sets.StringSet();
+
+for (var i = 0; i < 200; i++) {
+ var sa_old_size = sa.size();
+ sb.add(sa.pick());
+ assert.ok(sa.size() === sa_old_size);
+}
+
+assert.ok(sb.issubset(sa));
+assert.ok(nullset.pick() === null);
+assert.ok(nullset.pop() === null);
+assert.ok(nullset.issubset(nullset));
+
+// Make sure add and remove return the set, for chaining.
+var s5 = new sets.StringSet();
+s5.add(3).add(4).remove(3);
+assert.ok(s5.equals(new sets.StringSet([4])));
+
+// Test each() function
+function stringset_test_each(s) {
+ n = new sets.StringSet();
+ s.each(function(x) {
+ assert.ok(!n.has(x));
+ n.add(x);
+ });
+ assert.ok(n.equals(s));
+ assert.ok(s.equals(n));
+}
+set_test_each(new sets.StringSet([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]));
+set_test_each(new sets.StringSet([1, 2, 3, "11", "22", "33", "foo"]));
+set_test_each(new sets.StringSet());
+set_test_each(new sets.StringSet({foo: 42}));
+
+
+///////////////////////
+// sets.ObjectSet tests
+///////////////////////
+
+// Test basic set construction, add, remove, size, copy, equals, and has.
+var s1 = new sets.ObjectSet([{id:1,value:1},{id:2,value:2},{id:3,value:3}]);
+var s2 = new sets.ObjectSet();
+assert.equal(s1.size(), 3);
+assert.equal(s2.size(), 0);
+assert.equal(s1.has({id:2,value:2}), true);
+assert.equal(s1.has({id:4,value:4}), false);
+assert.equal(s2.has({id:1,value:1}), false);
+s1.remove({id:2,value:2});
+s1.add({id:42,value:42});
+s2.add({id:3,value:3}); s2.add({id:4,value:4});
+s2.remove({id:3,value:3});
+assert.equal(s1.has({id:2,value:2}), false);
+assert.equal(s2.has({id:3,value:3}), false);
+assert.equal(s2.has({id:4,value:4}), true);
+assert.ok(s1.equals(new sets.ObjectSet([{id:42,value:42},{id:1,value:1},{id:3,value:3}])));
+assert.ok(s2.equals(s2));
+assert.ok(s1.equals(s1.copy()));
+assert.ok(s1.copy() !== s1);
+assert.ok(!s1.equals(s2));
+assert.ok(typeof s1.array() == typeof [{id:1,value:1},{id:2,value:2},{id:3,value:3}]);
+
+// Test two-set operations, using mixed number and string keys.
+var s3 = new sets.ObjectSet([{id:1,value:1},{id:2,value:2},{id:3,value:3},{id:"11",value:"11"},{id:"22",value:"22"},{id:"33",value:"33"}]);
+var s4 = new sets.ObjectSet([{id:3,value:3},{id:"11",value:"11"},{id:"foo",value:"foo"}]);
+var nullset = new sets.ObjectSet();
+var s3sub = new sets.ObjectSet([{id:1,value:1},{id:"33",value:"33"},{id:2,value:2}]);
+assert.ok(s3.issuperset(s3sub));
+assert.ok(!s3sub.issuperset(s3));
+assert.ok(s3sub.issubset(s3));
+assert.ok(!s4.issubset(s3));
+assert.ok(s3.intersection(s4).equals(new sets.ObjectSet([{id:"11",value:"11"},{id:3,value:3}])));
+assert.ok(s3.difference(s4).equals(new sets.ObjectSet([{id:1,value:1},{id:2,value:2},{id:"22",value:"22"},{id:"33",value:"33"}])));
+assert.ok(s3.symmetric_difference(s4).equals(new sets.ObjectSet([{id:1,value:1},{id:2,value:2},{id:"22",value:"22"},{id:"33",value:"33"},{id:"foo",value:"foo"}])));
+assert.ok(nullset.union(nullset.copy()).size() === 0);
+
+// Test the pop operation
+var sa = new sets.ObjectSet([{id:3,value:3},{id:1,value:1}, {id:4,value:4},{id:1,value:1}, {id:5,value:5},{id:9,value:9}]);
+var sb = new sets.ObjectSet();
+
+for (var i = 0; i < 200; i++) {
+ var sa_copy = sa.copy();
+ sb.add(sa_copy.pop());
+ assert.ok(sa_copy.size() === sa.size() - 1);
+}
+
+assert.ok(sb.issubset(sa));
+
+// Test the pick operation, in much the same way.
+var sa = new sets.ObjectSet([{id:3,value:3},{id:1,value:1}, {id:4,value:4},{id:1,value:1}, {id:5,value:5},{id:9,value:9}]);
+var sb = new sets.ObjectSet();
+
+for (var i = 0; i < 200; i++) {
+ var sa_old_size = sa.size();
+ sb.add(sa.pick());
+ assert.ok(sa.size() === sa_old_size);
+}
+
+assert.ok(sb.issubset(sa));
+assert.ok(nullset.pick() === null);
+assert.ok(nullset.pop() === null);
+assert.ok(nullset.issubset(nullset));
+
+// Make sure add and remove return the set, for chaining.
+var s5 = new sets.ObjectSet();
+s5.add({id:3,value:3}).add({id:4,value:4}).remove({id:3,value:3});
+assert.ok(s5.equals(new sets.StringSet([{id:4,value:4}])));
+
+// Test each() function
+function stringset_test_each(s) {
+ n = new sets.ObjectSet();
+ s.each(function(x) {
+ assert.ok(!n.has(x));
+ n.add(x);
+ });
+ assert.ok(n.equals(s));
+ assert.ok(s.equals(n));
+}
+set_test_each(new sets.ObjectSet([{id:3,value:3},{id:1,value:1}, {id:4,value:4},{id:1,value:1}, {id:5,value:5},{id:9,value:9},{id:2,value:2},{id:6,value:6}, {id:5,value:5},{id:3,value:3}, {id:5,value:5},{id:8,value:8},{id:9,value:9}, {id:7,value:7},{id:9,value:9}]));
+set_test_each(new sets.ObjectSet([{id:1,value:1},{id:2,value:2},{id:"22",value:"22"},{id:"33",value:"33"},{id:"foo",value:"foo"}]));
+set_test_each(new sets.ObjectSet());
+set_test_each(new sets.ObjectSet({id:"foo",value:{foo: 42}}));
+
+
+// If we got to here, then...
+console.log('All tests passed!');
Please sign in to comment.
Something went wrong with that request. Please try again.