Passed
Push — main ( c73ca2...a94e44 )
by Dylan
03:37
created

src/SternBrocotTree.test.ts   A

Complexity

Total Complexity 6
Complexity/F 0

Size

Lines of Code 70
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 55
mnd 6
bc 6
fnc 0
dl 0
loc 70
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import {rationalApproximation, pathToValue, continuedFraction} from './SternBrocotTree'
2
3
test('Rational number for 13/7 is as expected', () => {
4
  expect(rationalApproximation(13/7).toString()).toBe('13/7')
5
})
6
7
test('Path to 1 is empty', () => {
8
  const a = 1
9
  const n = pathToValue(a).next()
10
  expect(n.value).toBeUndefined()
11
  expect(n.done).toBe(true)
12
})
13
14
test('Path to 3/7 is as expected', () => {
15
  const a = 3/7
16
  const ex = [false, false, true, true]
17
  const r = []
18
  for (const n of pathToValue(a)) {
19
    r.push(n)
20
  }
21
  expect(r).toStrictEqual(ex)
22
})
23
24
test('Continued fraction for Φ is all ones', () => {
25
  const Φ = (1 + Math.sqrt(5)) / 2
26
  const ex = Array(38).fill(1)
27
  const r = []
28
  for (const n of continuedFraction(Φ)) {
29
    r.push(n)
30
  }
31
  expect(r).toStrictEqual(ex)
32
})
33
34
test('Continued fraction for 355/113 is 3,7', () => {
35
  const a = 355/113
36
  const ex = [3, 7]
37
  const r = []
38
  for (const n of continuedFraction(a)) {
39
    r.push(n)
40
  }
41
  expect(r).toStrictEqual(ex)
42
})
43
44
test('Continued fraction for π is as expected', () => {
45
  const ex = [3, 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14]
46
  const r = []
47
  for (const n of continuedFraction(Math.PI)) {
48
    r.push(n)
49
  }
50
  expect(r).toStrictEqual(ex)
51
})
52
53
test('Continued fraction for e is as expected', () => {
54
  const ex = [2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1, 1, 12, 1, 1]
55
  const r = []
56
  for (const n of continuedFraction(Math.E)) {
57
    r.push(n)
58
  }
59
  expect(r).toStrictEqual(ex)
60
})
61
62
test('Continued fraction for √2 is as expected', () => {
63
  const ex = [1].concat( Array(20).fill(2) )
64
  const r = []
65
  for (const n of continuedFraction(Math.SQRT2)) {
66
    r.push(n)
67
  }
68
  expect(r).toStrictEqual(ex)
69
})
70