It might be nice to add something like: `public fu...
# opensavvy
d
It might be nice to add something like:
public fun <T> Array<out T>.random(random: Random): T
for Prepared's random... I don't know if this is good practice, but currently I just did:
FooEnum.entries.toTypedArray().random(random.accessUnsafe())
c
What does this do? Is it to get a random element of an array? In which case did you want to use this?
d
Making test data in a prepared block... I guess this is getting close to property testing, but I still feel there's a difference, since I'm not generating tons of random test cases but rather expressing that I don't care what the value of that property is for that one test.
c
Since it's an enum, wouldn't it be better to brute-force all cases?
Anyone you can write:
Copy code
suspend fun <T> TestDsl.randomOf(array: Array<T>) =
    array.random(random.accessUnsafe())
but I can't do anything much better before context parameters
d
Since it's an enum, wouldn't it be better to brute-force all cases?
In this case it doesn't involve any logic... so I just randomize it so that my hard-coded TDD first test won't pass...
Anyone you can write: ...
Yeah, I was wondering if accessUnsafe() was safe over here. And yeah... I guess until context params come out things are less pleasant... I didn't think of that that you needed the TestDsl context too 🤷🏼‍♂️.
c
The "unsafe" part here is about multi-threading
but honestly it's a bit weird anyway because, even if it is supposedly safe to use with multiple threads, you'll get non-deterministic results…
I didn't think of that that you needed the TestDsl context too
Guess what!
random
is a prepared value internally 😅
👍🏼 1
d
😮
Copy code
val TestDsl.random: Random
	get() = Random(this)
It creates a new instance for each call to random?
the actual random source is in a prepared value
d
Yeah, I understood, but why a new instance of even that?
It seems to be a stateless wrapper...
c
Where could I store it?
d
Maybe a value class?
c
(it is a stateless wrapper, yes;
time
and other helpers do the same)
Where would that value class be stored? I can't make this a global variable, since it must be different for each test.
d
If what you pass to the constructor is stored, then "creating" the value class is "free" so it doesn't need to be stored...
c
Sure, but this is a stateless helper, escape analysis will remove it anyway. And that wouldn't work for
time
since it takes 2 arguments
d
escape analysis will remove it anyway
I guess if that's so, then it's not a problem... I always tried making such wrappers value classes, but I'm not such an expert on compiler optimisations... interesting to know this...!
c
I don't know about other platforms, but at the very least on the JVM, such extremely short-lived objects are basically free (generational GCs for the win!). It's the same as
java.lang.Optional
👍🏼 1