From e2ec580acac2cd7e902b339e4a0ff614154195b9 Mon Sep 17 00:00:00 2001 From: kubikowski Date: Sun, 30 Oct 2022 18:30:51 -0600 Subject: [PATCH] added unit tests --- test/comparisons/disjoint.function.test.ts | 18 +++++++++---- test/comparisons/equivalence.function.test.ts | 14 +++++++++- .../proper-subset.function.test.ts | 18 +++++++++---- .../proper-superset.function.test.ts | 18 +++++++++---- test/comparisons/subset.function.test.ts | 18 +++++++++---- test/comparisons/superset.function.test.ts | 18 +++++++++---- test/constants/sort-testing-constants.ts | 4 +-- test/constants/testing-constants.ts | 10 +++++-- test/operations/difference.function.test.ts | 17 +++++++++++- test/operations/intersection.function.test.ts | 17 +++++++++++- test/operations/union.function.test.ts | 17 +++++++++++- test/operations/xor.function.test.ts | 26 +++++++++++++++---- 12 files changed, 157 insertions(+), 38 deletions(-) diff --git a/test/comparisons/disjoint.function.test.ts b/test/comparisons/disjoint.function.test.ts index c78ac21..d6fd728 100644 --- a/test/comparisons/disjoint.function.test.ts +++ b/test/comparisons/disjoint.function.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from '@jest/globals'; import { disjoint } from '../../src'; -import { empty, setA, setB, setC, setD } from '../constants/testing-constants'; +import { empty, setA, setB, setC, setD, setE, setF, universal } from '../constants/testing-constants'; describe('disjoint', () => { it('no sets are disjoint', () => { @@ -27,16 +27,24 @@ describe('disjoint', () => { expect(disjoint(setA, setB, setC)).toBe(false); }); + it('many sets with some shared values are not disjoint', () => { + expect(disjoint(setA, setB, setC, setD, setE, setF)).toBe(false); + }); + + it('any non-empty set and the empty set are disjoint', () => { + expect(disjoint(setA, empty)).toBe(true); + }); + + it('any non-empty set and the universal set are not disjoint', () => { + expect(disjoint(setA, universal)).toBe(false); + }); + it('the empty set is disjoint with itself', () => { expect(disjoint(empty, empty)).toBe(true); }); /* custom disjoint tests */ - it('any set and the empty set are disjoint', () => { - expect(disjoint(setA, empty)).toBe(true); - }); - it('two sets with no shared values are disjoint', () => { expect(disjoint(setA, setD)).toBe(true); }); diff --git a/test/comparisons/equivalence.function.test.ts b/test/comparisons/equivalence.function.test.ts index 558125f..cce9e98 100644 --- a/test/comparisons/equivalence.function.test.ts +++ b/test/comparisons/equivalence.function.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from '@jest/globals'; import { equivalence } from '../../src'; -import { empty, minimal, setA, setB, setC } from '../constants/testing-constants'; +import { empty, minimal, setA, setB, setC, setD, setE, setF, universal } from '../constants/testing-constants'; describe('equivalence', () => { it('no sets are equivalent', () => { @@ -27,6 +27,18 @@ describe('equivalence', () => { expect(equivalence(setA, setB, setC)).toBe(false); }); + it('many different sets are not equivalent', () => { + expect(equivalence(setA, setB, setC, setD, setE, setF)).toBe(false); + }); + + it('any non-empty set and the empty set are not equivalent', () => { + expect(equivalence(setA, empty)).toBe(false); + }); + + it('any non-universal set and the universal set are not equivalent', () => { + expect(equivalence(setA, universal)).toBe(false); + }); + it('the empty set is equivalent to itself', () => { expect(equivalence(empty, empty)).toBe(true); }); diff --git a/test/comparisons/proper-subset.function.test.ts b/test/comparisons/proper-subset.function.test.ts index 33128b1..5b10a57 100644 --- a/test/comparisons/proper-subset.function.test.ts +++ b/test/comparisons/proper-subset.function.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from '@jest/globals'; import { properSubset } from '../../src'; -import { empty, minimal, setA, setB, setC, setD, universal } from '../constants/testing-constants'; +import { empty, minimal, setA, setB, setC, setD, setE, setF, universal } from '../constants/testing-constants'; describe('proper subset', () => { it('no sets are proper subsets', () => { @@ -27,6 +27,18 @@ describe('proper subset', () => { expect(properSubset(setA, setB, setC)).toBe(false); }); + it('many sets with different values are not proper subsets', () => { + expect(properSubset(setA, setB, setC, setD, setE, setF)).toBe(false); + }); + + it('any non-empty set is not a proper subset of the empty set', () => { + expect(properSubset(setA, empty)).toBe(false); + }); + + it('any non-universal set is a proper subset of the universal set', () => { + expect(properSubset(setA, universal)).toBe(true); + }); + it('the empty set is not a proper subset of itself', () => { expect(properSubset(empty, empty)).toBe(false); }); @@ -41,10 +53,6 @@ describe('proper subset', () => { expect(properSubset(setD, setA)).toBe(false); }); - it('any non-universal set is a proper subset of the universal set', () => { - expect(properSubset(setA, universal)).toBe(true); - }); - it('the empty set is a proper subset of every non-empty set', () => { expect(properSubset(empty, minimal, setA, setB, setC, universal)).toBe(true); }); diff --git a/test/comparisons/proper-superset.function.test.ts b/test/comparisons/proper-superset.function.test.ts index cbf18b4..f189717 100644 --- a/test/comparisons/proper-superset.function.test.ts +++ b/test/comparisons/proper-superset.function.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from '@jest/globals'; import { properSuperset } from '../../src'; -import { empty, minimal, setA, setB, setC, setD, universal } from '../constants/testing-constants'; +import { empty, minimal, setA, setB, setC, setD, setE, setF, universal } from '../constants/testing-constants'; describe('proper superset', () => { it('no sets are proper supersets', () => { @@ -27,6 +27,18 @@ describe('proper superset', () => { expect(properSuperset(setA, setB, setC)).toBe(false); }); + it('many sets with different values are not proper supersets', () => { + expect(properSuperset(setA, setB, setC, setD, setE, setF)).toBe(false); + }); + + it('any non-empty set is a proper superset of the empty set', () => { + expect(properSuperset(setA, empty)).toBe(true); + }); + + it('any non-universal set is not a proper superset of the universal set', () => { + expect(properSuperset(setA, universal)).toBe(false); + }); + it('the empty set is not a proper superset of itself', () => { expect(properSuperset(empty, empty)).toBe(false); }); @@ -41,10 +53,6 @@ describe('proper superset', () => { expect(properSuperset(setA, setD)).toBe(false); }); - it('any non-empty set is a proper superset of the empty set', () => { - expect(properSuperset(setA, empty)).toBe(true); - }); - it('the universal set is a proper superset of every non-universal set', () => { expect(properSuperset(universal, setA, setB, setC, minimal, empty)).toBe(true); }); diff --git a/test/comparisons/subset.function.test.ts b/test/comparisons/subset.function.test.ts index 0ae96e4..c0c01a4 100644 --- a/test/comparisons/subset.function.test.ts +++ b/test/comparisons/subset.function.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from '@jest/globals'; import { subset } from '../../src'; -import { empty, minimal, setA, setB, setC, setD, universal } from '../constants/testing-constants'; +import { empty, minimal, setA, setB, setC, setD, setE, setF, universal } from '../constants/testing-constants'; describe('subset', () => { it('no sets are subsets', () => { @@ -27,6 +27,18 @@ describe('subset', () => { expect(subset(setA, setB, setC)).toBe(false); }); + it('many sets with different values are not subsets', () => { + expect(subset(setA, setB, setC, setD, setE, setF)).toBe(false); + }); + + it('any non-empty set is not a subset of the empty set', () => { + expect(subset(setA, empty)).toBe(false); + }); + + it('any non-universal set is a subset of the universal set', () => { + expect(subset(setA, universal)).toBe(true); + }); + it('the empty set is a subset of itself', () => { expect(subset(empty, empty)).toBe(true); }); @@ -41,10 +53,6 @@ describe('subset', () => { expect(subset(setD, setA)).toBe(false); }); - it('any set is a subset of the universal set', () => { - expect(subset(setA, universal)).toBe(true); - }); - it('the empty set is a subset of every set', () => { expect(subset(empty, minimal, setA, setB, setC, universal)).toBe(true); }); diff --git a/test/comparisons/superset.function.test.ts b/test/comparisons/superset.function.test.ts index d4402e5..b405b83 100644 --- a/test/comparisons/superset.function.test.ts +++ b/test/comparisons/superset.function.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from '@jest/globals'; import { superset } from '../../src'; -import { empty, minimal, setA, setB, setC, setD, universal } from '../constants/testing-constants'; +import { empty, minimal, setA, setB, setC, setD, setE, setF, universal } from '../constants/testing-constants'; describe('superset', () => { it('no sets are superset', () => { @@ -27,6 +27,18 @@ describe('superset', () => { expect(superset(setA, setB, setC)).toBe(false); }); + it('many sets with different values are not supersets', () => { + expect(superset(setA, setB, setC, setD, setE, setF)).toBe(false); + }); + + it('any non-empty set is a superset of the empty set', () => { + expect(superset(setA, empty)).toBe(true); + }); + + it('any non-universal set is not a superset of the universal set', () => { + expect(superset(setA, universal)).toBe(false); + }); + it('the empty set is a superset of itself', () => { expect(superset(empty, empty)).toBe(true); }); @@ -41,10 +53,6 @@ describe('superset', () => { expect(superset(setA, setD)).toBe(false); }); - it('any set is a superset of the empty set', () => { - expect(superset(setA, empty)).toBe(true); - }); - it('the universal set is a superset of every set', () => { expect(superset(universal, setA, setB, setC, minimal, empty)).toBe(true); }); diff --git a/test/constants/sort-testing-constants.ts b/test/constants/sort-testing-constants.ts index 482891d..d980bed 100644 --- a/test/constants/sort-testing-constants.ts +++ b/test/constants/sort-testing-constants.ts @@ -1,7 +1,7 @@ import { expect } from '@jest/globals'; -/* unordered universal set, contains: 0, 1, 2, 3, 4, 5, 6, 7 */ -export const unordered = new Set([ 4, 6, 1, 2, 5, 7, 0, 3 ]); +/* unordered universal set, contains: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 */ +export const unordered = new Set([ 4, 9, 6, 1, 8, 2, 5, 7, 0, 3 ]); /* (default) less than comparator function */ export function defaultComparator(a: T, b: T): number { diff --git a/test/constants/testing-constants.ts b/test/constants/testing-constants.ts index eb19bb8..3827981 100644 --- a/test/constants/testing-constants.ts +++ b/test/constants/testing-constants.ts @@ -9,6 +9,8 @@ * - 5: unique to setB * - 6: unique to setC * - 7: unique to setD + * - 8: unique to setE + * - 9: unique to setF */ /* contains: 0 */ @@ -21,8 +23,12 @@ export const setB = new Set([ 0, 1, 3, 5 ]); export const setC = new Set([ 0, 2, 3, 6 ]); /* contains: 7 */ export const setD = new Set([ 7 ]); +/* contains: 7 */ +export const setE = new Set([ 8 ]); +/* contains: 7 */ +export const setF = new Set([ 9 ]); -/* the universal set: U, contains: 0, 1, 2, 3, 4, 5, 6, 7 */ -export const universal = new Set([ 0, 1, 2, 3, 4, 5, 6, 7 ]); +/* the universal set: U, contains: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 */ +export const universal = new Set([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]); /* the empty set: ∅, contains: none */ export const empty = new Set(); diff --git a/test/operations/difference.function.test.ts b/test/operations/difference.function.test.ts index 9069937..f8253ec 100644 --- a/test/operations/difference.function.test.ts +++ b/test/operations/difference.function.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from '@jest/globals'; import { difference, equivalence } from '../../src'; -import { empty, setA, setB, setC, universal } from '../constants/testing-constants'; +import { empty, setA, setB, setC, setD, setE, setF, universal } from '../constants/testing-constants'; describe('difference', () => { const differenceAB = new Set([ 2, 4 ]); @@ -21,6 +21,11 @@ describe('difference', () => { expect(equivalence(result, empty)).toBe(true); }); + it('many of the same set has no difference overlap', () => { + const result = difference(setA, setA, setA); + expect(equivalence(result, empty)).toBe(true); + }); + it('two sets\' difference is a subset of the first', () => { const result = difference(setA, setB); expect(equivalence(result, differenceAB)).toBe(true); @@ -31,6 +36,11 @@ describe('difference', () => { expect(equivalence(result, differenceABC)).toBe(true); }); + it('many sets\' difference is a subset of the first', () => { + const result = difference(setA, setB, setC, setD, setE, setF); + expect(equivalence(result, differenceABC)).toBe(true); + }); + it('any sets\' difference with the empty set is itself', () => { const result = difference(setA, empty); expect(equivalence(result, setA)).toBe(true); @@ -40,4 +50,9 @@ describe('difference', () => { const result = difference(setA, universal); expect(equivalence(result, empty)).toBe(true); }); + + it('the empty sets\' difference with itself is itself', () => { + const result = difference(empty, empty); + expect(equivalence(result, empty)).toBe(true); + }); }); diff --git a/test/operations/intersection.function.test.ts b/test/operations/intersection.function.test.ts index 14affde..32972b4 100644 --- a/test/operations/intersection.function.test.ts +++ b/test/operations/intersection.function.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from '@jest/globals'; import { equivalence, intersection } from '../../src'; -import { empty, setA, setB, setC, universal } from '../constants/testing-constants'; +import { empty, setA, setB, setC, setD, setE, setF, universal } from '../constants/testing-constants'; describe('intersection', () => { const intersectionAB = new Set([ 0, 1 ]); @@ -21,6 +21,11 @@ describe('intersection', () => { expect(equivalence(result, setA)).toBe(true); }); + it('many of the same set intersection returns self', () => { + const result = intersection(setA, setA, setA); + expect(equivalence(result, setA)).toBe(true); + }); + it('two sets\' intersection is a subset of the first', () => { const result = intersection(setA, setB); expect(equivalence(result, intersectionAB)).toBe(true); @@ -31,6 +36,11 @@ describe('intersection', () => { expect(equivalence(result, intersectionABC)).toBe(true); }); + it('many sets\' intersection is a subset of the first', () => { + const result = intersection(setA, setB, setC, setD, setE, setF); + expect(equivalence(result, empty)).toBe(true); + }); + it('any sets\' intersection with the empty set is the empty set', () => { const result = intersection(setA, empty); expect(equivalence(result, empty)).toBe(true); @@ -40,4 +50,9 @@ describe('intersection', () => { const result = intersection(setA, universal); expect(equivalence(result, setA)).toBe(true); }); + + it('the empty sets\' intersection with itself is itself', () => { + const result = intersection(empty, empty); + expect(equivalence(result, empty)).toBe(true); + }); }); diff --git a/test/operations/union.function.test.ts b/test/operations/union.function.test.ts index d4b4d85..9e75d41 100644 --- a/test/operations/union.function.test.ts +++ b/test/operations/union.function.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from '@jest/globals'; import { equivalence, union } from '../../src'; -import { empty, setA, setB, setC, universal } from '../constants/testing-constants'; +import { empty, setA, setB, setC, setD, setE, setF, universal } from '../constants/testing-constants'; describe('union', () => { const unionAB = new Set([ 0, 1, 2, 3, 4, 5 ]); @@ -21,6 +21,11 @@ describe('union', () => { expect(equivalence(result, setA)).toBe(true); }); + it('many of the same set union returns self', () => { + const result = union(setA, setA, setA); + expect(equivalence(result, setA)).toBe(true); + }); + it('two sets\' union contains all values from both sets', () => { const result = union(setA, setB); expect(equivalence(result, unionAB)).toBe(true); @@ -31,6 +36,11 @@ describe('union', () => { expect(equivalence(result, unionABC)).toBe(true); }); + it('many sets\' union contains all values from all sets', () => { + const result = union(setA, setB, setC, setD, setE, setF); + expect(equivalence(result, universal)).toBe(true); + }); + it('any sets\' union with the empty set is itself', () => { const result = union(setA, empty); expect(equivalence(result, setA)).toBe(true); @@ -40,4 +50,9 @@ describe('union', () => { const result = union(setA, universal); expect(equivalence(result, universal)).toBe(true); }); + + it('the empty sets\' union with itself is itself', () => { + const result = union(empty, empty); + expect(equivalence(result, empty)).toBe(true); + }); }); diff --git a/test/operations/xor.function.test.ts b/test/operations/xor.function.test.ts index 99d601c..3ff6769 100644 --- a/test/operations/xor.function.test.ts +++ b/test/operations/xor.function.test.ts @@ -1,11 +1,12 @@ import { describe, expect, it } from '@jest/globals'; import { equivalence, xor } from '../../src'; -import { empty, setA, setB, setC, universal } from '../constants/testing-constants'; +import { empty, setA, setB, setC, setD, setE, setF, universal } from '../constants/testing-constants'; describe('xor', () => { const xorAB = new Set([ 2, 3, 4, 5 ]); const xorABC = new Set([ 4, 5, 6 ]); - const xorAU = new Set([ 3, 5, 6, 7 ]); + const xorABCDEF = new Set([ 4, 5, 6, 7, 8, 9 ]); + const xorAU = new Set([ 3, 5, 6, 7, 8, 9 ]); it('no sets xor returns empty set', () => { const result = xor(); @@ -17,11 +18,16 @@ describe('xor', () => { expect(equivalence(result, setA)).toBe(true); }); - it('equivalence: same set xor returns the empty set', () => { + it('same set xor returns the empty set', () => { const result = xor(setA, setA); expect(equivalence(result, empty)).toBe(true); }); + it('many of the same set xor returns the empty set', () => { + const result = xor(setA, setA, setA); + expect(equivalence(result, empty)).toBe(true); + }); + it('two different sets xor returns unique values', () => { const result = xor(setA, setB); expect(equivalence(result, xorAB)).toBe(true); @@ -32,13 +38,23 @@ describe('xor', () => { expect(equivalence(result, xorABC)).toBe(true); }); - it('any sets\' xor with the empty set is itself', () => { + it('many different sets xor returns unique values', () => { + const result = xor(setA, setB, setC, setD, setE, setF); + expect(equivalence(result, xorABCDEF)).toBe(true); + }); + + it('any non-empty sets\' xor with the empty set is itself', () => { const result = xor(setA, empty); expect(equivalence(result, setA)).toBe(true); }); - it('any sets\' xor with the universal set is the difference of the universal set and itself', () => { + it('any non-universal sets\' xor with the universal set is the difference of the universal set and itself', () => { const result = xor(setA, universal); expect(equivalence(result, xorAU)).toBe(true); }); + + it('the empty sets\' xor with itself is itself', () => { + const result = xor(empty, empty); + expect(equivalence(result, empty)).toBe(true); + }); });