CORE/tests/unit/misc.gd

195 lines
12 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:
# Init CORE
var core_config: CoreConfiguration = CoreConfiguration.new()
core_config.misc_stringify_show_type = true
core_config.misc_stringify_color_range8 = true
core_config.misc_stringify_array = true
core_config.misc_stringify_dictionary = true
await load_framework(core_config)
# Variables
var test_in_string: String = "null=%null%\nbool=%bool%\nint=%int%\nfloat=%float%\nstring=%string%\nstringname=%stringname%\ncolor=%color%\narray=%array%\ndict=%dictionary%\nnodepath=%nodepath%\nvec2=%vector2%\nvec2i=%vector2i%\nrect2=%rect2%\nrect2i=%rect2i%\ntrans2d=%transform2d%\nvec3=%vector3%\nvec3i=%vector3i%\nplane=%plane%\nquarternion=%quaternion%\naabb=%aabb%\ntrans3d=%transform3d%\nbasis=%basis%\nprojection=%projection%\nvec4=%vector4%\nvec4i=%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 = "null='null'\nbool='(bool) true'\nint='(int) 505'\nfloat='(float) 505.69'\nstring='(string) \"some string\"'\nstringname='(string) \"some string name\"'\ncolor='(color) r=(int) 214 g=(int) 5 b=(int) 50 a=(int) 255'\narray='(array) [ (string) \"item1\", (string) \"item2\", (bool) true, (int) 4 ]'\ndict='(dictionary) { (string) \"key0\": (bool) true, (int) 1 }'\nnodepath='(nodepath) /root/CORE'\nvec2='(vector2) x=(float) 3.14159274101257 y=(float) 6.28318548202515'\nvec2i='(vector2i) x=(int) 505 y=(int) 69'\nrect2='(rect2) size=(vector2) x=(float) 3.89000010490417 y=(float) 4.1100001335144 pos=(vector2) x=(float) 1.50999999046326 y=(float) 2.55999994277954 end=(vector2) x=(float) 5.40000009536743 y=(float) 6.67000007629395'\nrect2i='(rect2i) size=(vector2i) x=(int) 3 y=(int) 4 pos=(vector2i) x=(int) 1 y=(int) 2 end=(vector2i) x=(int) 4 y=(int) 6'\ntrans2d='(transform2d) x=(vector2) x=(float) 0.14854811131954 y=(float) 2.09473943710327 y=(vector2) x=(float) 2.03679251670837 y=(float) 0.83155089616776 origin=(vector2) x=(float) 4.09999990463257 y=(float) 4.19999980926514'\nvec3='(vector3) x=(float) 59.6660003662109 y=(float) nan z=(float) inf'\nvec3i='(vector3i) x=(int) 505 y=(int) 69 z=(int) 1713208182'\nplane='(plane) x=(float) 1.5 y=(float) 2.5 z=(float) 3.5 d=(float) 55.7776985168457 normal=(vector3) x=(float) 1.5 y=(float) 2.5 z=(float) 3.5'\nquarternion='(quaternion) x=(float) 1.54999995231628 y=(float) 2.78119993209839 z=(float) 3.6710000038147 w=(float) 4.88710021972656'\naabb='(aabb) size=(vector3) x=(float) 2.09999990463257 y=(float) 2.20000004768372 z=(float) 2.29999995231628 pos=(vector3) x=(float) 1.10000002384186 y=(float) 1.20000004768372 z=(float) 1.29999995231628 end=(vector3) x=(float) 3.19999980926514 y=(float) 3.40000009536743 z=(float) 3.59999990463257'\ntrans3d='(transform3d) basis=(basis) x=(vector3) x=(float) 1.11000001430511 y=(float) 1.12000000476837 z=(float) 1.12999999523163 y=(vector3) x=(float) 1.21000003814697 y=(float) 1.22000002861023 z=(float) 1.23000001907349 z=(vector3) x=(float) 1.30999994277954 y=(float) 1.32000005245209 z=(float) 1.33000004291534 origin=(vector3) x=(float) 2.09999990463257 y=(float) 2.20000004768372 z=(float) 2.29999995231628'\nbasis='(basis) x=(vector3) x=(float) 1.13580000400543 y=(float) 1.26871800422668 z=(float) 1.35818004608154 y=(vector3) x=(float) 2.14809989929199 y=(float) 2.25881910324097 z=(float) 2.3717999458313 z=(vector3) x=(float) 3.16585803031921 y=(float) 3.25815868377686 z=(float) 3.35715794563293'\nprojection='(projection) x=(vector4) x=(float) 1.11000001430511 y=(float) 1.12000000476837 z=(float) 1.12999999523163 w=(float) 0 y=(vector4) x=(float) 1.21000003814697 y=(float) 1.22000002861023 z=(float) 1.23000001907349 w=(float) 0 z=(vector4) x=(float) 1.30999994277954 y=(float) 1.32000005245209 z=(float) 1.33000004291534 w=(float) 0 w=(vector4) x=(float) 2.09999990463257 y=(float) 2.20000004768372 z=(float) 2.29999995231628 w=(float) 1'\nvec4='(vector4) x=(float) 1.55157995223999 y=(float) 2.51757097244263 z=(float) 3.58156991004944 w=(float) 4.5185718536377'\nvec4i='(vector4i) x=(int) 1 y=(int) 2 z=(int) 3 w=(int) 4'"
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) ->")
for line in test_in_string.split("\n"):
print_rich("[color=red]---> " + line + "[/color]")
await get_tree().create_timer(0.05).timeout # output overflow workaround
lerror("in (args) -> " + str(test_in_args))
await get_tree().create_timer(1).timeout # output overflow workaround
lerror("expected ->")
for line in test_out.split("\n"):
print_rich("[color=red]---> " + line + "[/color]")
await get_tree().create_timer(0.05).timeout # output overflow workaround
lerror("got ->")
for line in test_result.split("\n"):
print_rich("[color=red]---> " + line + "[/color]")
await get_tree().create_timer(0.05).timeout # output overflow workaround
rerror("Test did not return the right value (check logs)")
return
rok("All types except RIDs, Callables and Signals have been verified as working.")