aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/time/year.lux
blob: b43fb99eb7142e462964d8c42e634963a1cd14ff (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
96
97
(.module:
  [library
   [lux #*
    ["_" test (#+ Test)]
    [abstract
     [monad (#+ do)]
     [\\specification
      ["$." equivalence]
      ["$." order]
      ["$." codec]]]
    [control
     ["." try]
     ["." exception]]
    [data
     ["." bit ("#\." equivalence)]
     [text
      ["%" format (#+ format)]]]
    [math
     ["." random (#+ Random)]
     [number
      ["n" nat]
      ["i" int]]]]]
  [\\library
   ["." /
    ["/#" //
     ["#." duration]
     ["#." instant]
     ["#." date]]]])

(def: .public random
  (Random /.Year)
  (random.one (|>> /.year try.maybe) random.int))

(def: .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Year])
      ($_ _.and
          (_.for [/.equivalence]
                 ($equivalence.spec /.equivalence ..random))
          (_.for [/.order]
                 ($order.spec /.order ..random))
          (_.for [/.codec /.parser]
                 ($codec.spec /.equivalence /.codec ..random))
          
          (do random.monad
            [expected random.int]
            ($_ _.and
                (_.cover [/.year]
                         (bit\= (i.= +0 expected)
                                (case (/.year expected)
                                  (#try.Success _)
                                  false

                                  (#try.Failure _)
                                  true)))
                (_.cover [/.value]
                         (case (/.year expected)
                           (#try.Success year)
                           (i.= expected (/.value year))

                           (#try.Failure _)
                           (i.= +0 expected)))
                ))
          (_.cover [/.there_is_no_year_0]
                   (case (/.year +0)
                     (#try.Success _)
                     false

                     (#try.Failure error)
                     (exception.match? /.there_is_no_year_0 error)))
          (_.cover [/.days]
                   (n.= (.nat (//duration.ticks //duration.day //duration.normal_year))
                        /.days))
          (_.cover [/.epoch]
                   (\ /.equivalence =
                      (//date.year (//instant.date //instant.epoch))
                      /.epoch))
          (_.for [/.Period]
                 (_.cover [/.leap /.century /.era]
                          (n.= /.leap (n./ /.century /.era))))
          (let [leap (try.trusted (/.year (.int /.leap)))
                century (try.trusted (/.year (.int /.century)))
                era (try.trusted (/.year (.int /.era)))]
            ($_ _.and
                (_.cover [/.leap?]
                         (and (/.leap? leap)
                              (not (/.leap? century))
                              (/.leap? era)))
                (_.cover [/.leaps]
                         (and (i.= +1 (/.leaps leap))
                              (i.= (.int (n./ /.leap /.century))
                                   (/.leaps century))
                              (i.= (++ (i.* +4 (-- (/.leaps century))))
                                   (/.leaps era))))
                ))
          )))