perf: optimize min/max implementation
[e-mobility-charging-stations-simulator.git] / test / utils / Utils.test.ts
1 import { hoursToMilliseconds, hoursToSeconds } from 'date-fns';
2 import { expect } from 'expect';
3
4 import { Constants } from '../../src/utils/Constants';
5 import {
6 cloneObject,
7 convertToBoolean,
8 convertToDate,
9 convertToFloat,
10 convertToInt,
11 extractTimeSeriesValues,
12 formatDurationMilliSeconds,
13 formatDurationSeconds,
14 generateUUID,
15 getRandomFloat,
16 getRandomInteger,
17 hasOwnProp,
18 isArraySorted,
19 isEmptyArray,
20 isEmptyObject,
21 isEmptyString,
22 isIterable,
23 isNotEmptyArray,
24 isNotEmptyString,
25 isNullOrUndefined,
26 isObject,
27 isUndefined,
28 isValidTime,
29 max,
30 min,
31 once,
32 roundTo,
33 secureRandom,
34 sleep,
35 validateUUID,
36 } from '../../src/utils/Utils';
37
38 describe('Utils test suite', () => {
39 it('Verify generateUUID()/validateUUID()', () => {
40 const uuid = generateUUID();
41 expect(uuid).toBeDefined();
42 expect(uuid.length).toEqual(36);
43 expect(validateUUID(uuid)).toBe(true);
44 expect(validateUUID('abcdef00-0000-4000-0000-000000000000')).toBe(true);
45 expect(validateUUID('')).toBe(false);
46 // Shall invalidate Nil UUID
47 expect(validateUUID('00000000-0000-0000-0000-000000000000')).toBe(false);
48 expect(validateUUID('987FBC9-4BED-3078-CF07A-9141BA07C9F3')).toBe(false);
49 });
50
51 it('Verify sleep()', async () => {
52 const start = performance.now();
53 await sleep(1000);
54 const stop = performance.now();
55 expect(stop - start).toBeGreaterThanOrEqual(1000);
56 });
57
58 it('Verify formatDurationMilliSeconds()', () => {
59 expect(formatDurationMilliSeconds(0)).toBe('');
60 expect(formatDurationMilliSeconds(1000)).toBe('1 second');
61 expect(formatDurationMilliSeconds(hoursToMilliseconds(4380))).toBe('182 days 12 hours');
62 });
63
64 it('Verify formatDurationSeconds()', () => {
65 expect(formatDurationSeconds(0)).toBe('');
66 expect(formatDurationSeconds(1)).toBe('1 second');
67 expect(formatDurationSeconds(hoursToSeconds(4380))).toBe('182 days 12 hours');
68 });
69
70 it('Verify isValidTime()', () => {
71 expect(isValidTime(undefined)).toBe(false);
72 expect(isValidTime(null)).toBe(false);
73 expect(isValidTime('')).toBe(false);
74 expect(isValidTime({})).toBe(false);
75 expect(isValidTime([])).toBe(false);
76 expect(isValidTime(new Map())).toBe(false);
77 expect(isValidTime(new Set())).toBe(false);
78 expect(isValidTime(new WeakMap())).toBe(false);
79 expect(isValidTime(new WeakSet())).toBe(false);
80 expect(isValidTime(-1)).toBe(true);
81 expect(isValidTime(0)).toBe(true);
82 expect(isValidTime(1)).toBe(true);
83 expect(isValidTime(-0.5)).toBe(true);
84 expect(isValidTime(0.5)).toBe(true);
85 expect(isValidTime(new Date())).toBe(true);
86 });
87
88 it('Verify convertToDate()', () => {
89 expect(convertToDate(undefined)).toBe(undefined);
90 expect(() => convertToDate('')).toThrowError(new Error("Cannot convert to date: ''"));
91 expect(() => convertToDate('00:70:61')).toThrowError(
92 new Error("Cannot convert to date: '00:70:61'"),
93 );
94 expect(convertToDate(0)).toStrictEqual(new Date('1970-01-01T00:00:00.000Z'));
95 expect(convertToDate(-1)).toStrictEqual(new Date('1969-12-31T23:59:59.999Z'));
96 const dateStr = '2020-01-01T00:00:00.000Z';
97 let date = convertToDate(dateStr);
98 expect(date).toBeInstanceOf(Date);
99 expect(date).toStrictEqual(new Date(dateStr));
100 date = convertToDate(new Date(dateStr));
101 expect(date).toBeInstanceOf(Date);
102 expect(date).toStrictEqual(new Date(dateStr));
103 });
104
105 it('Verify convertToInt()', () => {
106 expect(convertToInt(undefined)).toBe(0);
107 expect(convertToInt(null)).toBe(0);
108 expect(convertToInt(0)).toBe(0);
109 const randomInteger = getRandomInteger();
110 expect(convertToInt(randomInteger)).toEqual(randomInteger);
111 expect(convertToInt('-1')).toBe(-1);
112 expect(convertToInt('1')).toBe(1);
113 expect(convertToInt('1.1')).toBe(1);
114 expect(convertToInt('1.9')).toBe(1);
115 expect(convertToInt('1.999')).toBe(1);
116 expect(convertToInt(-1)).toBe(-1);
117 expect(convertToInt(1)).toBe(1);
118 expect(convertToInt(1.1)).toBe(1);
119 expect(convertToInt(1.9)).toBe(1);
120 expect(convertToInt(1.999)).toBe(1);
121 expect(() => {
122 convertToInt('NaN');
123 }).toThrow("Cannot convert to integer: 'NaN'");
124 });
125
126 it('Verify convertToFloat()', () => {
127 expect(convertToFloat(undefined)).toBe(0);
128 expect(convertToFloat(null)).toBe(0);
129 expect(convertToFloat(0)).toBe(0);
130 const randomFloat = getRandomFloat();
131 expect(convertToFloat(randomFloat)).toEqual(randomFloat);
132 expect(convertToFloat('-1')).toBe(-1);
133 expect(convertToFloat('1')).toBe(1);
134 expect(convertToFloat('1.1')).toBe(1.1);
135 expect(convertToFloat('1.9')).toBe(1.9);
136 expect(convertToFloat('1.999')).toBe(1.999);
137 expect(convertToFloat(-1)).toBe(-1);
138 expect(convertToFloat(1)).toBe(1);
139 expect(convertToFloat(1.1)).toBe(1.1);
140 expect(convertToFloat(1.9)).toBe(1.9);
141 expect(convertToFloat(1.999)).toBe(1.999);
142 expect(() => {
143 convertToFloat('NaN');
144 }).toThrow("Cannot convert to float: 'NaN'");
145 });
146
147 it('Verify convertToBoolean()', () => {
148 expect(convertToBoolean(undefined)).toBe(false);
149 expect(convertToBoolean(null)).toBe(false);
150 expect(convertToBoolean('true')).toBe(true);
151 expect(convertToBoolean('false')).toBe(false);
152 expect(convertToBoolean('TRUE')).toBe(true);
153 expect(convertToBoolean('FALSE')).toBe(false);
154 expect(convertToBoolean('1')).toBe(true);
155 expect(convertToBoolean('0')).toBe(false);
156 expect(convertToBoolean(1)).toBe(true);
157 expect(convertToBoolean(0)).toBe(false);
158 expect(convertToBoolean(true)).toBe(true);
159 expect(convertToBoolean(false)).toBe(false);
160 expect(convertToBoolean('')).toBe(false);
161 expect(convertToBoolean('NoNBoolean')).toBe(false);
162 });
163
164 it('Verify secureRandom()', () => {
165 const random = secureRandom();
166 expect(typeof random === 'number').toBe(true);
167 expect(random).toBeGreaterThanOrEqual(0);
168 expect(random).toBeLessThan(1);
169 });
170
171 it('Verify getRandomInteger()', () => {
172 let randomInteger = getRandomInteger();
173 expect(Number.isSafeInteger(randomInteger)).toBe(true);
174 expect(randomInteger).toBeGreaterThanOrEqual(0);
175 expect(randomInteger).toBeLessThanOrEqual(Constants.MAX_RANDOM_INTEGER);
176 expect(randomInteger).not.toEqual(getRandomInteger());
177 randomInteger = getRandomInteger(0, -Constants.MAX_RANDOM_INTEGER);
178 expect(randomInteger).toBeGreaterThanOrEqual(-Constants.MAX_RANDOM_INTEGER);
179 expect(randomInteger).toBeLessThanOrEqual(0);
180 expect(() => getRandomInteger(0, 1)).toThrowError(
181 'The value of "max" is out of range. It must be greater than the value of "min" (1). Received 1',
182 );
183 expect(() => getRandomInteger(-1)).toThrowError(
184 'The value of "max" is out of range. It must be greater than the value of "min" (0). Received 0',
185 );
186 expect(() => getRandomInteger(Constants.MAX_RANDOM_INTEGER + 1)).toThrowError(
187 `The value of "max" is out of range. It must be <= ${
188 Constants.MAX_RANDOM_INTEGER + 1
189 }. Received 281_474_976_710_656`,
190 );
191 randomInteger = getRandomInteger(2, 1);
192 expect(randomInteger).toBeGreaterThanOrEqual(1);
193 expect(randomInteger).toBeLessThanOrEqual(2);
194 const maximum = 2.2,
195 minimum = 1.1;
196 randomInteger = getRandomInteger(maximum, minimum);
197 expect(randomInteger).toBeLessThanOrEqual(Math.floor(maximum));
198 expect(randomInteger).toBeGreaterThanOrEqual(Math.ceil(minimum));
199 });
200
201 it('Verify roundTo()', () => {
202 expect(roundTo(0, 2)).toBe(0);
203 expect(roundTo(0.5, 0)).toBe(1);
204 expect(roundTo(0.5, 2)).toBe(0.5);
205 expect(roundTo(-0.5, 0)).toBe(-1);
206 expect(roundTo(-0.5, 2)).toBe(-0.5);
207 expect(roundTo(1.005, 0)).toBe(1);
208 expect(roundTo(1.005, 2)).toBe(1.01);
209 expect(roundTo(2.175, 2)).toBe(2.18);
210 expect(roundTo(5.015, 2)).toBe(5.02);
211 expect(roundTo(-1.005, 2)).toBe(-1.01);
212 expect(roundTo(-2.175, 2)).toBe(-2.18);
213 expect(roundTo(-5.015, 2)).toBe(-5.02);
214 });
215
216 it('Verify getRandomFloat()', () => {
217 let randomFloat = getRandomFloat();
218 expect(typeof randomFloat === 'number').toBe(true);
219 expect(randomFloat).toBeGreaterThanOrEqual(0);
220 expect(randomFloat).toBeLessThanOrEqual(Number.MAX_VALUE);
221 expect(randomFloat).not.toEqual(getRandomFloat());
222 expect(() => getRandomFloat(0, 1)).toThrowError(new RangeError('Invalid interval'));
223 expect(() => getRandomFloat(Number.MAX_VALUE, -Number.MAX_VALUE)).toThrowError(
224 new RangeError('Invalid interval'),
225 );
226 randomFloat = getRandomFloat(0, -Number.MAX_VALUE);
227 expect(randomFloat).toBeGreaterThanOrEqual(-Number.MAX_VALUE);
228 expect(randomFloat).toBeLessThanOrEqual(0);
229 });
230
231 it('Verify extractTimeSeriesValues()', () => {
232 expect(extractTimeSeriesValues([])).toEqual([]);
233 expect(extractTimeSeriesValues([{ timestamp: Date.now(), value: 1.1 }])).toEqual([1.1]);
234 expect(
235 extractTimeSeriesValues([
236 { timestamp: Date.now(), value: 1.1 },
237 { timestamp: Date.now(), value: 2.2 },
238 ]),
239 ).toEqual([1.1, 2.2]);
240 });
241
242 it('Verify isObject()', () => {
243 expect(isObject('test')).toBe(false);
244 expect(isObject(undefined)).toBe(false);
245 expect(isObject(null)).toBe(false);
246 expect(isObject(0)).toBe(false);
247 expect(isObject([])).toBe(false);
248 expect(isObject([0, 1])).toBe(false);
249 expect(isObject(['0', '1'])).toBe(false);
250 expect(isObject({})).toBe(true);
251 expect(isObject({ 1: 1 })).toBe(true);
252 expect(isObject({ '1': '1' })).toBe(true);
253 expect(isObject(new Map())).toBe(true);
254 expect(isObject(new Set())).toBe(true);
255 expect(isObject(new WeakMap())).toBe(true);
256 expect(isObject(new WeakSet())).toBe(true);
257 });
258
259 it('Verify cloneObject()', () => {
260 const obj = { 1: 1 };
261 expect(cloneObject(obj)).toStrictEqual(obj);
262 expect(cloneObject(obj) === obj).toBe(false);
263 const nestedObj = { 1: obj, 2: obj };
264 expect(cloneObject(nestedObj)).toStrictEqual(nestedObj);
265 expect(cloneObject(nestedObj) === nestedObj).toBe(false);
266 const array = [1, 2];
267 expect(cloneObject(array)).toStrictEqual(array);
268 expect(cloneObject(array) === array).toBe(false);
269 const objArray = [obj, obj];
270 expect(cloneObject(objArray)).toStrictEqual(objArray);
271 expect(cloneObject(objArray) === objArray).toBe(false);
272 const date = new Date();
273 expect(cloneObject(date)).toStrictEqual(date);
274 expect(cloneObject(date) === date).toBe(false);
275 const map = new Map([['1', '2']]);
276 expect(cloneObject(map)).toStrictEqual({});
277 const set = new Set(['1']);
278 expect(cloneObject(set)).toStrictEqual({});
279 // The URL object seems to have not enumerable properties
280 const url = new URL('https://domain.tld');
281 expect(cloneObject(url)).toStrictEqual({});
282 const weakMap = new WeakMap([[{ 1: 1 }, { 2: 2 }]]);
283 expect(cloneObject(weakMap)).toStrictEqual({});
284 const weakSet = new WeakSet([{ 1: 1 }, { 2: 2 }]);
285 expect(cloneObject(weakSet)).toStrictEqual({});
286 });
287
288 it('Verify hasOwnProp()', () => {
289 expect(hasOwnProp('test', '')).toBe(false);
290 expect(hasOwnProp(undefined, '')).toBe(false);
291 expect(hasOwnProp(null, '')).toBe(false);
292 expect(hasOwnProp([], '')).toBe(false);
293 expect(hasOwnProp({}, '')).toBe(false);
294 expect(hasOwnProp({ 1: 1 }, 1)).toBe(true);
295 expect(hasOwnProp({ 1: 1 }, '1')).toBe(true);
296 expect(hasOwnProp({ 1: 1 }, 2)).toBe(false);
297 expect(hasOwnProp({ 1: 1 }, '2')).toBe(false);
298 expect(hasOwnProp({ '1': '1' }, '1')).toBe(true);
299 expect(hasOwnProp({ '1': '1' }, 1)).toBe(true);
300 expect(hasOwnProp({ '1': '1' }, '2')).toBe(false);
301 expect(hasOwnProp({ '1': '1' }, 2)).toBe(false);
302 });
303
304 it('Verify isIterable()', () => {
305 expect(isIterable('')).toBe(true);
306 expect(isIterable(' ')).toBe(true);
307 expect(isIterable('test')).toBe(true);
308 expect(isIterable(undefined)).toBe(false);
309 expect(isIterable(null)).toBe(false);
310 expect(isIterable(0)).toBe(false);
311 expect(isIterable([0, 1])).toBe(true);
312 expect(isIterable({ 1: 1 })).toBe(false);
313 expect(isIterable(new Map())).toBe(true);
314 expect(isIterable(new Set())).toBe(true);
315 expect(isIterable(new WeakMap())).toBe(false);
316 expect(isIterable(new WeakSet())).toBe(false);
317 });
318
319 it('Verify isEmptyString()', () => {
320 expect(isEmptyString('')).toBe(true);
321 expect(isEmptyString(' ')).toBe(true);
322 expect(isEmptyString(' ')).toBe(true);
323 expect(isEmptyString('test')).toBe(false);
324 expect(isEmptyString(' test')).toBe(false);
325 expect(isEmptyString('test ')).toBe(false);
326 expect(isEmptyString(undefined)).toBe(true);
327 expect(isEmptyString(null)).toBe(true);
328 expect(isEmptyString(0)).toBe(false);
329 expect(isEmptyString({})).toBe(false);
330 expect(isEmptyString([])).toBe(false);
331 expect(isEmptyString(new Map())).toBe(false);
332 expect(isEmptyString(new Set())).toBe(false);
333 expect(isEmptyString(new WeakMap())).toBe(false);
334 expect(isEmptyString(new WeakSet())).toBe(false);
335 });
336
337 it('Verify isNotEmptyString()', () => {
338 expect(isNotEmptyString('')).toBe(false);
339 expect(isNotEmptyString(' ')).toBe(false);
340 expect(isNotEmptyString(' ')).toBe(false);
341 expect(isNotEmptyString('test')).toBe(true);
342 expect(isNotEmptyString(' test')).toBe(true);
343 expect(isNotEmptyString('test ')).toBe(true);
344 expect(isNotEmptyString(undefined)).toBe(false);
345 expect(isNotEmptyString(null)).toBe(false);
346 expect(isNotEmptyString(0)).toBe(false);
347 expect(isNotEmptyString({})).toBe(false);
348 expect(isNotEmptyString([])).toBe(false);
349 expect(isNotEmptyString(new Map())).toBe(false);
350 expect(isNotEmptyString(new Set())).toBe(false);
351 expect(isNotEmptyString(new WeakMap())).toBe(false);
352 expect(isNotEmptyString(new WeakSet())).toBe(false);
353 });
354
355 it('Verify isUndefined()', () => {
356 expect(isUndefined(undefined)).toBe(true);
357 expect(isUndefined(null)).toBe(false);
358 expect(isUndefined('')).toBe(false);
359 expect(isUndefined(0)).toBe(false);
360 expect(isUndefined({})).toBe(false);
361 expect(isUndefined([])).toBe(false);
362 expect(isUndefined(new Map())).toBe(false);
363 expect(isUndefined(new Set())).toBe(false);
364 expect(isUndefined(new WeakMap())).toBe(false);
365 expect(isUndefined(new WeakSet())).toBe(false);
366 });
367
368 it('Verify isNullOrUndefined()', () => {
369 expect(isNullOrUndefined(undefined)).toBe(true);
370 expect(isNullOrUndefined(null)).toBe(true);
371 expect(isNullOrUndefined('')).toBe(false);
372 expect(isNullOrUndefined(0)).toBe(false);
373 expect(isNullOrUndefined({})).toBe(false);
374 expect(isNullOrUndefined([])).toBe(false);
375 expect(isNullOrUndefined(new Map())).toBe(false);
376 expect(isNullOrUndefined(new Set())).toBe(false);
377 expect(isNullOrUndefined(new WeakMap())).toBe(false);
378 expect(isNullOrUndefined(new WeakSet())).toBe(false);
379 });
380
381 it('Verify isEmptyArray()', () => {
382 expect(isEmptyArray([])).toBe(true);
383 expect(isEmptyArray([1, 2])).toBe(false);
384 expect(isEmptyArray(['1', '2'])).toBe(false);
385 expect(isEmptyArray(undefined)).toBe(false);
386 expect(isEmptyArray(null)).toBe(false);
387 expect(isEmptyArray('')).toBe(false);
388 expect(isEmptyArray('test')).toBe(false);
389 expect(isEmptyArray(0)).toBe(false);
390 expect(isEmptyArray({})).toBe(false);
391 expect(isEmptyArray(new Map())).toBe(false);
392 expect(isEmptyArray(new Set())).toBe(false);
393 expect(isEmptyArray(new WeakMap())).toBe(false);
394 expect(isEmptyArray(new WeakSet())).toBe(false);
395 });
396
397 it('Verify isNotEmptyArray()', () => {
398 expect(isNotEmptyArray([])).toBe(false);
399 expect(isNotEmptyArray([1, 2])).toBe(true);
400 expect(isNotEmptyArray(['1', '2'])).toBe(true);
401 expect(isNotEmptyArray(undefined)).toBe(false);
402 expect(isNotEmptyArray(null)).toBe(false);
403 expect(isNotEmptyArray('')).toBe(false);
404 expect(isNotEmptyArray('test')).toBe(false);
405 expect(isNotEmptyArray(0)).toBe(false);
406 expect(isNotEmptyArray({})).toBe(false);
407 expect(isNotEmptyArray(new Map())).toBe(false);
408 expect(isNotEmptyArray(new Set())).toBe(false);
409 expect(isNotEmptyArray(new WeakMap())).toBe(false);
410 expect(isNotEmptyArray(new WeakSet())).toBe(false);
411 });
412
413 it('Verify isEmptyObject()', () => {
414 expect(isEmptyObject({})).toBe(true);
415 expect(isEmptyObject({ 1: 1, 2: 2 })).toBe(false);
416 expect(isEmptyObject(new Map())).toBe(false);
417 expect(isEmptyObject(new Set())).toBe(false);
418 expect(isEmptyObject(new WeakMap())).toBe(false);
419 expect(isEmptyObject(new WeakSet())).toBe(false);
420 });
421
422 it('Verify isArraySorted()', () => {
423 expect(
424 isArraySorted([], (a, b) => {
425 return a - b;
426 }),
427 ).toBe(true);
428 expect(
429 isArraySorted([1], (a, b) => {
430 return a - b;
431 }),
432 ).toBe(true);
433 expect(isArraySorted<number>([1, 2, 3, 4, 5], (a, b) => a - b)).toBe(true);
434 expect(isArraySorted<number>([1, 2, 3, 5, 4], (a, b) => a - b)).toBe(false);
435 expect(isArraySorted<number>([2, 1, 3, 4, 5], (a, b) => a - b)).toBe(false);
436 });
437
438 it('Verify once()', () => {
439 let called = 0;
440 const fn = () => ++called;
441 const onceFn = once(fn, this);
442 const result1 = onceFn();
443 expect(called).toBe(1);
444 expect(result1).toBe(1);
445 const result2 = onceFn();
446 expect(called).toBe(1);
447 expect(result2).toBe(1);
448 const result3 = onceFn();
449 expect(called).toBe(1);
450 expect(result3).toBe(1);
451 });
452
453 it('Verify min()', () => {
454 expect(min(0, 1)).toBe(0);
455 expect(min(1, 0)).toBe(0);
456 expect(min(0, -1)).toBe(-1);
457 expect(min(-1, 0)).toBe(-1);
458 });
459
460 it('Verify max()', () => {
461 expect(max(0, 1)).toBe(1);
462 expect(max(1, 0)).toBe(1);
463 expect(max(0, -1)).toBe(0);
464 expect(max(-1, 0)).toBe(0);
465 });
466 });