CORE/tests/unit/misc.gd

186 lines
8.7 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()
# stringify_variables
func test_stringify_variables() -> void:
rskip("Unfinished")
return
# Init CORE
await load_framework()
# Variables
var test_in_string: String = "[b]null[/b]=%null%\n[b]bool[/b]=%bool%\n[b]int[/b]=%int%\n[b]float[/b]=%float%\n[b]string[/b]=%string%\n[b]stringname[/b]=%stringname%\n[b]color[/b]=%color%\n[b]rid[/b]=%rid%\n[b]array[/b]=%array%\n[b]dict[/b]=%dictionary%\n[b]nodepath[/b]=%nodepath%\n[b]call[/b]=%callable%\n[b]signal[/b]=%signal%\n[b]vec2[/b]=%vector2%\n[b]vec2i[/b]=%vector2i%\n[b]rect2[/b]=%rect2%\n[b]rect2i[/b]=%rect2i%\n[b]trans2d[/b]=%transform2d%\n[b]vec3[/b]=%vector3%\n[b]vec3i[/b]=%vector3i%\n[b]plane[/b]=%plane%\n[b]quarternion[/b]=%quaternion%\n[b]aabb[/b]=%aabb%\n[b]trans3d[/b]=%transform3d%\n[b]basis[/b]=%basis%\n[b]projection[/b]=%projection%\n[b]vec4[/b]=%vector4%\n[b]vec4i[/b]=%vector4i%"
var test_in_args: Dictionary = { "null": null, "bool": true, "int": 505, "float": 505.69, "string": "some string", "stringname": "some string name", "color": Color("#d60532"), "rid": RenderingServer.get_white_texture(), "array": [ "item1", "item2", true, 4 ], "dictionary": { "key0": true, 1: "key2" }, "nodepath": NodePath("/root/CORE"), "callable": func() -> void: pass, "signal": core.logger.log_event, "vector2": Vector2(PI, TAU), "vector2i": Vector2i(505, 69), "rect2": Rect2(1.51, 2.56, 3.89, 4.11), "rect2i": Rect2i(1, 2, 3, 4), "transform2d": Transform2D(1.5, Vector2(2.1, 2.2), 3.6, Vector2(4.1, 4.2)), "vector3": Vector3(59.666, NAN, INF), "vector3i": Vector3i(505, 69, 1713208182), "plane": Plane(Vector3(1.5, 2.5, 3.5), 55.7777), "quaternion": Quaternion(1.55, 2.7812, 3.671, 4.8871), "aabb": AABB(Vector3(1.1, 1.2, 1.3), Vector3(2.1, 2.2, 2.3)), "transform3d": Transform3D(Basis(Vector3(1.11, 1.12, 1.13), Vector3(1.21, 1.22, 1.23), Vector3(1.31, 1.32, 1.33)), Vector3(2.1, 2.2, 2.3)), "basis": Basis(Vector3(1.1358, 1.268718, 1.35818), Vector3(2.1481, 2.258819, 2.3718), Vector3(3.1658581, 3.2581587, 3.357158)), "projection": Projection(Transform3D(Basis(Vector3(1.11, 1.12, 1.13), Vector3(1.21, 1.22, 1.23), Vector3(1.31, 1.32, 1.33)), Vector3(2.1, 2.2, 2.3))), "vector4": Vector4(1.55158, 2.517571, 3.58157, 4.51857185), "vector4i": Vector4i(1, 2, 3, 4) }
var test_out: String = "unfinished"
var test_result: String = core.misc.stringify_variables(test_in_string, test_in_args)
if test_result != test_out:
lerror("Got invalid test result:")
lerror("in (string) -> " + test_in_string.replace("\n", "\\n"))
await get_tree().create_timer(1).timeout # output overflow workaround
lerror("in (args) -> " + str(test_in_args))
await get_tree().create_timer(1).timeout # output overflow workaround
lerror("expected -> " + test_out.replace("\n", "\\n"))
await get_tree().create_timer(1).timeout # output overflow workaround
lerror("got -> " + test_result.replace("\n", "\\n"))
await get_tree().create_timer(1).timeout # output overflow workaround
rerror("Test did not return the right value (check logs)")
return
rok()