From 1631f34cb2024e2727babb0f5e27ea36c80b89f3 Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Wed, 13 May 2026 17:11:22 +0800 Subject: [PATCH 1/2] feat(quickcheck): add Arbitrary impl for Map to match HashMap Map (LinkedHashMap) was missing the @quickcheck.Arbitrary impl that HashMap already has. Added to quickcheck/arbitrary.mbt alongside other builtin type impls. Generates keys/values independently to avoid a circular dependency with the tuple package. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- quickcheck/arbitrary.mbt | 23 +++++++++++++++++++++++ quickcheck/pkg.generated.mbti | 1 + 2 files changed, 24 insertions(+) diff --git a/quickcheck/arbitrary.mbt b/quickcheck/arbitrary.mbt index c016c6d3b7..26d06c59ad 100644 --- a/quickcheck/arbitrary.mbt +++ b/quickcheck/arbitrary.mbt @@ -168,3 +168,26 @@ pub impl[X : Arbitrary] Arbitrary for Array[X] with arbitrary(size, rs) { let len = if size == 0 { 0 } else { rs.next_positive_int() % size } Array::makei(len, x => X::arbitrary(x, rs)) } + +///| +/// Returns a randomly generated map containing arbitrary key-value pairs. +/// +/// Example: +/// +/// ```mbt check +/// test { +/// let samples : Array[Map[Int, String]] = @quickcheck.samples(5) +/// inspect(samples.length(), content="5") +/// } +/// ``` +pub impl[K : Arbitrary + Hash + Eq, V : Arbitrary] Arbitrary for Map[K, V] with arbitrary( + size, + rs, +) { + let m : Map[K, V] = {} + let len = if size == 0 { 0 } else { rs.next_positive_int() % size } + for _ in 0.. Date: Wed, 13 May 2026 17:32:36 +0800 Subject: [PATCH 2/2] fix(quickcheck): shrink size param when generating Map entries Per codex review: pass loop index i as the size argument to K::arbitrary and V::arbitrary, matching the pattern used by Array::makei. This prevents infinite recursion when Map appears inside a recursive Arbitrary type. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- quickcheck/arbitrary.mbt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/quickcheck/arbitrary.mbt b/quickcheck/arbitrary.mbt index 26d06c59ad..a12488e0c0 100644 --- a/quickcheck/arbitrary.mbt +++ b/quickcheck/arbitrary.mbt @@ -186,8 +186,8 @@ pub impl[K : Arbitrary + Hash + Eq, V : Arbitrary] Arbitrary for Map[K, V] with ) { let m : Map[K, V] = {} let len = if size == 0 { 0 } else { rs.next_positive_int() % size } - for _ in 0..