Rune - Fix array passing, optimize constant array indices
[rune.git] / tests / cast.d
1 #!/usr/local/bin/rune -x -d
2 #
3 #       Test simple casts.
4
5 import "sys";
6 import <stdio>;
7
8 alias stdio.File *stdout = stdio.stdout;
9
10 class Test {
11         int a;
12         int b;
13
14         public cast Test myfunc(int x) {
15                 Test v = ( x / 2, x * 2 );
16
17                 return(v);
18         }
19         public cast int myfunc2(Test x) {
20                 return(x.a + x.b);
21         }
22 }
23
24 class TestNoCast {
25         int a;
26         int b;
27 }
28
29 int
30 main(int ac, char **av)
31 {
32         int i;
33         Test test;
34         TestNoCast nocast;
35
36         stdout->show("count 0-9:");
37         for (i = 0; i < 10; ++i) {
38                 int j = (int)i;
39                 int k = (typeof(j))i;
40
41                 stdout->show(j);
42         }
43         test = 23;              # runs int->test cast
44         stdout->show("test expect 11:", test.a, "and 46:", test.b);
45         stdout->show("test expect 57:", (int)test);
46         test = (23);            # runs int->test cast
47         stdout->show("test expect 11:", test.a, "and 46:", test.b);
48         stdout->show("test expect 57:", (int)test);
49         test = (a:23);          # does not run int->test cast
50         stdout->show("test expect 23:", test.a, "and 0:", test.b);
51         stdout->show("test expect 23:", (int)test);
52         nocast = (23);
53         stdout->show("nocast expect 23:", nocast.a, "and 0:", nocast.b);
54         {
55             int8_t b8 = 257;
56             int16_t b16 = 257;
57             int32_t b32 = 257;
58             int64_t b64 = 257;
59             float64_t d = 5.5;
60             stdout->show("test expect 1: ", (double)b8);
61             stdout->show("test expect 5: ", (int32_t)d);
62         }
63
64         # These casts depend on a feature in the Rune resolver
65         # which modifies the type of constants to match operators.
66         #
67         {
68             int8_t b8 = b8 + 1;         # autocast int32_t -> int8_t
69             int8_t b9 = b9 + 127;       # autocast int32_t -> int8_t
70             int32_t b10 = b10 + 0x7FFFFFFF;
71             # this should have a resolver failure if we uncomment it
72             # int32_t b11 = b11 + 0x08FFFFFFFL;
73             stdout->show("test b8 1: ", b8);
74             stdout->show("test 98 127: ", b9);
75         }
76         {
77             float b8 = b8 + 1.0;        # autocast double -> float
78             double b10 = b10 + 1.2;
79             # this should have a resolver failure if we uncomment it
80             #float b9 = b9 + 1.1;       # autocast double -> float
81             stdout->show("test b8 1.0: ", b8);
82             stdout->show("test b10 1.2: ", b10);
83         }
84         return(0);
85 }