aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/aedifex/hash.lux
blob: e91b79e1f5f17d3b383363487ff594a26fa094a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
(.module:
  [library
   [lux #*
    ["_" test (#+ Test)]
    [abstract
     [monad (#+ do)]
     [\\specification
      ["$." equivalence]
      ["$." codec]]]
    [control
     ["." try]
     ["." exception]]
    [data
     ["." binary (#+ Binary)]
     [text
      ["%" format (#+ format)]]]
    [math
     ["." random (#+ Random)]
     [number
      ["n" nat]]]]]
  [\\program
   ["." /]]
  [test
   [lux
    [data
     ["_." binary]]]])

(def: .public (random hash)
  (All [h]
    (-> (-> Binary (/.Hash h))
        (Random (/.Hash h))))
  (do {! random.monad}
    [size (\ ! each (n.% 100) random.nat)]
    (\ ! each hash (_binary.random size))))

(def: .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Hash /.SHA-1 /.MD5])
      (`` ($_ _.and
              (_.for [/.equivalence]
                     ($_ _.and
                         ($equivalence.spec /.equivalence (..random /.sha-1))
                         ($equivalence.spec /.equivalence (..random /.md5))
                         ))
              (_.for [/.data]
                     ($_ _.and
                         (~~ (template [<hash> <constructor> <exception>]
                               [(do random.monad
                                  [expected (..random <hash>)]
                                  (_.cover [<hash> <constructor> <exception>]
                                           (and (case (<constructor> (/.data expected))
                                                  (#try.Success actual)
                                                  (\ /.equivalence = expected actual)

                                                  (#try.Failure error)
                                                  false)
                                                (case (<constructor> (\ binary.monoid composite
                                                                        (/.data expected)
                                                                        (/.data expected)))
                                                  (#try.Success actual)
                                                  false

                                                  (#try.Failure error)
                                                  (exception.match? <exception> error)))))]

                               [/.sha-1 /.as_sha-1 /.not_a_sha-1]
                               [/.md5 /.as_md5 /.not_a_md5]
                               ))))
              (~~ (template [<codec> <hash>]
                    [(_.for [<codec>]
                            ($codec.spec /.equivalence <codec> (..random <hash>)))]
                    
                    [/.sha-1_codec /.sha-1]
                    [/.md5_codec /.md5]
                    ))
              (_.for [/.not_a_hash]
                     ($_ _.and
                         (~~ (template [<codec> <hash>]
                               [(do random.monad
                                  [expected (..random <hash>)]
                                  (_.cover [<codec>]
                                           (case (\ <codec> decoded
                                                    (format (\ <codec> encoded expected)
                                                            "AABBCC"))
                                             (#try.Success actual)
                                             false

                                             (#try.Failure error)
                                             (exception.match? /.not_a_hash error))))]

                               [/.sha-1_codec /.sha-1]
                               [/.md5_codec /.md5]
                               ))))
              ))))