1*0Sstevel@tonic-gate#!./perl 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateprint "1..31\n"; 4*0Sstevel@tonic-gate 5*0Sstevel@tonic-gatemy $Is_EBCDIC = (ord('A') == 193) ? 1 : 0; 6*0Sstevel@tonic-gate 7*0Sstevel@tonic-gateprint vec($foo,0,1) == 0 ? "ok 1\n" : "not ok 1\n"; 8*0Sstevel@tonic-gateprint length($foo) == 0 ? "ok 2\n" : "not ok 2\n"; 9*0Sstevel@tonic-gatevec($foo,0,1) = 1; 10*0Sstevel@tonic-gateprint length($foo) == 1 ? "ok 3\n" : "not ok 3\n"; 11*0Sstevel@tonic-gateprint unpack('C',$foo) == 1 ? "ok 4\n" : "not ok 4\n"; 12*0Sstevel@tonic-gateprint vec($foo,0,1) == 1 ? "ok 5\n" : "not ok 5\n"; 13*0Sstevel@tonic-gate 14*0Sstevel@tonic-gateprint vec($foo,20,1) == 0 ? "ok 6\n" : "not ok 6\n"; 15*0Sstevel@tonic-gatevec($foo,20,1) = 1; 16*0Sstevel@tonic-gateprint vec($foo,20,1) == 1 ? "ok 7\n" : "not ok 7\n"; 17*0Sstevel@tonic-gateprint length($foo) == 3 ? "ok 8\n" : "not ok 8\n"; 18*0Sstevel@tonic-gateprint vec($foo,1,8) == 0 ? "ok 9\n" : "not ok 9\n"; 19*0Sstevel@tonic-gatevec($foo,1,8) = 0xf1; 20*0Sstevel@tonic-gateprint vec($foo,1,8) == 0xf1 ? "ok 10\n" : "not ok 10\n"; 21*0Sstevel@tonic-gateprint ((unpack('C',substr($foo,1,1)) & 255) == 0xf1 ? "ok 11\n" : "not ok 11\n"); 22*0Sstevel@tonic-gateprint vec($foo,2,4) == 1 ? "ok 12\n" : "not ok 12\n"; 23*0Sstevel@tonic-gateprint vec($foo,3,4) == 15 ? "ok 13\n" : "not ok 13\n"; 24*0Sstevel@tonic-gatevec($Vec, 0, 32) = 0xbaddacab; 25*0Sstevel@tonic-gateprint $Vec eq "\xba\xdd\xac\xab" ? "ok 14\n" : "not ok 14\n"; 26*0Sstevel@tonic-gateprint vec($Vec, 0, 32) == 3135089835 ? "ok 15\n" : "not ok 15\n"; 27*0Sstevel@tonic-gate 28*0Sstevel@tonic-gate# ensure vec() handles numericalness correctly 29*0Sstevel@tonic-gate$foo = $bar = $baz = 0; 30*0Sstevel@tonic-gatevec($foo = 0,0,1) = 1; 31*0Sstevel@tonic-gatevec($bar = 0,1,1) = 1; 32*0Sstevel@tonic-gate$baz = $foo | $bar; 33*0Sstevel@tonic-gateprint $foo eq "1" && $foo == 1 ? "ok 16\n" : "not ok 16\n"; 34*0Sstevel@tonic-gateprint $bar eq "2" && $bar == 2 ? "ok 17\n" : "not ok 17\n"; 35*0Sstevel@tonic-gateprint "$foo $bar $baz" eq "1 2 3" ? "ok 18\n" : "not ok 18\n"; 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate# error cases 38*0Sstevel@tonic-gate 39*0Sstevel@tonic-gate$x = eval { vec $foo, 0, 3 }; 40*0Sstevel@tonic-gateprint "not " if defined $x or $@ !~ /^Illegal number of bits in vec/; 41*0Sstevel@tonic-gateprint "ok 19\n"; 42*0Sstevel@tonic-gate$x = eval { vec $foo, 0, 0 }; 43*0Sstevel@tonic-gateprint "not " if defined $x or $@ !~ /^Illegal number of bits in vec/; 44*0Sstevel@tonic-gateprint "ok 20\n"; 45*0Sstevel@tonic-gate$x = eval { vec $foo, 0, -13 }; 46*0Sstevel@tonic-gateprint "not " if defined $x or $@ !~ /^Illegal number of bits in vec/; 47*0Sstevel@tonic-gateprint "ok 21\n"; 48*0Sstevel@tonic-gate$x = eval { vec($foo, -1, 4) = 2 }; 49*0Sstevel@tonic-gateprint "not " if defined $x or $@ !~ /^Negative offset to vec in lvalue context/; 50*0Sstevel@tonic-gateprint "ok 22\n"; 51*0Sstevel@tonic-gateprint "not " if vec('abcd', 7, 8); 52*0Sstevel@tonic-gateprint "ok 23\n"; 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate# UTF8 55*0Sstevel@tonic-gate# N.B. currently curiously coded to circumvent bugs elswhere in UTF8 handling 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate$foo = "\x{100}" . "\xff\xfe"; 58*0Sstevel@tonic-gate$x = substr $foo, 1; 59*0Sstevel@tonic-gateprint "not " if vec($x, 0, 8) != 255; 60*0Sstevel@tonic-gateprint "ok 24\n"; 61*0Sstevel@tonic-gateeval { vec($foo, 1, 8) }; 62*0Sstevel@tonic-gateprint "not " if $@; 63*0Sstevel@tonic-gateprint "ok 25\n"; 64*0Sstevel@tonic-gateeval { vec($foo, 1, 8) = 13 }; 65*0Sstevel@tonic-gateprint "not " if $@; 66*0Sstevel@tonic-gateprint "ok 26\n"; 67*0Sstevel@tonic-gateif ($Is_EBCDIC) { 68*0Sstevel@tonic-gate print "not " if $foo ne "\x8c\x0d\xff\x8a\x69"; 69*0Sstevel@tonic-gate print "ok 27\n"; 70*0Sstevel@tonic-gate} 71*0Sstevel@tonic-gateelse { 72*0Sstevel@tonic-gate print "not " if $foo ne "\xc4\x0d\xc3\xbf\xc3\xbe"; 73*0Sstevel@tonic-gate print "ok 27\n"; 74*0Sstevel@tonic-gate} 75*0Sstevel@tonic-gate$foo = "\x{100}" . "\xff\xfe"; 76*0Sstevel@tonic-gate$x = substr $foo, 1; 77*0Sstevel@tonic-gatevec($x, 2, 4) = 7; 78*0Sstevel@tonic-gateprint "not " if $x ne "\xff\xf7"; 79*0Sstevel@tonic-gateprint "ok 28\n"; 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gate# mixed magic 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate$foo = "\x61\x62\x63\x64\x65\x66"; 84*0Sstevel@tonic-gateprint "not " if vec(substr($foo, 2, 2), 0, 16) != 25444; 85*0Sstevel@tonic-gateprint "ok 29\n"; 86*0Sstevel@tonic-gatevec(substr($foo, 1,3), 5, 4) = 3; 87*0Sstevel@tonic-gateprint "not " if $foo ne "\x61\x62\x63\x34\x65\x66"; 88*0Sstevel@tonic-gateprint "ok 30\n"; 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate# A variation of [perl #20933] 91*0Sstevel@tonic-gate{ 92*0Sstevel@tonic-gate my $s = ""; 93*0Sstevel@tonic-gate vec($s, 0, 1) = 0; 94*0Sstevel@tonic-gate vec($s, 1, 1) = 1; 95*0Sstevel@tonic-gate my @r; 96*0Sstevel@tonic-gate $r[$_] = \ vec $s, $_, 1 for (0, 1); 97*0Sstevel@tonic-gate print "not " if (${ $r[0] } != 0 || ${ $r[1] } != 1); 98*0Sstevel@tonic-gate print "ok 31\n"; 99*0Sstevel@tonic-gate} 100