SYN SYN
[p5sagit/p5-mst-13.2.git] / t / op / vec.t
CommitLineData
a687059c 1#!./perl
2
22d4bb9c 3print "1..30\n";
a687059c 4
5print vec($foo,0,1) == 0 ? "ok 1\n" : "not ok 1\n";
6print length($foo) == 0 ? "ok 2\n" : "not ok 2\n";
7vec($foo,0,1) = 1;
8print length($foo) == 1 ? "ok 3\n" : "not ok 3\n";
a0ed51b3 9print unpack('C',$foo) == 1 ? "ok 4\n" : "not ok 4\n";
a687059c 10print vec($foo,0,1) == 1 ? "ok 5\n" : "not ok 5\n";
11
12print vec($foo,20,1) == 0 ? "ok 6\n" : "not ok 6\n";
13vec($foo,20,1) = 1;
14print vec($foo,20,1) == 1 ? "ok 7\n" : "not ok 7\n";
15print length($foo) == 3 ? "ok 8\n" : "not ok 8\n";
16print vec($foo,1,8) == 0 ? "ok 9\n" : "not ok 9\n";
17vec($foo,1,8) = 0xf1;
18print vec($foo,1,8) == 0xf1 ? "ok 10\n" : "not ok 10\n";
a0ed51b3 19print ((unpack('C',substr($foo,1,1)) & 255) == 0xf1 ? "ok 11\n" : "not ok 11\n");
a687059c 20print vec($foo,2,4) == 1 ? "ok 12\n" : "not ok 12\n";
21print vec($foo,3,4) == 15 ? "ok 13\n" : "not ok 13\n";
deb3007b 22vec($Vec, 0, 32) = 0xbaddacab;
23print $Vec eq "\xba\xdd\xac\xab" ? "ok 14\n" : "not ok 14\n";
24print vec($Vec, 0, 32) == 3135089835 ? "ok 15\n" : "not ok 15\n";
a687059c 25
4b19af01 26# ensure vec() handles numericalness correctly
27$foo = $bar = $baz = 0;
28vec($foo = 0,0,1) = 1;
29vec($bar = 0,1,1) = 1;
30$baz = $foo | $bar;
31print $foo eq "1" && $foo == 1 ? "ok 16\n" : "not ok 16\n";
32print $bar eq "2" && $bar == 2 ? "ok 17\n" : "not ok 17\n";
33print "$foo $bar $baz" eq "1 2 3" ? "ok 18\n" : "not ok 18\n";
22d4bb9c 34
35# error cases
36
37$x = eval { vec $foo, 0, 3 };
38print "not " if defined $x or $@ !~ /^Illegal number of bits in vec/;
39print "ok 19\n";
40$x = eval { vec $foo, 0, 0 };
41print "not " if defined $x or $@ !~ /^Illegal number of bits in vec/;
42print "ok 20\n";
43$x = eval { vec $foo, 0, -13 };
44print "not " if defined $x or $@ !~ /^Illegal number of bits in vec/;
45print "ok 21\n";
46$x = eval { vec($foo, -1, 4) = 2 };
47print "not " if defined $x or $@ !~ /^Assigning to negative offset in vec/;
48print "ok 22\n";
49print "not " if vec('abcd', 7, 8);
50print "ok 23\n";
51
52# UTF8
53# N.B. currently curiously coded to circumvent bugs elswhere in UTF8 handling
54
55$foo = "\x{100}" . "\xff\xfe";
56$x = substr $foo, 1;
57print "not " if vec($x, 0, 8) != 255;
58print "ok 24\n";
59eval { vec($foo, 1, 8) };
60print "not " if $@;
61print "ok 25\n";
62eval { vec($foo, 1, 8) = 13 };
63print "not " if $@;
64print "ok 26\n";
65print "not " if $foo ne "\xc4\x0d\xc3\xbf\xc3\xbe";
66print "ok 27\n";
67$foo = "\x{100}" . "\xff\xfe";
68$x = substr $foo, 1;
69vec($x, 2, 4) = 7;
70print "not " if $x ne "\xff\xf7";
71print "ok 28\n";
72
73# mixed magic
74
75$foo = "\x61\x62\x63\x64\x65\x66";
76print "not " if vec(substr($foo, 2, 2), 0, 16) != 25444;
77print "ok 29\n";
78vec(substr($foo, 1,3), 5, 4) = 3;
79print "not " if $foo ne "\x61\x62\x63\x34\x65\x66";
80print "ok 30\n";