scala - Why is my Scalacheck test with a custom Generator failing after discarding many cases, and how do I fix this? -


i newbie scala , writing first scalacheck suite.

i have data structure in program looks (list[double], list[double]) well-formed if each element of _1 strictly greater corresponding element of _2.

since it's more complicated in practice (although purpose of mwe can pretend there's there it), i've written custom generator it.

i added 2 trivial tests (including trivial of all, 1 == 1) , in both cases tests fail, messagegave after xx passed tests. yyy tests discarded.

why it, , how fix it?

attached test suite , output.


package com.foo.bar  import org.scalacheck._ import prop._ import arbitrary._  object foospecification extends properties("foointervals") {    type foointervals = (list[double], list[double])    /* supposed tuple of lists s.t. each element of _1    *  < corresponding element of _2    *     *  e.g. (list(1,3,5), list(2,4,6))    */    implicit def arbinterval : arbitrary[foointervals] =     arbitrary {       /**         * yields pair (low, high) s.t. low < high         */       def genpair : gen[(double, double)] = {         low <- arbitrary[double]         high <- arbitrary[double].suchthat(_ > low)       } yield (low, high)        /**         * yields (list(x_1,...,x_n), list(y_1,...,y_n))         * x_i < y_i forall , 1 <= n < 20         */       {         n <- gen.choose(1,20)         pairs : list[(double, double)] <- gen.containerofn[list, (double, double)](n, genpair)       } yield ((pairs.unzip._1, pairs.unzip._2))     }    property("1 == 1") = forall {     (b1: foointervals)     =>     1 == 1   }    property("_1.head < _2.head") = forall {     (b1: foointervals)     =>     b1._1.head < b1._2.head   } } 

[info] ! foointervals.1 == 1: gave after 32 passed tests. 501 tests discarded. [info] ! foointervals._1.head < _2.head: gave after 28 passed tests. 501 tests discarded. [info] scalatest [info] run completed in 1 second, 519 milliseconds. [info] total number of tests run: 0 [info] suites: completed 0, aborted 0 [info] tests: succeeded 0, failed 0, canceled 0, ignored 0, pending 0 [info] no tests executed. [error] failed: total 1, failed 1, errors 0, passed 0 [error] failed tests: [error]     com.foo.bar.foospecification 

arbitrary[double].suchthat(_ > low) problem. suchthat discard cases condition false. taking 2 random values , discarding cases 1 of values greater other lot. can use retryuntil instead of suchthat generate new values until condition satisfied instead of discarding values, has downside of potentially taking long time or looping forever if condition unlikely (imagine if got high value low, might loop long time high greater it, or forever if unlucky enough have maximum possible double value low.

what work gen.choose(low, double.maxvalue) select value between low , double.maxvalue (the biggest possible double).

using methods choose or oneofand others restrict generator select values want better generating possible arbitrary value , discarding or retrying invalid cases. should done if there few cases don't fit criteria compared total possibilities, , valid cases aren't defined using methods.


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -