1
exercism fetch ecmascript perfect-numbers

perfect-numbers.spec.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import PerfectNumbers from './perfect-numbers';

describe('Exercise - Perfect Numbers', () => {
  const perfectNumbers = new PerfectNumbers();

  describe('Perfect Numbers', () => {
    test('Smallest perfect number is classified correctly', () => {
      expect(perfectNumbers.classify(6)).toEqual('perfect');
    });

    xtest('Medium perfect number is classified correctly', () => {
      expect(perfectNumbers.classify(28)).toEqual('perfect');
    });

    xtest('Large perfect number is classified correctly', () => {
      expect(perfectNumbers.classify(33550336)).toEqual('perfect');
    });
  });

  describe('Abundant Numbers', () => {
    xtest('Smallest abundant number is classified correctly', () => {
      expect(perfectNumbers.classify(12)).toEqual('abundant');
    });

    xtest('Medium abundant number is classified correctly', () => {
      expect(perfectNumbers.classify(30)).toEqual('abundant');
    });

    xtest('Large abundant number is classified correctly', () => {
      expect(perfectNumbers.classify(33550335)).toEqual('abundant');
    });
  });

  describe('Deficient Numbers', () => {
    xtest('Smallest prime deficient number is classified correctly', () => {
      expect(perfectNumbers.classify(2)).toEqual('deficient');
    });

    xtest('Smallest non-prime deficient number is classified correctly', () => {
      expect(perfectNumbers.classify(4)).toEqual('deficient');
    });

    xtest('Medium deficient number is classified correctly', () => {
      expect(perfectNumbers.classify(32)).toEqual('deficient');
    });

    xtest('Large deficient number is classified correctly', () => {
      expect(perfectNumbers.classify(33550337)).toEqual('deficient');
    });

    xtest('Edge case (no factors other than itself) is classified correctly', () => {
      expect(perfectNumbers.classify(1)).toEqual('deficient');
    });
  });

  describe('Invalid Inputs', () => {
    xtest('Zero is rejected (not a natural number)', () => {
      expect(() => perfectNumbers.classify(0))
        .toThrow('Classification is only possible for natural numbers.');
    });

    xtest('Negative integer is rejected (not a natural number)', () => {
      expect(() => perfectNumbers.classify(-1))
        .toThrow('Classification is only possible for natural numbers.');
    });
  });
});