aboutsummaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorCalvin Rose <calsrose@gmail.com>2024-08-22 19:35:59 -0500
committerCalvin Rose <calsrose@gmail.com>2024-08-22 19:37:41 -0500
commit43ecd4f2d895d2f0dd9d5e109bcaa1b34a3bffb5 (patch)
treee0bd9a2efb38c20caa0872e51476c7c0bc89ac50 /test
parentBetter handle extra filewatch events on wine. (diff)
Add fixes for marshalling weak containers - Fix #1488
Weak containers did not preserve their weakness when marshalled. This fixes that for tables and arrays, as well as adds some tests for this. Also exposes functions for creating weak tables in janet.h
Diffstat (limited to 'test')
-rw-r--r--test/suite-marsh.janet77
1 files changed, 76 insertions, 1 deletions
diff --git a/test/suite-marsh.janet b/test/suite-marsh.janet
index 6e840910..b9f4d277 100644
--- a/test/suite-marsh.janet
+++ b/test/suite-marsh.janet
@@ -146,5 +146,80 @@ neldb\0\0\0\xD8\x05printG\x01\0\xDE\xDE\xDE'\x03\0marshal_tes/\x02
(def item (ev/take newchan))
(assert (= item newchan) "ev/chan marshalling"))
-(end-suite)
+# Issue #1488 - marshalling weak values
+(testmarsh (array/weak 10) "marsh array/weak")
+(testmarsh (table/weak-keys 10) "marsh table/weak-keys")
+(testmarsh (table/weak-values 10) "marsh table/weak-values")
+(testmarsh (table/weak 10) "marsh table/weak")
+
+# Now check that gc works with weak containers after marshalling
+
+# Turn off automatic GC for testing weak references
+(gcsetinterval 0x7FFFFFFF)
+
+# array
+(def a (array/weak 1))
+(array/push a @"")
+(assert (= 1 (length a)) "array/weak marsh 1")
+(def aclone (-> a marshal unmarshal))
+(assert (= 1 (length aclone)) "array/weak marsh 2")
+(gccollect)
+(assert (= 1 (length aclone)) "array/weak marsh 3")
+(assert (= 1 (length a)) "array/weak marsh 4")
+(assert (= nil (get a 0)) "array/weak marsh 5")
+(assert (= nil (get aclone 0)) "array/weak marsh 6")
+(assert (deep= a aclone) "array/weak marsh 7")
+
+# table weak keys and values
+(def t (table/weak 1))
+(def keep-key :key)
+(def keep-value :value)
+(put t :abc @"")
+(put t :key :value)
+(assert (= 2 (length t)) "table/weak marsh 1")
+(def tclone (-> t marshal unmarshal))
+(assert (= 2 (length tclone)) "table/weak marsh 2")
+(gccollect)
+(assert (= 1 (length tclone)) "table/weak marsh 3")
+(assert (= 1 (length t)) "table/weak marsh 4")
+(assert (= keep-value (get t keep-key)) "table/weak marsh 5")
+(assert (= keep-value (get tclone keep-key)) "table/weak marsh 6")
+(assert (deep= t tclone) "table/weak marsh 7")
+
+# table weak keys
+(def t (table/weak-keys 1))
+(put t @"" keep-value)
+(put t :key @"")
+(assert (= 2 (length t)) "table/weak-keys marsh 1")
+(def tclone (-> t marshal unmarshal))
+(assert (= 2 (length tclone)) "table/weak-keys marsh 2")
+(gccollect)
+(assert (= 1 (length tclone)) "table/weak-keys marsh 3")
+(assert (= 1 (length t)) "table/weak-keys marsh 4")
+(assert (deep= t tclone) "table/weak-keys marsh 5")
+
+# table weak values
+(def t (table/weak-values 1))
+(put t @"" keep-value)
+(put t :key @"")
+(assert (= 2 (length t)) "table/weak-values marsh 1")
+(def tclone (-> t marshal unmarshal))
+(assert (= 2 (length tclone)) "table/weak-values marsh 2")
+(gccollect)
+(assert (= 1 (length t)) "table/weak-value marsh 3")
+(assert (deep= t tclone) "table/weak-values marsh 4")
+
+# tables with prototypes
+(def t (table/weak-values 1))
+(table/setproto t @{:abc 123})
+(put t @"" keep-value)
+(put t :key @"")
+(assert (= 2 (length t)) "marsh weak tables with prototypes 1")
+(def tclone (-> t marshal unmarshal))
+(assert (= 2 (length tclone)) "marsh weak tables with prototypes 2")
+(gccollect)
+(assert (= 1 (length t)) "marsh weak tables with prototypes 3")
+(assert (deep= t tclone) "marsh weak tables with prototypes 4")
+(assert (deep= (getproto t) (getproto tclone)) "marsh weak tables with prototypes 5")
+(end-suite)