-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from ermolaevv/erm
Added ListTable
- Loading branch information
Showing
8 changed files
with
181 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,80 @@ | ||
#include "ListTable.h" | ||
template<class Key, class Value> | ||
Value* ListTable<Key, Value>::Find(Key key) | ||
{ | ||
return new Value(); | ||
data.Reset(); | ||
|
||
while (!data.IsListEnded()) { | ||
auto* record = data.GetDatValue(); | ||
if (record == nullptr) | ||
break; | ||
|
||
if (record->key == key) | ||
return &record->value; | ||
|
||
data.GoNext(); | ||
} | ||
|
||
throw std::runtime_error("Key not found in the table."); | ||
} | ||
|
||
template<class Key, class Value> | ||
void ListTable<Key, Value>::Insert(Key key, Value value) | ||
{ | ||
try { | ||
Value* recValue = Find(key); | ||
*recValue = value; | ||
} | ||
catch (std::runtime_error) { | ||
if (!IsFull()) | ||
data.InsLast(new Table<Key, Value>::STableRec<Key, Value>(key, value)); | ||
else | ||
throw std::length_error("Table is overflow"); | ||
} | ||
} | ||
|
||
template<class Key, class Value> | ||
void ListTable<Key, Value>::Delete(Key key) | ||
{ | ||
Value* recValue = Find(key); | ||
data.DelCurrent(); | ||
} | ||
|
||
template<class Key, class Value> | ||
size_t ListTable<Key, Value>::Reset(void) noexcept | ||
{ | ||
data.Reset(); | ||
return Table<Key, Value>::Reset(); | ||
} | ||
|
||
template<class Key, class Value> | ||
bool ListTable<Key, Value>::IsTabEnded(void) const noexcept | ||
{ | ||
return false; | ||
return data.IsListEnded(); | ||
} | ||
|
||
template<class Key, class Value> | ||
size_t ListTable<Key, Value>::GoNext(void) noexcept | ||
{ | ||
return size_t(); | ||
this->position = data.GoNext(); | ||
return this->position; | ||
} | ||
|
||
template<class Key, class Value> | ||
Key ListTable<Key, Value>::GetKey(void) const | ||
{ | ||
return Key(); | ||
auto* rec = data.GetDatValue(); | ||
if (rec == nullptr) | ||
throw std::runtime_error("Getting key from empty table"); | ||
return rec->key; | ||
} | ||
|
||
template<class Key, class Value> | ||
Value ListTable<Key, Value>::GetValuePtr(void) const | ||
{ | ||
return Value(); | ||
auto* rec = data.GetDatValue(); | ||
if (rec == nullptr) | ||
throw std::runtime_error("Getting value from empty table"); | ||
return rec->value; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#include <gtest.h> | ||
#include "ListTable.h" | ||
using namespace std; | ||
|
||
TEST(ListTable, Get_size) { | ||
ListTable<string, int> table; | ||
ASSERT_EQ(0, table.GetDataCount()); | ||
ASSERT_NO_THROW(table.Insert("r1", 1234)); | ||
ASSERT_EQ(1, table.GetDataCount()); | ||
} | ||
|
||
TEST(ListTable, Is_empty) { | ||
ListTable<string, int> table; | ||
ASSERT_EQ(true, table.IsEmpty()); | ||
ASSERT_NO_THROW(table.Insert("r1", 1234)); | ||
ASSERT_EQ(false, table.IsEmpty()); | ||
} | ||
|
||
TEST(ListTable, Is_full) { | ||
ListTable<string, int> table(1); | ||
ASSERT_EQ(false, table.IsFull()); | ||
ASSERT_NO_THROW(table.Insert("r1", 1234)); | ||
ASSERT_EQ(true, table.IsFull()); | ||
} | ||
|
||
TEST(ListTable, Insert) { | ||
ListTable<string, int> table; | ||
ASSERT_NO_THROW(table.Insert("r1", 1234)); | ||
ASSERT_EQ(1, table.GetDataCount()); | ||
} | ||
|
||
TEST(ListTable, Delete_exist_record) { | ||
ListTable<string, int> table; | ||
ASSERT_NO_THROW(table.Insert("r1", 1234)); | ||
ASSERT_NO_THROW(table.Delete("r1")); | ||
ASSERT_EQ(0, table.GetDataCount()); | ||
} | ||
|
||
TEST(ListTable, Delete_not_exist_record) { | ||
ListTable<string, int> table; | ||
ASSERT_THROW(table.Delete("r1"), std::runtime_error); | ||
} | ||
|
||
TEST(ListTable, Find_exist_record) { | ||
ListTable<string, int> table; | ||
ASSERT_NO_THROW(table.Insert("r1", 1234)); | ||
|
||
int* val; | ||
ASSERT_NO_THROW(val = table.Find("r1")); | ||
ASSERT_EQ(1234, *val); | ||
} | ||
|
||
TEST(ListTable, Find_not_exist_record) { | ||
ListTable<string, int> table; | ||
ASSERT_THROW(table.Find("r1"), std::runtime_error); | ||
} | ||
|
||
TEST(ListTable, Reset) { | ||
ListTable<string, int> table; | ||
ASSERT_EQ(0, table.Reset()); | ||
} | ||
|
||
TEST(ListTable, Is_table_ended) { | ||
ListTable<string, int> table; | ||
ASSERT_NO_THROW(table.Insert("r1", 1234)); | ||
ASSERT_EQ(false, table.IsTabEnded()); | ||
ASSERT_NO_THROW(table.GoNext()); | ||
ASSERT_EQ(true, table.IsTabEnded()); | ||
} | ||
|
||
TEST(ListTable, Go_next) { | ||
ListTable<string, int> table; | ||
ASSERT_NO_THROW(table.Insert("r1", 1234)); | ||
ASSERT_NO_THROW(table.Insert("r2", 1234)); | ||
ASSERT_EQ(1, table.GoNext()); | ||
ASSERT_EQ(0, table.GoNext()); | ||
} | ||
|
||
TEST(ListTable, Get_key) { | ||
ListTable<string, int> table; | ||
ASSERT_NO_THROW(table.Insert("r1", 1234)); | ||
ASSERT_EQ("r1", table.GetKey()); | ||
} | ||
|
||
TEST(ListTable, Get_key_from_empty_table) { | ||
ListTable<string, int> table; | ||
ASSERT_THROW(table.GetKey(), std::runtime_error); | ||
} | ||
|
||
TEST(ListTable, Get_value) { | ||
ListTable<string, int> table; | ||
ASSERT_NO_THROW(table.Insert("r1", 1234)); | ||
ASSERT_EQ(1234, table.GetValuePtr()); | ||
} | ||
|
||
TEST(ListTable, Get_value_from_empty_table) { | ||
ListTable<string, int> table; | ||
ASSERT_THROW(table.GetValuePtr(), std::runtime_error); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters