-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsample.cpp
More file actions
89 lines (77 loc) · 1.86 KB
/
Copy pathsample.cpp
File metadata and controls
89 lines (77 loc) · 1.86 KB
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
// Sample C++ source file for testing the UT test generator
#include "sample.hpp"
// Simple add function
int add(int a, int b) {
return a + b;
}
// Simple multiply function
int multiply(int a, int b) {
return a * b;
}
// Factorial function
int factorial(int n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
// Check if a number is prime
bool isPrime(int n) {
if (n <= 1) {
return false;
}
if (n <= 3) {
return true;
}
if (n % 2 == 0 || n % 3 == 0) {
return false;
}
for (int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
}
return true;
}
// Calculate sum of array
int sumArray(const std::vector<int>& arr) {
int total = 0;
for (int num : arr) {
total += num;
}
return total;
}
// Find maximum in array
int findMax(const std::vector<int>& arr) {
if (arr.empty()) {
return 0;
}
int maxVal = arr[0];
for (size_t i = 1; i < arr.size(); ++i) {
if (arr[i] > maxVal) {
maxVal = arr[i];
}
}
return maxVal;
}
// String reversal
std::string reverseString(const std::string& str) {
std::string result;
for (int i = str.length() - 1; i >= 0; --i) {
result += str[i];
}
return result;
}
#ifndef UNIT_TEST
int main() {
std::cout << "Add: " << add(2, 3) << std::endl;
std::cout << "Multiply: " << multiply(4, 5) << std::endl;
std::cout << "Factorial: " << factorial(5) << std::endl;
std::cout << "Is 7 prime? " << (isPrime(7) ? "Yes" : "No") << std::endl;
std::vector<int> arr = {1, 2, 3, 4, 5};
std::cout << "Sum: " << sumArray(arr) << std::endl;
std::cout << "Max: " << findMax(arr) << std::endl;
std::cout << "Reverse: " << reverseString("hello") << std::endl;
return 0;
}
#endif