CPP08
1.0
読み取り中…
検索中…
一致する文字列を見つけられません
main.cpp
[詳解]
1
/* ************************************************************************** */
2
/* */
3
/* ::: :::::::: */
4
/* main.cpp :+: :+: :+: */
5
/* +:+ +:+ +:+ */
6
/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7
/* +#+#+#+#+#+ +#+ */
8
/* Created: 2025/08/22 00:03:49 by kamitsui #+# #+# */
9
/* Updated: 2025/08/22 00:05:00 by kamitsui ### ########.fr */
10
/* */
11
/* ************************************************************************** */
12
24
#include "
Span.hpp
"
25
#include <
cstdlib
>
// For rand() and srand()
26
#include <
ctime
>
// For time()
27
#include <
iostream
>
28
#include <
vector
>
29
33
void
subject_test
() {
34
std::cout
<<
"--- Subject Test ---"
<<
std::endl
;
35
Span
sp =
Span
(5);
36
37
sp.
addNumber
(6);
38
sp.
addNumber
(3);
39
sp.
addNumber
(17);
40
sp.
addNumber
(9);
41
sp.
addNumber
(11);
42
43
std::cout
<<
"Shortest span: "
<< sp.
shortestSpan
() <<
std::endl
;
// Should be 2 (9 - 11)
44
std::cout
<<
"Longest span: "
<< sp.
longestSpan
() <<
std::endl
;
// Should be 14 (17 - 3)
45
std::cout
<<
std::endl
;
46
}
47
51
void
large_test
() {
52
std::cout
<<
"--- Large Scale Test (15,000 numbers) ---"
<<
std::endl
;
53
const
int
num_elements = 15000;
54
Span
sp =
Span
(num_elements);
55
56
std::vector<int>
source_numbers;
57
source_numbers.
reserve
(num_elements);
58
srand(time(0));
// Seed for random numbers
59
60
for
(
int
i = 0; i < num_elements; ++i) {
61
source_numbers.
push_back
(rand());
62
}
63
64
// Use the iterator range method to add numbers
65
try
{
66
sp.
addNumber
(source_numbers.
begin
(), source_numbers.
end
());
67
std::cout
<<
"Successfully added 15,000 numbers."
<<
std::endl
;
68
std::cout
<<
"Shortest span: "
<< sp.
shortestSpan
() <<
std::endl
;
69
std::cout
<<
"Longest span: "
<< sp.
longestSpan
() <<
std::endl
;
70
}
catch
(
const
std::exception
&e) {
71
std::cerr
<<
"Error during large test: "
<< e.
what
() <<
std::endl
;
72
}
73
std::cout
<<
std::endl
;
74
}
75
79
void
exception_test
() {
80
std::cout
<<
"--- Exception Tests ---"
<<
std::endl
;
81
Span
sp(3);
82
std::cout
<<
"Span created with size 3."
<<
std::endl
;
83
84
// Test NotEnoughNumbersException
85
std::cout
<<
"\nTesting shortestSpan with 0 elements..."
<<
std::endl
;
86
try
{
87
sp.
shortestSpan
();
88
}
catch
(
const
std::exception
&e) {
89
std::cerr
<<
"Caught expected exception: "
<< e.
what
() <<
std::endl
;
90
}
91
92
sp.
addNumber
(100);
93
std::cout
<<
"Added one number (100)."
<<
std::endl
;
94
std::cout
<<
"Testing longestSpan with 1 element..."
<<
std::endl
;
95
try
{
96
sp.
longestSpan
();
97
}
catch
(
const
std::exception
&e) {
98
std::cerr
<<
"Caught expected exception: "
<< e.
what
() <<
std::endl
;
99
}
100
101
// Test SpanFullException
102
sp.
addNumber
(200);
103
sp.
addNumber
(300);
104
std::cout
<<
"Filled the Span (100, 200, 300)."
<<
std::endl
;
105
std::cout
<<
"Attempting to add one more number..."
<<
std::endl
;
106
try
{
107
sp.
addNumber
(400);
108
}
catch
(
const
std::exception
&e) {
109
std::cerr
<<
"Caught expected exception: "
<< e.
what
() <<
std::endl
;
110
}
111
std::cout
<<
std::endl
;
112
}
113
119
int
main
() {
120
subject_test
();
121
large_test
();
122
exception_test
();
123
return
0;
124
}
Span.hpp
Provides a class that stores N integers and calculates their shortest and longest spans.
std::vector::begin
T begin(T... args)
std::cerr
Span
A class that stores up to N integers and calculates the shortest and longest spans.
Definition
Span.hpp:32
Span::shortestSpan
int shortestSpan()
Calculates the shortest span between stored numbers.
Definition
Span.cpp:53
Span::longestSpan
int longestSpan()
Calculates the longest span between stored numbers.
Definition
Span.cpp:74
Span::addNumber
void addNumber(int number)
Adds a single number to the span.
Definition
Span.cpp:45
std::cout
cstdlib
ctime
std::vector::end
T end(T... args)
std::endl
T endl(T... args)
main
int main()
Program entry point.
Definition
main.cpp:49
subject_test
void subject_test()
Basic test cases specified in the assignment.
Definition
main.cpp:33
exception_test
void exception_test()
Test whether exceptions are thrown correctly.
Definition
main.cpp:79
large_test
void large_test()
Test for handling large amounts of numerical data.
Definition
main.cpp:51
std::exception
iostream
std::vector::push_back
T push_back(T... args)
std::vector::reserve
T reserve(T... args)
vector
std::exception::what
T what(T... args)
ex01
main.cpp
構築:
1.9.8