CPP05 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
main.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* main.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/06/26 09:59:06 by kamitsui #+# #+# */
9/* Updated: 2025/06/29 05:47:09 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
21#include "Bureaucrat.hpp"
22#include <iostream>
23
28void print_header(const std::string &title) { std::cout << "\n\033[1;33m--- " << title << " ---\033[0m" << std::endl; }
29
34int main() {
35
36 // --- Test 1: Valid Bureaucrat Creation ---
37 print_header("Test 1: Valid Bureaucrat Creation");
38 try {
39 Bureaucrat b1("Zaphod", 1);
40 std::cout << b1 << std::endl;
41
42 Bureaucrat b2("Arthur", 150);
43 std::cout << b2 << std::endl;
44
45 Bureaucrat b3("Ford", 42);
46 std::cout << b3 << std::endl;
47 } catch (const std::exception &e) {
48 std::cerr << "Caught exception: " << e.what() << std::endl;
49 }
50
51 // --- Test 2: Grade Too High at Instantiation ---
52 print_header("Test 2: Grade Too High at Instantiation");
53 try {
54 Bureaucrat high("Highflyer", 0);
55 std::cout << high << std::endl;
56 } catch (const std::exception &e) {
57 std::cerr << "\033[1;31mCaught expected exception: " << e.what() << "\033[0m" << std::endl;
58 }
59
60 // --- Test 3: Grade Too Low at Instantiation ---
61 print_header("Test 3: Grade Too Low at Instantiation");
62 try {
63 Bureaucrat low("Lowballer", 151);
64 std::cout << low << std::endl;
65 } catch (const std::exception &e) {
66 std::cerr << "\033[1;31mCaught expected exception: " << e.what() << "\033[0m" << std::endl;
67 }
68
69 // --- Test 4: Grade Increment/Decrement ---
70 print_header("Test 4: Grade Increment/Decrement");
71 try {
72 Bureaucrat mid("Marvin", 75);
73 std::cout << "Initial: " << mid << std::endl;
74
75 mid.incrementGrade();
76 std::cout << "After increment: " << mid << std::endl;
77
78 mid.decrementGrade();
79 std::cout << "After decrement: " << mid << std::endl;
80
81 } catch (const std::exception &e) {
82 std::cerr << "Caught exception: " << e.what() << std::endl;
83 }
84
85 // --- Test 5: Incrementing to the Limit ---
86 print_header("Test 5: Incrementing to the Limit");
87 try {
88 Bureaucrat top("Trillian", 2);
89 std::cout << "Initial: " << top << std::endl;
90 top.incrementGrade();
91 std::cout << "After increment: " << top << std::endl;
92
93 std::cout << "Attempting to increment again..." << std::endl;
94 top.incrementGrade(); // This should throw
95 } catch (const std::exception &e) {
96 std::cerr << "\033[1;31mCaught expected exception: " << e.what() << "\033[0m" << std::endl;
97 }
98
99 // --- Test 6: Decrementing to the Limit ---
100 print_header("Test 6: Decrementing to the Limit");
101 try {
102 Bureaucrat bottom("Slartibartfast", 149);
103 std::cout << "Initial: " << bottom << std::endl;
104 bottom.decrementGrade();
105 std::cout << "After decrement: " << bottom << std::endl;
106
107 std::cout << "Attempting to decrement again..." << std::endl;
108 bottom.decrementGrade(); // This should throw
109 } catch (const std::exception &e) {
110 std::cerr << "\033[1;31mCaught expected exception: " << e.what() << "\033[0m" << std::endl;
111 }
112
113 // --- Test 7: Copy and Assignment ---
114 print_header("Test 7: Copy and Assignment");
115 try {
116 Bureaucrat original("Original", 50);
117 std::cout << "Original: " << original << std::endl;
118
119 // Test copy constructor
120 Bureaucrat copy(original);
121 std::cout << "Copy: " << copy << std::endl;
122
123 Bureaucrat assigner("Assigner", 100);
124 std::cout << "Assigner: " << assigner << std::endl;
125
126 // Test assignment operator
127 // Note: The name 'Copy' will not change, only its grade.
128 copy = assigner;
129 std::cout << "Copy after assignment from Assigner: " << copy << std::endl;
130 } catch (const std::exception &e) {
131 std::cerr << "Caught unexpected exception: " << e.what() << std::endl;
132 }
133
134 print_header("All tests finished. Bureaucrats being destroyed now.");
135 return 0;
136}
Represents a bureaucrat with a name and a grade.
void incrementGrade()
Increments the bureaucrat's grade (e.g., from 3 to 2).
void decrementGrade()
Decrements the bureaucrat's grade (e.g., from 2 to 3).
T endl(T... args)
void print_header(const std::string &title)
Prints a formatted header to the console.
Definition main.cpp:28
int main()
Main entry point of the program.
Definition main.cpp:34
Defines the Bureaucrat class, updated to execute AForms.
T what(T... args)