Skip to content

Instantly share code, notes, and snippets.

@zoffixznet

zoffixznet/p6.p6 Secret

Created October 26, 2017 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoffixznet/6d2e8ea8a25e6418f51e27f3103c6b9a to your computer and use it in GitHub Desktop.
Save zoffixznet/6d2e8ea8a25e6418f51e27f3103c6b9a to your computer and use it in GitHub Desktop.
use v6;
use Test;
use BSON::Document;
#-------------------------------------------------------------------------------
subtest "Initialize document", {
# Init via Seq
#
my BSON::Document $d .= new: ('a' ... 'z') Z=> 120..145;
is $d<a>, 120, "\$d<a> = $d<a>";
is $d<b>, 121, "\$d<b> = $d<b>";
is $d.elems, 26, "{$d.elems} elements";
# Add one element, encode and decode using new(Buf)
#
$d<aaa> = 11;
my Buf $b2 = $d.encode;
my BSON::Document $d2 .= new($b2);
is $d2.elems, 27, "{$d.elems} elements in decoded doc";
is $d2<aaa>, 11, "Item is $d2<aaa>";
# Init via list
#
$d .= new: (ppp => 100, qqq => ( d => 110, e => 120));
is $d<ppp>, 100, "\$d<ppp> = $d<ppp>";
is $d<qqq><d>, 110, "\$d<qqq><d> = $d<qqq><d>";
# Init via hash inhibited
throws-like { $d .= new: ppp => 100, qqq => ( d => 110, e => 120); },
X::BSON, 'Cannot use hashes on init',
:message(/:s Cannot use hash values on init/);
}
#-------------------------------------------------------------------------------
subtest "Ban the hash", {
my BSON::Document $d .= new;
throws-like {
$d<q> = {a => 20};
is $d<q><a>, 20, "Hash value $d<q><a>";
}, X::BSON, 'Cannot use hashes when assigning',
:message(/:s Cannot use hash values/);
$d.accept-hash(True);
$d<q> = {
a => 120, b => 121, c => 122, d => 123, e => 124, f => 125, g => 126,
h => 127, i => 128, j => 129, k => 130, l => 131, m => 132, n => 133,
o => 134, p => 135, q => 136, r => 137, s => 138, t => 139, u => 140,
v => 141, w => 142, x => 143, y => 144, z => 145
};
is $d<q><a>, 120, "Hash value $d<q><a>";
my $x = $d<q>.keys.sort;
nok $x eqv $d<q>.keys.List, 'Not same order';
$d.autovivify(True);
$d<e><f><g> = {b => 30};
is $d<e><f><g><b>, 30, "Autovivified hash value $d<e><f><g><b>";
}
#if %*ENV<TRAVIS>:exists or '/home/marcel/Languages/Perl6'.IO ~~ :d {
subtest "Big, wide and deep nesting", {
# Keys must be sufficiently long and value complex enough to keep a
# thread busy causing the process to runout of available threads
# which are by default 16.
my Num $count = 0.1e0;
my BSON::Document $d .= new;
for ('zxnbcvzbnxvc-aa', *.succ ... 'zxnbcvzbnxvc-bz') -> $char {
$d{$char} = ($count += 2.44e0);
}
my BSON::Document $dsub .= new;
for ('uqwteuyqwte-aa', *.succ ... 'uqwteuyqwte-bz') -> $char {
$dsub{$char} = ($count += 2.1e0);
}
for ('uqwteuyqwte-da', *.succ ... 'uqwteuyqwte-dz') -> $char {
$d<x1>{$char} = ($count += 2.1e0);
$d<x2><x1>{$char} = $dsub.clone;
$d<x2><x2><x3>{$char} = $dsub.clone;
}
for ('jhgsajhgasjdg-ca', *.succ ... 'jhgsajhgasjdg-cz') -> $char {
$d{$char} = ($count -= 0.02e0);
}
for ('uqwteuyqwte-ea', *.succ ... 'uqwteuyqwte-ez') -> $char {
$d<x3>{$char} = $dsub.clone;
$d<x4><x1>{$char} = $dsub.clone;
$d<x4><x2><x3>{$char} = $dsub.clone;
}
$dsub .= new($d.encode);
is-deeply $dsub, $d, 'document the same after encoding/decoding';
}
#}
done-testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment