File: int-inf.rep5.sml

package info (click to toggle)
mlton 20210117%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,464 kB
  • sloc: ansic: 27,682; sh: 4,455; asm: 3,569; lisp: 2,879; makefile: 2,347; perl: 1,169; python: 191; pascal: 68; javascript: 7
file content (83 lines) | stat: -rw-r--r-- 2,323 bytes parent folder | download | duplicates (3)
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
structure II = IntInf
structure MII = MLton.IntInf

structure BigWord = MLton.IntInf.BigWord
structure SmallInt = MLton.IntInf.SmallInt
datatype rep = datatype MLton.IntInf.rep
val toRep = MLton.IntInf.rep
val fromRep = MLton.IntInf.fromRep

fun checkToFrom ii =
  let
     fun bug str =
        print (concat ["checkToFrom ",
                       IntInf.toString ii,
                       " => ",
                       str,
                       "\n"])
     val r = toRep ii
     val () =
        if false
           then print (concat ["toRep ", IntInf.toString ii, " = ",
                               case r of
                                  Big _ => "Big\n"
                                | Small _ => "Small\n"])
        else ()
  in
     case fromRep r of
        NONE => bug "(isSome (fromRep r)) failed"
      | SOME ii' =>
           if ii = ii'
              then if r = toRep ii'
                      then ()
                   else bug "(r = (toRep ii')) failed"
           else bug "(ii = ii') failed"
  end

fun loop l =
   case l of
      nil => ()
    | (lo,hi)::l =>
         let
            fun iloop ii =
               if ii <= hi
                  then (checkToFrom ii; iloop (ii + 1))
               else loop l
         in
            iloop lo
         end

val tests =
   let
      val op + = SmallInt.+
      val op - = SmallInt.-
      val op div = SmallInt.div
      val two = SmallInt.fromInt 2
      val thirtytwo = SmallInt.fromInt 32
      val sixtyfour = thirtytwo + thirtytwo

      val min = valOf SmallInt.minInt
      val hmin = min div two
      val max = valOf SmallInt.maxInt
      val hmax = max div two
   in
      [(SmallInt.toLarge min, SmallInt.toLarge (min + sixtyfour)),
       (SmallInt.toLarge (hmin - thirtytwo), SmallInt.toLarge (hmin + thirtytwo)),
       (SmallInt.toLarge (hmax - thirtytwo), SmallInt.toLarge (hmax + thirtytwo)),
       (SmallInt.toLarge (max - sixtyfour), SmallInt.toLarge max)]
   end
   @
   let
      val prec = valOf SmallInt.precision
      val min = ~ (IntInf.pow (2, prec - 1))
      val hmin = min div 2
      val max = IntInf.pow (2, prec - 1) - 1
      val hmax = max div 2
   in
      [(min, (min + 64)),
       ((hmin - 32), (hmin + 32)),
       ((hmax - 32), (hmax + 32)),
       ((max - 64), max)]
   end

val () = loop tests