Skip to content

Commit 2963727

Browse files
Merge pull request #4 from silabs-ArghyaD/feature/unit_test_ai
Added sample cpp and header files
2 parents 90d9d78 + b8c6485 commit 2963727

2 files changed

Lines changed: 187 additions & 0 deletions

File tree

test_sample.cpp

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <string>
4+
#include <memory>
5+
6+
/**
7+
* @class Calculator
8+
* @brief A simple calculator class with basic arithmetic operations
9+
*/
10+
class Calculator {
11+
private:
12+
std::vector<double> history;
13+
14+
public:
15+
/**
16+
* @brief Add two numbers
17+
* @param a First number
18+
* @param b Second number
19+
* @return Sum of a and b
20+
*/
21+
double add(double a, double b) {
22+
double result = a + b;
23+
history.push_back(result);
24+
return result;
25+
}
26+
27+
/**
28+
* @brief Subtract two numbers
29+
* @param a First number
30+
* @param b Second number
31+
* @return Difference of a and b
32+
*/
33+
double subtract(double a, double b) {
34+
double result = a - b;
35+
history.push_back(result);
36+
return result;
37+
}
38+
39+
/**
40+
* @brief Multiply two numbers
41+
* @param a First number
42+
* @param b Second number
43+
* @return Product of a and b
44+
*/
45+
double multiply(double a, double b) {
46+
double result = a * b;
47+
history.push_back(result);
48+
return result;
49+
}
50+
51+
/**
52+
* @brief Divide two numbers
53+
* @param a Dividend
54+
* @param b Divisor
55+
* @return Quotient of a and b
56+
* @throws std::invalid_argument if divisor is zero
57+
*/
58+
double divide(double a, double b) {
59+
if (b == 0) {
60+
throw std::invalid_argument("Division by zero is not allowed");
61+
}
62+
double result = a / b;
63+
history.push_back(result);
64+
return result;
65+
}
66+
67+
/**
68+
* @brief Get the calculation history
69+
* @return Vector containing all previous results
70+
*/
71+
const std::vector<double>& getHistory() const {
72+
return history;
73+
}
74+
75+
/**
76+
* @brief Clear the calculation history
77+
*/
78+
void clearHistory() {
79+
history.clear();
80+
}
81+
};
82+
83+
/**
84+
* @brief Template function to find maximum of two values
85+
* @tparam T Type of the values
86+
* @param a First value
87+
* @param b Second value
88+
* @return Maximum of a and b
89+
*/
90+
template<typename T>
91+
T findMax(T a, T b) {
92+
return (a > b) ? a : b;
93+
}
94+
95+
/**
96+
* @brief Main function to demonstrate the Calculator class
97+
*/
98+
int main() {
99+
auto calculator = std::make_unique<Calculator>();
100+
101+
std::cout << "Testing Calculator class:" << std::endl;
102+
103+
try {
104+
double result1 = calculator->add(10.5, 5.3);
105+
std::cout << "10.5 + 5.3 = " << result1 << std::endl;
106+
107+
double result2 = calculator->multiply(4.0, 3.0);
108+
std::cout << "4.0 * 3.0 = " << result2 << std::endl;
109+
110+
double result3 = calculator->divide(15.0, 3.0);
111+
std::cout << "15.0 / 3.0 = " << result3 << std::endl;
112+
113+
// Test template function
114+
int maxInt = findMax(42, 37);
115+
std::cout << "Max of 42 and 37: " << maxInt << std::endl;
116+
117+
// Display history
118+
std::cout << "\nCalculation history:" << std::endl;
119+
const auto& history = calculator->getHistory();
120+
for (size_t i = 0; i < history.size(); ++i) {
121+
std::cout << "Result " << (i + 1) << ": " << history[i] << std::endl;
122+
}
123+
124+
} catch (const std::exception& e) {
125+
std::cerr << "Error: " << e.what() << std::endl;
126+
}
127+
128+
return 0;
129+
}

test_sample.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#ifndef TEST_SAMPLE_H
2+
#define TEST_SAMPLE_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
/**
9+
* @file test_sample.h
10+
* @brief Header file for test sample functions
11+
*/
12+
13+
/**
14+
* @brief Calculate the sum of two integers
15+
* @param a First integer
16+
* @param b Second integer
17+
* @return Sum of a and b
18+
*/
19+
int add(int a, int b);
20+
21+
/**
22+
* @brief Calculate the factorial of a number
23+
* @param n The number to calculate factorial for
24+
* @return Factorial of n
25+
*/
26+
long factorial(int n);
27+
28+
/**
29+
* @brief Structure to represent a point in 2D space
30+
*/
31+
typedef struct {
32+
double x; /**< X coordinate */
33+
double y; /**< Y coordinate */
34+
} Point;
35+
36+
/**
37+
* @brief Calculate distance between two points
38+
* @param p1 First point
39+
* @param p2 Second point
40+
* @return Distance between the points
41+
*/
42+
double calculateDistance(const Point* p1, const Point* p2);
43+
44+
/**
45+
* @brief Enum for operation types
46+
*/
47+
typedef enum {
48+
OP_ADD = 0, /**< Addition operation */
49+
OP_SUBTRACT, /**< Subtraction operation */
50+
OP_MULTIPLY, /**< Multiplication operation */
51+
OP_DIVIDE /**< Division operation */
52+
} OperationType;
53+
54+
#ifdef __cplusplus
55+
}
56+
#endif
57+
58+
#endif // TEST_SAMPLE_H

0 commit comments

Comments
 (0)