From a22af96b1731b03da1f580fa71f7d77dc3db50b0 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Tue, 28 Nov 2023 20:25:17 -0500 Subject: [PATCH] =?UTF-8?q?More=20thorough=20=E2=80=A2HashMap=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/cases/hash.bqn | 18 ++++++++++++++---- test/hashmap.bqn | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 test/hashmap.bqn diff --git a/test/cases/hash.bqn b/test/cases/hash.bqn index 7f571d49..b7a462e7 100644 --- a/test/cases/hash.bqn +++ b/test/cases/hash.bqn @@ -45,7 +45,17 @@ a←(•ParseFloat¨ "1.2"‿"-0"‿"0")∾-⊸⋈0÷0 ⋄ ! (0‿3‿3‿3⊏a) !"⊐: Rank of 𝕩 must be at least the cell rank of 𝕨 (2‿2‿2 ≡ ≢𝕨, ⟨2⟩ ≡ ≢𝕩)" % (2‿2‿2⥊1‿2)⊐1‿2 !"⊒: Rank of 𝕩 must be at least the cell rank of 𝕨 (2‿2‿2 ≡ ≢𝕨, ⟨2⟩ ≡ ≢𝕩)" % (2‿2‿2⥊1‿2)⊒1‿2 -⟨2⟩ %% ("abc"‿"de"‿"fgh" •HashMap ⥊¨↕3).Get "fgh" -∞ %% ∞ ("abc"‿"de"‿"fgh" •HashMap ⥊¨↕3).Get "fghi" -3 %% ("abc"‿"de"‿"fgh" •HashMap ⥊¨↕3).Count@ -"B" %% ("b" ("a"‿"b" •HashMap "A"‿"C").Set "B").Get "b" +# hashmap +("abc"‿"de"‿"fgh" •HashMap ⥊¨↕3).Get "fgh" %% ⟨2⟩ +∞ ("abc"‿"de"‿"fgh" •HashMap ⥊¨↕3).Get "fghi" %% ∞ +("abc"‿"de"‿"fgh" •HashMap ⥊¨↕3).Count"xy" %% 3 +("b" ("a"‿"b" •HashMap "A"‿"C").Set "B").Get "b" %% "B" +((•HashMap˜↕4).Set´ 1‿"one").Keys↕2 %% ↕4 +(((•HashMap˜↕4).Delete 1).Set´ 1‿"one").Keys@ %% 0‿2‿3‿1 +(((•HashMap˜↕4).Delete 1).Set´ 1‿"one").Values@ %% 0‿2‿3‿"one" +({𝕩.Set´"xy"}⍟4•HashMap˜↕0).Count@ %% 1 +!"•HashMap: Arguments must be lists (⟨⟩≡≢𝕨, ⟨3⟩≡≢𝕩)" % 'a' •HashMap "str" +!"•HashMap: 𝕨 and 𝕩 must have the same length (4≡≠𝕨, 3≡≠𝕩)" % "stri" •HashMap "str" +!"•HashMap: 𝕨 contained duplicate keys" % "strs" •HashMap "stri" +!"(hashmap).Get: key not found" % ("abc"‿"de"‿"fgh" •HashMap ⥊¨↕3).Get "fg" +!"(hashmap).Delete: key not found" % ("abc"‿"de"‿"fgh" •HashMap ⥊¨↕3).Delete 'a' diff --git a/test/hashmap.bqn b/test/hashmap.bqn new file mode 100644 index 00000000..f8c40904 --- /dev/null +++ b/test/hashmap.bqn @@ -0,0 +1,42 @@ +u ← ⌊100×(•UnixTime+1|100וMonoTime)@ +ro← •MakeRand •Show u +R ← ro.Range + +err ← 1 # Run operations that should error + +Test ← {iter 𝕊 n: + # Global set of keys that might be used + keys ← ⊒⊸(-⊸∾⍟(0<⊣)¨) (R⟜15 R¨ 1-˜1e6⌊∘⋆R⟜0) n + # Which keys are currently set, and count + c ← +´ mask ← 0 = n R 20+R 10 + # Value for key if set + vals ← ((R⟜≠⊏⊢)⟜⊢‿÷‿⥊‿↕ {𝕎𝕩}¨ R⟜10) n + + map ← keys •HashMap○(mask⊸/) vals + + # Single operations and checks + _er ← { err ? ! 0∘𝔽⎊1 𝕩⊑keys ; 1 } + Cnt ← {𝕊: ! c ≡ map.Count@ } + Key ← {𝕊: ! (mask/keys) ≡○∧ map.Keys@ } + Val ← {𝕊: ! (mask/vals) ≡○∧ map.Values@ } + Has ← { ! (𝕩⊑mask) ≡ map.Has 𝕩⊑keys } + Get ← { 𝕩⊑mask ? ! (𝕩⊑vals) ≡ map.Get 𝕩⊑keys ; map.Get _er 𝕩 } + Set ← { 𝕨 map.Set˜ 𝕩⊑keys ⋄ vals 𝕨˙⌾(𝕩⊸⊑)↩ ⋄ mask {c+↩¬𝕩⋄1}⌾(𝕩⊸⊑)↩ ⋄ @ } + Del ← { 𝕩⊑mask ? map.Delete 𝕩⊑keys ⋄ mask 0⌾(𝕩⊸⊑)↩ ⋄ c-↩1 ; map.Delete _er 𝕩 } + + Selection ← { (𝕨≤1)◶⟨R·⌊1.5⊸×,𝕨⟩⊸⌊⊸ro.Deal∘≠⊸⊏ 𝕩 } + RandVal ← R∘100 + ops ← ⟨Cnt, Has, Get, Del, (R∘n⊑vals˙)Set⊢, RandVal⊸Set⟩ + + # Larger sets: argument is number of ops (sometimes overridden) + FullCheck ← Key∘Val∘Cnt + Deletes ← { Del¨ 𝕩 Selection mask } + Restores ← { ⊏⟜vals⊸(Set¨) 𝕩 Selection ¬mask } + RandomOps ← (R⟜≠⊏⊢)⟜ops {𝕎𝕩}¨ R⟜n + RandomSame ← R∘≠⊸⊑∘ops {𝕎𝕩}¨ R⟜n + opSets ← FullCheck‿Deletes‿Restores‿RandomOps‿RandomSame + ((R⟜≠⊏⊢)⟜opSets {𝕎𝕩}¨ R⟜100) iter +} + +1e5 Test 1e2 +1e4 Test 1e4