CPP07 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
main.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* main.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/08/08 00:02:37 by kamitsui #+# #+# */
9/* Updated: 2025/08/08 00:03:53 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
18#include "Array.hpp"
19#include <iostream>
20#include <string>
21
26 std::cout << "--- Test: Default Constructor ---" << std::endl;
27 Array<int> a;
28 std::cout << "Size of default array: " << a.size() << std::endl;
29 try {
30 std::cout << "Accessing a[0]... ";
31 a[0] = 10; // This should throw an exception
32 std::cout << "SUCCESS (This should not be printed)" << std::endl;
33 } catch (const std::exception &e) {
34 std::cout << "Caught exception as expected: " << e.what() << std::endl;
35 }
37}
38
43 std::cout << "--- Test: Unsigned Int Constructor ---" << std::endl;
44 Array<int> a(5);
45 std::cout << "Size of array: " << a.size() << std::endl;
46 std::cout << "Initial values: ";
47 for (unsigned int i = 0; i < a.size(); ++i) {
48 std::cout << a[i] << " ";
49 }
51
52 // Modify values
53 for (unsigned int i = 0; i < a.size(); ++i) {
54 a[i] = i * 10;
55 }
56 std::cout << "Modified values: ";
57 for (unsigned int i = 0; i < a.size(); ++i) {
58 std::cout << a[i] << " ";
59 }
61}
62
67 std::cout << "--- Test: Copy Constructor and Assignment Operator ---" << std::endl;
69 original[0] = "one";
70 original[1] = "two";
71 original[2] = "three";
72
73 std::cout << "Original array: ";
74 for (unsigned int i = 0; i < original.size(); ++i) {
75 std::cout << original[i] << " ";
76 }
78
79 // Test Copy Constructor
81 std::cout << "Copied array: ";
82 for (unsigned int i = 0; i < copy.size(); ++i) {
83 std::cout << copy[i] << " ";
84 }
86
87 // Test Assignment Operator
90 std::cout << "Assigned array: ";
91 for (unsigned int i = 0; i < assigned.size(); ++i) {
92 std::cout << assigned[i] << " ";
93 }
95
96 // Modify copy and check original
97 copy[0] = "MODIFIED";
98 std::cout << "\nAfter modifying copy[0]:" << std::endl;
99 std::cout << "Original array: ";
100 for (unsigned int i = 0; i < original.size(); ++i) {
101 std::cout << original[i] << " ";
102 }
103 std::cout << " (unchanged)" << std::endl;
104 std::cout << "Copied array: ";
105 for (unsigned int i = 0; i < copy.size(); ++i) {
106 std::cout << copy[i] << " ";
107 }
109}
110
115 std::cout << "--- Test: Out of Bounds Access ---" << std::endl;
116 Array<int> a(10);
117 try {
118 std::cout << "Accessing a[10]... ";
119 a[10] = 42;
120 } catch (const std::exception &e) {
121 std::cout << "Caught exception as expected: " << e.what() << std::endl;
122 }
123
124 try {
125 std::cout << "Accessing a[-1]... ";
126 // Unsigned int conversion will make -1 a very large number
127 a[-1] = 42;
128 } catch (const std::exception &e) {
129 std::cout << "Caught exception as expected: " << e.what() << std::endl;
130 }
132}
133
138 std::cout << "--- Test: Const Array ---" << std::endl;
139 Array<int> a(3);
140 a[0] = 1;
141 a[1] = 2;
142 a[2] = 3;
143
144 const Array<int> const_a = a;
145 std::cout << "Const array values: ";
146 for (unsigned int i = 0; i < const_a.size(); ++i) {
147 std::cout << const_a[i] << " "; // This uses the const version of operator[]
148 }
150
151 // The following line would cause a compilation error, which is correct:
152 // const_a[0] = 100;
153 std::cout << "Attempting to modify const_a[0] would cause a compile error." << std::endl;
155}
156
165int main() {
171
172 // You can uncomment this block to check for leaks with valgrind
173 // {
174 // Array<int> a(1000);
175 // }
176 // std::cout << "Test with large allocation finished." << std::endl;
177
178 return 0;
179}
Contains the declaration of the Array class template.
A template class that represents a dynamic array.
Definition Array.hpp:29
unsigned int size() const
Returns the number of elements in the Array.
T endl(T... args)
int main(void)
The main entry point of the program.
Definition main.cpp:29
void test_const_array()
Tests the usage of a const Array object.
Definition main.cpp:137
void test_default_constructor()
Tests the default constructor of the Array class.
Definition main.cpp:25
void test_out_of_bounds()
Tests out-of-bounds access to ensure exceptions are thrown.
Definition main.cpp:114
void test_copy_and_assignment()
Tests the copy constructor and assignment operator.
Definition main.cpp:66
void test_uint_constructor()
Tests the constructor that takes an unsigned int as a parameter.
Definition main.cpp:42
void printElement(const T &element)
Prints an element to the standard output.
Definition iter.hpp:65