1
+ const assert = require ( 'assert' ) ;
2
+ const utils = require ( '../../utils/math' ) ;
3
+
4
+ describe ( 'math' , function ( ) {
5
+ describe ( 'backToPositive' , ( ) => {
6
+ it ( 'goes back to 0 if it goes 1 by 1' , ( ) => {
7
+ assert . equal ( utils . backToPositive ( - 5 , 1 ) , 0 ) ;
8
+ } ) ;
9
+
10
+ it ( 'goes back to the first positive value' , ( ) => {
11
+ assert . equal ( utils . backToPositive ( - 105 , 51 ) , 48 ) ;
12
+ } ) ;
13
+ } ) ;
14
+
15
+ describe ( 'coordBetween' , ( ) => {
16
+ it ( 'returns true if it is within the ranges' , ( ) => {
17
+ assert . equal ( utils . coordBetween (
18
+ { x : 5 , y : 6 } ,
19
+ { x : 0 , y : 1 } ,
20
+ { x : 6 , y : 7 } ,
21
+ ) , true ) ;
22
+ } ) ;
23
+
24
+ it ( 'returns true if it is on the first point' , ( ) => {
25
+ assert . equal ( utils . coordBetween (
26
+ { x : 0 , y : 1 } ,
27
+ { x : 0 , y : 1 } ,
28
+ { x : 6 , y : 7 } ,
29
+ ) , true ) ;
30
+ } ) ;
31
+
32
+ it ( 'returns true if it is on the last point' , ( ) => {
33
+ assert . equal ( utils . coordBetween (
34
+ { x : 6 , y : 7 } ,
35
+ { x : 0 , y : 1 } ,
36
+ { x : 6 , y : 7 } ,
37
+ ) , true ) ;
38
+ } ) ;
39
+
40
+ it ( 'returns false if it is beyond the first point' , ( ) => {
41
+ assert . equal ( utils . coordBetween (
42
+ { x : 0 , y : 0 } ,
43
+ { x : 0 , y : 1 } ,
44
+ { x : 6 , y : 7 } ,
45
+ ) , false ) ;
46
+ } ) ;
47
+
48
+ it ( 'returns false if it is beyond the last point' , ( ) => {
49
+ assert . equal ( utils . coordBetween (
50
+ { x : 7 , y : 7 } ,
51
+ { x : 0 , y : 1 } ,
52
+ { x : 6 , y : 7 } ,
53
+ ) , false ) ;
54
+ } ) ;
55
+ } ) ;
56
+
57
+ describe ( 'coordMove' , ( ) => {
58
+ it ( 'goes up' , ( ) => {
59
+ assert . deepEqual ( utils . coordMove ( { x : 5 , y : 6 } , 'U' ) , { x : 5 , y : 5 } ) ;
60
+ } ) ;
61
+
62
+ it ( 'goes down' , ( ) => {
63
+ assert . deepEqual ( utils . coordMove ( { x : 5 , y : 6 } , 'D' ) , { x : 5 , y : 7 } ) ;
64
+ } ) ;
65
+
66
+ it ( 'goes left' , ( ) => {
67
+ assert . deepEqual ( utils . coordMove ( { x : 5 , y : 6 } , 'L' ) , { x : 4 , y : 6 } ) ;
68
+ } ) ;
69
+
70
+ it ( 'goes right' , ( ) => {
71
+ assert . deepEqual ( utils . coordMove ( { x : 5 , y : 6 } , 'R' ) , { x : 6 , y : 6 } ) ;
72
+ } ) ;
73
+ } ) ;
74
+
75
+ describe ( 'distanceFromOrigin' , ( ) => {
76
+ it ( 'handles positive values' , ( ) => {
77
+ assert . equal ( utils . distanceFromOrigin ( { x : 5 , y : 6 } ) , 11 ) ;
78
+ } ) ;
79
+
80
+ it ( 'handles negative values' , ( ) => {
81
+ assert . equal ( utils . distanceFromOrigin ( { x : - 5 , y : - 6 } ) , 11 ) ;
82
+ } ) ;
83
+ } ) ;
84
+
85
+ describe ( 'makeValueCorrect' , ( ) => {
86
+ it ( 'returns the lowest extremity if the value is below' , ( ) => {
87
+ assert . equal ( utils . makeValueCorrect ( - 1 , 0 , 5 ) , 0 ) ;
88
+ } ) ;
89
+
90
+ it ( 'returns the highest extremity if the value is above' , ( ) => {
91
+ assert . equal ( utils . makeValueCorrect ( 6 , 0 , 5 ) , 5 ) ;
92
+ } ) ;
93
+
94
+ it ( 'returns the same value if the value is between the extremities' , ( ) => {
95
+ assert . equal ( utils . makeValueCorrect ( 3 , 0 , 5 ) , 3 ) ;
96
+ } ) ;
97
+ } ) ;
98
+
99
+ describe ( 'minMax' , ( ) => {
100
+ it ( 'returns the same array if it is already sorted' , ( ) => {
101
+ assert . deepEqual ( utils . minMax ( [ 0 , 1 ] ) , [ 0 , 1 ] ) ;
102
+ } ) ;
103
+
104
+ it ( 'returns the same array if both values are the same' , ( ) => {
105
+ assert . deepEqual ( utils . minMax ( [ 2 , 2 ] ) , [ 2 , 2 ] ) ;
106
+ } ) ;
107
+
108
+ it ( 'returns the reversed array if the two values are switched' , ( ) => {
109
+ assert . deepEqual ( utils . minMax ( [ 2 , 1 ] ) , [ 1 , 2 ] ) ;
110
+ } ) ;
111
+ } ) ;
112
+
113
+ describe ( 'valueBetween' , ( ) => {
114
+ it ( 'returns true if it is between two values' , ( ) => {
115
+ assert . equal ( utils . valueBetween ( - 3 , - 5 , - 1 ) , true ) ;
116
+ } ) ;
117
+
118
+ it ( 'returns true if it is at the lowest extremity' , ( ) => {
119
+ assert . equal ( utils . valueBetween ( - 5 , - 5 , - 1 ) , true ) ;
120
+ } ) ;
121
+
122
+ it ( 'returns true if it is at the highest extremity' , ( ) => {
123
+ assert . equal ( utils . valueBetween ( - 1 , - 5 , - 1 ) , true ) ;
124
+ } ) ;
125
+
126
+ it ( 'returns false if it is lower than the lowest extremity' , ( ) => {
127
+ assert . equal ( utils . valueBetween ( - 6 , - 5 , - 1 ) , false ) ;
128
+ } ) ;
129
+
130
+ it ( 'returns false if it is greater than the highest extremity' , ( ) => {
131
+ assert . equal ( utils . valueBetween ( 0 , - 5 , - 1 ) , false ) ;
132
+ } ) ;
133
+ } ) ;
134
+ } ) ;
0 commit comments