aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/aedifex/hash.lux
blob: 745ec091093e0e01571763fa19adfa72a21352eb (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:
  [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 [<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 compose
                                                                    (/.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>]
                    [(_.with-cover [<codec>]
                       ($codec.spec /.equivalence <codec> (..random <hash>)))]
                    
                    [/.sha-1-codec /.sha-1]
                    [/.md5-codec /.md5]
                    ))
              (_.with-cover [/.not-a-hash]
                ($_ _.and
                    (~~ (template [<codec> <hash>]
                          [(do random.monad
                             [expected (..random <hash>)]
                             (_.cover [<codec>]
                                      (case (:: <codec> decode
                                                (format (:: <codec> encode expected)
                                                        "AABBCC"))
                                        (#try.Success actual)
                                        false

                                        (#try.Failure error)
                                        (exception.match? /.not-a-hash error))))]

                          [/.sha-1-codec /.sha-1]
                          [/.md5-codec /.md5]
                          ))))
              ))))