<commit_before>use v6;
use Test;
use BSON;
use BSON::Binary;
use UUID;
use Digest::MD5;

#-------------------------------------------------------------------------------
# Binary object. Generic binary
#
subtest {
#  my BSON::Binary $bin-obj .= new;
  my Buf $raw-bin = Buf.new(0x55 xx 3);
  my BSON::Binary $bin-obj .= new(
    data => $raw-bin,
  );

  is-deeply( $bin-obj.Buf, $raw-bin, 'compare data');

  my Array $bin-test = [ 0x03, 0x00, 0x00, 0x00,          # Size of buf
                         0x00,                            # Generic binary type
                         0x55 xx 3,                       # Raw Buf
                       ];
  my Buf $enc-bin = $bin-obj.enc_binary;
  is-deeply( $enc-bin.list, $bin-test, 'encode general binary test');

  my $index = 0;
  $bin-obj .= new;
  $bin-obj.dec_binary( $enc-bin.list, $index);
  is-deeply( $bin-obj.Buf, $raw-bin, 'compare data after decoding');
  is( $index, $bin-test.elems, "Index is shifted $index bytes");

}, "Test generic binary data";

#-------------------------------------------------------------------------------
# Binary object. UUID binary
#
subtest {
  my UUID $uuid .= new: version => 4;

  my BSON::Binary $bin-obj .= new(
    data => $uuid.Blob,
    type => $BSON::UUID
  );

  is-deeply( $bin-obj.Buf, $uuid.Blob, 'compare uuid binary data');

  my Array $bin-test = [ 0x10, 0x00, 0x00, 0x00,          # Size of buf
                         $BSON::UUID,                     # UUID binary type
                         $uuid.Blob.list,                 # Raw Buf
                       ];
  my Buf $enc-bin = $bin-obj.enc_binary;
  is-deeply( $enc-bin.list, $bin-test, 'encode uuid test');

  my $index = 0;
  $bin-obj .= new;
  $bin-obj.dec_binary( $enc-bin.list, $index);
  is-deeply( $bin-obj.Buf.list,
             $uuid.Blob.list,
             'compare uuid data after decoding'
           );
  is( $index, $bin-test.elems, "Index is shifted $index bytes");
  is( $bin-obj.get_type, $BSON::UUID, "Test UUID type");

}, "Test uuid binary data";

#-------------------------------------------------------------------------------
# Binary object. MD5 binary
#
subtest {
  class my-md5 {
    has utf8 $.enc-md5;
    has Str $.md5;
    submethod BUILD ( Str :$text ) {
      $!md5 = Digest::MD5.md5_hex($text);
      $!enc-md5 = $!md5.encode;
    }

    method Buf ( --> Buf ) {
      my Int @m;
      my Buf $b = $!enc-md5 ~ Buf.new();
      for @$b -> $h, $l {
        @m.push($h +< 4 +| $l);
      }

      return Buf.new(@m);
    }
  }

  my my-md5 $md5 .= new( text => 'Something I like to be md5-ed');
  my BSON::Binary $bin-obj .= new( data => $md5.Buf, type => $BSON::MD5);

  is-deeply( $bin-obj.Buf, $md5.Buf, 'compare md5 binary data');

  my Array $bin-test = [ 0x10, 0x00, 0x00, 0x00,          # Size of buf
                         $BSON::MD5,                      # MD5 binary type
                         $md5.Buf.list,                   # Raw Buf
                       ];
  my Buf $enc-bin = $bin-obj.enc_binary;
  is-deeply( $enc-bin.list, $bin-test, 'encode md5 test');

  my $index = 0;
  $bin-obj .= new;
  $bin-obj.dec_binary( $enc-bin.list, $index);
  is-deeply( $bin-obj.Buf.list,
             $md5.Buf.list,
             'compare md5 data after decoding'
           );
  is( $index, $bin-test.elems, "Index is shifted $index bytes");
  is( $bin-obj.get_type, $BSON::MD5, "Test MD5 type");

}, "Test md5 binary data";
<commit_msg>improvement in using md5 binary<commit_after>use v6;
use Test;
use BSON;
use BSON::Binary;
use UUID;
use Digest::MD5;

#-------------------------------------------------------------------------------
# Binary object. Generic binary
#
subtest {
#  my BSON::Binary $bin-obj .= new;
  my Buf $raw-bin = Buf.new(0x55 xx 3);
  my BSON::Binary $bin-obj .= new(
    data => $raw-bin,
  );

  is-deeply( $bin-obj.Buf, $raw-bin, 'compare data');

  my Array $bin-test = [ 0x03, 0x00, 0x00, 0x00,          # Size of buf
                         0x00,                            # Generic binary type
                         0x55 xx 3,                       # Raw Buf
                       ];
  my Buf $enc-bin = $bin-obj.enc_binary;
  is-deeply( $enc-bin.list, $bin-test, 'encode general binary test');

  my $index = 0;
  $bin-obj .= new;
  $bin-obj.dec_binary( $enc-bin.list, $index);
  is-deeply( $bin-obj.Buf, $raw-bin, 'compare data after decoding');
  is( $index, $bin-test.elems, "Index is shifted $index bytes");

}, "Test generic binary data";

#-------------------------------------------------------------------------------
# Binary object. UUID binary
#
subtest {
  my UUID $uuid .= new: version => 4;

  my BSON::Binary $bin-obj .= new(
    data => $uuid.Blob,
    type => $BSON::UUID
  );

  is-deeply( $bin-obj.Buf, $uuid.Blob, 'compare uuid binary data');

  my Array $bin-test = [ 0x10, 0x00, 0x00, 0x00,          # Size of buf
                         $BSON::UUID,                     # UUID binary type
                         $uuid.Blob.list,                 # Raw Buf
                       ];
  my Buf $enc-bin = $bin-obj.enc_binary;
  is-deeply( $enc-bin.list, $bin-test, 'encode uuid test');

  my $index = 0;
  $bin-obj .= new;
  $bin-obj.dec_binary( $enc-bin.list, $index);
  is-deeply( $bin-obj.Buf.list,
             $uuid.Blob.list,
             'compare uuid data after decoding'
           );
  is( $index, $bin-test.elems, "Index is shifted $index bytes");
  is( $bin-obj.get_type, $BSON::UUID, "Test UUID type");

}, "Test uuid binary data";

#-------------------------------------------------------------------------------
# Binary object. MD5 binary
#
subtest {

  my Digest::MD5 $md5 .= new;
  my Buf $md5-b = $md5.md5_buf('Something I like to be md5-ed');
  my BSON::Binary $bin-obj .= new( data => $md5-b, type => $BSON::MD5);

  is-deeply( $bin-obj.Buf, $md5-b, 'compare md5 binary data');

  my Array $bin-test = [ 0x10, 0x00, 0x00, 0x00,          # Size of buf
                         $BSON::MD5,                      # MD5 binary type
                         $md5-b.list,                     # Raw Buf
                       ];
  my Buf $enc-bin = $bin-obj.enc_binary;
  is-deeply( $enc-bin.list, $bin-test, 'encode md5 test');

  my $index = 0;
  $bin-obj .= new;
  $bin-obj.dec_binary( $enc-bin.list, $index);
  is-deeply( $bin-obj.Buf.list,
             $md5-b.list,
             'compare md5 data after decoding'
           );
  is( $index, $bin-test.elems, "Index is shifted $index bytes");
  is( $bin-obj.get_type, $BSON::MD5, "Test MD5 type");

}, "Test md5 binary data";

{'fraction_non_alphanumeric': 0.2211890243902439, 'fraction_numerical': 0.023628048780487805, 'mean_word_length': 1.513793103448276, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1015033', 'n_tokens_mistral': 2358, 'n_tokens_neox': 2215, 'n_words': 590}