summaryrefslogtreecommitdiff
path: root/source/test.cc
diff options
context:
space:
mode:
Diffstat (limited to 'source/test.cc')
-rw-r--r--source/test.cc96
1 files changed, 96 insertions, 0 deletions
diff --git a/source/test.cc b/source/test.cc
new file mode 100644
index 0000000..09e6bc5
--- /dev/null
+++ b/source/test.cc
@@ -0,0 +1,96 @@
+#include <algorithm>
+#include <array>
+#include <cstdlib>
+#include <exception>
+#include <iostream>
+#include <random>
+#include <ranges>
+#include <string>
+#include <string_view>
+
+#include <book_store/book.hh>
+#include <book_store/customer.hh>
+#include <book_store/random.hh>
+#include <book_store/utility.hh>
+
+auto main() -> int {
+ using namespace book_store;
+ using namespace book_store::utility;
+
+ std::array<product::book, 100> books;
+ std::array<std::string, 100> book_titles;
+ std::array<consumer::customer, 100> members;
+ std::array<std::size_t, 100> member_ids;
+ std::mt19937 random_number_generator(std::random_device{}());
+ utility::random::book_random_engine random(random_number_generator);
+ auto perform = [](std::string_view name, auto action) {
+ std::cout << name << " ...";
+
+ action();
+
+ std::cout << " ok.\n";
+ };
+
+ perform("populating books and book_titles", [&]() {
+ for (auto [index, book] : std::ranges::views::enumerate(books)) {
+ auto random_title = random.title();
+
+ book_titles[static_cast<std::size_t>(index)] = random_title;
+ book =
+ product::book{random_title, random.authors(), random.publisher(),
+ random.isbn(), random.price_usd(), random.copy_count()};
+ }
+ });
+ perform("verifying all books are present", [&]() {
+ for (const auto &title : book_titles) {
+ if (std::ranges::find(book_titles, title) == book_titles.end()) {
+ clear_cerr();
+
+ std::cerr << "error: title not found" << '\n';
+
+ std::terminate();
+ }
+ }
+ });
+ perform("verifying book copy count increment", [&]() {
+ for (int i = 0; i < 100; ++i) {
+ auto &book = books[std::uniform_int_distribution<std::size_t>(
+ 0, books.size() - 1)(random_number_generator)];
+ auto copy_count = book.copies();
+ auto random_copy_count = static_cast<product::book::size_type>(
+ std::uniform_int_distribution<std::size_t>(0, 100)(
+ random_number_generator));
+
+ book.copies(copy_count + random_copy_count);
+
+ if (book.copies() != copy_count + random_copy_count) {
+ clear_cerr();
+
+ std::cerr << "error: invalid copy count after increment" << '\n';
+
+ std::terminate();
+ }
+ }
+ });
+ perform("populating members and member_ids", [&]() {
+ for (auto [index, member] : std::views::enumerate(members)) {
+ auto random_name = random.name();
+ auto random_surname = random.name();
+ auto random_id = random.id();
+
+ member_ids[static_cast<std::size_t>(index)] = random_id;
+ member = consumer::customer{random_name, random_surname, random_id};
+ }
+ });
+ perform("verifying all members are present", [&]() {
+ for (auto member_id : member_ids) {
+ if (std::ranges::find(member_ids, member_id) == member_ids.end()) {
+ std::cerr << "error: id not found" << '\n';
+
+ std::terminate();
+ }
+ }
+ });
+
+ return 0;
+}