|
| 1 | +const expect = require('chai').expect; |
| 2 | +const Set = require('../../examples/datastructures/set'); |
| 3 | + |
| 4 | +describe('=> SET', function() { |
| 5 | + let numbers; |
| 6 | + before(function () { |
| 7 | + numbers = new Set(); |
| 8 | + }); |
| 9 | + |
| 10 | + it('should create an Empty Set', function(done) { |
| 11 | + expect(numbers.dataStore).to.deep.equal([]); |
| 12 | + done(); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should add element into the Set', function(done) { |
| 16 | + expect(numbers.add(1)).to.equal(true); |
| 17 | + expect(numbers.add(2)).to.equal(true); |
| 18 | + expect(numbers.add(3)).to.equal(true); |
| 19 | + expect(numbers.dataStore.length).to.equal(3); |
| 20 | + expect(numbers.dataStore).to.deep.equal([1,2,3]); |
| 21 | + done(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should display elements of Set', function(done) { |
| 25 | + expect(numbers.show()).to.equal("1,2,3"); |
| 26 | + done(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should return size of Set', function(done) { |
| 30 | + expect(numbers.size()).to.equal(3); |
| 31 | + done(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should check if a given Set is a subset of the Original Set', function(done) { |
| 35 | + tempSet = new Set(); |
| 36 | + tempSet.add(1); |
| 37 | + tempSet.add(2); |
| 38 | + tempSet.add(3); |
| 39 | + expect(numbers.subset(tempSet)).to.equal(true); |
| 40 | + done(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should contain some elements in the Set', function(done) { |
| 44 | + expect(numbers.contains(1)).to.equal(true); |
| 45 | + expect(numbers.contains(31)).to.equal(false); |
| 46 | + done(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should not add duplicate elements into the Set', function(done) { |
| 50 | + expect(numbers.add(1)).to.equal(false); |
| 51 | + expect(numbers.add(2)).to.equal(false); |
| 52 | + expect(numbers.dataStore.length).to.equal(3); |
| 53 | + expect(numbers.dataStore).to.deep.equal([1,2,3]); |
| 54 | + done(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should remove element from the Set', function(done) { |
| 58 | + expect(numbers.remove(1)).to.equal(true); |
| 59 | + expect(numbers.dataStore.length).to.equal(2); |
| 60 | + expect(numbers.dataStore).to.deep.equal([2,3]); |
| 61 | + done(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should not remove element which does not exist in Set', function(done) { |
| 65 | + expect(numbers.remove(1)).to.equal(false); |
| 66 | + expect(numbers.dataStore.length).to.equal(2); |
| 67 | + expect(numbers.dataStore).to.deep.equal([2,3]); |
| 68 | + done(); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('=> union, intersection, difference', function() { |
| 72 | + before(function() { |
| 73 | + numbers2 = new Set(); |
| 74 | + numbers.add(1); |
| 75 | + numbers2.add(1); |
| 76 | + numbers2.add(2); |
| 77 | + numbers2.add(4); |
| 78 | + numbers2.add(5); |
| 79 | + numbers2.add(6); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should compute Union of 2 Sets', function(done) { |
| 83 | + union = numbers.union(numbers2); |
| 84 | + expect(union.dataStore.length).to.equal(2); |
| 85 | + expect(union.contains(1)).to.equal(true); |
| 86 | + expect(union.contains(2)).to.equal(true); |
| 87 | + done(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should compute Intersection of 2 Sets', function(done) { |
| 91 | + intersect = numbers.intersect(numbers2); |
| 92 | + expect(intersect.dataStore.length).to.equal(2); |
| 93 | + expect(intersect.contains(1)).to.equal(true); |
| 94 | + expect(intersect.contains(2)).to.equal(true); |
| 95 | + done(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should compute Difference of 2 Sets', function(done) { |
| 99 | + difference = numbers.difference(numbers2); |
| 100 | + expect(difference.dataStore.length).to.equal(1); |
| 101 | + expect(difference.contains(3)).to.equal(true); |
| 102 | + done(); |
| 103 | + }); |
| 104 | + |
| 105 | + }); |
| 106 | + |
| 107 | +}); |
0 commit comments