~james/core

core/sets/gen.go -rw-r--r-- 409 bytes
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package sets

import (
	"math/rand"
	"reflect"
	"testing/quick"
)

func (_ Set[T]) Generate(rand *rand.Rand, size int) reflect.Value {
	s := New[T]()

	var zero T
	itemType := reflect.TypeOf(zero)

	for {
		if s.Len() >= size {
			break
		}

		item, ok := quick.Value(itemType, rand)
		if !ok {
			continue
		}

		if val, ok := item.Interface().(T); ok {
			s.Insert(val)
		}
	}

	return reflect.ValueOf(s)
}