summaryrefslogtreecommitdiffstats
path: root/tests/016/arith.tl
blob: 52e8667b1749b7bd1442ed9bdf83de8426c8a24d (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
(load "../common.tl")

(for ((i 0) (j 1))
     ((< i 512))
     ((inc i) (inc j j))
  (let ((k (expt 2 i)))
    (vtest j k)))

(for ((h 0) (i 1) (j 1) (c 0))
     ((< c 100))
     ((inc c)
      (pset h i
            i j
            j (+ i j)))
  (vtest h (- j i)))

(defvarl p100 '(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79
                83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163
                167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251
                257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349
                353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443
                449 457 461 463 467 479 487 491 499 503 509 521 523 541))

(vtest [reduce-left * p100] (lcm . p100))

(for ((prod [reduce-left * p100])
      (l100 p100))
     (l100)
     ((set prod (trunc prod (pop l100))))
  (vtest prod (lcm . l100)))

(each ((i (range 0 (length p100))))
  (let* ((a [apply * [p100 0..i]])
         (b [apply * [p100 i..:]])
         (c (* a b)))
    (vtest (trunc c a) b)
    (vtest (trunc c b) a)
    (vtest (mod c a) 0)
    (vtest (mod c b) 0)
    (vtest (mod (pred c) a) (pred a))
    (vtest (mod (pred c) b) (pred b))))

(test (digits 0) (0))
(test (digits 1) (1))
(test (digits 9) (9))
(test (digits 10) (1 0))
(test (digits 19) (1 9))
(test (digits 20) (2 0))
(test (digits 37) (3 7))
(test (digits 100) (1 0 0))
(test (digits 101) (1 0 1))
(test (digits 999) (9 9 9))
(test (digits 1000) (1 0 0 0))
(test (digits 0 2) (0))
(test (digits 1 2) (1))
(test (digits 2 2) (1 0))
(test (digits 3 2) (1 1))
(test (digits 7 2) (1 1 1))
(test (digits 8 2) (1 0 0 0))

(test (bracket 0 10 20 30) 0)
(test (bracket 9 10 20 30) 0)
(test (bracket 10 10 20 30) 1)
(test (bracket 15 10 20 30) 1)
(test (bracket 25 10 20 30) 2)
(test (bracket 30 10 20 30) 3)

(test [apply bracket '(0 10 20 30)] 0)
(test [apply bracket '(9 10 20 30)] 0)
(test [apply bracket '(10 10 20 30)] 1)
(test [apply bracket '(15 10 20 30)] 1)
(test [apply bracket '(25 10 20 30)] 2)
(test [apply bracket '(30 10 20 30)] 3)

(test (typeof fixnum-max) fixnum)
(test (typeof (succ fixnum-max)) bignum)
(test (typeof fixnum-min) fixnum)
(test (typeof (pred fixnum-min)) bignum)

(test (< fixnum-min fixnum-max) t)
(test (< (pred fixnum-min) fixnum-min) t)
(test (> (succ fixnum-max) fixnum-max) t)

(test (ffi-put #xA5 (ffi le-int16))
      #b'A500')
(test (ffi-put #xA5 (ffi be-int16))
      #b'00A5')
(test (mequal (ffi-put #xA5 (ffi int16))
              #b'A500'
              #b'00A5') t)

(test (ffi-put #xAABBCC (ffi le-int32))
      #b'CCBBAA00')
(test (ffi-put #xAABBCC (ffi be-int32))
      #b'00AABBCC')
(test (mequal (ffi-put #xAABBCC (ffi int32))
              #b'CCBBAA00'
              #b'00AABBCC') t)

(test (ffi-put #xAABBCCDDEE (ffi le-int64))
      #b'EEDDCCBBAA000000')
(test (ffi-put #xAABBCCDDEE (ffi be-int64))
      #b'000000AABBCCDDEE')
(test (mequal (ffi-put #xAABBCCDDEE (ffi int64))
              #b'EEDDCCBBAA000000'
              #b'000000AABBCCDDEE') t)

(test (ffi-get #b'A500' (ffi le-int16))
      #xA5)
(test (ffi-get #b'00A5' (ffi be-int16))
      #xA5)


(test (ffi-get #b'CCBBAA00' (ffi le-int32))
      #xAABBCC)
(test (ffi-get #b'00AABBCC' (ffi be-int32))
      #xAABBCC)


(test (ffi-get #b'EEDDCCBBAA000000' (ffi le-int64))
      #xAABBCCDDEE)
(test (ffi-get #b'000000AABBCCDDEE' (ffi be-int64))
      #xAABBCCDDEE)

(test (mequal (ffi-put #x-8000 (ffi int16))
              #b'0080'
              #b'8000') t)

(test (ffi-get (ffi-put #x-8000 (ffi int16)) (ffi int16))
      #x-8000)

(test (mequal (ffi-put #x-80000000 (ffi int32))
              #b'00000080'
              #b'80000000') t)

(test (ffi-get (ffi-put #x-80000000 (ffi int32)) (ffi int32))
      #x-80000000)

(test (mequal (ffi-put #x-8000000000000000 (ffi int64))
              #b'0000000000000080'
              #b'8000000000000000') t)

(test (ffi-get (ffi-put #x-8000000000000000 (ffi int64)) (ffi int64))
      #x-8000000000000000)

(mtest
  (sum #()) 0
  (sum #(1)) 1
  (sum #(1 2)) 3
  (sum #(1 2 3)) 6
  (sum #() (op * 10)) 0
  (sum #(1) (op * 10)) 10
  (sum #(1 2) (op * 10)) 30
  (sum #(1 2 3) (op * 10)) 60
  (sum 1..10) 45
  (sum 2..10) 44)

(mtest
  (prod #()) 1
  (prod #(1)) 1
  (prod #(1 2)) 2
  (prod #(1 2 3)) 6
  (prod #() (op * 10)) 1
  (prod #(1) (op * 10)) 10
  (prod #(1 2) (op * 10)) 200
  (prod #(1 2 3) (op * 10)) 6000
  (prod 2..8) 5040
  (prod 3..8) 2520)