Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion quickcheck/README.mbt.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ test "custom type shrinking" {
debug_inspect(
@quickcheck.Shrink::shrink(point).collect(),
content=(
#|[{ x: 1, y: 1 }, { x: 0, y: 1 }, { x: 2, y: 0 }]
#|[{ x: 0, y: 1 }, { x: 1, y: 1 }, { x: 2, y: 0 }]
),
)
}
Expand Down
34 changes: 34 additions & 0 deletions quickcheck/driver_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,37 @@ test "context is attached to raised failures too" {
),
)
}

///|
priv struct LargeShrinkInput(Int) derive(Debug)

///|
impl @quickcheck.Arbitrary for LargeShrinkInput with fn arbitrary(_, _) {
LargeShrinkInput(10000)
}

///|
impl @shrink.Shrink for LargeShrinkInput with fn shrink(input) {
@shrink.Shrink::shrink(input.0).map(value => LargeShrinkInput(value))
}

///|
test "numeric shrinking reaches the boundary within the default budget" {
let report = @quickcheck.report(
(input : LargeShrinkInput) => input.0 < 5000,
count=1,
max_size=0,
)
debug_inspect(
report,
content=(
#|Falsified(
#| counterexample=LargeShrinkInput(5000),
#| tests=1,
#| size=0,
#| shrinks=1,
#| shrink_attempts=15,
#|)
),
)
}
1 change: 1 addition & 0 deletions quickcheck/moon.pkg
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
}

import {
"moonbitlang/core/bench",
"moonbitlang/core/float",
"moonbitlang/core/double",
} for "test"
19 changes: 12 additions & 7 deletions quickcheck/shrink/collection.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

///|
pub impl[T : Shrink] Shrink for @list.List[T] with fn shrink(xs) {
let n = xs.length()
guard !xs.is_empty() else { return [||] }
fn shr_sub_terms(lst : @list.List[T]) {
match lst {
Empty => [||]
Expand All @@ -25,12 +25,17 @@ pub impl[T : Shrink] Shrink for @list.List[T] with fn shrink(xs) {
}
}

[
for k = n / 2; k > 0; k = k / 2 => k
]
.rev_iter()
.flat_map(k => removes_list(k, n, xs))
.concat(shr_sub_terms(xs))
[|xs.take(0)|].concat(
[|()|].flat_map(_ => {
let n = xs.length()
[
for k = n / 2; k > 0; k = k / 2 => k
]
.iter()
.flat_map(k => removes_list(k, n, xs))
.concat([|()|].flat_map(_ => shr_sub_terms(xs)))
}),
)
}

///|
Expand Down
106 changes: 65 additions & 41 deletions quickcheck/shrink/collection_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,43 @@ test "shrink int list" {
@shrink.Shrink::shrink(il).collect(),
content=(
#|[
#| <List: []>,
#| <List: [1, 2, 3]>,
#| <List: [4, 5, 6]>,
#| <List: [1, 2, 3, 4, 5]>,
#| <List: [1, 2, 3, 4, 6]>,
#| <List: [1, 2, 3, 5, 6]>,
#| <List: [1, 2, 4, 5, 6]>,
#| <List: [1, 3, 4, 5, 6]>,
#| <List: [2, 3, 4, 5, 6]>,
#| <List: [1, 2, 3]>,
#| <List: [4, 5, 6]>,
#| <List: [0, 2, 3, 4, 5, 6]>,
#| <List: [1, 1, 3, 4, 5, 6]>,
#| <List: [1, 0, 3, 4, 5, 6]>,
#| <List: [1, 2, 2, 4, 5, 6]>,
#| <List: [1, 1, 3, 4, 5, 6]>,
#| <List: [1, 2, 0, 4, 5, 6]>,
#| <List: [1, 2, 3, 3, 5, 6]>,
#| <List: [1, 2, 3, 2, 5, 6]>,
#| <List: [1, 2, 2, 4, 5, 6]>,
#| <List: [1, 2, 3, 0, 5, 6]>,
#| <List: [1, 2, 3, 4, 4, 6]>,
#| <List: [1, 2, 3, 4, 3, 6]>,
#| <List: [1, 2, 3, 2, 5, 6]>,
#| <List: [1, 2, 3, 3, 5, 6]>,
#| <List: [1, 2, 3, 4, 0, 6]>,
#| <List: [1, 2, 3, 4, 5, 5]>,
#| <List: [1, 2, 3, 4, 5, 3]>,
#| <List: [1, 2, 3, 4, 3, 6]>,
#| <List: [1, 2, 3, 4, 4, 6]>,
#| <List: [1, 2, 3, 4, 5, 0]>,
#| <List: [1, 2, 3, 4, 5, 3]>,
#| <List: [1, 2, 3, 4, 5, 5]>,
#|]
),
)
}

///|
test "single-element list can shrink to empty" {
let input : @list.List[Int] = List([1])
debug_inspect(
@shrink.Shrink::shrink(input).collect(),
content="[<List: []>, <List: [0]>]",
)
}

///|
test "shrink queue" {
let input = @queue.Queue([true, false, true])
Expand Down Expand Up @@ -97,11 +107,11 @@ test "shrink immutable vector" {
#| <Vector: []>,
#| <Vector: [4]>,
#| <Vector: [2]>,
#| <Vector: [1, 4]>,
#| <Vector: [0, 4]>,
#| <Vector: [2, 3]>,
#| <Vector: [2, 2]>,
#| <Vector: [1, 4]>,
#| <Vector: [2, 0]>,
#| <Vector: [2, 2]>,
#| <Vector: [2, 3]>,
#|]
),
)
Expand All @@ -118,13 +128,13 @@ test "shrink mutable hash set" {
#| <HashSet: []>,
#| <HashSet: [4]>,
#| <HashSet: [10]>,
#| <HashSet: [9, 4]>,
#| <HashSet: [8, 4]>,
#| <HashSet: [4, 5]>,
#| <HashSet: [0, 4]>,
#| <HashSet: [10, 3]>,
#| <HashSet: [10, 2]>,
#| <HashSet: [4, 5]>,
#| <HashSet: [8, 4]>,
#| <HashSet: [9, 4]>,
#| <HashSet: [0, 10]>,
#| <HashSet: [10, 2]>,
#| <HashSet: [10, 3]>,
#|]
),
)
Expand All @@ -140,11 +150,11 @@ test "shrink mutable sorted set" {
#| <SortedSet: []>,
#| <SortedSet: [4]>,
#| <SortedSet: [2]>,
#| <SortedSet: [1, 4]>,
#| <SortedSet: [0, 4]>,
#| <SortedSet: [2, 3]>,
#| <SortedSet: [2]>,
#| <SortedSet: [1, 4]>,
#| <SortedSet: [0, 2]>,
#| <SortedSet: [2]>,
#| <SortedSet: [2, 3]>,
#|]
),
)
Expand All @@ -161,11 +171,11 @@ test "shrink immutable hash set" {
#| <HashSet: []>,
#| <HashSet: [7]>,
#| <HashSet: [3]>,
#| <HashSet: [2, 7]>,
#| <HashSet: [0, 7]>,
#| <HashSet: [3, 6]>,
#| <HashSet: [3, 4]>,
#| <HashSet: [2, 7]>,
#| <HashSet: [0, 3]>,
#| <HashSet: [3, 4]>,
#| <HashSet: [3, 6]>,
#|]
),
)
Expand All @@ -181,11 +191,11 @@ test "shrink immutable sorted set" {
#| <SortedSet: []>,
#| <SortedSet: [5]>,
#| <SortedSet: [2]>,
#| <SortedSet: [1, 5]>,
#| <SortedSet: [0, 5]>,
#| <SortedSet: [2, 4]>,
#| <SortedSet: [2, 3]>,
#| <SortedSet: [1, 5]>,
#| <SortedSet: [0, 2]>,
#| <SortedSet: [2, 3]>,
#| <SortedSet: [2, 4]>,
#|]
),
)
Expand All @@ -205,8 +215,8 @@ test "shrink builtin map" {
#| [],
#| [(1, false)],
#| [(2, true)],
#| [(1, false)],
#| [(0, true), (1, false)],
#| [(1, false)],
#| [(2, false), (1, false)],
#| [(2, true), (0, false)],
#|]
Expand All @@ -220,7 +230,12 @@ test "shrink linked set" {
let candidates = @shrink.Shrink::shrink(input)
.map(@set.Set::to_array)
.collect()
debug_inspect(candidates, content="[[], [1], [2], [1], [0, 1], [2, 0]]")
debug_inspect(
candidates,
content=(
#|[[], [1], [2], [0, 1], [1], [2, 0]]
),
)
}

///|
Expand All @@ -229,7 +244,12 @@ test "shrink deque" {
let candidates = @shrink.Shrink::shrink(input)
.map(@deque.Deque::to_array)
.collect()
debug_inspect(candidates, content="[[], [1], [2], [1, 1], [0, 1], [2, 0]]")
debug_inspect(
candidates,
content=(
#|[[], [1], [2], [0, 1], [1, 1], [2, 0]]
),
)
}

///|
Expand All @@ -240,7 +260,9 @@ test "shrink mutable priority queue" {
.collect()
debug_inspect(
candidates,
content="[[], [2], [4], [3, 2], [2, 2], [2, 0], [4, 1], [4, 0]]",
content=(
#|[[], [2], [4], [2, 0], [2, 2], [3, 2], [4, 0], [4, 1]]
),
)
}

Expand All @@ -252,7 +274,9 @@ test "shrink immutable priority queue" {
.collect()
debug_inspect(
candidates,
content="[[], [2], [4], [3, 2], [2, 2], [2, 0], [4, 1], [4, 0]]",
content=(
#|[[], [2], [4], [2, 0], [2, 2], [3, 2], [4, 0], [4, 1]]
),
)
}

Expand All @@ -268,8 +292,8 @@ test "shrink mutable hash map" {
#| <HashMap: { 2: true }>,
#| <HashMap: { 1: false }>,
#| <HashMap: { 0: false, 2: true }>,
#| <HashMap: { 1: true }>,
#| <HashMap: { 0: true, 1: false }>,
#| <HashMap: { 1: true }>,
#| <HashMap: { 1: false, 2: false }>,
#|]
),
Expand All @@ -286,8 +310,8 @@ test "shrink mutable sorted map" {
#| <SortedMap: {}>,
#| <SortedMap: { true: 1 }>,
#| <SortedMap: { false: 2 }>,
#| <SortedMap: { false: 1, true: 1 }>,
#| <SortedMap: { false: 0, true: 1 }>,
#| <SortedMap: { false: 1, true: 1 }>,
#| <SortedMap: { false: 1 }>,
#| <SortedMap: { false: 2, true: 0 }>,
#|]
Expand All @@ -306,16 +330,16 @@ test "shrink immutable hash map" {
#| <HashMap: {}>,
#| <HashMap: { 4: 2 }>,
#| <HashMap: { 3: 7 }>,
#| <HashMap: { 2: 7, 4: 2 }>,
#| <HashMap: { 0: 7, 4: 2 }>,
#| <HashMap: { 3: 6, 4: 2 }>,
#| <HashMap: { 3: 4, 4: 2 }>,
#| <HashMap: { 2: 7, 4: 2 }>,
#| <HashMap: { 3: 0, 4: 2 }>,
#| <HashMap: { 3: 7 }>,
#| <HashMap: { 2: 2, 3: 7 }>,
#| <HashMap: { 3: 4, 4: 2 }>,
#| <HashMap: { 3: 6, 4: 2 }>,
#| <HashMap: { 0: 2, 3: 7 }>,
#| <HashMap: { 3: 7, 4: 1 }>,
#| <HashMap: { 2: 2, 3: 7 }>,
#| <HashMap: { 3: 7 }>,
#| <HashMap: { 3: 7, 4: 0 }>,
#| <HashMap: { 3: 7, 4: 1 }>,
#|]
),
)
Expand All @@ -332,8 +356,8 @@ test "shrink immutable sorted map" {
#| <SortedMap: { 3: true }>,
#| <SortedMap: { 1: false }>,
#| <SortedMap: { 0: false, 3: true }>,
#| <SortedMap: { 1: false, 2: true }>,
#| <SortedMap: { 0: true, 1: false }>,
#| <SortedMap: { 1: false, 2: true }>,
#| <SortedMap: { 1: false, 3: false }>,
#|]
),
Expand Down
4 changes: 2 additions & 2 deletions quickcheck/shrink/composite.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
pub impl[T : Shrink] Shrink for T? with fn shrink(x) {
match x {
None => [||]
Some(v) => Shrink::shrink(v).map(v1 => Some(v1)).concat([|None|])
Some(v) => [|None|].concat(Shrink::shrink(v).map(v1 => Some(v1)))
}
}

Expand Down Expand Up @@ -47,7 +47,7 @@ pub impl[X : Shrink] Shrink for Array[X] with fn shrink(xs) {
]
.iter()
.flat_map(k => removes_array(k, n, xs))
.concat(shr_sub_terms(view))
.concat([|()|].flat_map(_ => shr_sub_terms(view)))
}

///|
Expand Down
Loading
Loading