File: casts.bsh

package info (click to toggle)
bsh 2.0b4-20
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 4,224 kB
  • sloc: java: 23,431; xml: 4,500; sh: 139; makefile: 24
file content (59 lines) | stat: -rw-r--r-- 1,225 bytes parent folder | download | duplicates (10)
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
#!/bin/java bsh.Interpreter

source("TestHarness.bsh");

f=(float)5;

assert( ((int)0.5) == 0 );

a=0.3;
assert( ((int)a) == 0 );

d=(double)+1;

// cast error should be changed to throw classcastexception which could 
// be caught
assert( isEvalError( "i=(Integer)\"foo\";") );

try {
  i=(Integer)"foo";
} catch ( ArithmeticException e0 ) {
	assert(false); // just checking
} catch ( ClassCastException e ) {
	flag();
}

assert(flag()==1);

// this is ok of course
String s = null;
// but this should be as well
String s2 = (String)null;

(Object)null;
int x = (int)'a';

// can't cast null to primitive type
assert( isEvalError("(int)null"));

// can't cast void to anything
assert( isEvalError("(int)xyz"));
assert( isEvalError("(Object)xyz"));

// casting primitives to object types used to be illegal
// Now they get boxed
//assert( isEvalError("(Object)5"));
//assert( isEvalError("(Object)'a'"));
// assert( isEvalError("(Object)true"));

// boolean can be cast only in the trivial case
(boolean)true;
// can't cast boolean to other primitive type
assert( isEvalError("(int)true"));

// can't cast object to primitive type
assert( isEvalError("(int)\"foo\""));
assert( isEvalError("(boolean)\"foo\""));

complete();