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
|
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
use strict;
use warnings;
use lib 'buildlib';
use Test::More tests => 28;
use Lucy::Test::TestUtils qw( utf8_test_strings );
use Lucy::Util::StringHelper qw( utf8ify utf8_flag_off );
use bytes;
no bytes;
my ( @nums, $packed, $template, $ram_file );
sub check_round_trip {
my ( $type, $expected ) = @_;
my $write_method = "write_$type";
my $read_method = "read_$type";
my $file = Lucy::Store::RAMFile->new;
my $outstream = Lucy::Store::OutStream->open( file => $file )
or die Lucy->error;
$outstream->$write_method($_) for @$expected;
$outstream->close;
my $instream = Lucy::Store::InStream->open( file => $file )
or die Lucy->error;
my @got;
push @got, $instream->$read_method for @$expected;
is_deeply( \@got, $expected, $type );
return $file;
}
sub check_round_trip_bytes {
my ( $message, $expected ) = @_;
my $file = Lucy::Store::RAMFile->new;
my $outstream = Lucy::Store::OutStream->open( file => $file );
for (@$expected) {
$outstream->write_c32( bytes::length($_) );
$outstream->print($_);
}
$outstream->close;
my $instream = Lucy::Store::InStream->open( file => $file )
or die Lucy->error;
my @got;
for (@$expected) {
my $buf;
my $len = $instream->read_c32;
$instream->read( $buf, $len );
push @got, $buf;
}
is_deeply( \@got, $expected, $message );
return $file;
}
@nums = ( -128 .. 127 );
$packed = pack( 'c256', @nums );
$ram_file = check_round_trip( 'i8', \@nums );
is( $ram_file->get_contents, $packed,
"pack and write_i8 handle signed bytes identically" );
@nums = ( 0 .. 255 );
$packed = pack( 'C*', @nums );
$ram_file = check_round_trip( 'u8', \@nums );
is( $ram_file->get_contents, $packed,
"pack and write_u8 handle unsigned bytes identically" );
@nums = map { $_ * 1_000_000 + int( rand() * 1_000_000 ) } -1000 .. 1000;
push @nums, ( -1 * ( 2**31 ), 2**31 - 1 );
check_round_trip( 'i32', \@nums );
@nums = map { $_ * 1_000_000 + int( rand() * 1_000_000 ) } 1000 .. 3000;
push @nums, ( 0, 1, 2**32 - 1 );
$packed = pack( 'N*', @nums );
$ram_file = check_round_trip( 'u32', \@nums );
is( $ram_file->get_contents, $packed,
"pack and write_u32 handle unsigned int32s identically" );
@nums = map { $_ * 2 } 0 .. 5;
check_round_trip( 'u64', \@nums );
@nums = map { $_ * 2**31 } 0 .. 2000;
$_ += int( rand( 2**16 ) ) for @nums;
check_round_trip( 'u64', \@nums );
@nums = ( 0 .. 127 );
check_round_trip( 'c32', \@nums );
@nums = ( 128 .. 500 );
$packed = pack( 'w*', @nums );
$ram_file = check_round_trip( 'c32', \@nums );
is( $ram_file->get_contents, $packed, "C32 is equivalent to Perl's pack w" );
@nums = ( 0 .. 127 );
check_round_trip( 'c64', \@nums );
@nums = ( 128 .. 500 );
$packed = pack( 'w*', @nums );
$ram_file = check_round_trip( 'c64', \@nums );
is( $ram_file->get_contents, $packed, "C64 is equivalent to Perl's pack w" );
@nums = map { $_ * 2**31 } 0 .. 2000;
$_ += int( rand( 2**16 ) ) for @nums;
check_round_trip( 'c64', \@nums );
# rand (always?) has 64-bit precision, but we need 32-bit - so truncate via
# pack/unpack.
@nums = map {rand} 0 .. 100;
$packed = pack( 'f*', @nums );
@nums = unpack( 'f*', $packed );
check_round_trip( 'f32', \@nums );
@nums = map {rand} 0 .. 100;
check_round_trip( 'f64', \@nums );
my @items;
for ( 0, 22, 300 ) {
@items = ( 'a' x $_ );
check_round_trip_bytes( "buf of length $_", \@items );
check_round_trip( 'string', \@items );
}
{
my @stuff = ( qw( a b c d 1 ), "\n", "\0", " ", " ", "\xf0\x9d\x84\x9e" );
my @items = ();
for ( 1 .. 50 ) {
my $string_len = int( rand() * 5 );
my $str = '';
$str .= $stuff[ rand @stuff ] for 1 .. $string_len;
push @items, $str;
}
check_round_trip_bytes( "50 binary bufs", \@items );
}
my ( $smiley, $not_a_smiley, $frowny ) = utf8_test_strings();
check_round_trip( "string", [ $smiley, $frowny ] );
my $latin = "ma\x{f1}ana";
$ram_file = check_round_trip( "string", [$latin] );
my $unibytes = $latin;
utf8ify($unibytes);
utf8_flag_off($unibytes);
my $slurped = $ram_file->get_contents;
substr( $slurped, 0, 1, "" ); # ditch c32 at head of string;
is( $slurped, $unibytes, "write_string upgrades to utf8" );
|