#!/usr/bin/env python3 # -*- coding: utf-8 -*- from random import randint from typing import Sequence, Self from ErrorsManager import ErrorsManager class FullError: def __init__(self:Self, errors:ErrorsManager, value:int|str|Sequence[int]) -> None: self.integer:int = 0 self.string:str = "" self.array:list[int] = [] if ErrorsManager.is_integer(value): self.integer = value self.string = errors.to_string(value) self.array = errors.to_array(value) elif ErrorsManager.isstring(value): self.integer = errors.to_integer(value) self.string = value self.array = errors.to_array(value) elif ErrorsManager.is_array(value): self.integer = errors.to_integer(value) self.string = errors.to_string(value) self.array = list(value) @staticmethod def print(array:Sequence[int]) -> str: return "[" + ", ".join(str(i) for i in array) + "]" class Tests: @staticmethod def errors(): errors:ErrorsManager = ErrorsManager() error:FullError = FullError(errors, 217934237) reset:FullError = 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) print(f"RESET: from {-5} bits {12}") print(f"INTEGER: {errors.to_string_binary(error.integer)} - {errors.to_string_binary(reset.integer)}") print(f"STRING: {errors.to_string_binary(error.string)} - {errors.to_string_binary(reset.string)}") print(f"ARRAY: {errors.to_string_binary(error.array)} - {errors.to_string_binary(reset.array)}") print() @staticmethod def conversions(tests: int = 10, inputs: dict = None): errors:ErrorsManager = ErrorsManager(inputs) for _ in range(tests): error:FullError = FullError(errors, randint(0, 1 << 16)) print(f"INTEGER: {errors.to_integer(error.integer)}, {errors.to_integer(error.string)}, {errors.to_integer(error.array)}") print(f"STRING: {errors.to_string(error.integer)}, {errors.to_string(error.string)}, {errors.to_string(error.array)}") print(f"ARRAY: {FullError.print(errors.to_array(error.integer))}, {FullError.print(errors.to_array(error.string))}, {FullError.print(errors.to_array(error.array))}") print() return errors @staticmethod def binaries(tests: int = 10, inputs: dict = None): errors:ErrorsManager = ErrorsManager(inputs) for _ in range(tests): error:FullError = FullError(errors, randint(0, 1 << 16)) integer:str = errors.to_string_binary(error.integer) string:str = errors.to_string_binary(error.string) array:str = errors.to_string_binary(error.array) print("OK: " + ("YES" if integer == string == array else "NO")) print(f"INTEGER: {integer}") print(f"STRING: {string}") print(f"ARRAY: {array}") print() return errors @staticmethod def alphabet(tests: int = 10): for _ in range(tests): errors:ErrorsManager = Tests.conversions(1, {"base": randint(2, 128)}) print(f"^^^ ALPHABET: {', '.join(errors.get_alphabet())} ^^^") print() @staticmethod def bitwise(tests: int = 10): errors:ErrorsManager = ErrorsManager() for _ in range(tests): error:FullError = FullError(errors, randint(0, 1 << randint(0, 16))) shifted:FullError = FullError(errors, 0) unshifted:FullError = FullError(errors, 0) bitwise:int = randint(-10, 10) print(f"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) print(f"INTEGER: {errors.to_string_binary(error.integer)} - {error.integer}") print(f"STRING: {errors.to_string_binary(error.string)} - {error.string}") print(f"ARRAY: {errors.to_string_binary(error.array)} - {FullError.print(error.array)}") print(f"INTEGER: {errors.to_string_binary(shifted.integer)} - {shifted.integer}") print(f"STRING: {errors.to_string_binary(shifted.string)} - {shifted.string}") print(f"ARRAY: {errors.to_string_binary(shifted.array)} - {FullError.print(shifted.array)}") print(f"INTEGER: {errors.to_string_binary(unshifted.integer)} - {unshifted.integer}") print(f"STRING: {errors.to_string_binary(unshifted.string)} - {unshifted.string}") print(f"ARRAY: {errors.to_string_binary(unshifted.array)} - {FullError.print(unshifted.array)}") print() @staticmethod def bitwise_sucesive(tests: int = 10): errors = ErrorsManager() error = FullError(errors, randint(0, 1 << 16)) i:int print(f"INTEGER: {errors.to_string_binary(error.integer)} - {error.integer}") print(f"STRING: {errors.to_string_binary(error.string)} - {error.string}") print(f"ARRAY: {errors.to_string_binary(error.array)} - {FullError.print(error.array)}") print() for i in range(-tests, tests): shifted:FullError = FullError(errors, 0) print(f"BITWISE: {i}") shifted.integer = errors.bitwise(error.integer, i) shifted.string = errors.bitwise(error.string, i) shifted.array = errors.bitwise(error.array, i) print(f"INTEGER: {errors.to_string_binary(shifted.integer)} - {shifted.integer}") print(f"STRING: {errors.to_string_binary(shifted.string)} - {shifted.string}") print(f"ARRAY: {errors.to_string_binary(shifted.array)} - {FullError.print(shifted.array)}") print() @staticmethod def bits(tests: int = 10): errors:ErrorsManager = ErrorsManager() for _ in range(tests): error:FullError = FullError(errors, randint(0, 1 << randint(0, 28))) from_value:int = randint(-15, 15) bits_value:int = randint(-13, 13) from_values:list[int] = [from_value, from_value, from_value] bits_values:list[int] = [bits_value, bits_value, bits_value] from_values[0], bits_values[0] = errors.get_from_bits(error.integer, from_values[0], bits_values[0]) from_values[1], bits_values[1] = errors.get_from_bits(error.string, from_values[1], bits_values[1]) from_values[2], bits_values[2] = errors.get_from_bits(error.array, from_values[2], bits_values[2]) print(f"CODE: {error.integer} - {error.string} - {FullError.print(error.array)}") print(f"ERROR: {errors.to_string_binary(error.integer)} - {errors.get_bits(error.integer)} - {errors.get_bits(error.string)} - {errors.get_bits(error.array)}") print(f"FROM: {from_value} - {from_values[0]}, {from_values[1]}, {from_values[2]}") print(f"BITS: {bits_value} - {bits_values[0]}, {bits_values[1]}, {bits_values[2]}") print() @staticmethod def reset(tests: int = 10): errors:ErrorsManager = ErrorsManager() for _ in range(tests): error:FullError = FullError(errors, randint(0, 1 << 28)) from_value:int = randint(-15, 15) bits_value:int = randint(-13, 13) reversed:bool = randint(0, 1) == 0 reset:FullError = FullError(errors, 0) reset.integer = errors.reset(error.integer, from_value, bits_value, reversed) reset.string = errors.reset(error.string, from_value, bits_value, reversed) reset.array = errors.reset(error.array, from_value, bits_value, reversed) print(f"RESET: from {from_value} bits {bits_value} reversed {reversed}") print(f"INTEGER: {errors.to_string_binary(error.integer)} - {errors.to_string_binary(reset.integer)}") print(f"STRING: {errors.to_string_binary(error.string)} - {errors.to_string_binary(reset.string)}") print(f"ARRAY: {errors.to_string_binary(error.array)} - {errors.to_string_binary(reset.array)}") print() @staticmethod def ranges(tests: int = 10): errors:ErrorsManager = ErrorsManager() for _ in range(tests): error:FullError = FullError(errors, randint(0, 1 << 28)) from_value:int = randint(-15, 15) bits_value:int = randint(-13, 13) range_error:FullError = FullError(errors, 0) print(f"RANGE: from {from_value} bits {bits_value}") errors.get_from_bits(error.string, from_value, bits_value) print(f"REAL: from {from_value} bits {bits_value}") range_error.integer = errors.get_range(error.integer, from_value, bits_value) range_error.string = errors.get_range(error.string, from_value, bits_value) range_error.array = errors.get_range(error.array, from_value, bits_value) print(f"INTEGER: {errors.to_string_binary(error.integer)} - {errors.to_string_binary(range_error.integer)}") print(f"STRING: {errors.to_string_binary(error.string)} - {errors.to_string_binary(range_error.string)}") print(f"ARRAY: {errors.to_string_binary(error.array)} - {errors.to_string_binary(range_error.array)}") print() @staticmethod def has(tests: int = 10): errors:ErrorsManager = ErrorsManager() for _ in range(tests): error:FullError = FullError(errors, randint(0, 1 << 28)) from_value:int = randint(-15, 15) bits_value:int = randint(-13, 13) print(f"HAS: from {from_value} bits {bits_value}") print(f"INTEGER: {errors.has(error.integer, from_value, bits_value)} - {errors.to_string_binary(error.integer)} - {errors.to_string_binary(errors.get_range(error.integer, from_value, bits_value))}") print(f"STRING: {errors.has(error.string, from_value, bits_value)} - {errors.to_string_binary(error.string)} - {errors.to_string_binary(errors.get_range(error.string, from_value, bits_value))}") print(f"ARRAY: {errors.has(error.array, from_value, bits_value)} - {errors.to_string_binary(error.array)} - {errors.to_string_binary(errors.get_range(error.array, from_value, bits_value))}") print() @staticmethod def set(tests: int = 10): errors:ErrorsManager = ErrorsManager() for _ in range(tests): error:FullError = FullError(errors, randint(0, 1 << 15)) value:FullError = FullError(errors, randint(0, 1 << 15)) from_value:int = randint(-15, 15) bits_value:int = randint(-13, 13) set_integer:FullError = FullError(errors, 0) set_string:FullError = FullError(errors, 0) set_array:FullError = FullError(errors, 0) set_integer.integer = errors.set(error.integer, value.integer, from_value, bits_value) set_string.integer = errors.set(error.integer, value.string, from_value, bits_value) set_array.integer = errors.set(error.integer, value.array, from_value, bits_value) set_integer.string = errors.set(error.string, value.integer, from_value, bits_value) set_string.string = errors.set(error.string, value.string, from_value, bits_value) set_array.string = errors.set(error.string, value.array, from_value, bits_value) set_integer.array = errors.set(error.array, value.integer, from_value, bits_value) set_string.array = errors.set(error.array, value.string, from_value, bits_value) set_array.array = errors.set(error.array, value.array, from_value, bits_value) print(f"SET: from {from_value} bits {bits_value} to [{value.string}, {FullError.print(value.array)}, {value.integer}]") print(f"ERROR: {errors.to_string_binary(error.integer)} - VALUE: {errors.to_string_binary(value.integer)}") print(f"INTEGER: {errors.to_string_binary(set_integer.integer)} - {errors.to_string_binary(set_integer.string)} - {errors.to_string_binary(set_integer.array)}") print(f"STRING: {errors.to_string_binary(set_string.integer)} - {errors.to_string_binary(set_string.string)} - {errors.to_string_binary(set_string.array)}") print(f"ARRAY: {errors.to_string_binary(set_array.integer)} - {errors.to_string_binary(set_array.string)} - {errors.to_string_binary(set_array.array)}") print() # Tests.errors() # Tests.conversions() # Tests.binaries() # Tests.alphabet() # Tests.bitwise() # Tests.bitwise_sucesive() # Tests.bits() # Tests.reset() # Tests.ranges() # Tests.has() Tests.set()