BEGIN {
unshift
@INC
,
'.'
}
sub
set {Set::Toolkit->new(
@_
)};
my
$default_set
=
sub
{
my
$set
= set();
$set
->insert(
qw(a b c)
);
$set
->insert(
{
a
=> 123,
b
=>
'abc'
},
{
a
=> 123,
b
=>
'def'
},
{
a
=> 456,
b
=>
'hij'
},
);
return
$set
;
};
{
my
$desc
=
"testing construction"
;
my
$set
= Set::Toolkit->new();
isa_ok(
$set
,
'Set::Toolkit'
,
"$desc: set isa Set::Toolkit"
);
}
{
my
$desc
=
"scalar insertion"
;
my
$set
= set();
$set
->insert(
'a'
);
is(
$set
->size, 1,
"$desc: size is updated"
);
$set
->insert(
'a'
);
is(
$set
->size, 1,
"$desc: sets are unique by default"
);
is(
$set
->is_unique, 1,
"$desc: sets *know* they are unique by default"
);
$set
->insert(
qw(b c)
);
is(
$set
->size, 3,
"$desc: multiple unique inserts work"
);
$set
->insert(
qw(b c)
);
is(
$set
->size, 3,
"$desc: multiple dupes don't get inserted"
);
$set
->insert(
qw(d c e a)
);
is(
$set
->size, 5,
"$desc: mixed (dup/unique) only insert unique"
);
}
{
my
$desc
=
"unordered by default"
;
my
$set
=
$default_set
->();
my
@els
=
$set
->elements;
is(
scalar
(
@els
), 6,
"$desc: the ->elements method returns all values"
);
isnt(
$els
[0],
'a'
,
"$desc: the elements are shuffled"
);
@els
=
$set
->ordered_elements;
is(
$els
[0],
'a'
,
"$desc: order can be coerced"
);
$set
->is_ordered(1);
@els
=
$set
->elements;
is(
$els
[0],
'a'
,
"$desc: order can be required"
);
@els
=
$set
->unordered_elements;
isnt(
$els
[0],
'a'
,
"$desc: disorder can be coerced"
);
}
{
my
$desc
=
"ref insertion"
;
my
$set
= set();
$set
->insert(
qw(a b c)
);
$set
->insert({
abc
=> 123});
is(
$set
->size, 4,
"$desc: inserted a hashref after some scalars"
);
}
{
my
$desc
=
"removal"
;
my
$set
=
$default_set
->();
$set
->remove(
'b'
);
is(
$set
->size, 5,
"$desc: removing a scalar decrements size"
);
}