UNPKG

3.96 kBJavaScriptView Raw
1const algebra = require('algebra')
2const notDefined = require('not-defined')
3
4const Real = algebra.Real
5const VectorSpace = algebra.VectorSpace
6
7const methodBinaryOperator = require('./features/methodBinaryOperator')
8const staticBinaryOperator = require('./features/staticBinaryOperator')
9const staticUnaryOperator = require('./features/staticUnaryOperator')
10
11const R2 = VectorSpace(Real)(2)
12const R3 = VectorSpace(Real)(3)
13
14describe('VectorSpace', () => {
15 describe('data', () => {
16 const v = new R2([0, 1])
17
18 it('is enumerable', () => {
19 v.propertyIsEnumerable('data').should.be.ok
20 })
21
22 it('is immutable', () => {
23 ;(() => {
24 'use strict'
25 v.data = [2, 1]
26 }).should.throwError()
27 })
28 })
29
30 describe('addition()', () => {
31 const operator = 'addition'
32
33 it('is a static method', staticBinaryOperator(R2, operator, [0, 2], [-1, 3], [-1, 5]))
34
35 it('is a class method', methodBinaryOperator(R2, operator, [0, 1], [1, 1], [1, 2]))
36
37 it('accepts multiple arguments', () => {
38 R2.addition([1, -1], [2, -2], [3, -3]).should.deepEqual([6, -6])
39
40 const vector = new R2([1, -1])
41 vector.addition([2, -2], [3, -3]).data.should.eql([6, -6])
42 })
43 })
44
45 describe('subtraction()', () => {
46 const operator = 'subtraction'
47
48 it('is a static method', staticBinaryOperator(R2, operator, [0, 2], [-1, 3], [1, -1]))
49
50 it('is a class method', methodBinaryOperator(R2, operator, [0, 1], [1, 1], [-1, 0]))
51
52 it('accepts multiple arguments', () => {
53 R2.subtraction([6, -6], [2, -2], [3, -3]).should.deepEqual([1, -1])
54
55 const vector = new R2([6, -6])
56 vector.subtraction([2, -2], [3, -3]).data.should.eql([1, -1])
57 })
58 })
59
60 describe('scalarProduct()', () => {
61 it('is a static operator', () => {
62 const data = R2.scalarProduct([0, 1], [1, 1])
63
64 data.should.eql(1)
65 })
66
67 it('is a class method', () => {
68 const vector1 = new R2([0, 1])
69 const vector2 = new R2([1, 1])
70
71 const scalar = vector1.scalarProduct(vector2)
72
73 scalar.data.should.be.eql(1)
74 })
75
76 it('is returns a scalar', () => {
77 const vector1 = new R2([0, 1])
78 const vector2 = new R2([1, 1])
79
80 const scalar = vector1.scalarProduct(vector2)
81
82 scalar.data.should.be.eql(1)
83 })
84 })
85
86 describe('dotProduct()', () => {
87 it('is an alias of scalarProduct()', () => {
88 R2.scalarProduct.should.be.eql(R2.dotProduct)
89
90 const vector = new R2([0, 1])
91 vector.scalarProduct.should.be.eql(vector.dotProduct)
92 })
93 })
94
95 describe('dot()', () => {
96 it('is an alias of scalarProduct()', () => {
97 R2.scalarProduct.should.be.eql(R2.dot)
98
99 const vector = new R2([0, 1])
100 vector.scalarProduct.should.be.eql(vector.dot)
101 })
102 })
103
104 describe('norm', () => {
105 it('is an attribute holding a scalar', () => {
106 const vector1 = new R2([0, 1])
107 const vector2 = new R3([1, 1, 2])
108
109 vector1.norm.data.should.be.eql(1)
110 vector2.norm.data.should.be.eql(6)
111 })
112 })
113
114 describe('norm()', () => {
115 const operator = 'norm'
116
117 it('is a static method', () => {
118 staticUnaryOperator(R2, operator, [0, 1], 1)
119 staticUnaryOperator(R3, operator, [1, 1, 2], 6)
120 })
121 })
122
123 describe('crossProduct()', () => {
124 const operator = 'crossProduct'
125
126 it('is a static method', () => {
127 staticBinaryOperator(R3, operator, [3, -3, 1], [4, 9, 2], [-15, -2, 39])
128 })
129
130 it('is a class method', () => {
131 methodBinaryOperator(R3, operator, [3, -3, 1], [-12, 12, -4], [0, 0, 0])
132 })
133
134 it('is defined only in dimension 3', () => {
135 notDefined(R2.cross).should.be.ok
136
137 const vector = new R2([1, 0])
138 notDefined(vector.cross).should.be.ok
139 })
140 })
141
142 describe('cross()', () => {
143 it('is an alias of crossProduct()', () => {
144 R3.crossProduct.should.be.eql(R3.cross)
145
146 const vector = new R3([1, 0, 1])
147 vector.crossProduct.should.be.eql(vector.cross)
148 })
149 })
150})