### ----------------------------------------------------
### -- Hash::LRU
### -- Licenses: Artistic-2.0
### -- Authors: Elizabeth Mattijsen
### -- File: t/Cache-LRU.rakutest
### ----------------------------------------------------
use Test;

use Hash::LRU;

plan 38;

#my $cache = Cache::LRU.new(size => 3);
my $cache = my % is LRU(elements  => 3);

ok ! defined $cache.get('a');

is $cache.set('a' , 1), 1;
is $cache.get('a'), 1;

is $cache.set('b' , 2), 2;
is $cache.get('a'), 1;
is $cache.get('b'), 2;

is $cache.set('c' , 3), 3;
is $cache.get('a'), 1;
is $cache.get('b'), 2;
is $cache.get('c'), 3;

is $cache.set('b' , 4), 4;
is $cache.get('a'), 1;
is $cache.get('b'), 4;
is $cache.get('c'), 3;

my $keep = $cache.get('a');
is $keep, 1; # the order is now a , c , b
is $cache.set('d' , 5), 5;
is $cache.get('a'), 1;
ok ! defined $cache.get('b');
is $cache.get('c'), 3;
is $cache.get('d'), 5; # the order is now d , c , a
is $cache.set('e', 6), 6;
ok ! defined $cache.get('a');
ok ! defined $cache.get('b');
is $cache.get('c'), 3;
is $cache.get('d'), 5;
is $cache.get('e'), 6;

is $cache.remove('d'), 5;
is $cache.get('c'), 3;
ok ! defined $cache.get('d');
is $cache.get('e'), 6;

$cache.clear;
ok ! defined $cache.get('c');
ok ! defined $cache.get('e');

# Supports associative operations
is ( $cache< a b c > = 1, 2, 3 ), ( 1, 2, 3);
ok $cache<a>:exists;
is $cache<a>, 1;
is ++$cache<b>, 3;
is $cache<c>:delete, 3;
ok $cache<c>:!exists;

# vim: expandtab shiftwidth=4


