From: Dobrica Pavlinusic Date: Thu, 29 Jul 2010 13:03:06 +0000 (+0200) Subject: added from_hash and test roundtrip X-Git-Tag: RFID-Biblio-0.02~120 X-Git-Url: http://git.rot13.org/?p=Biblio-RFID.git;a=commitdiff_plain;h=ce9d70e4bf2232ea3d444c1930cb6fd8be34096c;hp=324de0d12567568b5c2ef6b6e4c8babb5ff01d48 added from_hash and test roundtrip --- diff --git a/lib/RFID/Biblio/RFID501.pm b/lib/RFID/Biblio/RFID501.pm index 333fa25..dc30fe8 100644 --- a/lib/RFID/Biblio/RFID501.pm +++ b/lib/RFID/Biblio/RFID501.pm @@ -3,6 +3,8 @@ package RFID::Biblio::RFID501; use warnings; use strict; +use Data::Dump qw(dump); + =head1 NAME RFID::Biblio::RFID501 - RFID Standard for Libraries @@ -87,18 +89,19 @@ sub to_hash { $data = join('', @$data) if ref $data eq 'ARRAY'; - warn "## to_hash $data\n"; + warn "## to_hash ",dump($data); - my ( $u1, $set_item, $u2, $type, $content, $br_lib, $custom ) = unpack('C4Z16Nl>',$data); + my ( $u1, $set_item, $u2, $type, $content, $br_lib, $custom, $zero ) = unpack('C4Z16Nl>l',$data); my $hash = { - u1 => $u1, # FIXME + u1 => $u1, # FIXME 0x04 set => ( $set_item & 0xf0 ) >> 4, total => ( $set_item & 0x0f ), - u2 => $u2, # FIXME + u2 => $u2, # FIXME 0x00 type => $type, type_label => $item_type->{$type}, + content => $content, branch => $br_lib >> 20, @@ -107,7 +110,29 @@ sub to_hash { custom => $custom, }; + warn "expected first byte to be 0x04, not $u1\n" if $u1 != 4; + warn "expected third byte to be 0x00, not $u2\n" if $u2 != 0; + warn "expected last block to be zero, not $zero\n" if $zero != 0; + return $hash; } +sub from_hash { + my ( $self, $hash ) = @_; + + return pack('C4Z16Nl>l', + 0x04, + ( $hash->{set} << 4 ) | ( $hash->{total} & 0x0f ), + 0x00, + $hash->{type}, + + $hash->{content}, + + ( $hash->{branch} << 20 ) | ( $hash->{library} & 0x000fffff ), + + $hash->{custom}, + 0x00, + ); +} + 1; diff --git a/t/05-RFID501.t b/t/05-RFID501.t index 19e470f..b4010d8 100755 --- a/t/05-RFID501.t +++ b/t/05-RFID501.t @@ -1,6 +1,6 @@ #!/usr/bin/perl -use Test::More tests => 4; +use Test::More tests => 7; use Data::Dump qw(dump); use lib 'lib'; @@ -9,13 +9,8 @@ BEGIN { use_ok( 'RFID::Biblio::RFID501' ); } -ok( my $hash = RFID::Biblio::RFID501->to_hash( "\x04\x11\x00\x00200912310123\x00\x00\x00\x00" ), 'decode_tag' ); -diag dump $hash; - -ok( $hash = RFID::Biblio::RFID501->to_hash( "\x04\x11\x00\x011301234567\x00\x00\x00\x00\x00\x00" ), 'decode_tag' ); -diag dump $hash; - -my $tag = [ +my $tags = +[ [ "\4\21\0\0", 2009, "0101", @@ -24,8 +19,27 @@ my $tag = [ "\xFF\xFF\xFF\xFF", "\x7F\xFF\xFF\xFF", "\0\0\0\0", -]; +],[ + "\4\21\0\1", + 1302, + "0037", + "67\0\0", + "\0\0\0\0", + "\0\0\0\0", + "\0\0\0\0", + "\0\0\0\0", +] ]; -ok( $hash = RFID::Biblio::RFID501->to_hash( $tag ), 'decode_tag' ); -diag dump $hash; +foreach my $tag ( @$tags ) { + + ok( $hash = RFID::Biblio::RFID501->to_hash( $tag ), 'to_hash' ); + diag dump $hash; + + ok( $bytes = RFID::Biblio::RFID501->from_hash( $hash ), 'from_hash' ); + my $orig = join('', @$tag); + cmp_ok( $bytes, 'eq', $orig, 'roundtrip' ); + + diag dump( $orig, $bytes ); + +}