summaryrefslogtreecommitdiff
path: root/src/mosmllib/test/packreallittle.sml
blob: 3bd29ef45c01e70fd9a378f189a3a9ce942456ae (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
use "auxil.sml";
load "PackRealLittle";
local
    structure P = PackRealLittle
    fun hex w = StringCvt.padLeft #"0" 2 (Word8.fmt StringCvt.HEX w)
    fun pow n i =
        let fun loop 0 acc = acc
              | loop i acc = loop (i-1) (n * acc)
        in  loop i 1
        end
in
 
fun roundtrip r =
    let val packed = P.toBytes r
        val endian = if P.isBigEndian then "(big endian)\n"
                     else "(little endian)\n"
    in  app print ["  ", Real.fmt (StringCvt.FIX NONE) r, " is enconded as "]
      ; Word8Vector.app (fn w => app print [hex w, " "]) packed
      ; print endian
      ; Real.== (r, P.fromBytes packed)
    end
 
val test_roundtrip =
    check'(fn _ =>
              List.all roundtrip
                       [~420.0,
                        ~1.1,
                        ~0.123456789,
                        ~0.0,
                        0.0,
                        1.0,
                        1.1,
                        2.0,
                        420E6,
                        real (valOf Int.maxInt),
                        real (pow 2 53 - 1),
                        real (pow 2 53),
                        real (pow 2 53 + 1),
                        Math.pi,
                        Math.e]);
 
val encoded =
    [0wx40, 0wx09, 0wx21, 0wxFB, 0wx54, 0wx44, 0wx2D, 0wx18,
     0wx40, 0wx09, 0wx21, 0wxFB, 0wx54, 0wx44, 0wx2D, 0wx18]
 
val test_subvec =
    let val encoded = Word8Vector.fromList encoded
    in  check'(fn _ => Real.== (P.subVec(encoded, 0),
                                P.subVec(encoded, 1)))
    end;
 
val test_subarr =
    let val encoded = Word8Array.fromList encoded
    in  check'(fn _ => Real.== (P.subArr(encoded, 0),
                                P.subArr(encoded, 1)))
    end;
 
val test_update0 =
    let val encoded = Word8Array.fromList encoded
    in  check'(fn _ =>
                  ( P.update(encoded, 0, 0.0)
                  ; Real.== (P.subArr(encoded, 0),
                             0.0)
                  ))
    end;
 
val test_update1 =
    let val encoded = Word8Array.fromList encoded
    in  check'(fn _ =>
                  ( P.update(encoded, 1, 1.0)
                  ; Real.== (P.subArr(encoded, 1),
                             1.0)
                  ))
    end;
end