Skip to content
Browse files

- added basic deletion tests

  • Loading branch information...
1 parent d36d5b3 commit fc32af3b7bfe3f989ee8e3875fa33fc80cac1962 aaron cohen committed Aug 10, 2011
Showing with 30 additions and 13 deletions.
  1. +2 −1 lib/Tree/SEMETrie.pm
  2. +2 −0 lib/Tree/SEMETrie/Fast.pm
  3. +26 −12 t/03-basic-insertion.t
View
3 lib/Tree/SEMETrie.pm
@@ -10,13 +10,14 @@ BEGIN {
my $PACKAGE = __PACKAGE__;
my $IMPLEMENTATION = 'Tree::SEMETrie::Fast';
- my @METHODS = qw{new childs value has_childs has_value add find};
+ my @METHODS = qw{new childs value has_childs has_value add find remove};
no strict 'refs';
no warnings 'once';
eval "require $IMPLEMENTATION";
*{"$PACKAGE\::$_"} = \*{"$IMPLEMENTATION\::$_"} for @METHODS;
*{"$PACKAGE\::children"} = \*{"$IMPLEMENTATION\::childs"};
+ *{"$PACKAGE\::erase"} = \*{"$IMPLEMENTATION\::remove"};
*{"$PACKAGE\::has_children"} = \*{"$IMPLEMENTATION\::has_childs"};
*{"$PACKAGE\::insert"} = \*{"$IMPLEMENTATION\::add"};
*{"$PACKAGE\::lookup"} = \*{"$IMPLEMENTATION\::find"};
View
2 lib/Tree/SEMETrie/Fast.pm
@@ -303,6 +303,8 @@ sub erase {
my $self = shift;
my ($key) = @_;
+ return undef unless defined $key;
+
my $grand_parent_node = undef;
my $parent_node = undef;
my $node = $self;
View
38 t/03-basic-insertion.t
@@ -3,49 +3,63 @@
use strict;
use warnings;
-use Test::More tests => 25;
+use Test::More tests => 40;
use Test::Exception;
use Tree::SEMETrie;
my $trie = Tree::SEMETrie->new();
#Check that a trie will only find what was inserted
-ok ! $trie->find('a'), 'Non-existent key not found';
+ok ! $trie->find('a'), 'Non-existent key not found';
+ok ! $trie->remove('a'), 'Non-existent key not removed';
#Check that undefined keys cannot be stored
-dies_ok { $trie->add(undef) } 'Undefined key can not be stored';
-ok ! defined($trie->find(undef)), 'Undefined key was not retrieved';
+dies_ok { $trie->add(undef) } 'Storing undefined key throws error';
+ok ! defined($trie->find(undef)), 'Retrieving undefined key returns undef';
+ok ! defined($trie->remove(undef)), 'Removing undefined key returns undef';
#Check that a key can be stored and retrieved
ok $trie->add('a'), 'Key without defined value added successfully';
ok $trie->find('a'), 'Key without defined value found';
ok $trie->find('a')->has_value, 'Key without defined value has value';
ok ! defined($trie->find('a')->value), 'Value of key without defined value is undefined';
+ok ! defined($trie->remove('a')), 'Removing Key without defined value returns undef';
+ok ! $trie->find('a'), 'Removed key without defined value not found';
#Check that a key-value pair can be stored and retrieved
ok $trie->add('b', 2), 'Key with defined value added successfully';
ok $trie->find('b'), 'Key with defined value found';
ok $trie->find('b')->has_value, 'Key with defined value has value';
is $trie->find('b')->value, 2, 'Key with defined value fetched successfully';
-
-#Check that the empty string is equivalent to the root
-ok $trie->add('', 'root-value'), 'Empty string key added successfully';
-is_deeply $trie->find(''), $trie, 'Empty string key is equivalent to root';
+is $trie->remove('b'), 2, 'Removing key with defined value returns its value';
+ok ! $trie->find('b'), 'Removed key with defined value not found';
#Check that numerical values are not an issue
ok $trie->add(0, 6), 'Numerically false key added successfully';
ok $trie->find(0), 'Numerically false key found';
ok $trie->find(0)->has_value, 'Numerically false key has value';
is $trie->find(0)->value, 6, 'Numerically false key fetched successfully';
+is $trie->remove(0), 6, 'Removing numerically false key returns its value';
+ok ! $trie->find(0), 'Removed numerically false key not found';
-ok $trie->add(27, 8), 'Numerical key added successfully';
-ok $trie->find(27), 'Numerical key found';
-ok $trie->find(27)->has_value, 'Numerical key has value';
-is $trie->find(27)->value, 8, 'Numerical key fetched successfully';
+ok $trie->add(9, 8), 'Numerically true key added successfully';
+ok $trie->find(9), 'Numerically true key found';
+ok $trie->find(9)->has_value, 'Numerically true key has value';
+is $trie->find(9)->value, 8, 'Numerically true key fetched successfully';
+is $trie->remove(9), 8, 'Removing numerically true key returns its value';
+ok ! $trie->find(9), 'Removed numerically true key not found';
#Check that unicode is not an issue
ok $trie->add("\x{b3c3}", 10), 'Unicode key added succesffuly';
ok $trie->find("\x{b3c3}"), 'Unicode key found';
ok $trie->find("\x{b3c3}")->has_value, 'Unicode key has value';
is $trie->find("\x{b3c3}")->value, 10, 'Unicode key fetched successfully';
+is $trie->remove("\x{b3c3}"), 10, 'Removing unicode key returns its value';
+ok ! $trie->find("\x{b3c3}"), 'Removed unicode key not found';
+#Check that the empty string is equivalent to the root
+ok $trie->add('', 'root-value'), 'Empty string key added successfully';
+is $trie->find(''), $trie, 'Empty string key is equivalent to root';
+is $trie->remove(''), 'root-value', 'Removing empty string key returns root value';
+ok $trie->find(''), 'Empty strng key can always be found';
+ok ! $trie->find('')->has_value, 'Removed empty string key has no value';

0 comments on commit fc32af3

Please sign in to comment.
Something went wrong with that request. Please try again.