Phase 6: Unit Testing (Real-World Practice)
In professional software development, programs are not trusted until they have
automated tests. As the final step of this assignment, you will add a unit
testing component after finishing your implementation and report.
Objectives
• Validate the correctness of your search functions in isolation.
• Exercise edge cases without running the full CLI program.
• Gain real-world practice with automated testing.
What to Test
Write unit tests only for your core logic (do not call main() from main.cpp).
At a minimum, test:
• count matches linear
• count matches binary sorted
• count matches binary recursive
• (Optional) comparator functions for Book (operator==, operator<)
Starter File: tests.cpp
Below is a minimal unit test driver using assert. It is assuming that you created
enums form language and type but that is not necessarily required. You must
add at least 5 of your own tests (e.g., empty input, duplicates, type/language
mismatches).
#include <cassert>
#include <iostream>
#include <vector>
#include "book.h"
#include "search.h"
void test_linear_hit() {
std::vector<Book> newbooks = { {123, Lang::english, Type::New} };
std::vector<Book> requests = { {123, Lang::english, Type::New} };
assert(count_matches_linear(newbooks, requests) == 1);
}
void test_linear_miss() {
std::vector<Book> newbooks = { {123, Lang::english, Type::New} };
1std::vector<Book> requests = { {124, Lang::english, Type::New} };
assert(count_matches_linear(newbooks, requests) == 0);
}
int main() {
test_linear_hit();
test_linear_miss();
std::cout << "All unit tests passed!" << std::endl;
return 0;
}
2