ErrorsManager/Public/ecma/Tests.ecma.js

434 lines
16 KiB
JavaScript

"use strict";
import {ErrorsManager} from "./ErrorsManager.ecma.js";
/**
* @typedef {import("./ErrorsManager.ecma.js").ErrorsManager} ErrorsManager
*/
/**
* @class FullError
* @constructor
* @returns {void}
* @access public
* @static
*/
export const FullError = (function(){
/**
* @constructs FullError
* @param {!ErrorsManager} errors
* @param {!(string|Array.<number>|number)} code
* @returns {void}
* @access private
* @static
*/
const FullError = function(errors, code){
/** @type {FullError} */
const self = this;
/** @type {number} */
this.integer = 0;
/** @type {string} */
this.string = "";
/** @type {Array.<number>} */
this.array = [];
/**
* @returns {void}
* @access public
*/
const constructor = () => {
if(ErrorsManager.is_string(code)){
self.string = code;
self.integer = errors.to_integer(code);
self.array = errors.to_array(code);
}else if(ErrorsManager.is_array(code)){
self.array = code;
self.integer = errors.to_integer(code);
self.string = errors.to_string(code);
}else if(ErrorsManager.is_integer(code)){
self.integer = code;
self.string = errors.to_string(code);
self.array = errors.to_array(code);
};
};
constructor();
};
/**
* @param {!Array.<number>} array
* @returns {string}
* @access public
* @static
*/
FullError.print = array => "[" + array.join(", ") + "]";
return FullError;
})();
/**
* @class Tests
* @constructor
* @returns {void}
* @access public
* @static
*/
export const Tests = (function(){
/**
* @constructs Tests
* @returns {void}
* @access private
* @static
*/
const Tests = function(){};
/**
* @param {!number} from
* @param {!number} to
* @returns {!number}
* @access public
* @static
*/
Tests.random = (from, to) => Math.floor(Math.random() * (to - from + 1)) + from >> 0;
/**
* @returns {void}
* @access public
* @static
*/
Tests.errors = () => {
/** @type {ErrorsManager} */
const errors = new ErrorsManager(),
/** @type {FullError} */
error = new FullError(errors, 217934237),
/** @type {FullError} */
reset = new FullError(errors, 0);
reset.integer = errors.reset(error.integer, -5, 12);
reset.string = errors.reset(error.string, -5, 12);
reset.array = errors.reset(error.array, -5, 12);
console.log(`RESET: from ${-5} bits ${12}`);
console.log(`INTEGER: ${errors.to_string_binary(error.integer)} - ${errors.to_string_binary(reset.integer)}`);
console.log(`STRING: ${errors.to_string_binary(error.string)} - ${errors.to_string_binary(reset.string)}`);
console.log(`ARRAY: ${errors.to_string_binary(error.array)} - ${errors.to_string_binary(reset.array)}`);
console.log();
};
/**
* @returns {void}
* @access public
* @static
*/
Tests.conversions = (tests = 10, inputs = null) => {
/** @type {ErrorsManager} */
const errors = new ErrorsManager(inputs);
for(let i = 0; i < tests; i ++){
/** @type {FullError} */
const error = new FullError(errors, Tests.random(0, 1 << 16));
console.log(`INTEGER: ${errors.to_integer(error.integer)}, ${errors.to_integer(error.string)}, ${errors.to_integer(error.array)}`);
console.log(`STRING: ${errors.to_string(error.integer)}, ${errors.to_string(error.string)}, ${errors.to_string(error.array)}`);
console.log(`ARRAY: ${FullError.print(errors.to_array(error.integer))}, ${FullError.print(errors.to_array(error.string))}, ${FullError.print(errors.to_array(error.array))}`);
console.log();
};
return errors;
};
Tests.alphabet = (tests = 10) => {
for(let i = 0; i < tests; i ++){
/** @type {ErrorsManager} */
const errors = Tests.conversions(1, {
"base" : Tests.random(2, 128)
});
console.log(`^^^ ALPHABET: ${errors.get_alphabet()} ^^^`);
console.log();
};
};
/**
* @returns {void}
* @access public
* @static
*/
Tests.bitwise = (tests = 10) => {
/** @type {ErrorsManager} */
const errors = new ErrorsManager();
for(let i = 0; i < tests; i ++){
/** @type {FullError} */
const error = new FullError(errors, Tests.random(0, 1 << Tests.random(0, 16))),
/** @type {FullError} */
shifted = new FullError(errors, 0),
/** @type {FullError} */
unshifted = new FullError(errors, 0),
/** @type {number} */
bitwise = Tests.random(-10, 10);
console.log(`BITWISE: ${bitwise}`);
shifted.integer = errors.bitwise(error.integer, bitwise);
shifted.string = errors.bitwise(error.string, bitwise);
shifted.array = errors.bitwise(error.array, bitwise);
unshifted.integer = errors.bitwise(shifted.integer, -bitwise);
unshifted.string = errors.bitwise(shifted.string, -bitwise);
unshifted.array = errors.bitwise(shifted.array, -bitwise);
console.log(`INTEGER: ${errors.to_string_binary(error.integer)} - ${error.integer}`);
console.log(`STRING: ${errors.to_string_binary(error.string)} - ${error.string}`);
console.log(`ARRAY: ${errors.to_string_binary(error.array)} - ${FullError.print(error.array)}`);
console.log(`INTEGER: ${errors.to_string_binary(shifted.integer)} - ${shifted.integer}`);
console.log(`STRING: ${errors.to_string_binary(shifted.string)} - ${shifted.string}`);
console.log(`ARRAY: ${errors.to_string_binary(shifted.array)} - ${FullError.print(shifted.array)}`);
console.log(`INTEGER: ${errors.to_string_binary(unshifted.integer)} - ${unshifted.integer}`);
console.log(`STRING: ${errors.to_string_binary(unshifted.string)} - ${unshifted.string}`);
console.log(`ARRAY: ${errors.to_string_binary(unshifted.array)} - ${FullError.print(unshifted.array)}`);
console.log();
};
};
/**
* @returns {void}
* @access public
* @static
*/
Tests.bitwise_sucesive = (tests = 10) => {
/** @type {ErrorsManager} */
const errors = new ErrorsManager(),
/** @type {FullError} */
error = new FullError(errors, Tests.random(0, 1 << 16));
console.log(`INTEGER: ${errors.to_string_binary(error.integer)} - ${error.integer}`);
console.log(`STRING: ${errors.to_string_binary(error.string)} - ${error.string}`);
console.log(`ARRAY: ${errors.to_string_binary(error.array)} - ${FullError.print(error.array)}`);
console.log();
for(let i = -tests; i < tests; i ++){
/** @type {FullError} */
const shifted = new FullError(errors, 0);
console.log(`BITWISE: ${i}`);
shifted.integer = errors.bitwise(error.integer, i);
shifted.string = errors.bitwise(error.string, i);
shifted.array = errors.bitwise(error.array, i);
console.log(`INTEGER: ${errors.to_string_binary(shifted.integer)} - ${shifted.integer}`);
console.log(`STRING: ${errors.to_string_binary(shifted.string)} - ${shifted.string}`);
console.log(`ARRAY: ${errors.to_string_binary(shifted.array)} - ${FullError.print(shifted.array)}`);
console.log();
};
};
/**
* @returns {void}
* @access public
* @static
*/
Tests.bits = (tests = 10) => {
/** @type {ErrorsManager} */
const errors = new ErrorsManager();
for(let i = 0; i < tests; i++){
/** @type {FullError} */
const error = new FullError(errors, Tests.random(0, 1 << Tests.random(0, 28))),
/** @type {number} */
from_value = Tests.random(-15, 15),
/** @type {number} */
bits_value = Tests.random(-13, 13),
/** @type {[number, number, number]} */
from = [from_value, from_value, from_value],
/** @type {[number, number, number]} */
bits = [bits_value, bits_value, bits_value];
[from[0], bits[0]] = errors.get_from_bits(error.integer, from[0], bits[0]);
[from[1], bits[1]] = errors.get_from_bits(error.string, from[1], bits[1]);
[from[2], bits[2]] = errors.get_from_bits(error.array, from[2], bits[2]);
console.log(`CODE: ${error.integer} - ${error.string} - ${FullError.print(error.array)}`);
console.log(`ERROR: ${errors.to_string_binary(error.integer)} - ${errors.get_bits(error.integer)} - ${errors.get_bits(error.string)} - ${errors.get_bits(error.array)}`);
console.log(`FROM: ${from_value} - ${from[0]}, ${from[1]}, ${from[2]}`);
console.log(`BITS: ${bits_value} - ${bits[0]}, ${bits[1]}, ${bits[2]}`);
console.log();
};
};
/**
* @returns {void}
* @access public
* @static
*/
Tests.reset = (tests = 10) => {
/** @type {ErrorsManager} */
const errors = new ErrorsManager();
for(let i = 0; i < tests; i ++){
/** @type {FullError} */
const error = new FullError(errors, Tests.random(0, 1 << 28)),
/** @type {number} */
from = Tests.random(-15, 15),
/** @type {number} */
bits = Tests.random(-13, 13),
/** @type {FullError} */
reset = new FullError(errors, 0);
reset.integer = errors.reset(error.integer, from, bits);
reset.string = errors.reset(error.string, from, bits);
reset.array = errors.reset(error.array, from, bits);
console.log(`RESET: from ${from} bits ${bits}`);
console.log(`INTEGER: ${errors.to_string_binary(error.integer)} - ${errors.to_string_binary(reset.integer)}`);
console.log(`STRING: ${errors.to_string_binary(error.string)} - ${errors.to_string_binary(reset.string)}`);
console.log(`ARRAY: ${errors.to_string_binary(error.array)} - ${errors.to_string_binary(reset.array)}`);
console.log();
};
};
/**
* @returns {void}
* @access public
* @static
*/
Tests.ranges = (tests = 10) => {
/** @type {ErrorsManager} */
const errors = new ErrorsManager();
for(let i = 0; i < tests; i ++){
/** @type {FullError} */
const error = new FullError(errors, Tests.random(0, 1 << 28)),
/** @type {number} */
from = Tests.random(-15, 15),
/** @type {number} */
bits = Tests.random(-13, 13),
/** @type {FullError} */
range = new FullError(errors, 0);
console.log(`RANGE: from ${from} bits ${bits}`);
[from, bits] = errors.get_from_bits(error.string, from, bits);
console.log(`REAL: from ${from} bits ${bits}`);
range.integer = errors.get_range(error.integer, from, bits);
range.string = errors.get_range(error.string, from, bits);
range.array = errors.get_range(error.array, from, bits);
console.log(`INTEGER: ${errors.to_string_binary(error.integer)} - ${errors.to_string_binary(range.integer)}`);
console.log(`STRING: ${errors.to_string_binary(error.string)} - ${errors.to_string_binary(range.string)}`);
console.log(`ARRAY: ${errors.to_string_binary(error.array)} - ${errors.to_string_binary(range.array)}`);
console.log();
};
};
/**
* @returns {void}
* @access public
* @static
*/
Tests.has = (tests = 10) => {
/** @type {ErrorsManager} */
const errors = new ErrorsManager();
for(let i = 0; i < tests; i ++){
/** @type {FullError} */
const error = new FullError(errors, Tests.random(0, 1 << 28));
const from = Tests.random(-15, 15);
const bits = Tests.random(-13, 13);
console.log(`HAS: from ${from} bits ${bits}`);
console.log(`INTEGER: ${errors.has(error.integer, from, bits)} - ${errors.to_string_binary(error.integer)} - ${errors.to_string_binary(errors.get_range(error.integer, from, bits))}`);
console.log(`STRING: ${errors.has(error.string, from, bits)} - ${errors.to_string_binary(error.string)} - ${errors.to_string_binary(errors.get_range(error.string, from, bits))}`);
console.log(`ARRAY: ${errors.has(error.array, from, bits)} - ${errors.to_string_binary(error.array)} - ${errors.to_string_binary(errors.get_range(error.array, from, bits))}`);
console.log();
};
};
/**
* @returns {void}
* @access public
* @static
*/
Tests.set = (tests = 10) => {
/** @type {ErrorsManager} */
const errors = new ErrorsManager();
for(let i = 0; i < tests; i ++){
/** @type {FullError} */
const error = new FullError(errors, Tests.random(0, 1 << 15)),
/** @type {FullError} */
value = new FullError(errors, Tests.random(0, 1 << 15)),
/** @type {number} */
from = Tests.random(-15, 15),
/** @type {number} */
bits = Tests.random(-13, 13),
/** @type {FullError} */
set_integer = new FullError(errors, 0),
/** @type {FullError} */
set_string = new FullError(errors, 0),
/** @type {FullError} */
set_array = new FullError(errors, 0);
set_integer.integer = errors.set(error.integer, value.integer, from, bits);
set_string.integer = errors.set(error.integer, value.string, from, bits);
set_array.integer = errors.set(error.integer, value.array, from, bits);
set_integer.string = errors.set(error.string, value.integer, from, bits);
set_string.string = errors.set(error.string, value.string, from, bits);
set_array.string = errors.set(error.string, value.array, from, bits);
set_integer.array = errors.set(error.array, value.integer, from, bits);
set_string.array = errors.set(error.array, value.string, from, bits);
set_array.array = errors.set(error.array, value.array, from, bits);
console.log(`SET: from ${from} bits ${bits} to [${value.string}, ${FullError.print(value.array)}, ${value.integer}]`);
console.log(`ERROR: ${errors.to_string_binary(error.integer)} - VALUE: ${errors.to_string_binary(value.integer)}`);
console.log(`INTEGER: ${errors.to_string_binary(set_integer.integer)} - ${errors.to_string_binary(set_integer.string)} - ${errors.to_string_binary(set_integer.array)}`);
console.log(`STRING: ${errors.to_string_binary(set_string.integer)} - ${errors.to_string_binary(set_string.string)} - ${errors.to_string_binary(set_string.array)}`);
console.log(`ARRAY: ${errors.to_string_binary(set_array.integer)} - ${errors.to_string_binary(set_array.string)} - ${errors.to_string_binary(set_array.array)}`);
console.log();
};
};
return Tests;
})();