-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfull_test.cpp
193 lines (152 loc) · 4.06 KB
/
full_test.cpp
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <iostream>
#include <string>
#include <stdexcept>
void DataCorruption_Handler()
{
while(1);
}
void SigMismatch_Handler()
{
while(1);
}
// Simple function
void simpleFunction() {
std::cout << "Simple function" << std::endl;
}
// Function with parameters and return value
int add(int a, int b) {
return a + b;
}
// Function overloading
int multiply(int a, int b) {
return a * b;
}
double multiply(double a, double b) {
return a * b;
}
// Class with member functions and constructor
class MyClass {
public:
MyClass(int x, int y) : a(x), b(y) {}
// Member function
int sum() const {
return a + b;
}
// Virtual function for testing polymorphism
virtual void print() const {
std::cout << "MyClass: a = " << a << ", b = " << b << std::endl;
}
protected:
int a, b;
};
// Derived class overriding a virtual function
class DerivedClass : public MyClass {
public:
DerivedClass(int x, int y, int z) : MyClass(x, y), c(z) {}
// Override virtual function
void print() const override {
std::cout << "DerivedClass: a = " << a << ", b = " << b << ", c = " << c << std::endl;
}
private:
int c;
};
// Template function
template <typename T>
T square(T x) {
return x * x;
}
// Template class
template <typename T>
class TemplateClass {
public:
TemplateClass(T x, T y) : a(x), b(y) {}
T product() const {
return a * b;
}
private:
T a, b;
};
// Recursive function
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
// Function that throws an exception
void riskyFunction(bool throwException) {
if (throwException) {
throw std::runtime_error("An error occurred in riskyFunction!");
}
std::cout << "RiskyFunction executed successfully." << std::endl;
}
// Sret
struct State
{
double x0;
float x1;
float x2;
float x3;
};
class Class
{
public:
Class() {
std::cout << "Class constructor" << std::endl;
state = {1.0, 2.f, 3.f, 4.f};
}
State testSretDuplication(){
return state;
}
private:
State state;
};
// noexcept
void TestNoExcept() noexcept
{
volatile MyClass m_data(1,2);
}
// testing unexpected special variable
static Class staticClass;
int main() {
std::cout << "Starting main" << std::endl;
std::cout << "staticClass: " << staticClass.testSretDuplication().x3 << std::endl;
// Call simple function
simpleFunction();
// Call function with parameters
std::cout << "Add 3 + 4 = " << add(3, 4) << std::endl;
// Call overloaded functions
std::cout << "Multiply 3 * 4 = " << multiply(3, 4) << std::endl;
std::cout << "Multiply 2.5 * 4.5 = " << multiply(2.5, 4.5) << std::endl;
// Test class and member function
MyClass myObj(5, 7);
std::cout << "Sum of MyClass: " << myObj.sum() << std::endl;
myObj.print();
// Test derived class with overridden virtual function
DerivedClass derivedObj(3, 6, 9);
derivedObj.print();
// Test template function
std::cout << "Square of 5: " << square(5) << std::endl;
std::cout << "Square of 2.5: " << square(2.5) << std::endl;
// Test template class
TemplateClass<int> intObj(3, 4);
TemplateClass<double> doubleObj(2.5, 3.5);
std::cout << "Product of intObj: " << intObj.product() << std::endl;
std::cout << "Product of doubleObj: " << doubleObj.product() << std::endl;
// Test recursive function
std::cout << "Factorial of 5: " << factorial(5) << std::endl;
// Test exception handling
try {
riskyFunction(true); // This will throw an exception
} catch (const std::runtime_error& e) {
std::cerr << "Caught exception: " << e.what() << std::endl;
}
try {
riskyFunction(false); // This will not throw an exception
} catch (const std::runtime_error& e) {
std::cerr << "Caught exception: " << e.what() << std::endl;
}
TestNoExcept();
// Test sret
Class sretTest;
std::cout << "test sret: " << sretTest.testSretDuplication().x0 << std::endl;
return 0;
}