Invariants

Invariant properties are those which should never change in the context of the operation. For example, sorting the contents of the string or changing the case of it should never change its length. The next property demonstrates that it holds for normal as well as for uppercase strings:

scala> forAll((a: String) => a.sorted.length == a.length).check
+ OK, passed 100 tests.
scala> forAll((a: String) => a.toUpperCase().length == a.length).check
! Falsified after 50 passed tests.
> ARG_0:
> ARG_0_ORIGINAL:

Or well, for toUpperCase at least it should work if the locale matches the contents of the string or the string only contains ASCII symbols:

scala> forAll(Gen.asciiStr)((a: String) => a.toUpperCase().length == a.length).check
+ OK, passed 100 tests.

Here we went a bit ahead of ourselves and used Gen.asciiStr to generate strings which only contain ASCII chars.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.30.236