no
warnings
qw(portable)
;
Scalar::Type->
import
(
'is_bool'
)
if
(bool_supported());
(
undef
,
my
$filename
) = tempfile(
UNLINK
=> 1);
Data::CompactReadonly->create(
$filename
, []);
isa_ok(
my
$data
= Data::CompactReadonly->
read
(
$filename
),
'Data::CompactReadonly::V0::Array::Byte'
,
"can create an Array::Byte"
);
isa_ok(
$data
,
'Data::CompactReadonly::Array'
,
"and that isa Data::CompactReadonly::Array"
);
is(
$data
->count(), 0,
"it's empty"
);
is((
stat
(
$filename
))[7], 7,
"file size is correct"
);
my
$true
= 1 == 1;
my
$false
= 1 == 0;
my
$array
= [
0x10000,
undef
,
"apple"
,
0x1,
0x100,
3.4,
0x12345678,
0x100000000,
0x100000000,
"apple"
,
$true
,
$true
,
$false
,
'x'
x 256
];
Data::CompactReadonly->create(
$filename
,
$array
);
isa_ok(
$data
= Data::CompactReadonly->
read
(
$filename
),
'Data::CompactReadonly::V0::Array::Byte'
,
"got another Array::Byte"
);
is(
$data
->_ptr_size(), 1,
"pointers are 1 byte"
);
is(
$data
->count(), 14,
"got a non-empty array"
);
is(
$data
->element(0), 0x10000,
"read a Medium from the array"
);
is(
$data
->element(1),
undef
,
"read a Null"
);
is(
$data
->element(2),
'apple'
,
"read a Text::Byte"
);
is(
$data
->element(3), 1,
"read a Byte"
);
is(
$data
->element(4), 256,
"read a Short"
);
cmp_float(
$data
->element(5), 3.4,
"read a Float64"
);
is(
$data
->element(6), 0x12345678,
"read a Long"
);
is(
$data
->element(7), 0x100000000,
"read a Huge"
);
is(
$data
->element(8), 0x100000000,
"read another Huge"
);
is(
$data
->element(9),
'apple'
,
"read another Text"
);
ok(
$data
->element(10),
"read a True"
);
if
(bool_supported) {
ok(is_bool(
$data
->element(10)),
"and on super-modern perl the Boolean flag is set correctly"
);
}
ok(
$data
->element(11),
"read another True"
);
if
(bool_supported) {
ok(is_bool(
$data
->element(11)),
"and on super-modern perl the Boolean flag is set correctly"
);
}
ok(!
$data
->element(12),
"read a False"
);
if
(bool_supported) {
ok(is_bool(
$data
->element(12)),
"and on super-modern perl the Boolean flag is set correctly"
);
}
is(
$data
->element(13),
'x'
x 256,
"read another Text"
);
is((
stat
(
$filename
))[7], 322,
"file size is correct"
);
push
@{
$array
}, [],
$array
;
Data::CompactReadonly->create(
$filename
,
$array
);
isa_ok(
$data
= Data::CompactReadonly->
read
(
$filename
),
'Data::CompactReadonly::V0::Array::Byte'
,
"got another Array::Byte"
);
is(
$data
->_ptr_size(), 2,
"pointers are 2 bytes"
);
is(
$data
->count(), 16,
"got a non-empty array"
);
is(
$data
->element(0), 0x10000,
"read a Medium from the array"
);
is(
$data
->element(1),
undef
,
"read a Null"
);
is(
$data
->element(2),
'apple'
,
"read a Text::Byte"
);
is(
$data
->element(3), 1,
"read a Byte"
);
is(
$data
->element(4), 256,
"read a Short"
);
cmp_float(
$data
->element(5), 3.4,
"read a Float64"
);
is(
$data
->element(6), 0x12345678,
"read a Long"
);
is(
$data
->element(7), 0x100000000,
"read a Huge"
);
is(
$data
->element(8), 0x100000000,
"read another Huge"
);
is(
$data
->element(9),
'apple'
,
"read another Text"
);
ok(
$data
->element(10),
"read a True"
);
if
(bool_supported) {
ok(is_bool(
$data
->element(10)),
"and on super-modern perl the Boolean flag is set correctly"
);
}
ok(
$data
->element(11),
"read another True"
);
if
(bool_supported) {
ok(is_bool(
$data
->element(11)),
"and on super-modern perl the Boolean flag is set correctly"
);
}
ok(!
$data
->element(12),
"read a False"
);
if
(bool_supported) {
ok(is_bool(
$data
->element(12)),
"and on super-modern perl the Boolean flag is set correctly"
);
}
is(
$data
->element(13),
'x'
x 256,
"read a Text::Short"
);
isa_ok(
my
$embedded_array
=
$data
->element(14),
'Data::CompactReadonly::V0::Array::Byte'
,
"can embed an array in an array"
);
is(
$embedded_array
->count(), 0,
"sub-array is empty"
);
is(
$data
->element(15)->element(15)->element(14)->id(),
$embedded_array
->id(),
"circular array-refs work"
);
is((
stat
(
$filename
))[7], 322 + 2 + 16 + 2,
"file size is correct"
);
Data::CompactReadonly->create(
$filename
, {});
isa_ok(
$data
= Data::CompactReadonly->
read
(
$filename
),
'Data::CompactReadonly::V0::Dictionary::Byte'
,
"got a Dictionary::Byte"
);
is(
$data
->count(), 0,
"it's empty"
);
is(
$data
->_ptr_size(), 1,
"pointers are 1 byte"
);
my
$hash
= {
true
=>
$true
,
false
=>
$false
,
$false
=>
$false
,
float
=> 3.14,
byte
=> 65,
short
=> 65534,
medium
=> 65536,
long
=> 0x1000000,
huge
=> 0xffffffff1,
array
=> [],
dict
=> {},
null
=>
undef
,
text
=>
'hi mum!'
,
'hi mum!'
=>
'hi mum!'
,
"\x{5317}\x{4eac}\x{5e02}"
=>
'Beijing'
,
'Beijing'
=>
"\x{5317}\x{4eac}\x{5e02}"
,
2
=> 65,
900
=> 65,
6.
28
=> 65,
zzlongtext
=>
'z'
x 300,
};
Data::CompactReadonly->create(
$filename
,
$hash
);
isa_ok(
$data
= Data::CompactReadonly->
read
(
$filename
),
'Data::CompactReadonly::V0::Dictionary::Byte'
,
"got a Dictionary::Byte"
);
isa_ok(
$data
,
'Data::CompactReadonly::Dictionary'
,
"and that isa Data::CompactReadonly::Dictionary"
);
is(
$data
->count(), 20,
"20 entries"
);
is(
$data
->_ptr_size(), 1,
"pointers are 1 byte"
);
cmp_float(
$data
->element(
'float'
), 3.14,
"read a Float64"
);
ok(
$data
->element(
'true'
),
"read a True"
);
if
(bool_supported) {
ok(is_bool(
$data
->element(
'true'
)),
"and on super-modern perl the Boolean flag is set correctly"
);
}
ok(!
$data
->element(
'false'
),
"read a False"
);
if
(bool_supported) {
ok(is_bool(
$data
->element(
'false'
)),
"and on super-modern perl the Boolean flag is set correctly"
);
}
ok(!
$data
->element(
$false
),
"False as a key"
);
if
(bool_supported) {
ok(is_bool(
$data
->element(
$false
)),
"and on super-modern perl the Boolean flag is set correctly"
);
}
is(
$data
->element(
'byte'
), 65,
"read a Byte"
);
is(
$data
->element(
'short'
), 65534,
"read a Short"
);
is(
$data
->element(
'medium'
), 65536,
"read a Medium"
);
is(
$data
->element(
'long'
), 0x1000000,
"read a Long"
);
is(
$data
->element(
'huge'
), 0xffffffff1,
"read a Huge"
);
is(
$data
->element(
'null'
),
undef
,
"read a Null"
);
is(
$data
->element(
'text'
),
'hi mum!'
,
"read a Text::Byte"
);
is(
$data
->element(
'hi mum!'
),
'hi mum!'
,
"read the same text again (reused)"
);
is(
$data
->element(
'zzlongtext'
),
'z'
x 300,
"read a Text::Short"
);
isa_ok(
$embedded_array
=
$data
->element(
'array'
),
'Data::CompactReadonly::V0::Array::Byte'
,
"read an array from the Dictionary"
);
is(
$embedded_array
->count(), 0,
"array is empty"
);
isa_ok(
my
$embedded_dict
=
$data
->element(
'dict'
),
'Data::CompactReadonly::V0::Dictionary::Byte'
,
"read a dictionary from the Dictionary"
);
is(
$embedded_dict
->count(), 0,
"dict is empty"
);
is(
$data
->element(
"\x{5317}\x{4eac}\x{5e02}"
),
"Beijing"
,
"non-ASCII keys work"
);
is(
$data
->element(
'Beijing'
),
"\x{5317}\x{4eac}\x{5e02}"
,
"non-ASCII values work"
);
is((
stat
(
$filename
))[7], 524,
"file size is correct"
);
if
(bool_supported) {
ok((!
grep
{ is_bool(
$_
) }
$data
->indices),
"bools as keys are stringified"
);
}
$hash
->{zzz} =
'say the bees'
;
Data::CompactReadonly->create(
$filename
,
$hash
);
isa_ok(
$data
= Data::CompactReadonly->
read
(
$filename
),
'Data::CompactReadonly::V0::Dictionary::Byte'
,
"got a Dictionary::Byte"
);
is(
$data
->count(), 21,
"got a hash with 18 entries"
);
is(
$data
->_ptr_size(), 2,
"pointers are 2 bytes"
);
is(
$data
->element(
'null'
),
undef
,
"read a Null"
);
is(
$data
->element(
'text'
),
'hi mum!'
,
"read a Text::Byte"
);
is(
$data
->element(
'zzz'
),
'say the bees'
,
"can retrieve data after the long text"
);
is(
(
stat
(
$filename
))[7],
587,
"file size is correct"
);
$hash
= {
'Bond'
=>
'007'
,
'007'
=>
'Bond'
,
'0.07'
=>
'Baby Bond'
,
'00.7'
=>
'Weird Bond'
,
'000'
=>
'Georgian Bond'
,
array
=> [ 5,
'four'
, [ 3 ], {
two
=> 2 }, 1 ],
'7.0'
=>
'seven point oh'
,
'7.00'
=>
'seven point oh oh'
,
'7.10'
=>
'seven point one oh'
,
};
$hash
->{dict} =
$hash
;
$hash
->{
$_
} =
$_
foreach
(0 .. 65536);
push
@{
$hash
->{array}},
$hash
->{array};
Data::CompactReadonly->create(
$filename
,
$hash
);
isa_ok(
$data
= Data::CompactReadonly->
read
(
$filename
),
'Data::CompactReadonly::V0::Dictionary::Medium'
,
"got a Dictionary::Medium"
);
isa_ok(
$data
,
'Data::CompactReadonly::Dictionary'
,
"and that isa Data::CompactReadonly::Dictionary"
);
is(
$data
->count(), 65547,
"right number of elements"
);
is(
$data
->_ptr_size(), 3,
"pointers are 3 bytes"
);
is(
$data
->element(
'array'
)->element(2)->element(0), 3,
"can retrieve from an array in an array in a hash"
);
is(
$data
->element(
'array'
)->element(3)->element(
'two'
), 2,
"can retrieve from a hash in an array in a hash"
);
is(
$data
->element(
'dict'
)->element(65535), 65535,
"can retrieve from an array in a hash"
);
is(
$data
->element(
'dict'
)->element(
'array'
)->element(3)->element(
'two'
), 2,
"can retrieve from a hash in an array in a hash in a hash"
);
is(
$data
->element(
'Bond'
),
'007'
,
"can store text that looks like a number with leading zeroes"
);
is(
$data
->element(
'007'
),
'Bond'
,
"... and use it as a key too"
);
is(
$data
->element(0.07),
'Baby Bond'
,
"zero point something works when presented as a number"
);
is(
$data
->element(
'0.07'
),
'Baby Bond'
,
"zero point something works when presented as text"
);
is(
$data
->element(
'00.7'
),
'Weird Bond'
,
"00.7 isn't numeric, gets properly encoded as text"
);
is(
$data
->element(
'000'
),
'Georgian Bond'
,
'but 000 is a bunch of characters'
);
is(
$data
->element(
'7.0'
),
'seven point oh'
,
'trailing zeroes on strings that look like floats are preserved (7.0)'
);
is(
$data
->element(
'7.00'
),
'seven point oh oh'
,
'trailing zeroes on strings that look like floats are preserved (7.00)'
);
is(
$data
->element(
'7.10'
),
'seven point one oh'
,
'trailing zeroes on strings that look like floats are preserved (7.10)'
);
throws_ok {
$data
->element(7.1) }
qr/Invalid element: 7.1: doesn't exist/
,
"key 7.10 is not the same as key 7.1"
;
Data::CompactReadonly->create(
$filename
, [
{
aardvark
=>
'bat'
,
cat
=>
'doge'
},
[ [ 65, 66 ], {
aardvark
=>
'bat'
,
cat
=>
'doge'
} ],
[ 65, 66 ]
]);
is((
stat
(
$filename
))[7], 54,
"arrays and dictionaries aren't repeated if their contents are identical"
);
done_testing;