CORE/tests/unit/misc.gd

158 lines
5.8 KiB
GDScript

# CORE FRAMEWORK TEST FILE
# Copyright (c) 2024 The StarOpenSource Project & Contributors
# Licensed under the GNU Affero General Public License v3
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
extends 'res://tests/unitbase.gd'
# byte2mib
func test_byte2mib() -> void:
# Init CORE
await load_framework()
# Variables
var test_in: int = 51758516
var test_out: float = 49.36076736450195
var test_out_flat: float = 49.0
if core.misc.byte2mib(test_in, false) != test_out:
rerror("Non-flat test did not return the right value (in '" + str(test_in) + "', expected '" + str(test_out) + "', got '" + str(core.misc.byte2mib(test_in, false)) + "')")
return
if core.misc.byte2mib(test_in, true) != test_out_flat:
rerror("Flat test did not return the right value (in '" + str(test_in) + "', expected '" + str(test_out) + "', got '" + str(core.misc.byte2mib(test_in, true)) + "')")
return
rok()
# mib2byte
func test_mib2byte() -> void:
# Init CORE
await load_framework()
# Variables
var test_in: float = 49.36076736450195
var test_out: float = 51758516.0
var test_out_flat: float = test_out
if core.misc.mib2byte(test_in, false) != test_out:
rerror("Non-flat test did not return the right value (in '" + str(test_in) + "', expected '" + str(test_out) + "', got '" + str(core.misc.mib2byte(test_in, false)) + "')")
return
if core.misc.mib2byte(test_in, true) != test_out_flat:
rerror("Flat test did not return the right value (in '" + str(test_in) + "', expected '" + str(test_out) + "', got '" + str(core.misc.mib2byte(test_in, true)) + "')")
return
rok()
# mib2gib
func test_mib2gib() -> void:
# Init CORE
await load_framework()
# Variables
var test_in: float = 1500
var test_out: float = 1.46484375
var test_out_flat: float = 1.0
if core.misc.mib2gib(test_in, false) != test_out:
rerror("Non-flat test did not return the right value (in '" + str(test_in) + "', expected '" + str(test_out) + "', got '" + str(core.misc.mib2gib(test_in, false)) + "')")
return
if core.misc.mib2gib(test_in, true) != test_out_flat:
rerror("Flat test did not return the right value (in '" + str(test_in) + "', expected '" + str(test_out) + "', got '" + str(core.misc.mib2gib(test_in, true)) + "')")
return
rok()
# gib2mib
func test_gib2mib() -> void:
# Init CORE
await load_framework()
# Variables
var test_in: float = 1.46484375
var test_out: float = 1500.0
var test_out_flat: float = 1500.0
if core.misc.gib2mib(test_in, false) != test_out:
rerror("Non-flat test did not return the right value (in '" + str(test_in) + "', expected '" + str(test_out) + "', got '" + str(core.misc.gib2mib(test_in, false)) + "')")
return
if core.misc.gib2mib(test_in, true) != test_out_flat:
rerror("Flat test did not return the right value (in '" + str(test_in) + "', expected '" + str(test_out) + "', got '" + str(core.misc.gib2mib(test_in, true)) + "')")
return
rok()
# format_stringarray
func test_format_stringarray() -> void:
# Init CORE
await load_framework()
# Variables
var test_in: Array[String] = ["StarOpenSource", "JeremyStarTM", "Contributors"]
var test_out: String = "StarOpenSource, JeremyStarTM & Contributors"
if core.misc.format_stringarray(test_in) != test_out:
rerror("Test did not return the right value (in '" + str(test_in) + "', expected '" + str(test_out) + "', got '" + str(core.misc.format_stringarray(test_in)) + "')")
return
rok()
# format_stringarray (chaotic)
func test_format_stringarray_chaotic() -> void:
# Init CORE
await load_framework()
# Variables
var test_in: Array[String] = ["StarOpenSource", "JeremyStarTM", "Contributors"]
var test_out: String = "aaaaStarOpenSourcebbbb$#!TaaaaJeremyStarTMbbbb :3 aaaaContributorsbbbb"
if core.misc.format_stringarray(test_in, "aaaa", "bbbb", "$#!T", " :3 ") != test_out:
rerror("Test did not return the right value (in '" + str(test_in) + "', expected '" + str(test_out) + "', got '" + str(core.misc.format_stringarray(test_in)) + "')")
return
rok()
# array_to_stringarray & stringarray_to_array
func test_array_stringarray_conversion() -> void:
# Init CORE
await load_framework()
# Variables
var test_in: Array = ["StarOpenSource", "JeremyStarTM", "Contributors"]
var test_out: Array[String] = ["StarOpenSource", "JeremyStarTM", "Contributors"]
if core.misc.stringarray_to_array(core.misc.array_to_stringarray(test_in)) != test_out:
rerror("Test did not return the right value (in '" + str(test_in) + "', expected '" + str(test_out) + "', got '" + str(core.misc.stringarray_to_array(core.misc.array_to_stringarray(test_in))) + "')")
return
rok()
# get_center
func test_get_center() -> void:
# Init CORE
await load_framework()
# Variables
var test_in_parent: Vector2 = Vector2(5919.591, 6815.9514)
var test_in_child: Vector2 = Vector2(69.69, 666.0)
var test_out: Vector2 = Vector2(test_in_parent.x/2-test_in_child.x/2, test_in_parent.y/2-test_in_child.y/2)
if core.misc.get_center(test_in_parent, test_in_child) != test_out:
rerror("Test did not return the right value (in (parent) '" + str(test_in_parent) + "', in (child) '" + str(test_in_child) + "', expected '" + str(test_out) + "', got '" + str(core.misc.stringarray_to_array(core.misc.get_center(test_in_parent, test_in_child))) + "')")
return
rok()