///|
test "adding zero is an identity" {
@quickcheck.check((x : Int) => x + 0 == x)
}///|
test "division identity" {
@quickcheck.check((x : Int) => x / x == 1, filter=x => x != 0)
}///|
test "configured property run" {
@quickcheck.check(
(xs : Array[Int]) => xs.length() >= 0,
count=200,
max_size=50,
max_shrinks=100,
discard_ratio=10,
seed=2026,
)
}///|
test "inspect a counterexample" {
let report = @quickcheck.report(
(_ : Int) => false,
count=1,
max_size=0,
max_shrinks=0,
seed=7,
)
debug_inspect(
report,
content=(
#|Falsified(
#| counterexample=-1028290551,
#| tests=1,
#| size=0,
#| shrinks=0,
#| shrink_attempts=0,
#|)
),
)
}///|
test "context describes the shrunk counterexample" {
let report = @quickcheck.report(
(x : Int) => x < 3,
counterexample_context=x => "rendered input <\{x}>",
seed=7,
)
debug_inspect(
report,
content=(
#|Falsified(
#| counterexample=3,
#| context="rendered input <3>",
#| tests=2,
#| size=1,
#| shrinks=30,
#| shrink_attempts=33,
#|)
),
)
}///|
test "observe generated cases" {
let report = @quickcheck.report(
(_ : Unit) => true,
observe=_ => {
[@quickcheck.label("unit"), @quickcheck.classify(true, "generated")]
},
count=2,
)
debug_inspect(
report,
content=(
#|Passed(
#| tests=2,
#| observations={ labels: { <List: ["unit"]>: 2 }, classes: { "generated": 2 } },
#|)
),
)
}///|
test "basic generation" {
let b : Bool = @quickcheck.gen()
inspect(b, content="true")
let x : Int = @quickcheck.gen()
inspect(
x,
content=(
#|1118850684
),
)
// Generate with size parameter
let sized : Array[Int] = @quickcheck.gen(size=5)
inspect(sized.length() <= 5, content="true")
}///|
test "multiple samples" {
let ints : Array[Int] = @quickcheck.samples(5)
debug_inspect(
ints,
content=(
#|[1118850684, -99999, 846697896, -134217729, 67108863]
),
)
let strings : Array[String] = @quickcheck.samples(12)
debug_inspect(
strings[5:10],
content=(
#|<ArrayView: ["(K!", "", "vx2\b", "", "Hp9\u{18}Rx"]>
),
)
}///|
test "builtin types" {
// Basic types
let v : (Bool, Char, Byte) = @quickcheck.gen()
debug_inspect(
v,
content=(
#|(false, '~', 0x4d)
),
)
// Numeric types
let v : (Int, Int64, UInt, UInt64, Float, Double, BigInt) = @quickcheck.gen()
debug_inspect(
v,
content=(
#|(
#| -99999,
#| 5259998046134461054,
#| 228947857,
#| 8766027650639656979,
#| 0.23986786603927612,
#| 0.7917029935679342,
#| -2,
#|)
),
)
// Collections
let v : (String, Bytes, Iter[Int]) = @quickcheck.gen()
let (s, b, iter) = v
debug_inspect(
(s, b, iter.to_array()),
content=(
#|("", <Bytes: []>, [])
),
)
}///|
priv struct Point {
x : Int
y : Int
} derive(Debug)
///|
impl @quickcheck.Arbitrary for Point with fn arbitrary(size, r0) {
let r1 = r0.split()
let y = @quickcheck.Arbitrary::arbitrary(size, r1)
{ x: @quickcheck.Arbitrary::arbitrary(size, r0), y, }
}
///|
impl @quickcheck.Shrink for Point with fn shrink(self) {
@quickcheck.Shrink::shrink((self.x, self.y)).map(pair => {
x: pair.0,
y: pair.1,
})
}
///|
test "custom type generation" {
let point : Point = @quickcheck.gen()
debug_inspect(
point,
content=(
#|{ x: -99999, y: 2 }
),
)
let points : Array[Point] = @quickcheck.samples(10)
debug_inspect(
points[6:],
content=(
#|<ArrayView:
#| [
#| { x: 46354256, y: 1201652877 },
#| { x: 1, y: 2147483646 },
#| { x: 108552206, y: -1 },
#| { x: -1073741824, y: 2147483647 },
#| ]>
),
)
}
///|
test "custom type shrinking" {
let point : Point = { x: 2, y: 1, }
debug_inspect(
@quickcheck.Shrink::shrink(point).collect(),
content=(
#|[{ x: 1, y: 1 }, { x: 0, y: 1 }, { x: 2, y: 0 }]
),
)
}impl Arbitrary for FixedArray[X]impl Arbitrary for ReadOnlyArray[X]impl Arbitrary for PriorityQueue[X]impl Arbitrary for PriorityQueue[X]fn[A : Arbitrary, B : Arbitrary, C : Arbitrary] arbitrary(size : Int, r0 : RandomState) -> (A, B, C)type Generator[T]impl Show for Observationtype QuickCheckReport[A]impl Debug for QuickCheckReport[A]#callsite(autofill(loc))
fn[A : Arbitrary + Shrink + Debug] check(property : (A) -> Bool raise?, filter? : (A) -> Bool, observe? : (A) -> Array[Observation], counterexample_context? : (A) -> String, count? : UInt, max_size? : UInt, max_shrinks? : UInt, discard_ratio? : UInt, seed? : UInt64, loc~ : SourceLoc) -> Unit raisetest "adding zero is an identity" {
@quickcheck.check((x : Int) => x + 0 == x)
}fn[A : Arbitrary + Shrink] report(property : (A) -> Bool raise?, filter? : (A) -> Bool, observe? : (A) -> Array[Observation], counterexample_context? : (A) -> String, count? : UInt, max_size? : UInt, max_shrinks? : UInt, discard_ratio? : UInt, seed? : UInt64) -> QuickCheckReport[A]test "spawn draws from Arbitrary" {
let strings : @quickcheck.Generator[String] = @quickcheck.spawn()
let lengths = strings.samples(count=3, size=4, seed=37).map(s => s.length())
debug_inspect(
lengths,
content=(
#|[3, 2, 1]
),
)
}Install
Installed by default