Files
owniverse/tests/test_quantity_catalog.gd
T

64 lines
3.3 KiB
GDScript
Raw Normal View History

extends SceneTree
const Math = preload("res://world/owniverse/quantity_math.gd")
var checks: int = 0
var failures: int = 0
func _initialize() -> void:
call_deferred("run")
func check(condition: bool, message: String) -> void:
checks += 1
if not condition:
failures += 1
push_error(message)
func run() -> void:
var Catalog = load("res://world/owniverse/entity_catalog.gd")
check(Math.allocate_whole({}, 10.0) == {}, "Empty allocation")
check(Math.allocate_whole({"b": 1.0, "a": 1.0}, 1.0) == {"a": 1.0, "b": 0.0}, "Stable lexical tie")
for limit in range(30):
var requests: Dictionary = {"a": 3.9, "b": 9.1, "c": -2.0, "d": 7.0}
var result: Dictionary = Math.allocate_whole(requests, limit)
check(Math.sum(result) == minf(limit, 19.0), "Conserve bounded allocation")
for id in result:
check(result[id] == floorf(result[id]) and result[id] >= 0.0 and result[id] <= floorf(maxf(0.0, requests[id])), "Whole bounded branch")
check(Math.normalize_distribution({"a": -1.0, "b": 0.0}) == {}, "Nonpositive weights")
check(Math.normalize_distribution({"a": 2.0, "b": 6.0}) == {"a": 0.25, "b": 0.75}, "Normalize weights")
check(Math.production_step([0.25, 0.1, 2.0]) == 20.0, "Common whole production step")
check(Math.production_step([]) == 1.0, "Free production step")
var target: Dictionary = {}
var produced: float = 0.0
for i in 10:
produced += Math.accumulate_whole(target, "remainders", "a", 0.1)
check(produced == 1.0, "Fractional production carries")
var remainders: Dictionary = {}
var totals: Dictionary = {"a": 0.0, "b": 0.0, "c": 0.0}
for i in 30:
var result: Dictionary = Math.split(remainders, "split", 1.0, {"a": 1.0, "b": 1.0, "c": 1.0})
check(Math.sum(result) == 1.0, "Split conserves count")
for id in result: totals[id] += result[id]
check(totals == {"a": 10.0, "b": 10.0, "c": 10.0}, "Rounding debt distributes over time")
produced = 0.0
for i in 10: produced += Math.accumulate_rate(remainders, "rate", 1.0, 0.1)
check(produced == 1.0, "Rate carries")
var catalog = Catalog.new()
var rows: Dictionary = root.get_node("Global").OwniverseEntities
catalog.load_definitions(rows)
check(catalog.entities.size() == rows.size(), "Catalog loads all definitions")
for id in catalog.entities:
var entity = catalog.entities[id]
check(catalog.automatic_producers.has(entity) == (entity.product_per_year_type != ""), "Producer index")
check(catalog.unlockable_entities.has(entity) == (not entity.unlock.is_empty()), "Unlock index")
if entity.group_as != "":
check(catalog.groups[entity.group_as].has(id) and catalog.members(entity.group_as).has(id), "Group index")
if entity.event_group != "":
check(catalog.event_groups[entity.event_group].has(id), "Event index")
var members: Array = catalog.members("Star")
members.clear()
check(not catalog.members("Star").is_empty(), "Group results are independent")
check(Catalog.new().entities.is_empty(), "Catalog instances are independent")
for entity in catalog.entities.values(): entity.free()
print("Quantity and catalog: %d checks, %d failures" % [checks, failures])
quit(1 if failures else 0)