Skip to content

Commit adf6981

Browse files
Migrate doctest for Vector2.js
1 parent 6eeb989 commit adf6981

File tree

2 files changed

+60
-69
lines changed

2 files changed

+60
-69
lines changed

Data-Structures/Vectors/Vector2.js

Lines changed: 2 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4,75 +4,6 @@
44
* https://en.wikipedia.org/wiki/Vector_(mathematics_and_physics)).
55
*/
66

7-
/*
8-
Doctests
9-
10-
// Test equalsExactly-method
11-
> new Vector2(1, 0).equalsExactly(new Vector2(1, 0))
12-
true
13-
> new Vector2(1.23, 4.56).equalsExactly(new Vector2(0, 0))
14-
false
15-
16-
// Test equalsApproximately-method
17-
> new Vector2(1, 0).equalsApproximately(new Vector2(1, 0.0000001), 0.000001)
18-
true
19-
> new Vector2(1.23, 4.56).equalsApproximately(new Vector2(1.24, 4.56), 0.000001)
20-
false
21-
22-
// Test add-method
23-
> new Vector2(1, 0).add(new Vector2(0, 1)).equalsApproximately(new Vector2(1, 1), 0.000001)
24-
true
25-
> new Vector2(-3.3, -9).add(new Vector2(-2.2, 3)).equalsApproximately(new Vector2(-5.5, -6), 0.000001)
26-
true
27-
28-
// Test subtract-method
29-
> new Vector2(1, 0).subtract(new Vector2(0, 1)).equalsApproximately(new Vector2(1, -1), 0.000001)
30-
true
31-
> new Vector2(234.5, 1.7).subtract(new Vector2(3.3, 2.7)).equalsApproximately(new Vector2(231.2, -1), 0.000001)
32-
true
33-
34-
// Test multiply-method
35-
> new Vector2(1, 0).multiply(5).equalsApproximately(new Vector2(5, 0), 0.000001)
36-
true
37-
> new Vector2(3.41, -7.12).multiply(-3.1).equalsApproximately(new Vector2(-10.571, 22.072), 0.000001)
38-
true
39-
40-
// Test length-method
41-
> new Vector2(1, 0).length()
42-
1
43-
> new Vector2(-1, 1).length()
44-
Math.sqrt(2)
45-
46-
// Test normalize-method
47-
> new Vector2(1, 0).normalize().equalsApproximately(new Vector2(1, 0), 0.000001)
48-
true
49-
> new Vector2(1, -1).normalize().equalsApproximately(new Vector2(Math.sqrt(2) / 2, -Math.sqrt(2) / 2), 0.000001)
50-
true
51-
52-
// Test distance-method
53-
> new Vector2(0, 0).distance(new Vector2(0, -1))
54-
1
55-
> new Vector2(1, 0).distance(new Vector2(0, 1))
56-
Math.sqrt(2)
57-
58-
// Test dotProduct-method
59-
> new Vector2(1, 0).dotProduct(new Vector2(0, 1))
60-
0
61-
> new Vector2(1, 2).dotProduct(new Vector2(3, 4))
62-
1 * 3 + 2 * 4
63-
64-
// Test rotate-method
65-
> new Vector2(0, -1).rotate(Math.PI / 2).equalsApproximately(new Vector2(1, 0), 0.000001)
66-
true
67-
> new Vector2(1.23, -4.56).rotate(Math.PI).equalsApproximately(new Vector2(-1.23, 4.56), 0.000001)
68-
true
69-
70-
// Test angleBetween-method
71-
> new Vector2(1, 0).angleBetween(new Vector2(0, 1))
72-
Math.PI / 2
73-
> new Vector2(1, 0).angleBetween(new Vector2(1, -1))
74-
-Math.PI / 4
75-
*/
767
class Vector2 { // eslint-disable-line no-unused-vars
778
constructor (x, y) {
789
this.x = x
@@ -203,3 +134,5 @@ class Vector2 { // eslint-disable-line no-unused-vars
203134
return Math.atan2(vector.y, vector.x) - Math.atan2(this.y, this.x)
204135
}
205136
}
137+
138+
export { Vector2 }
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { Vector2 } from './Vector2.js'
2+
3+
describe('Tests for Vector2 data scructure implementation', () => {
4+
test('tests equalsExactly-method', () => {
5+
expect(new Vector2(1, 0).equalsExactly(new Vector2(1, 0))).toBe(true)
6+
expect(new Vector2(1.23, 4.56).equalsExactly(new Vector2(0, 0))).toBe(false)
7+
})
8+
9+
test('tests equalsApproximately-method', () => {
10+
expect(new Vector2(1, 0).equalsApproximately(new Vector2(1, 0.0000001), 0.000001)).toBe(true)
11+
expect(new Vector2(1.23, 4.56).equalsApproximately(new Vector2(1.24, 4.56), 0.000001)).toBe(false)
12+
})
13+
14+
test('tests add-method', () => {
15+
expect(new Vector2(1, 0).add(new Vector2(0, 1)).equalsApproximately(new Vector2(1, 1), 0.000001)).toBe(true)
16+
expect(new Vector2(-3.3, -9).add(new Vector2(-2.2, 3)).equalsApproximately(new Vector2(-5.5, -6), 0.000001)).toBe(true)
17+
})
18+
19+
test('tests subtract-method', () => {
20+
expect(new Vector2(1, 0).subtract(new Vector2(0, 1)).equalsApproximately(new Vector2(1, -1), 0.000001)).toBe(true)
21+
expect(new Vector2(234.5, 1.7).subtract(new Vector2(3.3, 2.7)).equalsApproximately(new Vector2(231.2, -1), 0.000001)).toBe(true)
22+
})
23+
24+
test('tests multiply-method', () => {
25+
expect(new Vector2(1, 0).multiply(5).equalsApproximately(new Vector2(5, 0), 0.000001)).toBe(true)
26+
expect(new Vector2(3.41, -7.12).multiply(-3.1).equalsApproximately(new Vector2(-10.571, 22.072), 0.000001)).toBe(true)
27+
})
28+
29+
test('tests length-method', () => {
30+
expect(new Vector2(1, 0).length()).toBe(1)
31+
expect(new Vector2(-1, 1).length()).toBe(Math.sqrt(2))
32+
})
33+
34+
test('tests normalize-method', () => {
35+
expect(new Vector2(1, 0).normalize().equalsApproximately(new Vector2(1, 0), 0.000001)).toBe(true)
36+
expect(new Vector2(1, -1).normalize().equalsApproximately(new Vector2(Math.sqrt(2) / 2, -Math.sqrt(2) / 2), 0.000001)).toBe(true)
37+
})
38+
39+
test('tests distance-method', () => {
40+
expect(new Vector2(0, 0).distance(new Vector2(0, -1))).toBe(1)
41+
expect(new Vector2(1, 0).distance(new Vector2(0, 1))).toBe(Math.sqrt(2))
42+
})
43+
44+
test('tests dotProduct-method', () => {
45+
expect(new Vector2(1, 0).dotProduct(new Vector2(0, 1))).toBe(0)
46+
expect(new Vector2(1, 2).dotProduct(new Vector2(3, 4))).toBe(1 * 3 + 2 * 4)
47+
})
48+
49+
test('tests rotate-method', () => {
50+
expect(new Vector2(0, -1).rotate(Math.PI / 2).equalsApproximately(new Vector2(1, 0), 0.000001)).toBe(true)
51+
expect(new Vector2(1.23, -4.56).rotate(Math.PI).equalsApproximately(new Vector2(-1.23, 4.56), 0.000001)).toBe(true)
52+
})
53+
54+
test('tests angleBetween-method', () => {
55+
expect(new Vector2(1, 0).angleBetween(new Vector2(0, 1))).toBe(Math.PI / 2)
56+
expect(new Vector2(1, 0).angleBetween(new Vector2(1, -1))).toBe(-Math.PI / 4)
57+
})
58+
})

0 commit comments

Comments
 (0)