From f011dcafb0f583c89ed9971238fd83ddcbdb5438 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Mon, 29 Sep 2025 17:11:58 +0200 Subject: Lift restriction on tables as table keys As demonstrated by the unit tests, there is no problem with using a table as a table key --- unit_tests/scripts/linda/send_receive_tables.lua | 41 ++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 unit_tests/scripts/linda/send_receive_tables.lua (limited to 'unit_tests/scripts') diff --git a/unit_tests/scripts/linda/send_receive_tables.lua b/unit_tests/scripts/linda/send_receive_tables.lua new file mode 100644 index 0000000..08b1f16 --- /dev/null +++ b/unit_tests/scripts/linda/send_receive_tables.lua @@ -0,0 +1,41 @@ +local lanes = require "lanes" + +-- a newly created linda doesn't contain anything +local l = lanes.linda() + +-- send a table with subtables, both as keys and values, making sure the structure is preserved + +local t1 = {["name"] = "t1"} +local t2 = {["name"] = "t2"} + +local t3 = { + ["name"] = "t3", + ["t1"] = t1, + [t1] = t2, -- table t1 as key, table t2 as value + ["t2"] = t2, + [t2] = t1 -- table t2 as key, table t1 as value +} + +-- add recursivity for good measure +t3["t3"] = t3 +t3[t3] = t3 +t1["t3"] = t3 +t2["t3"] = t3 + +l:send("data", t3) +local k, t = l:receive("data") +assert(k == "data" and type(t) == "table" and t.name == "t3") +-- when keys are strings +assert(t["t3"] == t) +assert(type(t["t1"]) == "table") +assert(t["t1"].name == "t1") +assert(type(t["t2"]) == "table") +assert(t["t2"].name == "t2") +assert(t["t1"].t3 == t) +assert(t["t2"].t3 == t) +-- when keys are tables +assert(t[t.t1] == t.t2) +assert(t[t.t1]["t3"] == t) +assert(t[t.t2] == t.t1) +assert(t[t.t2]["t3"] == t) +assert(t[t.t3] == t.t3) -- cgit v1.2.3-55-g6feb