diff --git a/array/array.mbt b/array/array.mbt index 2bded37ac..f2fc9191d 100644 --- a/array/array.mbt +++ b/array/array.mbt @@ -310,15 +310,6 @@ pub fn[A, B] zip_to_iter2(self : Array[A], other : Array[B]) -> Iter2[A, B] { }) } -///| -pub impl[X : @quickcheck.Arbitrary] @quickcheck.Arbitrary for Array[X] with arbitrary( - size, - rs -) { - let len = if size == 0 { 0 } else { rs.next_positive_int() % size } - Array::makei(len, fn(x) { X::arbitrary(x, rs) }) -} - ///| /// Join an array of strings using the provided `separator`. /// diff --git a/array/fixedarray.mbt b/array/fixedarray.mbt index dec52e24a..6ca8937a0 100644 --- a/array/fixedarray.mbt +++ b/array/fixedarray.mbt @@ -1240,12 +1240,3 @@ test "FixedArray::join" { let fixed_array_empty : FixedArray[String] = [] inspect(fixed_array_empty.join(","), content="") } - -///| -pub impl[X : @quickcheck.Arbitrary] @quickcheck.Arbitrary for FixedArray[X] with arbitrary( - size, - rs -) { - let len = if size == 0 { 0 } else { rs.next_positive_int() % size } - FixedArray::makei(len, fn(x) { X::arbitrary(x, rs) }) -} diff --git a/array/moon.pkg.json b/array/moon.pkg.json index c39987410..f952c87ec 100644 --- a/array/moon.pkg.json +++ b/array/moon.pkg.json @@ -2,15 +2,14 @@ "import": [ "moonbitlang/core/builtin", "moonbitlang/core/bytes", - "moonbitlang/core/string", - "moonbitlang/core/quickcheck", - "moonbitlang/core/quickcheck/splitmix" + "moonbitlang/core/string" ], "test-import": [ "moonbitlang/core/char", "moonbitlang/core/test", "moonbitlang/core/random", - "moonbitlang/core/json" + "moonbitlang/core/json", + "moonbitlang/core/quickcheck" ], "targets": { "array_js.mbt": ["js"], diff --git a/array/view.mbt b/array/view.mbt index 6e76dcfbd..7e21e46e8 100644 --- a/array/view.mbt +++ b/array/view.mbt @@ -486,11 +486,3 @@ pub impl[A : Hash] Hash for View[A] with hash_combine(self, hasher) { hasher.combine(e) } } - -///| -pub impl[A : @quickcheck.Arbitrary] @quickcheck.Arbitrary for View[A] with arbitrary( - size, - rs -) { - Array::arbitrary(size, rs)[:] -} diff --git a/bigint/bigint.mbt b/bigint/bigint.mbt index 63d828ce9..e4f4e559d 100644 --- a/bigint/bigint.mbt +++ b/bigint/bigint.mbt @@ -51,15 +51,6 @@ pub impl @json.FromJson for BigInt with from_json(json, path) { BigInt::from_string(s) } -///| -pub impl @quickcheck.Arbitrary for BigInt with arbitrary(size, rs) { - if size == 0 { - 0 - } else { - rs.next_int64() |> BigInt::from_int64 - } -} - ///| /// Tests equality between a `BigInt` and an `Int` value. /// diff --git a/bigint/moon.pkg.json b/bigint/moon.pkg.json index 93fcbcace..97c8b70bb 100644 --- a/bigint/moon.pkg.json +++ b/bigint/moon.pkg.json @@ -4,8 +4,6 @@ "moonbitlang/core/char", "moonbitlang/core/uint", "moonbitlang/core/json", - "moonbitlang/core/quickcheck", - "moonbitlang/core/quickcheck/splitmix", "moonbitlang/core/string" ], "targets": { diff --git a/option/moon.pkg.json b/option/moon.pkg.json index fcefce093..27b0875a2 100644 --- a/option/moon.pkg.json +++ b/option/moon.pkg.json @@ -2,7 +2,7 @@ "import": [ "moonbitlang/core/builtin", "moonbitlang/core/quickcheck", - "moonbitlang/core/quickcheck/splitmix" + "moonbitlang/core/random" ], "targets": { "panic_test.mbt": ["not", "native", "llvm"] diff --git a/priority_queue/moon.pkg.json b/priority_queue/moon.pkg.json index a59fd308e..806711002 100644 --- a/priority_queue/moon.pkg.json +++ b/priority_queue/moon.pkg.json @@ -3,7 +3,7 @@ "moonbitlang/core/builtin", "moonbitlang/core/array", "moonbitlang/core/quickcheck", - "moonbitlang/core/quickcheck/splitmix" + "moonbitlang/core/random" ], "test-import": ["moonbitlang/core/random"], "targets": { diff --git a/quickcheck/arbitrary.mbt b/quickcheck/arbitrary.mbt index 0b632ea24..2b6ebe0c9 100644 --- a/quickcheck/arbitrary.mbt +++ b/quickcheck/arbitrary.mbt @@ -18,7 +18,7 @@ pub(open) trait Arbitrary { // `arbitrary` function takes a random number generator and a number that // determines the size of the generated value as arguments, and returns a // randomly generated value of certain type. - arbitrary(Int, @splitmix.RandomState) -> Self + arbitrary(Int, &@random.Source) -> Self } ///| @@ -124,13 +124,38 @@ pub impl[X : Arbitrary] Arbitrary for Iter[X] with arbitrary(size, rs) { } ///| -pub fn[T : Arbitrary] gen(size? : Int, state? : @splitmix.RandomState) -> T { +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, fn(x) { X::arbitrary(x, rs) }) +} + +///| +pub impl[X : Arbitrary] Arbitrary for FixedArray[X] with arbitrary(size, rs) { + let len = if size == 0 { 0 } else { rs.next_positive_int() % size } + FixedArray::makei(len, fn(x) { X::arbitrary(x, rs) }) +} + +///| +pub impl[A : Arbitrary] Arbitrary for @array.View[A] with arbitrary(size, rs) { + (Arbitrary::arbitrary(size, rs) : Array[A])[:] +} + +///| +pub impl Arbitrary for @bigint.BigInt with arbitrary(size, rs) { + let len = if size == 0 { 0 } else { rs.next_positive_int() % size } + rs.next_bigint(len) +} + +///| +pub fn[T : Arbitrary] gen(size? : Int, state? : &@random.Source) -> T { let size = match size { None => 0 Some(x) => x } let state = match state { - None => @splitmix.RandomState::default() + None => { + @splitmix.RandomState::default() as &@random.Source + } Some(x) => x } Arbitrary::arbitrary(size, state) diff --git a/quickcheck/moon.pkg.json b/quickcheck/moon.pkg.json index e61bbe447..9d75a9104 100644 --- a/quickcheck/moon.pkg.json +++ b/quickcheck/moon.pkg.json @@ -1,14 +1,15 @@ { "import": [ "moonbitlang/core/quickcheck/splitmix", + "moonbitlang/core/array", "moonbitlang/core/builtin", - "moonbitlang/core/char" + "moonbitlang/core/char", + "moonbitlang/core/random", + "moonbitlang/core/bigint" ], "test-import": [ - "moonbitlang/core/array", - "moonbitlang/core/bigint", "moonbitlang/core/float", "moonbitlang/core/double", "moonbitlang/core/tuple" ] -} +} \ No newline at end of file diff --git a/quickcheck/splitmix/moon.pkg.json b/quickcheck/splitmix/moon.pkg.json index 56cd56a09..33a63dce2 100644 --- a/quickcheck/splitmix/moon.pkg.json +++ b/quickcheck/splitmix/moon.pkg.json @@ -1,3 +1,6 @@ { - "import": ["moonbitlang/core/builtin"] + "import": [ + "moonbitlang/core/builtin", + "moonbitlang/core/random" + ] } diff --git a/quickcheck/splitmix/random.mbt b/quickcheck/splitmix/random.mbt index 1bd981613..2f03d0570 100644 --- a/quickcheck/splitmix/random.mbt +++ b/quickcheck/splitmix/random.mbt @@ -18,6 +18,13 @@ struct RandomState { gamma : UInt64 } derive(Show, Default) +///| +pub impl @random.Source for RandomState with next(self) { + let { seed, gamma } = self + self.seed = seed + gamma + mix64(self.seed) +} + ///| let golden_gamma : UInt64 = 0x9e3779b97f4a7c15 diff --git a/random/source.mbt b/random/source.mbt new file mode 100644 index 000000000..8e5d34e5b --- /dev/null +++ b/random/source.mbt @@ -0,0 +1,51 @@ +///| +pub fn next_uint64(self : &Source) -> UInt64 { + Rand(self).uint64() +} + +///| +pub fn next_uint(self : &Source) -> UInt { + Rand(self).uint() +} + +///| +pub fn next_int64(self : &Source) -> Int64 { + Rand(self).uint64().reinterpret_as_int64() +} + +///| +pub fn next_two_uint(self : &Source) -> (UInt, UInt) { + let u1 = Rand(self).uint() + let u2 = Rand(self).uint() + (u1, u2) +} + +///| +pub fn next_int(self : &Source) -> Int { + Rand(self).uint().reinterpret_as_int() +} + +///| +pub fn next_positive_int(self : &Source) -> Int { + Rand(self).int() +} + +///| +pub fn next_float(self : &Source) -> Float { + Rand(self).float() +} + +///| +pub fn next_double(self : &Source) -> Double { + Rand(self).double() +} + +///| +pub fn next_bigint(self : &Source, bits : Int) -> @bigint.BigInt { + Rand(self).bigint(bits) +} + +///| +pub fn split(self : &Source) -> &Source { + self +} diff --git a/tuple/moon.pkg.json b/tuple/moon.pkg.json index b52641119..113b29577 100644 --- a/tuple/moon.pkg.json +++ b/tuple/moon.pkg.json @@ -2,7 +2,7 @@ "import": [ "moonbitlang/core/builtin", "moonbitlang/core/quickcheck", - "moonbitlang/core/quickcheck/splitmix" + "moonbitlang/core/random" ], "test-import": [ "moonbitlang/core/char"