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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
################################################################################
#
# $Project: /Convert-Binary-C $
# $Author: mhx $
# $Date: 2009/03/15 04:10:59 +0100 $
# $Revision: 15 $
# $Source: /tests/213_string.t $
#
################################################################################
#
# Copyright (c) 2002-2009 Marcus Holland-Moritz. All rights reserved.
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
################################################################################
use Test;
use Convert::Binary::C @ARGV;
$^W = 1;
BEGIN { plan tests => 91 }
eval {
$C{B} = new Convert::Binary::C LongSize => 4,
LongLongSize => 8,
ByteOrder => 'BigEndian';
};
ok($@,'',"failed to create Convert::Binary::C object");
eval {
$C{B}->parse( <<'ENDC' );
typedef signed long long int i_64;
typedef unsigned long long int u_64;
typedef signed long int i_32;
typedef unsigned long int u_32;
ENDC
};
ok($@,'',"failed to parse code");
eval {
$C{L} = $C{B}->clone->ByteOrder( 'LittleEndian' );
};
ok($@,'',"failed to clone LittleEndian object");
@bytes = ( 0xAB, 0x54, 0xA9, 0x8C, 0xEB, 0x1F, 0x0A, 0xD2 );
$str{B} = pack 'C*', @bytes;
$str{L} = pack 'C*', reverse @bytes;
%order = (
B => 'BigEndian',
L => 'LittleEndian',
);
@tests = (
{
type => 'u_64',
B => "12345678901234567890",
L => "12345678901234567890",
},
{
type => 'i_64',
B => "-6101065172474983726",
L => "-6101065172474983726",
},
{
type => 'u_32',
B => "2874452364",
L => "3944680146",
},
{
type => 'i_32',
B => "-1420514932",
L => "-350287150",
},
);
for my $test ( @tests ) {
for my $bo ( qw( B L ) ) {
print "# unpack $order{$bo} $test->{type}\n";
eval { $val = $C{$bo}->unpack( $test->{type}, $str{$bo} ) };
ok($@,'',"unpack failed");
ok($val, $test->{$bo}, "wrong value");
}
}
@tests = (
{
type => 'u_64',
B => " + 12345678901234567890",
L => "12345678901234567890",
},
{
type => 'i_64',
B => " -6101065172474983726",
L => "- 6101065172474983726",
},
{
type => 'u_32',
B => " + 2874452364",
L => "3944680146",
},
{
type => 'i_32',
B => "- 1420514932",
L => " - 350287150",
},
);
for my $test ( @tests ) {
for my $bo ( qw( B L ) ) {
print "# pack $order{$bo} $test->{type}\n";
eval { $val = $C{$bo}->pack( $test->{type}, $test->{$bo} ) };
ok($@,'',"pack failed for $order{$bo} $test->{type} test");
ok(length($val), $C{$bo}->sizeof($test->{type}), "wrong string size" );
ok($val, substr($str{$bo}, 0, length($val)), "wrong string");
}
}
@tests = (
{
name => 'dec',
type => 'u_64',
str => pack("C*", 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0x0E),
B => " 81985529216486670",
L => "1066697293388129025",
},
{
name => 'hex',
type => 'u_64',
str => pack("C*", 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0x0E),
B => "0x0123456789aBCd0E",
L => " 0x0ECdaB8967452301",
},
{
name => 'oct',
type => 'u_64',
str => pack("C*", 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0x0E),
B => "04432126361152746416",
L => " 073155270454721221401",
},
{
name => 'bin',
type => 'u_64',
str => pack("C*", 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0x0E),
B => " 0b100100011010001010110011110001001101010111100110100001110 ",
L => "0b111011001101101010111000100101100111010001010010001100000001 ",
},
{
name => 'dec',
type => 'u_32',
str => pack("C*", 0x00, 0xaf, 0xfe, 0x00),
B => " 11533824 ",
L => " 16690944",
},
{
name => 'hex',
type => 'u_32',
str => pack("C*", 0x00, 0xaf, 0xfe, 0x00),
B => " 0x00AffE00",
L => "0x00FeaF00 ",
},
{
name => 'oct',
type => 'u_32',
str => pack("C*", 0x00, 0xaf, 0xfe, 0x00),
B => " 053777000",
L => " 077527400 ",
},
{
name => 'bin',
type => 'u_32',
str => pack("C*", 0x00, 0xaf, 0xfe, 0x00),
B => " 0b101011111111111000000000",
L => " 0b111111101010111100000000",
},
);
for my $test ( @tests ) {
for my $bo ( qw( B L ) ) {
print "# pack $test->{name} $order{$bo} $test->{type}\n";
eval { $val = $C{$bo}->pack( $test->{type}, $test->{$bo} ) };
ok($@,'',"pack failed for $order{$bo} $test->{type} test");
ok(length($val), $C{$bo}->sizeof($test->{type}), "wrong string size" );
ok($val, substr($test->{str}, 0, length($val)), "wrong string");
}
}
|