-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_tests.cpp
101 lines (87 loc) · 3.25 KB
/
hash_tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "HashTable.hpp"
#include "Book.hpp"
/**
* @brief This file performs all unit tests for the hash table class
* and it's main operations including search,insert and remove
* @see README.md for compilation command
*
* hash_tests.cpp
* AUTHOR : M00716650
* CREATED : 8/03/2021
* UPDATED : 27/04/2021
*
*/
Hash h = Hash(419);
Book b = Book();
// testing hash function giving different output for each key
TEST_CASE("testing hash function", "[hash]")
{
REQUIRE(h.hash_title("Head First SQL") == 207);
REQUIRE(h.hash_title("Handbook of Graph Theory") == 108);
REQUIRE(h.hash_title("Learning MySQL") == 95);
// testing that hash function gives same hash value for equal input
REQUIRE(h.hash_title("Learning MySQL") == h.hash_title("Learning MySQL") );
}
// testing to see if ISBN gets stored entily when it's type is unsigned long
TEST_CASE("testing ISBN gets stored entirly","[ISBN]")
{
b.setISBN(9780130457868);
REQUIRE(b.getISBN() == 9780130457868);
}
// testing insert functionality when inserting dublicate books
TEST_CASE("testing dublicate insertion of books","[Insert]")
{
std::vector<std::string> authors;
authors.push_back("J. B. Priestley");
authors.push_back("Tom H");
Book book = Book("Inspector Calls",authors,9780130457868,2);
// checking for dublicates returning false when book is not already stored
REQUIRE(h.check_dublicates(book.getTitle()) == false);
h.insert(book);
// checking for dublicates returning true when same book exists
REQUIRE(h.check_dublicates(book.getTitle()) == true);
}
TEST_CASE("removing books","[Remove]"){
std::vector<std::string> authors;
authors.push_back("John Steinbeck");
authors.push_back("Tom H");
Book book = Book("of mice and men",authors,9780130457868,3);
h.insert(book);
// removing all copies
for (size_t i = 0; i < book.getQuantity(); i++)
{
h.remove_book(book.getTitle());
// testing book exists when removing 1 copy each iteration
REQUIRE(h.check_dublicates("of mice and men") == true);
}
// testing book shouldn't exist when book object is removed
h.remove_book(book.getTitle());
REQUIRE(h.check_dublicates("of mice and men") == false);
}
/**
* @brief testing when searching for two books with same hash value.
* then search function should only return the matching book title
* and not the first book in the table cell
*/
TEST_CASE("testing search function","[search]"){
// inserting two book objects that same the hash value
std::vector<std::string> authors;
authors.push_back("John Steinbeck");
authors.push_back("Tom H");
Book book1 = Book("Logic Programming '89",authors,9780130457868,3);
Book book2 = Book("Operating Systems and Middleware",authors,9780130457868,3);
// checking that hash value for both books are equal
REQUIRE(h.hash_title(book1.getTitle()) == h.hash_title(book2.getTitle()));
h.insert(book1);
h.insert(book2);
/*
performing a search for first book and second book by it's title
and checking that returned result matches searched title
*/
Book result1 = h.search(book1.getTitle());
REQUIRE(result1.getTitle() == book1.getTitle());
Book result2 = h.search(book2.getTitle());
REQUIRE(result2.getTitle() == book2.getTitle());
}