File: CaseOf.sml

package info (click to toggle)
smlsharp 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 123,732 kB
  • sloc: ansic: 16,725; sh: 4,347; makefile: 2,191; java: 742; haskell: 493; ruby: 305; cpp: 284; pascal: 256; ml: 255; lisp: 141; asm: 97; sql: 74
file content (572 lines) | stat: -rw-r--r-- 15,332 bytes parent folder | download | duplicates (2)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
structure CaseOf =
struct
open SMLUnit.Test SMLUnit.Assert

  fun testCaseOfInt () =
      let
        val _ = case 1 of
                     1 => ()
                   | _ => fail "NG"

        val _ = case 1 of
                     ~1 => fail "NG"
                   | _ => () 

        val _ = case ~1 of
                     ~1 => ()
                   | _ => fail "NG"

        val _ = case 3 of
                     1 => fail "NG"
                   | 2 => fail "NG"
                   | 3 => ()
                   | _ => fail "NG"

        val _ = case 0 of
                     ~0 => ()
                   | _ => fail "NG"
                   
        val _ = case 127 : int8 of
                     127 => ()
                   | _ => fail "NG"

        val _ = case 32767 : int16 of
                     32767 => ()
                   | _ => fail "NG"

        val _ = case 2147483647 : int32 of
                     2147483647 => ()
                   | _ => fail "NG"

        val _ = case 9223372036854775807 : int64 of
                     9223372036854775807 => ()
                   | _ => fail "NG"

        val _ = case 9223372036854775808 : intInf of
                     9223372036854775808 => ()
                   | _ => fail "NG"
      in
        ()
      end

  fun testCaseOfIntFunction () =
      let
        fun f1 x = case x of
                        1 => ()
                      | _ => fail "NG"
        val v1 = f1 (1 : int)

        fun f2 x = case x of
                        127 => ()
                      | _ => fail "NG"
        val v2 = f2 (127 : int8)

        fun f3 x = case x of
                        32767 => ()
                      | _ => fail "NG"
        val v3 = f3 (32767 : int16)

        fun f4 x = case x of
                        2147483647 => ()
                      | _ => fail "NG"
        val v4 = f4 (2147483647 : int32)

        fun f5 x = case x of
                        9223372036854775807 => ()
                      | _ => fail "NG"
        val v5 = f5 (9223372036854775807 : int64)

        fun f6 x = case x of
                        9223372036854775808 => ()
                      | _ => fail "NG"
        val v6 = f6 (9223372036854775808 : intInf)
      in
        ()
      end

  fun testCaseOfWord () =
      let
        val _ = case 0w1 of
                     0w1 => ()
                   | _ => fail "NG"

        val _ = case 0w1 of
                     0w2 => fail "NG"
                   | _ => ()

        val _ = case 0wxFF : word8 of
                     0w255 => ()
                   | _ => fail "NG"

        val _ = case 0wxFFFF : word16 of
                     0w65535 => ()
                   | _ => fail "NG"

        val _ = case 0wxFFFFFFFF : word32 of
                     0w4294967295 => ()
                   | _ => fail "NG"

        val _ = case 0wxFFFFFFFFFFFFFFFF : word64 of
                     0w18446744073709551615 => ()
                   | _ => fail "NG"
      in
        ()
      end

  fun testCaseOfWordFunction() =
      let
        fun f1 x = case x of
                        0w1 => ()
                      | _ => fail "NG"
        val v1 = f1 (0w1 : word)

        fun f2 x = case x of
                        0w255 => ()
                      | _ => fail "NG"
        val v2 = f2 (0wxFF : word8)

        fun f3 x = case x of
                        0w65535 => ()
                      | _ => fail "NG"
        val v3 = f3 (0wxFFFF : word16)

        fun f4 x = case x of
                        0w4294967295 => ()
                      | _ => fail "NG"
        val v4 = f4 (0wxFFFFFFFF : word32)

        fun f5 x = case x of
                        0w18446744073709551615 => ()
                      | _ => fail "NG"
        val v5 = f5 (0wxFFFFFFFFFFFFFFFF : word64)

      in
        ()
      end

  fun testCaseOfReal () =
      let
        val _ = case 1.0 of
                     n => ()

        val _ = case 1.0 : real32 of
                     n => ()
      in
        ()
      end

  fun testCaseOfRealFunction () =
      let
        fun f1 x = case x + 1.0 of
                        _ => ()
        val v1 = f1 (1.0 : real)

        fun f2 x = case x + 1.0 of
                        _ => ()
        val v2 = f2 (1.0 : real32)
      in
        ()
      end

  fun testCaseOfChar () =
      let
        val _ = case #"A" of
                     #"A" => ()
                   | _ => fail "NG"

        val _ = case #"a" of
                     #"A" => fail "NG"
                   | _ => ()

        val _ = case #"A" of
                     #"\065" => ()
                   | _ => fail "NG"

        val _ = case #"C" of
                     #"A" => fail "NG"
                   | #"B" => fail "NG"
                   | #"C" => ()
                   | _ => fail "NG"
      in
        ()
      end

  fun testCaseOfCharFunction () =
      let
        fun f1 x = case x of
                        #"A" => ()
                      | _ => fail "NG"
        val v1 = f1 #"A"
      in
        ()
      end

  fun testCaseOfString () =
      let
        val _ = case "" of
                     "" => ()
                   | _ => fail "NG"

        val _ = case "ABC" of
                     "ABC" => ()
                   | _ => fail "NG"

        val _ = case "F" of
                     "ABC" => fail "NG"
                   | "DE" => fail "NG"
                   | "F" => ()
                   | _ => fail "NG"

        val _ = case "\255" of
                     "\u00FF" => ()
                   | _ => fail "NG"
      in
        ()
      end

  fun testCaseOfStringFunction() =
      let
        fun f1 x = case x of
                        "ABC" => ()
                      | _ => fail "NG"
        val v1 = f1 "ABC"
      in
        ()
      end

  fun testCaseOfConstructor () =
      let
        datatype dt1 = DT1
                     | DT2
        fun f1 x = case x of
                        DT1 => ()
                      | DT2 => fail "NG"
        val v1 = f1 DT1

        fun f2 x = case x of
                        DT1 => ()
                      | _ => fail "NG"
        val v2 = f2 DT1

        fun f4 x = case x of
                        x as DT1 => x
                      | x : dt1 as DT2 => x
        val v4 = f4 DT1
        val v5 = f4 DT2
        val _ = assertTrue (v4 = DT1)
        val _ = assertTrue (v5 = DT2)
      in
        ()
      end

  fun testCaseOfRecord () =
      let
        val _ = case {} of
                     {} => ()

        val _ = case {} of
                     {...} => ()

        val _ = case {A=1} of
                     {A=1} => ()
                   | {A=n} => fail "NG"

        val _ = case {A=1, B="A"} of
                     {A=1, B="B"} => fail "NG"
                   | {A=2, B="A"} => fail "NG"
                   | {A=1, B="A"} => ()
                   | _ => fail "NG"

        val _ = case {A=1, B="A"} of
                     {A : int as 2, B="A"} => fail "NG"
                   | {A : int as x, B="A"} => (assertEqualInt 1 A;
                                               assertEqualInt 1 x)
                   | _ => fail "NG"

        val _ = case {A=1, B="A"} of
                     v as {A : int as 2, ...} => fail "NG"
                   | v as {A : int as 1, ...} => (assertTrue ({A=1, B="A"} = v);
                                                  assertEqualInt 1 A)
                   | _ => fail "NG"
      in
        ()
      end

  fun testCaseOfRecordFunction () =
      let
        fun f1 x = case x of
                        {...} => x
        val v1 = f1 {}
        val v2 = f1 {A=1}
        val v3 = f1 {A=1, B="A"}
        val v4 = f1 {A=fn x => x}

        fun f2 x = case {A=1} of
                        {A as x} => x
        val v5 = f2 ()
        val _ = assertEqualInt 1 v5

        fun f3 x = case x of
                        {...}:{A:'a} => x
        val v6 = f3 {A=1}
        val v7 = f3 {A="A"}
        val _ = assertTrue ({A=1} = v6)
        val _ = assertTrue ({A="A"} = v7)

        fun f4 x = case x of
                        {A=x, ...} => x
        val v8 = f4 {A=1, B="A"}
        val v9 = f4 {A="A", C=1}
        val _ = assertEqualInt 1 v8
        val _ = assertEqualString "A" v9
      in
        ()
      end

  fun testCaseOfTuple () =
      let
        val _ = case () of
                     () => ()

        val _ = case (1, "A") of
                     (1, "A") => ()
                   | _ => fail "NG"

        val _ = case (1, "A") of
                     {1 = 1, 2 = "A"} => ()
                   | _ => fail "NG"

        val _ = case (1, "A") of
                     {1 = 1, ...} => ()
                   | _ => fail "NG"

        val _ = case (1, "A") of
                     (x as 1, y as "B") => fail "NG"
                   | (x as 2, y as "A") => fail "NG"
                   | (x as 1, y as "A") => (assertEqualInt 1 x;
                                            assertEqualString "A" y)
                   | _ => fail "NG"

        val _ = case (1, 2, 3) of
                     (1, 1, _) => fail "NG"
                   | (_, 2, 2) => fail "NG"
                   | (3, _, 3) => fail "NG"
                   | (1, 2, 3) => ()
                   | _ => fail "NG"
      in
        ()
      end

  fun testCaseOfTupleFunction () =
      let
        fun f1 x = case x of
                        (n, m) => ()
        val v1 = f1 (1, 2)
        val v2 = f1 ("A", "B")
        val v3 = f1 (1, "A")

        fun f2 x = case x of
                        (n, m) : 'a * 'a => ()
        val v4 = f2 (1, 2)
        val v5 = f2 ("A", "B")

        fun f3 x = case x of
                        (1, m) => ()
                      | _ => fail "NG"
        val v6 = f3 (1, 2)
        val v7 = f3 (1, "A")
      in
        ()
      end

  fun testCaseOfList () =
      let
        val _ = case [] of
                     [] => ()
                   | _ => fail "NG"

        val _ = case [1] of
                     [1] => ()
                   | _ => fail "NG"

        val _ = case [1,2,3] of
                     [1] => fail "NG"
                   | [1,2] => fail "NG"
                   | [1,2,3] => ()
                   | _ => fail "NG"

        val _ = case [1,2,3] of
                     [1] => fail "NG"
                   | [1,2] => fail "NG"
                   | (1::2::[3]) => ()
                   | _ => fail "NG"
      in
        ()
      end

  fun testCaseOfListFunction () =
      let
        fun f1 x = case [x] of
                        [x] => x
                      | _ => fail "NG"
        val v1 = f1 1
        val v2 = f1 "A"
        val _ = assertEqualInt 1 v1
        val _ = assertEqualString "A" v2

        fun f2 x = case x of
                        [x:'a] => x
                      | _ => fail "NG"
        val v3 = f2 [1]
        val v4 = f2 ["A"]
        val _ = assertEqualInt 1 v3
        val _ = assertEqualString "A" v4
      in
        ()
      end


  fun testCaseOfInfix () =
      let
        datatype dt1 = OP1 of dt1 * dt1
                     | V1
                     | V2
                     | V3
        val v1 = OP1 (V1, V2)
        infix OP1
        val _ = case v1 of
                     V1 OP1 V2 => ()
                   | _ => fail "NG"

        val v2 = V1 OP1 V2 OP1 V3
        val (v3, v4) = case v2 of
                            x OP1 y => (x, y)
                          | _ => fail "NG"
        val _ = assertTrue (op OP1 (V1, V2) = v3)
        val _ = assertTrue (V3 = v4)
      in
        ()
      end

  fun testCaseOfInfixr () =
      let
        datatype dt1 = OP1 of dt1 * dt1
                     | V1
                     | V2
                     | V3
        val v1 = OP1 (V1, V2)
        infixr OP1
        val _ = case v1 of
                     V1 OP1 V2 => ()
                   | _ => fail "NG"

        val v2 = V1 OP1 V2 OP1 V3
        val (v3, v4) = case v2 of
                            x OP1 y => (x, y)
                          | _ => fail "NG"
        val _ = assertTrue (V1 = v3)
        val _ = assertTrue (op OP1 (V2, V3) = v4)
      in
        ()
      end

  fun testCaseOfInfixLevel () =
      let
        datatype dt1 = OP1 of int * dt1
                     | OP2 of dt1 * string
                     | V1

        val v1 = OP2 (OP1 (1, V1), "A")
        infix 3 OP1
        infix 2 OP2
        val _ = case v1 of
                     1 OP1 (V1 OP2 "A") => fail "NG"
                   | 1 OP1 V1 OP2 "A" => ()
                   | _ => fail "NG"

        infix 4 OP2
        val _ = case v1 of
                     1 OP1 V1 OP2 "A" => fail "NG"
                   | (1 OP1 V1) OP2 "A" => ()
                   | _ => fail "NG"
      in
        ()
      end

  fun testCaseOfLayeredPattern () =
      let
        val _ = case 1 of 
                     x as 2 => fail "NG"
                   | x as 1 => ()
                   | _ => fail "NG"
                   
        fun f1 x = case x of 
                     x : {A:int, B:string} as {A=2, ...} => fail "NG"
                   | x as {A=1, ...} => x
                   | _ => fail "NG"
        val v1 = f1 {A=1, B="A"}
        val _ = assertTrue ({A=1, B="A"} = v1)
      in
        ()
      end

  fun testCaseOfPolymorphicFunction() =
      let
        fun f1 x = case x of
                        v => v
        val v1 = f1 1
        val v2 = f1 "ABC"
        val v3 = f1 (1, 2)
        val v4 = f1 {A = 1, B = "A"}
        val v5 = f1 [1, 2]

        val _ = assertEqualInt 1 v1
        val _ = assertEqualString "ABC" v2
        val _ = assertTrue ((1, 2) = v3)
        val _ = assertTrue ({A = 1, B = "A"} = v4)
        val _ = assertTrue ([1, 2] = v5)

        fun f2 x = case x of
                        v : 'a => v
        val v6 = f2 1
        val v7 = f2 "ABC"
        val _ = assertEqualInt 1 v6
        val _ = assertEqualString "ABC" v7

        fun f3 x = case x of
                        v : 'a as y => v
        val v8 = f3 1
        val v9 = f3 "ABC"
        val _ = assertEqualInt 1 v8
        val _ = assertEqualString "ABC" v9
      in
        ()
      end


  val tests = TestList [
    Test ("testCaseOfInt", testCaseOfInt),
    Test ("testCaseOfIntFunction", testCaseOfIntFunction),
    Test ("testCaseOfWord", testCaseOfWord),
    Test ("testCaseOfWordFunction", testCaseOfWordFunction),
    Test ("testCaseOfReal", testCaseOfReal),
    Test ("testCaseOfRealFunction", testCaseOfRealFunction),
    Test ("testCaseOfChar", testCaseOfChar),
    Test ("testCaseOfCharFunction", testCaseOfCharFunction),
    Test ("testCaseOfString", testCaseOfString),
    Test ("testCaseOfStringFunction", testCaseOfStringFunction),
    Test ("testCaseOfConstructor", testCaseOfConstructor),
    Test ("testCaseOfRecord", testCaseOfRecord),
    Test ("testCaseOfRecordFunction", testCaseOfRecordFunction),
    Test ("testCaseOfTuple", testCaseOfTuple),
    Test ("testCaseOfTupleFunction", testCaseOfTupleFunction),
    Test ("testCaseOfList", testCaseOfList),
    Test ("testCaseOfListFunction", testCaseOfListFunction),
    Test ("testCaseOfInfix", testCaseOfInfix),
    Test ("testCaseOfInfixr", testCaseOfInfixr),
    Test ("testCaseOfInfixLevel", testCaseOfInfixLevel),
    Test ("testCaseOfLayeredPattern", testCaseOfLayeredPattern),
    Test ("testCaseOfPolymorphicFunction", testCaseOfPolymorphicFunction)
  ]

end