set.test.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { equal, intersect } from './set';
  2. describe('equal', () => {
  3. it('returns false for two sets of differing sizes', () => {
  4. const s1 = new Set([1, 2, 3]);
  5. const s2 = new Set([4, 5, 6, 7]);
  6. expect(equal(s1, s2)).toBe(false);
  7. });
  8. it('returns false for two sets where one is a subset of the other', () => {
  9. const s1 = new Set([1, 2, 3]);
  10. const s2 = new Set([1, 2, 3, 4]);
  11. expect(equal(s1, s2)).toBe(false);
  12. });
  13. it('returns false for two sets with uncommon elements', () => {
  14. const s1 = new Set([1, 2, 3, 4]);
  15. const s2 = new Set([1, 2, 5, 6]);
  16. expect(equal(s1, s2)).toBe(false);
  17. });
  18. it('returns false for two deeply equivalent sets', () => {
  19. const s1 = new Set([{ a: 1 }, { b: 2 }, { c: 3 }, { d: 4 }]);
  20. const s2 = new Set([{ a: 1 }, { b: 2 }, { c: 3 }, { d: 4 }]);
  21. expect(equal(s1, s2)).toBe(false);
  22. });
  23. it('returns true for two sets with the same elements', () => {
  24. const s1 = new Set([1, 2, 3, 4]);
  25. const s2 = new Set([4, 3, 2, 1]);
  26. expect(equal(s1, s2)).toBe(true);
  27. });
  28. });
  29. describe('intersect', () => {
  30. it('returns an empty set for two sets without any common elements', () => {
  31. const s1 = new Set([1, 2, 3, 4]);
  32. const s2 = new Set([5, 6, 7, 8]);
  33. expect(intersect(s1, s2)).toEqual(new Set());
  34. });
  35. it('returns an empty set for two deeply equivalent sets', () => {
  36. const s1 = new Set([{ a: 1 }, { b: 2 }, { c: 3 }, { d: 4 }]);
  37. const s2 = new Set([{ a: 1 }, { b: 2 }, { c: 3 }, { d: 4 }]);
  38. expect(intersect(s1, s2)).toEqual(new Set());
  39. });
  40. it('returns a set containing common elements between two sets of the same size', () => {
  41. const s1 = new Set([1, 2, 3, 4]);
  42. const s2 = new Set([5, 2, 7, 4]);
  43. expect(intersect(s1, s2)).toEqual(new Set([2, 4]));
  44. });
  45. it('returns a set containing common elements between two sets of differing sizes', () => {
  46. const s1 = new Set([1, 2, 3, 4]);
  47. const s2 = new Set([5, 4, 3, 2, 1]);
  48. expect(intersect(s1, s2)).toEqual(new Set([1, 2, 3, 4]));
  49. });
  50. });