(.module: [lux #* ["_" test (#+ Test)] [abstract [monad (#+ do)] {[0 #spec] [/ ["$." equivalence] ["$." codec]]}] [control ["." try] ["." exception]] [data ["." binary (#+ Binary)] [number ["n" nat]] [text ["%" format (#+ format)]]] [math ["." random (#+ Random)]]] {#program ["." /]} [test [lux [data ["_." binary]]]]) (def: (random hash) (All [h] (-> (-> Binary (/.Hash h)) (Random (/.Hash h)))) (do {! random.monad} [size (\ ! map (n.% 100) random.nat)] (\ ! map hash (_binary.random size)))) (def: #export test Test (<| (_.covering /._) (_.with-cover [/.Hash /.SHA-1 /.MD5]) (`` ($_ _.and (_.with-cover [/.equivalence] ($_ _.and ($equivalence.spec /.equivalence (..random /.sha-1)) ($equivalence.spec /.equivalence (..random /.md5)) )) (_.with-cover [/.data] ($_ _.and (~~ (template [ ] [(do random.monad [expected (..random )] (_.cover [ ] (and (case ( (/.data expected)) (#try.Success actual) (\ /.equivalence = expected actual) (#try.Failure error) false) (case ( (\ binary.monoid compose (/.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] )))) (~~ (template [ ] [(_.with-cover [] ($codec.spec /.equivalence (..random )))] [/.sha-1-codec /.sha-1] [/.md5-codec /.md5] )) (_.with-cover [/.not-a-hash] ($_ _.and (~~ (template [ ] [(do random.monad [expected (..random )] (_.cover [] (case (\ decode (format (\ encode expected) "AABBCC")) (#try.Success actual) false (#try.Failure error) (exception.match? /.not-a-hash error))))] [/.sha-1-codec /.sha-1] [/.md5-codec /.md5] )))) ))))