(.require [library [lux (.except) [abstract [monad (.only do)] [\\specification ["$[0]" equivalence] ["$[0]" codec]]] [control ["[0]" try] ["[0]" exception]] [data ["[0]" binary (.only Binary)] [text ["%" \\format (.only format)]]] [math ["[0]" random (.only Random)] [number ["n" nat]]] [test ["_" property (.only Test)]]]] [\\program ["[0]" /]] [test [lux [data ["_[0]" binary]]]]) (def .public (random hash) (All (_ h) (-> (-> Binary (/.Hash h)) (Random (/.Hash h)))) (do [! random.monad] [size (at ! each (n.% 100) random.nat)] (at ! each hash (_binary.random size)))) (def .public test Test (<| (_.covering /._) (_.for [/.Hash /.SHA-1 /.MD5]) (`` (all _.and (_.for [/.equivalence] (all _.and ($equivalence.spec /.equivalence (..random /.sha-1)) ($equivalence.spec /.equivalence (..random /.md5)) )) (_.for [/.data] (all _.and (,, (with_template [ ] [(do random.monad [expected (..random )] (_.coverage [ ] (and (when ( (/.data expected)) {try.#Success actual} (at /.equivalence = expected actual) {try.#Failure error} false) (when ( (at binary.monoid composite (/.data expected) (/.data expected))) {try.#Success actual} false {try.#Failure error} (exception.match? error)))))] [/.sha-1 /.as_sha-1 /.not_a_sha-1] [/.md5 /.as_md5 /.not_a_md5] )))) (,, (with_template [ ] [(_.for [] ($codec.spec /.equivalence (..random )))] [/.sha-1_codec /.sha-1] [/.md5_codec /.md5] )) (_.for [/.not_a_hash] (all _.and (,, (with_template [ ] [(do random.monad [expected (..random )] (_.coverage [] (when (at decoded (format (at encoded expected) "AABBCC")) {try.#Success actual} false {try.#Failure error} (exception.match? /.not_a_hash error))))] [/.sha-1_codec /.sha-1] [/.md5_codec /.md5] )))) ))))