summaryrefslogtreecommitdiff
path: root/source/test.cc
blob: 09e6bc53b8d3bd1878519208f02d300942c2ab6c (plain) (blame)
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
#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;
}