added from_hash and test roundtrip
authorDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 29 Jul 2010 13:03:06 +0000 (15:03 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 29 Jul 2010 13:03:06 +0000 (15:03 +0200)
lib/RFID/Biblio/RFID501.pm
t/05-RFID501.t

index 333fa25..dc30fe8 100644 (file)
@@ -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;
index 19e470f..b4010d8 100755 (executable)
@@ -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 );
+
+}