3 // Copyright 2021 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
13 type Gen[A any] func() (A, bool)
15 func Combine[T1, T2, T any](g1 Gen[T1], g2 Gen[T2], join func(T1, T2) T) Gen[T] {
16 return func() (T, bool) {
26 return join(t1, t2), true
30 type Pair[A, B any] struct {
35 func _NewPair[A, B any](a A, b B) Pair[A, B] {
36 return Pair[A, B]{a, b}
39 func Combine2[A, B any](ga Gen[A], gb Gen[B]) Gen[Pair[A, B]] {
40 return Combine(ga, gb, _NewPair[A, B])
44 var g1 Gen[int] = func() (int, bool) { return 3, true }
45 var g2 Gen[string] = func() (string, bool) { return "x", false }
46 var g3 Gen[string] = func() (string, bool) { return "y", true }
48 gc := Combine(g1, g2, _NewPair[int, string])
49 if got, ok := gc(); ok {
50 panic(fmt.Sprintf("got %v, %v, wanted -/false", got, ok))
52 gc2 := Combine2(g1, g2)
53 if got, ok := gc2(); ok {
54 panic(fmt.Sprintf("got %v, %v, wanted -/false", got, ok))
57 gc3 := Combine(g1, g3, _NewPair[int, string])
58 if got, ok := gc3(); !ok || got.A != 3 || got.B != "y" {
59 panic(fmt.Sprintf("got %v, %v, wanted {3, y}, true", got, ok))
61 gc4 := Combine2(g1, g3)
62 if got, ok := gc4(); !ok || got.A != 3 || got.B != "y" {
63 panic(fmt.Sprintf("got %v, %v, wanted {3, y}, true", got, ok))