#!/usr/local/bin/rune -x -d # # Test simple casts. import "sys"; import ; alias stdio.File *stdout = stdio.stdout; class Test { int a; int b; public cast Test myfunc(int x) { Test v = ( x / 2, x * 2 ); return(v); } public cast int myfunc2(Test x) { return(x.a + x.b); } } class TestNoCast { int a; int b; } int main(int ac, char **av) { int i; Test test; TestNoCast nocast; stdout->show("count 0-9:"); for (i = 0; i < 10; ++i) { int j = (int)i; int k = (typeof(j))i; stdout->show(j); } test = 23; # runs int->test cast stdout->show("test expect 11:", test.a, "and 46:", test.b); stdout->show("test expect 57:", (int)test); test = (23); # runs int->test cast stdout->show("test expect 11:", test.a, "and 46:", test.b); stdout->show("test expect 57:", (int)test); test = (a:23); # does not run int->test cast stdout->show("test expect 23:", test.a, "and 0:", test.b); stdout->show("test expect 23:", (int)test); nocast = (23); stdout->show("nocast expect 23:", nocast.a, "and 0:", nocast.b); { int8_t b8 = 257; int16_t b16 = 257; int32_t b32 = 257; int64_t b64 = 257; float64_t d = 5.5; stdout->show("test expect 1: ", (double)b8); stdout->show("test expect 5: ", (int32_t)d); } # These casts depend on a feature in the Rune resolver # which modifies the type of constants to match operators. # { int8_t b8 = b8 + 1; # autocast int32_t -> int8_t int8_t b9 = b9 + 127; # autocast int32_t -> int8_t int32_t b10 = b10 + 0x7FFFFFFF; # this should have a resolver failure if we uncomment it # int32_t b11 = b11 + 0x08FFFFFFFL; stdout->show("test b8 1: ", b8); stdout->show("test 98 127: ", b9); } { float b8 = b8 + 1.0; # autocast double -> float double b10 = b10 + 1.2; # this should have a resolver failure if we uncomment it #float b9 = b9 + 1.1; # autocast double -> float stdout->show("test b8 1.0: ", b8); stdout->show("test b10 1.2: ", b10); } return(0); }