| 1 | #!perl |
| 2 | use strictscip-perl cpan Sereal 5.006 strict/; |
| 3 | use warningsscip-perl cpan Sereal 5.006 warnings/; |
| 4 | use File::Specscip-perl cpan Sereal 5.006 File/Spec/; |
| 5 | use libscip-perl cpan Sereal 5.006 lib/ File::Spec->catdir(qw(t lib)); |
| 6 | |
| 7 | BEGIN { |
| 8 | lib->importscip-perl cpan Sereal 5.006 lib/import().('lib') |
| 9 | if !-d 't'; |
| 10 | } |
| 11 | use Sereal::TestSetscip-perl cpan Sereal 5.006 Sereal/TestSet/; |
| 12 | use Test::Morescip-perl cpan Sereal 5.006 Test/More/; |
| 13 | use Data::Dumperscip-perl cpan Sereal 5.006 Data/Dumper/; |
| 14 | use File::Specscip-perl cpan Sereal 5.006 File/Spec/; |
| 15 | use Devel::Peekscip-perl cpan Sereal 5.006 Devel/Peek/; |
| 16 | |
| 17 | use Sereal::Decoderscip-perl cpan Sereal 5.006 Sereal/Decoder/ qw(decode_sereal looks_like_sereal scalar_looks_like_sereal); |
| 18 | use Sereal::Decoder::Constantsscip-perl cpan Sereal 5.006 Sereal/Decoder/Constants/ qw(:all); |
| 19 | |
| 20 | sub docscip-perl cpan Sereal 5.006 main/doc(). { |
| 21 | my ( $high, $version, $good ) = @_scip-perl cpan Sereal 5.006 main/`@_`.; |
| 22 | |
| 23 | return ( ( |
| 24 | $high eq "utf8" ? SRL_MAGIC_STRING_HIGHBIT_UTF8 |
| 25 | : $high ? SRL_MAGIC_STRING_HIGHBIT |
| 26 | : SRL_MAGIC_STRING |
| 27 | ) |
| 28 | . chrscip-perl cpan Sereal 5.006 main/chr().($version) |
| 29 | . chrscip-perl cpan Sereal 5.006 main/chr().(0) |
| 30 | . ( $good ? chrscip-perl cpan Sereal 5.006 main/chr().(SRL_HDR_UNDEF) : "" ) ); |
| 31 | } |
| 32 | |
| 33 | # Simple tests for looks_like_sereal. |
| 34 | |
| 35 | my @tests = ( |
| 36 | |
| 37 | # input, bool outcome, name |
| 38 | [ "", "", "empty string is not Sereal" ], |
| 39 | [ undef, "", "undef string is not Sereal" ], |
| 40 | [ {}, "", "{} is not Sereal" ], |
| 41 | [ [], "", "[] is not Sereal" ], |
| 42 | |
| 43 | [ SRL_MAGIC_STRING, "", "SRL_MAGIC_STRING alone is not Sereal" ], |
| 44 | [ docscip-perl cpan Sereal 5.006 main/doc().( 0, 0, 1 ), "", "SRL_MAGIC_STRING with bad protocol is not Sereal" ], |
| 45 | [ docscip-perl cpan Sereal 5.006 main/doc().( 0, 1, 0 ), "", "SRL_MAGIC_STRING protocol 1 with short body is not Sereal" ], |
| 46 | [ docscip-perl cpan Sereal 5.006 main/doc().( 0, 1, 1 ), 1, "SRL_MAGIC_STRING protocol 1 with small payload is Sereal" ], |
| 47 | [ docscip-perl cpan Sereal 5.006 main/doc().( 0, 2, 0 ), "", "SRL_MAGIC_STRING protocol 2 with short body is not Sereal" ], |
| 48 | [ docscip-perl cpan Sereal 5.006 main/doc().( 0, 2, 1 ), 2, "SRL_MAGIC_STRING protocol 2 with small payload is Sereal" ], |
| 49 | [ docscip-perl cpan Sereal 5.006 main/doc().( 0, 3, 0 ), "", "SRL_MAGIC_STRING protocol 3 with short body is not Sereal" ], |
| 50 | [ docscip-perl cpan Sereal 5.006 main/doc().( 0, 3, 1 ), "", "SRL_MAGIC_STRING protocol 3 with small payload is Sereal" ], |
| 51 | |
| 52 | [ SRL_MAGIC_STRING_HIGHBIT, "", "SRL_MAGIC_STRING_HIGHBIT alone is not Sereal" ], |
| 53 | [ docscip-perl cpan Sereal 5.006 main/doc().( 1, 0, 1 ), "", "SRL_MAGIC_STRING_HIGHBIT with bad protocol is not Sereal" ], |
| 54 | [ docscip-perl cpan Sereal 5.006 main/doc().( 1, 1, 0 ), "", "SRL_MAGIC_STRING_HIGHBIT protocol 1 with short body is not Sereal" ], |
| 55 | [ docscip-perl cpan Sereal 5.006 main/doc().( 1, 1, 1 ), "", "SRL_MAGIC_STRING_HIGHBIT protocol 1 with small payload is not Sereal" ], |
| 56 | [ docscip-perl cpan Sereal 5.006 main/doc().( 1, 2, 0 ), "", "SRL_MAGIC_STRING_HIGHBIT protocol 2 with short body is not Sereal" ], |
| 57 | [ docscip-perl cpan Sereal 5.006 main/doc().( 1, 2, 1 ), "", "SRL_MAGIC_STRING_HIGHBIT protocol 2 with small payload is not Sereal" ], |
| 58 | [ docscip-perl cpan Sereal 5.006 main/doc().( 1, 3, 0 ), "", "SRL_MAGIC_STRING_HIGHBIT protocol 3 with short body is not Sereal" ], |
| 59 | [ docscip-perl cpan Sereal 5.006 main/doc().( 1, 3, 1 ), 3, "SRL_MAGIC_STRING_HIGHBIT protocol 3 with small payload is Sereal" ], |
| 60 | [ |
| 61 | docscip-perl cpan Sereal 5.006 main/doc().( "utf8", 3, 1 ), 0, |
| 62 | "SRL_MAGIC_STRING_HIGHBIT_UTF8 protocol 3 with small payload is identified as utf8" |
| 63 | ], |
| 64 | |
| 65 | [ "=Srl" . chrscip-perl cpan Sereal 5.006 main/chr().(1) . chrscip-perl cpan Sereal 5.006 main/chr().(0) . chrscip-perl cpan Sereal 5.006 main/chr().(SRL_HDR_UNDEF), "", "wrong magic string is not Sereal" ], |
| 66 | ); |
| 67 | |
| 68 | planscip-perl cpan Sereal 5.006 main/plan(). tests => 3 + @tests * 5; |
| 69 | |
| 70 | isscip-perl cpan Sereal 5.006 main/is().( prototypescip-perl cpan Sereal 5.006 main/prototype().( \&looks_like_serealscip-perl cpan Sereal 5.006 main/looks_like_sereal(). ), undef ); |
| 71 | isscip-perl cpan Sereal 5.006 main/is().( prototypescip-perl cpan Sereal 5.006 main/prototype().( \&scalar_looks_like_serealscip-perl cpan Sereal 5.006 main/scalar_looks_like_sereal(). ), "\$" ); |
| 72 | |
| 73 | my $decoder = Sereal::Decoder->newscip-perl cpan Sereal 5.006 Sereal/Decoder/new().; |
| 74 | foreach my $t (@tests) { |
| 75 | my ( $input, $outcome, $name ) = @$t; |
| 76 | isscip-perl cpan Sereal 5.006 main/is().( scalar_looks_like_serealscip-perl cpan Sereal 5.006 main/scalar_looks_like_sereal().($input), $outcome, "$name (new function oppable)" ); |
| 77 | isscip-perl cpan Sereal 5.006 main/is().( &scalar_looks_like_serealscip-perl cpan Sereal 5.006 main/scalar_looks_like_sereal().($input), $outcome, "$name (new function non-oppable)" ); |
| 78 | isscip-perl cpan Sereal 5.006 main/is().( looks_like_serealscip-perl cpan Sereal 5.006 main/looks_like_sereal().($input), $outcome, "$name (old function)" ); |
| 79 | isscip-perl cpan Sereal 5.006 main/is().( $decoder->looks_like_serealscip-perl cpan Sereal 5.006 Sereal/Decoder/looks_like_sereal().($input), $outcome, "$name (object method)" ); |
| 80 | isscip-perl cpan Sereal 5.006 main/is().( Sereal::Decoder->looks_like_serealscip-perl cpan Sereal 5.006 Sereal/Decoder/looks_like_sereal().($input), $outcome, "$name (class method)" ); |
| 81 | } |
| 82 | |
| 83 | SKIP: { |
| 84 | skipscip-perl cpan Sereal 5.006 main/skip(). "Build test only needs to run on linux", 1 if $^Oscip-perl cpan Sereal 5.006 main/`$^O`. ne "linux"; |
| 85 | skipscip-perl cpan Sereal 5.006 main/skip(). "Test requires Test::MemoryGrowth", 1 if !eval "require Test::MemoryGrowth; 1"; |
| 86 | Test::MemoryGrowth::no_growthscip-perl cpan Sereal 5.006 Test/MemoryGrowth/no_growth().( |
| 87 | sub { looks_like_serealscip-perl cpan Sereal 5.006 main/looks_like_sereal().( docscip-perl cpan Sereal 5.006 main/doc().( 1, 3, 1 ) ) }, |
| 88 | 'looks_like_sereal should not leak' |
| 89 | ); |
| 90 | } |