test-check

nwjsmith 2017-10-14T16:28:47.000016Z

I have a function that takes as input a large set of uniformly distributed random numbers and a directed, acyclic graph. It returns a random topological ordering of the graph. I’d like to hook this into test.check such that if I run test.check/quick-check twice with the same seed parameter then the same topological orderings will be generated.

nwjsmith 2017-10-14T16:29:30.000052Z

As I understand it, test.check’s generators and combinators are designed to keep me from having to see the underlying RNG

nwjsmith 2017-10-14T16:30:57.000077Z

and that’s worked really well until now, when I need a lot of uniformly distributed random numbers

nwjsmith 2017-10-14T16:32:22.000006Z

Is the test.check.generators/->Generator function considered part of the public API?

2017-10-14T16:33:30.000077Z

@nwjsmith so if you had a generator for uniformly distributed random numbers, would that solve it?

nwjsmith 2017-10-14T16:36:22.000062Z

@gfredericks yes, that would work beautifully

nwjsmith 2017-10-14T16:37:31.000111Z

Then I could do a (gen/vector (gen/rand-int max) num-elements) and fmap my random-topological-ordering over it

nwjsmith 2017-10-14T16:55:10.000001Z

(def gen-rand-double
  (gen/no-shrink
   (gen/->Generator
    (fn [rnd _size]
      (rose/pure (random/rand-double rnd))))))
?

nwjsmith 2017-10-14T16:58:05.000085Z

Ah, there’s random/rand-long.

(def gen-rand-long
  (gen/no-shrink
   (gen/->Generator
    (fn [rnd _size]
      (rose/pure (random/rand-long rnd))))))

(gen/generate (gen/vector gen-rand-long) 10 100) ;; => [6200754525179398429 -3542882520031353397]
(gen/generate (gen/vector gen-rand-long) 10 100) ;; => [6200754525179398429 -3542882520031353397]

nwjsmith 2017-10-14T16:58:35.000005Z

That seems to do the trick

nwjsmith 2017-10-14T17:01:15.000019Z

I’m not really sure what the effects of no-shrink are here, or what rose/pure is doing. I’ll have to watch you’re old talk again to brush up on the internals.

nwjsmith 2017-10-14T17:02:17.000048Z

Your talk from this week was fantastic by the way, really good guide to building custom generators.

2017-10-14T17:04:01.000043Z

(also, why do they have to be uniformly distributed?)

nwjsmith 2017-10-14T17:06:07.000087Z

the algorithm is on page 2. I’ve got to generate a large number of random swaps of a topological ordering.

nwjsmith 2017-10-14T17:06:19.000090Z

(the bottom of page 2)

nwjsmith 2017-10-14T17:07:46.000060Z

At first I tried gen/choose, but I ended up with really bad distributions on the generator.

2017-10-14T17:09:02.000107Z

Choose is uniform, unlike most other builtin generators

nwjsmith 2017-10-14T17:09:35.000050Z

Hrm. Maybe I just thought I’d end up with really bad distributions

nwjsmith 2017-10-14T17:09:50.000027Z

I’ll try choose again.

2017-10-14T17:09:53.000065Z

no-shrink is a noop in your code I believe

2017-10-14T17:11:12.000084Z

You need uniform ints or doubles?

nwjsmith 2017-10-14T17:11:23.000023Z

ints

2017-10-14T17:11:32.000144Z

What range?

nwjsmith 2017-10-14T17:12:46.000031Z

between 0 and 2n-1, where n is the number of nodes in the graph.

2017-10-14T17:13:05.000056Z

Oooh, yeah, choose is bad for large n

2017-10-14T17:14:23.000070Z

Foolproof is (gen/vector (gen/choose 0 1) n), then fmap the bits to a bigint

2017-10-14T17:15:41.000015Z

But even if the algorithm demands uniform, I'm curious if testing with a nonuniform generator would actually be bad

2017-10-14T17:19:36.000071Z

But either way, there's no builtin bigint generator yet, which makes it harder for you :(

nwjsmith 2017-10-14T17:23:27.000087Z

Putting it together with the approach you suggested should work though. I’m going to experiment a bit with all of the approaches above. Will report back.

nwjsmith 2017-10-14T17:23:56.000029Z

BTW do you have a strategy for testing generators? Specifically how they shrink, their distribution, performance etc.

nwjsmith 2017-10-14T17:26:12.000013Z

Experimenting at the REPL has been working for me, but now I have all of these useful graph-related generators and I’m hoping to open source them. Might have to come up with my own strategy.

2017-10-14T17:41:37.000006Z

No, that's an interesting idea. You can see examples in the tests of testing distribution & shrinking a bit, but it's all ad-hoc

2017-10-14T17:41:48.000049Z

Shrinking is particularly weird to test

2017-10-14T17:42:01.000086Z

Maybe...