Skip to content

Commit

Permalink
Added some unit tests & fixed bug in TBHashTable.
Browse files Browse the repository at this point in the history
Added missing GetNumItems and decrement count
when items are removed.
  • Loading branch information
fruxo committed Nov 26, 2016
1 parent 85632c8 commit 898cc72
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions Demo/VisualStudio/tb_static.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<ClCompile Include="..\..\src\tb\tests\test_tb_color.cpp" />
<ClCompile Include="..\..\src\tb\tests\test_tb_dimension.cpp" />
<ClCompile Include="..\..\src\tb\tests\test_tb_geometry.cpp" />
<ClCompile Include="..\..\src\tb\tests\test_tb_hashtable.cpp" />
<ClCompile Include="..\..\src\tb\tests\test_tb_linklist.cpp" />
<ClCompile Include="..\..\src\tb\tests\test_tb_node_ref_tree.cpp" />
<ClCompile Include="..\..\src\tb\tests\test_tb_object.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions Demo/VisualStudio/tb_static.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@
<ClCompile Include="..\..\src\tb\image\tb_image_manager.cpp">
<Filter>Source Files\image</Filter>
</ClCompile>
<ClCompile Include="..\..\src\tb\tests\test_tb_hashtable.cpp">
<Filter>Source Files\tests</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\tb\tb_language.h">
Expand Down
1 change: 1 addition & 0 deletions src/tb/tb_hashtable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ void *TBHashTable::Remove(uint32 key)
prev_item->next = item->next;
else
m_buckets[bucket] = item->next;
m_num_items--;
void *content = item->content;
delete item;
return content;
Expand Down
5 changes: 4 additions & 1 deletion src/tb/tb_hashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class TBHashTable
the current number of items. */
uint32 GetSuitableBucketsCount() const;

/** Get the number of items in the hash table. */
uint32 GetNumItems() const { return m_num_items; }

#ifdef TB_RUNTIME_DEBUG_INFO
/** Print out some debug info about the hash table. */
void Debug();
Expand Down Expand Up @@ -101,7 +104,7 @@ class TBHashTableOf : public TBHashTable
// FIX: Don't do public inheritance! Either inherit privately and forward, or use a private member backend!
public:
T *Get(uint32 key) const { return (T*) TBHashTable::Get(key); }

T *Remove(uint32 key) { return (T*) TBHashTable::Remove(key); }
protected:
virtual void DeleteContent(void *content) { delete (T*) content; }
};
Expand Down
1 change: 1 addition & 0 deletions src/tb/tests/tb_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ TB_FORCE_LINK_TEST_GROUP(tb_color);
TB_FORCE_LINK_TEST_GROUP(tb_dimension_converter);
TB_FORCE_LINK_TEST_GROUP(tb_geometry);
TB_FORCE_LINK_TEST_GROUP(tb_linklist);
TB_FORCE_LINK_TEST_GROUP(tb_hashtable);
TB_FORCE_LINK_TEST_GROUP(tb_node_ref_tree);
TB_FORCE_LINK_TEST_GROUP(tb_object);
TB_FORCE_LINK_TEST_GROUP(tb_parser);
Expand Down
55 changes: 55 additions & 0 deletions src/tb/tests/test_tb_hashtable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// ================================================================================
// == This file is a part of Turbo Badger. (C) 2011-2016, Emil Segerås ==
// == See tb_core.h for more information. ==
// ================================================================================

#include "tb_test.h"
#include "tb_hashtable.h"

#ifdef TB_UNIT_TESTING

using namespace tb;

TB_TEST_GROUP(tb_hashtable)
{
class Apple
{
public:
Apple(int id) : id(id) { total_apple_count++; }
~Apple() { total_apple_count--; }

int id;
static int total_apple_count;
};
int Apple::total_apple_count = 0;

TBHashTableOf<Apple> map;

TB_TEST(simple)
{
map.Add(1, new Apple(1));
map.Add(2, new Apple(2));
TB_VERIFY(map.GetNumItems() == 2);
TB_VERIFY(map.Get(1)->id == 1);
TB_VERIFY(map.Get(2)->id == 2);
TB_VERIFY(map.Remove(1)->id == 1);
TB_VERIFY(map.Remove(2)->id == 2);
TB_VERIFY(map.GetNumItems() == 0);
}

TB_TEST(autodelete)
{
// Check that the apples really are destroyed.
int old_total_apple_count = Apple::total_apple_count;
// Scope for TBLinkListAutoDeleteOf
{
TBHashTableAutoDeleteOf<Apple> autodelete_list;
autodelete_list.Add(1, new Apple(1));
autodelete_list.Add(2, new Apple(2));
TB_VERIFY(Apple::total_apple_count == old_total_apple_count + 2);
}
TB_VERIFY(Apple::total_apple_count == old_total_apple_count);
}
}

#endif // TB_UNIT_TESTING

0 comments on commit 898cc72

Please sign in to comment.