Rune - Content-locking work 1/2
[rune.git] / tests / cast.d
1 #!/usr/local/bin/rune -x
2 #
3 #       Test simple casts.
4
5 limport "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.b);
21         }
22 }
23
24 int
25 main(int ac, char **av)
26 {
27         int i;
28         Test test;
29
30         stdout->show("count 0-9:");
31         for (i = 0; i < 10; ++i) {
32                 int j = (int)i;
33                 int k = (typeof(j))i;
34
35                 stdout->show(j);
36         }
37         test = 23;
38         stdout->show("test expect 11:", test.a, "and 46:", test.b);
39         stdout->show("test expect 46:", (int)test);
40         {
41             int8_t b8 = 257;
42             int16_t b16 = 257;
43             int32_t b32 = 257;
44             int64_t b64 = 257;
45             float64_t d = 5.5;
46             stdout->show("test expect 1: ", (double)b8);
47             stdout->show("test expect 5: ", (int32_t)d);
48         }
49
50         # These casts depend on a feature in the Rune resolver
51         # which modifies the type of constants to match operators.
52         #
53         {
54             int8_t b8 = b8 + 1;         # autocast int32_t -> int8_t
55             int8_t b9 = b9 + 127;       # autocast int32_t -> int8_t
56             int32_t b10 = b10 + 0x7FFFFFFF;
57             # this should have a resolver failure if we uncomment it
58             # int32_t b11 = b11 + 0x08FFFFFFFL;
59             stdout->show("test b8 1: ", b8);
60             stdout->show("test 98 127: ", b9);
61         }
62         {
63             float b8 = b8 + 1.0;        # autocast double -> float
64             double b10 = b10 + 1.2;
65             # this should have a resolver failure if we uncomment it
66             #float b9 = b9 + 1.1;       # autocast double -> float
67             stdout->show("test b8 1.0: ", b8);
68             stdout->show("test b10 1.2: ", b10);
69         }
70         return(0);
71 }