From 07418490d17684eda975af8ca98de656bb03467e Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Fri, 21 Jul 2023 18:43:04 +0100 Subject: [PATCH 01/27] chore: removed duplicate header --- math/area.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/math/area.cpp b/math/area.cpp index 691fe91f0fc..09e845aa744 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -9,9 +9,8 @@ * @author [Focusucof](https://github.com/Focusucof) */ #define _USE_MATH_DEFINES -#include /// for assert -#include /// for M_PI definition and pow() -#include +#include /// for assert +#include /// for M_PI definition and pow() #include /// for uint16_t datatype #include /// for IO operations From 2c467c915d497265facb2cd8ed2fa6b84d62fcce Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Fri, 21 Jul 2023 18:43:32 +0100 Subject: [PATCH 02/27] chore: reordered module doxygen comment --- math/area.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/math/area.cpp b/math/area.cpp index 09e845aa744..acb6427466b 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -15,8 +15,8 @@ #include /// for IO operations /** - * @namespace math * @brief Mathematical algorithms + * @namespace math */ namespace math { /** From 178762e9cac72ee54a641672e1895d31dd08fddf Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Fri, 21 Jul 2023 18:50:26 +0100 Subject: [PATCH 03/27] chore: fixed some of the header documentation. --- math/area.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/math/area.cpp b/math/area.cpp index acb6427466b..577ee5c2c20 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -1,14 +1,18 @@ /** * @file - * @brief Implementations for the [area](https://en.wikipedia.org/wiki/Area) of - * various shapes - * @details The area of a shape is the amount of 2D space it takes up. + * @brief Basic function implementations for working out the + * [area](https://en.wikipedia.org/wiki/Area) of various shapes. + * + * @details + * The area of a shape is the amount of 2D space it takes up. * All shapes have a formula to get the area of any given shape. * These implementations support multiple return types. * * @author [Focusucof](https://github.com/Focusucof) */ -#define _USE_MATH_DEFINES + +#define _USE_MATH_DEFINES /// This definition is required to use M_PI. + #include /// for assert #include /// for M_PI definition and pow() #include /// for uint16_t datatype From d29f5837a84deabe6ffaef8c3d763ff94bb1951d Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Fri, 21 Jul 2023 18:57:54 +0100 Subject: [PATCH 04/27] chore: added T parameter tag --- math/area.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/math/area.cpp b/math/area.cpp index 577ee5c2c20..e1bf369d864 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -25,6 +25,7 @@ namespace math { /** * @brief area of a [square](https://en.wikipedia.org/wiki/Square) (l * l) + * @tparam T the type of the input parameter (e.g., `int`, `float`, etc.) * @param length is the length of the square * @returns area of square */ From fe5980a02f434c6f6b443148b61f012747b017c0 Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Fri, 21 Jul 2023 19:02:13 +0100 Subject: [PATCH 05/27] chore: added some more tparam tags --- math/area.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/math/area.cpp b/math/area.cpp index e1bf369d864..e3e01e5a60a 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -25,7 +25,8 @@ namespace math { /** * @brief area of a [square](https://en.wikipedia.org/wiki/Square) (l * l) - * @tparam T the type of the input parameter (e.g., `int`, `float`, etc.) + * @tparam T the type of the input parameter 'length' and return value (e.g., + * `int`, `float`, etc.) * @param length is the length of the square * @returns area of square */ @@ -36,6 +37,8 @@ T square_area(T length) { /** * @brief area of a [rectangle](https://en.wikipedia.org/wiki/Rectangle) (l * w) + * @tparam T the type of the input parameters and return value (e.g., `int`, + * `float`, etc.) * @param length is the length of the rectangle * @param width is the width of the rectangle * @returns area of the rectangle @@ -48,6 +51,8 @@ T rect_area(T length, T width) { /** * @brief area of a [triangle](https://en.wikipedia.org/wiki/Triangle) (b * h / * 2) + * @tparam T the type of the input parameters and return value (e.g., `int`, + * `float`, etc.) * @param base is the length of the bottom side of the triangle * @param height is the length of the tallest point in the triangle * @returns area of the triangle @@ -60,6 +65,8 @@ T triangle_area(T base, T height) { /** * @brief area of a [circle](https://en.wikipedia.org/wiki/Area_of_a_circle) (pi * * r^2) + * @tparam T the type of the input parameter and return value (e.g., `int`, + * `float`, etc.) * @param radius is the radius of the circle * @returns area of the circle */ @@ -71,6 +78,8 @@ T circle_area(T radius) { /** * @brief area of a [parallelogram](https://en.wikipedia.org/wiki/Parallelogram) * (b * h) + * @tparam T the type of the input parameters and return value (e.g., `int`, + * `float`, etc.) * @param base is the length of the bottom side of the parallelogram * @param height is the length of the tallest point in the parallelogram * @returns area of the parallelogram @@ -83,6 +92,8 @@ T parallelogram_area(T base, T height) { /** * @brief surface area of a [cube](https://en.wikipedia.org/wiki/Cube) ( 6 * (l * * l)) + * @tparam T the type of the input parameter and return value (e.g., `int`, + * `float`, etc.) * @param length is the length of the cube * @returns surface area of the cube */ @@ -94,6 +105,8 @@ T cube_surface_area(T length) { /** * @brief surface area of a [sphere](https://en.wikipedia.org/wiki/Sphere) ( 4 * * pi * r^2) + * @tparam T the type of the input parameter and return value (e.g., `int`, + * `float`, etc.) * @param radius is the radius of the sphere * @returns surface area of the sphere */ @@ -105,6 +118,8 @@ T sphere_surface_area(T radius) { /** * @brief surface area of a [cylinder](https://en.wikipedia.org/wiki/Cylinder) * (2 * pi * r * h + 2 * pi * r^2) + * @tparam T the type of the input parameters and return value (e.g., `int`, + * `float`, etc.) * @param radius is the radius of the cylinder * @param height is the height of the cylinder * @returns surface area of the cylinder From c5052e1d2dba6b8595972b7d2193c4b2e77db59a Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Fri, 21 Jul 2023 19:25:19 +0100 Subject: [PATCH 06/27] chare: note to self about how to tidy this module. --- math/area.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/math/area.cpp b/math/area.cpp index e3e01e5a60a..b4a0f569be2 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -288,6 +288,56 @@ static void test() { std::cout << "TEST PASSED" << std::endl << std::endl; } +/** + * @brief This self test is used to test the basic functionality of the + * square_area function to see if it behaves as expected. + * @returns void + */ +static void test_square_area_functionality() { + // Given we the lengths of different squares. + uint8_t square_a_side_length = 20u; + uint16_t square_b_side_length = 1024u; + uint32_t square_c_side_length = 35233030u; + int8_t square_d_side_length = -15; + int16_t square_e_side_length = -512; + int32_t square_f_side_length = -1234567; + float square_g_side_length = 500.1f; + + // When we calculate the area of the different squares + uint8_t actual_area_square_a = + math::square_area(square_a_side_length); + uint16_t actual_area_square_b = + math::square_area(square_b_side_length); + uint32_t actual_area_square_c = + math::square_area(square_c_side_length); + int8_t actual_area_square_d = + math::square_area(square_d_side_length); + int16_t actual_area_square_e = + math::square_area(square_e_side_length); + int32_t actual_area_square_f = + math::square_area(square_f_side_length); + float actual_area_square_g = math::square_area(square_g_side_length); + + // Then we should get the area calculated as we expect. + // is the expected == actual? + assert(400u == actual_area_square_a); + assert(400u == actual_area_square_b); + assert(400u == actual_area_square_c); + assert(400u == actual_area_square_d); + assert(400u == actual_area_square_e); + assert(400u == actual_area_square_f); + assert(400u == actual_area_square_g); + + std::cout << "TEST PASSED" << std::endl << std::endl; + + // NOTE TO SELF: I started adding test for the square area. The use of the T + // template is a really cool feature. I don't think it is useful here as I + // have found out, calculating a uint16_t with a large area results a number + // which is larger than the given type so in this case we would need to + // possibly have integer types and float types. with specified data types + // for the parameters. +} + /** * @brief Main function * @returns 0 on exit From 093b6480e7cd9fecbe7dede9ffe5834eb5da030a Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Sat, 22 Jul 2023 09:27:44 +0100 Subject: [PATCH 07/27] fix: specific data types for area calculation --- math/area.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/math/area.cpp b/math/area.cpp index b4a0f569be2..8ceb5029089 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -23,16 +23,21 @@ * @namespace math */ namespace math { + /** * @brief area of a [square](https://en.wikipedia.org/wiki/Square) (l * l) - * @tparam T the type of the input parameter 'length' and return value (e.g., - * `int`, `float`, etc.) * @param length is the length of the square - * @returns area of square + * @returns area of square. */ -template -T square_area(T length) { - return length * length; +uint32_t square_area(uint16_t length) { + /* + The parameter type of this function replaces the template type T because + it is safer. If we use the same parameter type as the return type then + there is a potential for the calculated value to roll over and give an + incorrect calculation. + */ + uint32_t area_of_square = length * length; + return area_of_square; } /** From 15aafe0ceebe0d684ced97f9bf727437fb4825dd Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Sat, 22 Jul 2023 09:35:29 +0100 Subject: [PATCH 08/27] chore: using wider data type for square area --- math/area.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/math/area.cpp b/math/area.cpp index 8ceb5029089..8781a61953b 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -29,14 +29,18 @@ namespace math { * @param length is the length of the square * @returns area of square. */ -uint32_t square_area(uint16_t length) { +uint64_t square_area(uint32_t length) { /* The parameter type of this function replaces the template type T because it is safer. If we use the same parameter type as the return type then there is a potential for the calculated value to roll over and give an incorrect calculation. + + e.g. (UINT8_T_MAX * UINT8_T_MAX) >>> UINT8_T_MAX + To store this calculation we would need to specify a return type of + uint16_t */ - uint32_t area_of_square = length * length; + uint64_t area_of_square = length * length; return area_of_square; } From e5aa28680adab97c83f3bb3b583192b2f0397e0a Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Sat, 22 Jul 2023 09:40:03 +0100 Subject: [PATCH 09/27] fix: specified specific types for rectangle area calculation --- math/area.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/math/area.cpp b/math/area.cpp index 8781a61953b..1aa89f69b46 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -36,7 +36,7 @@ uint64_t square_area(uint32_t length) { there is a potential for the calculated value to roll over and give an incorrect calculation. - e.g. (UINT8_T_MAX * UINT8_T_MAX) >>> UINT8_T_MAX + e.g. (UINT8_T_MAX * UINT8_T_MAX) == 65025 To store this calculation we would need to specify a return type of uint16_t */ @@ -46,15 +46,13 @@ uint64_t square_area(uint32_t length) { /** * @brief area of a [rectangle](https://en.wikipedia.org/wiki/Rectangle) (l * w) - * @tparam T the type of the input parameters and return value (e.g., `int`, - * `float`, etc.) * @param length is the length of the rectangle * @param width is the width of the rectangle * @returns area of the rectangle */ -template -T rect_area(T length, T width) { - return length * width; +uint64_t rect_area(uint32_t length, uint32_t width) { + uint64_t area_of_rectangle = length * width; + return area_of_rectangle; } /** From 2d35c535c9dc946d8532058da02e54e1ee7893a9 Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Sat, 22 Jul 2023 09:48:50 +0100 Subject: [PATCH 10/27] fix: fixed type issue with triangle area --- math/area.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/math/area.cpp b/math/area.cpp index 1aa89f69b46..0d2c4ae2287 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -56,17 +56,15 @@ uint64_t rect_area(uint32_t length, uint32_t width) { } /** - * @brief area of a [triangle](https://en.wikipedia.org/wiki/Triangle) (b * h / - * 2) - * @tparam T the type of the input parameters and return value (e.g., `int`, - * `float`, etc.) + * @brief area of a [triangle](https://en.wikipedia.org/wiki/Triangle) (b * h)/2 * @param base is the length of the bottom side of the triangle * @param height is the length of the tallest point in the triangle * @returns area of the triangle */ template -T triangle_area(T base, T height) { - return base * height / 2; +uint64_t triangle_area(uint32_t base, uint32_t height) { + uint64_t area_of_triangle = (base * height) / 2; + return area_of_triangle; } /** From fe85a0e9a043b5142d1a410f5515decf015658de Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Sat, 22 Jul 2023 09:52:39 +0100 Subject: [PATCH 11/27] fix: set specific data types for area of circle --- math/area.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/math/area.cpp b/math/area.cpp index 0d2c4ae2287..bb4a0902ade 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -62,22 +62,20 @@ uint64_t rect_area(uint32_t length, uint32_t width) { * @returns area of the triangle */ template -uint64_t triangle_area(uint32_t base, uint32_t height) { +double triangle_area(uint32_t base, uint32_t height) { uint64_t area_of_triangle = (base * height) / 2; return area_of_triangle; } /** - * @brief area of a [circle](https://en.wikipedia.org/wiki/Area_of_a_circle) (pi - * * r^2) - * @tparam T the type of the input parameter and return value (e.g., `int`, - * `float`, etc.) + * @brief area of a [circle](https://en.wikipedia.org/wiki/Area_of_a_circle) + * (pi * r^2) * @param radius is the radius of the circle * @returns area of the circle */ -template -T circle_area(T radius) { - return M_PI * pow(radius, 2); +double circle_area(uint32_t radius) { + double area_of_circle = M_PI * pow(radius, 2); + return area_of_circle; } /** From c4e1850a1d7090c5f261dcf4e9e23672fff02285 Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Sat, 22 Jul 2023 10:04:04 +0100 Subject: [PATCH 12/27] fix: updated parallelogram function --- math/area.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/math/area.cpp b/math/area.cpp index bb4a0902ade..cc999d27f95 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -81,15 +81,13 @@ double circle_area(uint32_t radius) { /** * @brief area of a [parallelogram](https://en.wikipedia.org/wiki/Parallelogram) * (b * h) - * @tparam T the type of the input parameters and return value (e.g., `int`, - * `float`, etc.) * @param base is the length of the bottom side of the parallelogram * @param height is the length of the tallest point in the parallelogram * @returns area of the parallelogram */ -template -T parallelogram_area(T base, T height) { - return base * height; +uint64_t parallelogram_area(uint32_t base, uint32_t height) { + uint64_t area_of_parallelogram = base * height; + return area_of_parallelogram; } /** From 04c7447822ae4d9f02c50591a3ff478da13177ed Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Sat, 22 Jul 2023 10:09:35 +0100 Subject: [PATCH 13/27] fix: cube surface area function --- math/area.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/math/area.cpp b/math/area.cpp index cc999d27f95..7a404c60f42 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -91,16 +91,14 @@ uint64_t parallelogram_area(uint32_t base, uint32_t height) { } /** - * @brief surface area of a [cube](https://en.wikipedia.org/wiki/Cube) ( 6 * (l - * * l)) - * @tparam T the type of the input parameter and return value (e.g., `int`, - * `float`, etc.) + * @brief surface area of a [cube](https://en.wikipedia.org/wiki/Cube) + * ( 6 * (l * l)) * @param length is the length of the cube * @returns surface area of the cube */ -template -T cube_surface_area(T length) { - return 6 * length * length; +uint64_t cube_surface_area(uint16_t length) { + uint64_t surface_area_of_cube = 6 * length * length; + return surface_area_of_cube; } /** From 404036b78c801f732672d8c6049f7fde2e2e1a56 Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Sat, 22 Jul 2023 10:11:20 +0100 Subject: [PATCH 14/27] fix: sphere surface area funcion --- math/area.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/math/area.cpp b/math/area.cpp index 7a404c60f42..8f1894937f7 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -104,14 +104,12 @@ uint64_t cube_surface_area(uint16_t length) { /** * @brief surface area of a [sphere](https://en.wikipedia.org/wiki/Sphere) ( 4 * * pi * r^2) - * @tparam T the type of the input parameter and return value (e.g., `int`, - * `float`, etc.) * @param radius is the radius of the sphere * @returns surface area of the sphere */ -template -T sphere_surface_area(T radius) { - return 4 * M_PI * pow(radius, 2); +double sphere_surface_area(uint16_t radius) { + double surface_area_of_sphere = 4 * M_PI * pow(radius, 2); + return surface_area_of_sphere; } /** From e322c7fc5ab3d7a1da50c5dcae6f6980de89cb97 Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Sat, 22 Jul 2023 10:12:58 +0100 Subject: [PATCH 15/27] docs: updated function comments --- math/area.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/math/area.cpp b/math/area.cpp index 8f1894937f7..d02a4025c60 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -39,6 +39,10 @@ uint64_t square_area(uint32_t length) { e.g. (UINT8_T_MAX * UINT8_T_MAX) == 65025 To store this calculation we would need to specify a return type of uint16_t + + This is important to know for the following functions because it dictates + what data type we decide upon when setting up the function parameters and + the value returned by the function. */ uint64_t area_of_square = length * length; return area_of_square; From 53e8d6b8241414d87b3583324250ad5896381fa6 Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Sat, 22 Jul 2023 10:15:02 +0100 Subject: [PATCH 16/27] fix: fixed cylinder surface area functionality --- math/area.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/math/area.cpp b/math/area.cpp index d02a4025c60..d766b2c10be 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -119,16 +119,16 @@ double sphere_surface_area(uint16_t radius) { /** * @brief surface area of a [cylinder](https://en.wikipedia.org/wiki/Cylinder) * (2 * pi * r * h + 2 * pi * r^2) - * @tparam T the type of the input parameters and return value (e.g., `int`, - * `float`, etc.) * @param radius is the radius of the cylinder * @param height is the height of the cylinder * @returns surface area of the cylinder */ -template -T cylinder_surface_area(T radius, T height) { - return 2 * M_PI * radius * height + 2 * M_PI * pow(radius, 2); +double cylinder_surface_area(uint16_t radius, uint16_t height) { + double surface_area_of_cylinder = + 2 * M_PI * radius * height + 2 * M_PI * pow(radius, 2); + return surface_area_of_cylinder; } + } // namespace math /** From 20ccc360961ba25724beb0949c607801ca76fe87 Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Sat, 22 Jul 2023 10:57:24 +0100 Subject: [PATCH 17/27] test: removed bulky test case --- math/area.cpp | 199 ++------------------------------------------------ 1 file changed, 8 insertions(+), 191 deletions(-) diff --git a/math/area.cpp b/math/area.cpp index d766b2c10be..f9392be2c93 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -131,164 +131,6 @@ double cylinder_surface_area(uint16_t radius, uint16_t height) { } // namespace math -/** - * @brief Self-test implementations - * @returns void - */ -static void test() { - // I/O variables for testing - uint16_t int_length = 0; // 16 bit integer length input - uint16_t int_width = 0; // 16 bit integer width input - uint16_t int_base = 0; // 16 bit integer base input - uint16_t int_height = 0; // 16 bit integer height input - uint16_t int_expected = 0; // 16 bit integer expected output - uint16_t int_area = 0; // 16 bit integer output - - float float_length = NAN; // float length input - float float_expected = NAN; // float expected output - float float_area = NAN; // float output - - double double_length = NAN; // double length input - double double_width = NAN; // double width input - double double_radius = NAN; // double radius input - double double_height = NAN; // double height input - double double_expected = NAN; // double expected output - double double_area = NAN; // double output - - // 1st test - int_length = 5; - int_expected = 25; - int_area = math::square_area(int_length); - - std::cout << "AREA OF A SQUARE (int)" << std::endl; - std::cout << "Input Length: " << int_length << std::endl; - std::cout << "Expected Output: " << int_expected << std::endl; - std::cout << "Output: " << int_area << std::endl; - assert(int_area == int_expected); - std::cout << "TEST PASSED" << std::endl << std::endl; - - // 2nd test - float_length = 2.5; - float_expected = 6.25; - float_area = math::square_area(float_length); - - std::cout << "AREA OF A SQUARE (float)" << std::endl; - std::cout << "Input Length: " << float_length << std::endl; - std::cout << "Expected Output: " << float_expected << std::endl; - std::cout << "Output: " << float_area << std::endl; - assert(float_area == float_expected); - std::cout << "TEST PASSED" << std::endl << std::endl; - - // 3rd test - int_length = 4; - int_width = 7; - int_expected = 28; - int_area = math::rect_area(int_length, int_width); - - std::cout << "AREA OF A RECTANGLE (int)" << std::endl; - std::cout << "Input Length: " << int_length << std::endl; - std::cout << "Input Width: " << int_width << std::endl; - std::cout << "Expected Output: " << int_expected << std::endl; - std::cout << "Output: " << int_area << std::endl; - assert(int_area == int_expected); - std::cout << "TEST PASSED" << std::endl << std::endl; - - // 4th test - double_length = 2.5; - double_width = 5.7; - double_expected = 14.25; - double_area = math::rect_area(double_length, double_width); - - std::cout << "AREA OF A RECTANGLE (double)" << std::endl; - std::cout << "Input Length: " << double_length << std::endl; - std::cout << "Input Width: " << double_width << std::endl; - std::cout << "Expected Output: " << double_expected << std::endl; - std::cout << "Output: " << double_area << std::endl; - assert(double_area == double_expected); - std::cout << "TEST PASSED" << std::endl << std::endl; - - // 5th test - int_base = 10; - int_height = 3; - int_expected = 15; - int_area = math::triangle_area(int_base, int_height); - - std::cout << "AREA OF A TRIANGLE" << std::endl; - std::cout << "Input Base: " << int_base << std::endl; - std::cout << "Input Height: " << int_height << std::endl; - std::cout << "Expected Output: " << int_expected << std::endl; - std::cout << "Output: " << int_area << std::endl; - assert(int_area == int_expected); - std::cout << "TEST PASSED" << std::endl << std::endl; - - // 6th test - double_radius = 6; - double_expected = - 113.09733552923255; // rounded down because the double datatype - // truncates after 14 decimal places - double_area = math::circle_area(double_radius); - - std::cout << "AREA OF A CIRCLE" << std::endl; - std::cout << "Input Radius: " << double_radius << std::endl; - std::cout << "Expected Output: " << double_expected << std::endl; - std::cout << "Output: " << double_area << std::endl; - assert(double_area == double_expected); - std::cout << "TEST PASSED" << std::endl << std::endl; - - // 7th test - int_base = 6; - int_height = 7; - int_expected = 42; - int_area = math::parallelogram_area(int_base, int_height); - - std::cout << "AREA OF A PARALLELOGRAM" << std::endl; - std::cout << "Input Base: " << int_base << std::endl; - std::cout << "Input Height: " << int_height << std::endl; - std::cout << "Expected Output: " << int_expected << std::endl; - std::cout << "Output: " << int_area << std::endl; - assert(int_area == int_expected); - std::cout << "TEST PASSED" << std::endl << std::endl; - - // 8th test - double_length = 5.5; - double_expected = 181.5; - double_area = math::cube_surface_area(double_length); - - std::cout << "SURFACE AREA OF A CUBE" << std::endl; - std::cout << "Input Length: " << double_length << std::endl; - std::cout << "Expected Output: " << double_expected << std::endl; - std::cout << "Output: " << double_area << std::endl; - assert(double_area == double_expected); - std::cout << "TEST PASSED" << std::endl << std::endl; - - // 9th test - double_radius = 10.0; - double_expected = 1256.6370614359172; // rounded down because the whole - // value gets truncated - double_area = math::sphere_surface_area(double_radius); - - std::cout << "SURFACE AREA OF A SPHERE" << std::endl; - std::cout << "Input Radius: " << double_radius << std::endl; - std::cout << "Expected Output: " << double_expected << std::endl; - std::cout << "Output: " << double_area << std::endl; - assert(double_area == double_expected); - std::cout << "TEST PASSED" << std::endl << std::endl; - - // 10th test - double_radius = 4.0; - double_height = 7.0; - double_expected = 276.46015351590177; - double_area = math::cylinder_surface_area(double_radius, double_height); - - std::cout << "SURFACE AREA OF A CYLINDER" << std::endl; - std::cout << "Input Radius: " << double_radius << std::endl; - std::cout << "Input Height: " << double_height << std::endl; - std::cout << "Expected Output: " << double_expected << std::endl; - std::cout << "Output: " << double_area << std::endl; - assert(double_area == double_expected); - std::cout << "TEST PASSED" << std::endl << std::endl; -} - /** * @brief This self test is used to test the basic functionality of the * square_area function to see if it behaves as expected. @@ -296,47 +138,22 @@ static void test() { */ static void test_square_area_functionality() { // Given we the lengths of different squares. - uint8_t square_a_side_length = 20u; - uint16_t square_b_side_length = 1024u; + uint32_t square_a_side_length = 20u; + uint32_t square_b_side_length = 1024u; uint32_t square_c_side_length = 35233030u; - int8_t square_d_side_length = -15; - int16_t square_e_side_length = -512; - int32_t square_f_side_length = -1234567; - float square_g_side_length = 500.1f; // When we calculate the area of the different squares - uint8_t actual_area_square_a = - math::square_area(square_a_side_length); - uint16_t actual_area_square_b = - math::square_area(square_b_side_length); - uint32_t actual_area_square_c = - math::square_area(square_c_side_length); - int8_t actual_area_square_d = - math::square_area(square_d_side_length); - int16_t actual_area_square_e = - math::square_area(square_e_side_length); - int32_t actual_area_square_f = - math::square_area(square_f_side_length); - float actual_area_square_g = math::square_area(square_g_side_length); + uint64_t actual_area_square_a = math::square_area(square_a_side_length); + uint64_t actual_area_square_b = math::square_area(square_b_side_length); + uint64_t actual_area_square_c = math::square_area(square_c_side_length); // Then we should get the area calculated as we expect. // is the expected == actual? assert(400u == actual_area_square_a); - assert(400u == actual_area_square_b); - assert(400u == actual_area_square_c); - assert(400u == actual_area_square_d); - assert(400u == actual_area_square_e); - assert(400u == actual_area_square_f); - assert(400u == actual_area_square_g); + assert(1048576u == actual_area_square_b); + assert(1241366402980900u == actual_area_square_c); std::cout << "TEST PASSED" << std::endl << std::endl; - - // NOTE TO SELF: I started adding test for the square area. The use of the T - // template is a really cool feature. I don't think it is useful here as I - // have found out, calculating a uint16_t with a large area results a number - // which is larger than the given type so in this case we would need to - // possibly have integer types and float types. with specified data types - // for the parameters. } /** @@ -344,6 +161,6 @@ static void test_square_area_functionality() { * @returns 0 on exit */ int main() { - test(); // run self-test implementations + test_square_area_functionality(); // run self-test implementations return 0; } From ba3f7a3ef86b5104e56042f3e07a4e8b5891699f Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Sat, 5 Aug 2023 12:22:40 +0100 Subject: [PATCH 18/27] chore: added some more comments and added another test --- math/area.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/math/area.cpp b/math/area.cpp index f9392be2c93..ea9f22a9b8d 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -44,7 +44,7 @@ uint64_t square_area(uint32_t length) { what data type we decide upon when setting up the function parameters and the value returned by the function. */ - uint64_t area_of_square = length * length; + uint64_t area_of_square = (uint64_t)length * (uint64_t)length; return area_of_square; } @@ -55,7 +55,16 @@ uint64_t square_area(uint32_t length) { * @returns area of the rectangle */ uint64_t rect_area(uint32_t length, uint32_t width) { - uint64_t area_of_rectangle = length * width; + /* + You may notice that these variables are prepended with (uint64_t) + This is because when doing the multiplication we need to give these data + types a wider area of memory when doing this calculation so we need a 64 + bit variable. + + We could make the parameters 64 bit but that would require adding checks + to make sure out calculation doesn't exceed 64 bits. So this is easier. + */ + uint64_t area_of_rectangle = (uint64_t)length * (uint64_t)width; return area_of_rectangle; } @@ -141,17 +150,68 @@ static void test_square_area_functionality() { uint32_t square_a_side_length = 20u; uint32_t square_b_side_length = 1024u; uint32_t square_c_side_length = 35233030u; + uint32_t square_d_side_length = 0u; // When we calculate the area of the different squares uint64_t actual_area_square_a = math::square_area(square_a_side_length); uint64_t actual_area_square_b = math::square_area(square_b_side_length); uint64_t actual_area_square_c = math::square_area(square_c_side_length); + uint64_t actual_area_square_d = math::square_area(square_d_side_length); // Then we should get the area calculated as we expect. // is the expected == actual? assert(400u == actual_area_square_a); assert(1048576u == actual_area_square_b); assert(1241366402980900u == actual_area_square_c); + assert(0u == actual_area_square_d); + + std::cout << "TEST PASSED" << std::endl << std::endl; +} + +/** + * @brief This self test is used to test the basic functionality of the + * square_area function to see if it behaves as expected. + * @returns void + */ +static void test_square_area_functionality() { + // Given we the lengths of different squares. + uint32_t square_a_side_length = 0u; + uint32_t square_b_side_length = 1024u; + uint32_t square_c_side_length = 35233030u; + + // When we calculate the area of the different squares + uint64_t actual_area_square_a = math::square_area(square_a_side_length); + uint64_t actual_area_square_b = math::square_area(square_b_side_length); + uint64_t actual_area_square_c = math::square_area(square_c_side_length); + + // Then we should get the area calculated as we expect. + // is the expected == actual? + assert(400u == actual_area_square_a); + assert(1048576u == actual_area_square_b); + assert(1241366402980900u == actual_area_square_c); + + std::cout << "TEST PASSED" << std::endl << std::endl; +} + +/** + * @brief This self test is used to test the basic functionality of the + * rect_area function to see if it behaves as expected. + * @returns void + */ +static void test_rectangle_area_functionality() { + // Given we the lengths of different squares. + uint32_t rectangle_side_length = 1024u; + uint32_t rectangle_side_width = 35233030u; + + // When we calculate the area of the different squares + uint64_t actual_area_rectangle = + math::rect_area(rectangle_side_length, rectangle_side_width); + uint64_t actual_area_square_b = math::square_area(square_b_side_length); + uint64_t actual_area_square_c = math::square_area(square_c_side_length); + + // Then we should get the area calculated as we expect. + // is the expected == actual? + assert(400u == actual_area_rectangle); std::cout << "TEST PASSED" << std::endl << std::endl; } From a886cf7755e3bd077469559ca20704cf84fbde50 Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Sat, 5 Aug 2023 12:23:04 +0100 Subject: [PATCH 19/27] fix: removed undeclared variables --- math/area.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/math/area.cpp b/math/area.cpp index ea9f22a9b8d..ff3b52d5b8b 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -206,8 +206,6 @@ static void test_rectangle_area_functionality() { // When we calculate the area of the different squares uint64_t actual_area_rectangle = math::rect_area(rectangle_side_length, rectangle_side_width); - uint64_t actual_area_square_b = math::square_area(square_b_side_length); - uint64_t actual_area_square_c = math::square_area(square_c_side_length); // Then we should get the area calculated as we expect. // is the expected == actual? From 06be4e8907840e4354cc43b1fc6cb13919c75d98 Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Sat, 5 Aug 2023 12:27:25 +0100 Subject: [PATCH 20/27] fix: removed unused functions --- math/area.cpp | 30 +++--------------------------- 1 file changed, 3 insertions(+), 27 deletions(-) diff --git a/math/area.cpp b/math/area.cpp index ff3b52d5b8b..7d3a8f65b7f 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -168,37 +168,12 @@ static void test_square_area_functionality() { std::cout << "TEST PASSED" << std::endl << std::endl; } -/** - * @brief This self test is used to test the basic functionality of the - * square_area function to see if it behaves as expected. - * @returns void - */ -static void test_square_area_functionality() { - // Given we the lengths of different squares. - uint32_t square_a_side_length = 0u; - uint32_t square_b_side_length = 1024u; - uint32_t square_c_side_length = 35233030u; - - // When we calculate the area of the different squares - uint64_t actual_area_square_a = math::square_area(square_a_side_length); - uint64_t actual_area_square_b = math::square_area(square_b_side_length); - uint64_t actual_area_square_c = math::square_area(square_c_side_length); - - // Then we should get the area calculated as we expect. - // is the expected == actual? - assert(400u == actual_area_square_a); - assert(1048576u == actual_area_square_b); - assert(1241366402980900u == actual_area_square_c); - - std::cout << "TEST PASSED" << std::endl << std::endl; -} - /** * @brief This self test is used to test the basic functionality of the * rect_area function to see if it behaves as expected. * @returns void */ -static void test_rectangle_area_functionality() { +static void test_rect_area_functionality() { // Given we the lengths of different squares. uint32_t rectangle_side_length = 1024u; uint32_t rectangle_side_width = 35233030u; @@ -209,7 +184,7 @@ static void test_rectangle_area_functionality() { // Then we should get the area calculated as we expect. // is the expected == actual? - assert(400u == actual_area_rectangle); + assert(36078622720u == actual_area_rectangle); std::cout << "TEST PASSED" << std::endl << std::endl; } @@ -220,5 +195,6 @@ static void test_rectangle_area_functionality() { */ int main() { test_square_area_functionality(); // run self-test implementations + test_rect_area_functionality(); return 0; } From 3b02ed3277f8719a32638c04245e64ddfeaf3500 Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Sat, 5 Aug 2023 12:28:02 +0100 Subject: [PATCH 21/27] docs: updated gherkin test comments. --- math/area.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/math/area.cpp b/math/area.cpp index 7d3a8f65b7f..591b15654c7 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -174,11 +174,11 @@ static void test_square_area_functionality() { * @returns void */ static void test_rect_area_functionality() { - // Given we the lengths of different squares. + // Given we have a rectangle length and width. uint32_t rectangle_side_length = 1024u; uint32_t rectangle_side_width = 35233030u; - // When we calculate the area of the different squares + // When we calculate the area of the rectangle. uint64_t actual_area_rectangle = math::rect_area(rectangle_side_length, rectangle_side_width); From ac1db38b73880a2715c25a09094c2d4b3594b8d4 Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Sat, 5 Aug 2023 12:44:50 +0100 Subject: [PATCH 22/27] chore: added triangle area test --- math/area.cpp | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/math/area.cpp b/math/area.cpp index 591b15654c7..91a9a7d63fd 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -74,9 +74,8 @@ uint64_t rect_area(uint32_t length, uint32_t width) { * @param height is the length of the tallest point in the triangle * @returns area of the triangle */ -template double triangle_area(uint32_t base, uint32_t height) { - uint64_t area_of_triangle = (base * height) / 2; + double area_of_triangle = (base * height) / 2; return area_of_triangle; } @@ -165,7 +164,7 @@ static void test_square_area_functionality() { assert(1241366402980900u == actual_area_square_c); assert(0u == actual_area_square_d); - std::cout << "TEST PASSED" << std::endl << std::endl; + std::cout << "TEST PASSED: Square Area" << std::endl << std::endl; } /** @@ -186,7 +185,28 @@ static void test_rect_area_functionality() { // is the expected == actual? assert(36078622720u == actual_area_rectangle); - std::cout << "TEST PASSED" << std::endl << std::endl; + std::cout << "TEST PASSED: Rectangle Area" << std::endl << std::endl; +} + +/** + * @brief This self test is used to test the basic functionality of the + * triangle_area function to see if it behaves as expected. + * @returns void + */ +static void test_triangle_area_functionality() { + // Given we have a rectangle base length and height. + uint32_t triangle_base_length = 2420u; + uint32_t triangle_height = 115642u; + + // When we calculate the area of the triangle. + double actual_triangle_area = + math::triangle_area(triangle_base_length, triangle_height); + + // Then we should get the area calculated as we expect. + // is the expected == actual? + assert(139926820u == actual_triangle_area); + + std::cout << "TEST PASSED: Triangle Area" << std::endl << std::endl; } /** @@ -196,5 +216,6 @@ static void test_rect_area_functionality() { int main() { test_square_area_functionality(); // run self-test implementations test_rect_area_functionality(); + test_triangle_area_functionality(); return 0; } From 47d72d5d7cd41c80725630117f9407832b603656 Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Sat, 5 Aug 2023 12:55:13 +0100 Subject: [PATCH 23/27] docs: added correct docs name --- math/area.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/math/area.cpp b/math/area.cpp index 91a9a7d63fd..1806a990b4d 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -194,7 +194,7 @@ static void test_rect_area_functionality() { * @returns void */ static void test_triangle_area_functionality() { - // Given we have a rectangle base length and height. + // Given we have a triangle base length and height. uint32_t triangle_base_length = 2420u; uint32_t triangle_height = 115642u; From c375cdf0ee5867e4c0d2bd7c8b36539a2cbded69 Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Sat, 5 Aug 2023 13:00:23 +0100 Subject: [PATCH 24/27] chore: added circle area test --- math/area.cpp | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/math/area.cpp b/math/area.cpp index 1806a990b4d..02422fbf9a6 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -98,7 +98,7 @@ double circle_area(uint32_t radius) { * @returns area of the parallelogram */ uint64_t parallelogram_area(uint32_t base, uint32_t height) { - uint64_t area_of_parallelogram = base * height; + uint64_t area_of_parallelogram = (uint64_t)base * (uint64_t)height; return area_of_parallelogram; } @@ -109,7 +109,7 @@ uint64_t parallelogram_area(uint32_t base, uint32_t height) { * @returns surface area of the cube */ uint64_t cube_surface_area(uint16_t length) { - uint64_t surface_area_of_cube = 6 * length * length; + uint64_t surface_area_of_cube = 6u * (uint64_t)length * (uint64_t)length; return surface_area_of_cube; } @@ -209,6 +209,25 @@ static void test_triangle_area_functionality() { std::cout << "TEST PASSED: Triangle Area" << std::endl << std::endl; } +/** + * @brief This self test is used to test the basic functionality of the + * circle_area function to see if it behaves as expected. + * @returns void + */ +static void test_circle_area_functionality() { + // Given we have a circle radius. + uint32_t circle_radius = 555; + + // When we calculate the area of the circle. + double actual_circle_area = math::circle_area(circle_radius); + + // Then we should get the area calculated as we expect. + // is the expected == actual? + assert(967689.0771f == actual_circle_area); + + std::cout << "TEST PASSED: Circle Area" << std::endl << std::endl; +} + /** * @brief Main function * @returns 0 on exit @@ -217,5 +236,6 @@ int main() { test_square_area_functionality(); // run self-test implementations test_rect_area_functionality(); test_triangle_area_functionality(); + test_circle_area_functionality(); return 0; } From 005d4d5de8e769dd1d2db4263b653415403f5f75 Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Sat, 5 Aug 2023 13:08:35 +0100 Subject: [PATCH 25/27] chore: added parallelogram area test. --- math/area.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/math/area.cpp b/math/area.cpp index 02422fbf9a6..12e4eceb7d5 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -216,18 +216,39 @@ static void test_triangle_area_functionality() { */ static void test_circle_area_functionality() { // Given we have a circle radius. - uint32_t circle_radius = 555; + uint32_t circle_radius = 555u; // When we calculate the area of the circle. double actual_circle_area = math::circle_area(circle_radius); // Then we should get the area calculated as we expect. // is the expected == actual? - assert(967689.0771f == actual_circle_area); + assert(967689.07712199597 == actual_circle_area); std::cout << "TEST PASSED: Circle Area" << std::endl << std::endl; } +/** + * @brief This self test is used to test the basic functionality of the + * parallelogram_area function to see if it behaves as expected. + * @returns void + */ +static void test_parallelogram_area_functionality() { + // Given we the base and height of a parallelogram. + uint32_t parallelogram_base = 20u; + uint32_t parallelogram_height = 1024u; + + // When we calculate the area of the parallelogram + uint64_t actual_area_parallelogram = + math::parallelogram_area(parallelogram_base, parallelogram_height); + + // Then we should get the area calculated as we expect. + // is the expected == actual? + assert(20480u == actual_area_parallelogram); + + std::cout << "TEST PASSED: Parallelogram Area" << std::endl << std::endl; +} + /** * @brief Main function * @returns 0 on exit @@ -237,5 +258,6 @@ int main() { test_rect_area_functionality(); test_triangle_area_functionality(); test_circle_area_functionality(); + test_parallelogram_area_functionality(); return 0; } From 6020dbaacfbca6db0591780e068e858125a22596 Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Sat, 5 Aug 2023 13:15:12 +0100 Subject: [PATCH 26/27] chore: added cube surface area test --- math/area.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/math/area.cpp b/math/area.cpp index 12e4eceb7d5..70d7495060c 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -249,6 +249,26 @@ static void test_parallelogram_area_functionality() { std::cout << "TEST PASSED: Parallelogram Area" << std::endl << std::endl; } +/** + * @brief This self test is used to test the basic functionality of the + * cube_surface_area function to see if it behaves as expected. + * @returns void + */ +static void test_cube_surface_area_functionality() { + // Given we the length of one face of the cube. + uint16_t cube_face_length = 4121u; + + // When we calculate the cube surface area. + uint64_t actual_cube_surface_area = + math::cube_surface_area(cube_face_length); + + // Then we should get the area calculated as we expect. + // is the expected == actual? + assert(101895846u == actual_cube_surface_area); + + std::cout << "TEST PASSED: Cube Surface Area" << std::endl << std::endl; +} + /** * @brief Main function * @returns 0 on exit @@ -259,5 +279,6 @@ int main() { test_triangle_area_functionality(); test_circle_area_functionality(); test_parallelogram_area_functionality(); + test_cube_surface_area_functionality(); return 0; } From bd6726fdec9d04153ef5a3e39bdde2bf3707c289 Mon Sep 17 00:00:00 2001 From: rbevin777 Date: Sat, 5 Aug 2023 13:26:38 +0100 Subject: [PATCH 27/27] chore: added tests now one for each function. --- math/area.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/math/area.cpp b/math/area.cpp index 70d7495060c..230b9398af8 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -269,6 +269,48 @@ static void test_cube_surface_area_functionality() { std::cout << "TEST PASSED: Cube Surface Area" << std::endl << std::endl; } +/** + * @brief This self test is used to test the basic functionality of the + * sphere_surface_area function to see if it behaves as expected. + * @returns void + */ +static void test_sphere_surface_area_functionality() { + // Given we the radius of the sphere. + uint16_t sphere_radius = 5678u; + + // When we calculate the sphere surface area. + uint64_t actual_sphere_surface_area = + math::sphere_surface_area(sphere_radius); + + // Then we should get the area calculated as we expect. + // is the expected == actual? + assert(405135817 == actual_sphere_surface_area); + + std::cout << "TEST PASSED: Sphere Surface Area" << std::endl << std::endl; +} + +/** + * @brief This self test is used to test the basic functionality of the + * cylinder_surface_area function to see if it behaves as expected. + * @returns void + */ +static void test_cylinder_surface_area_functionality() { + // Given we the radius and the height of the cylinder. + uint16_t cylinder_radius = 42u; + uint16_t cylinder_height = 413u; + + // When we calculate the cylinder surface area. + uint64_t actual_cylinder_surface_area = + math::cylinder_surface_area(cylinder_radius, cylinder_height); + + // Then we should get the area calculated as we expect. + // is the expected == actual? + assert(120071 == actual_cylinder_surface_area); + + std::cout << "TEST PASSED: Sphere Surface Area" << std::endl + << std::endl; // sphere_surface_area +} + /** * @brief Main function * @returns 0 on exit @@ -280,5 +322,7 @@ int main() { test_circle_area_functionality(); test_parallelogram_area_functionality(); test_cube_surface_area_functionality(); + test_sphere_surface_area_functionality(); + test_cylinder_surface_area_functionality(); return 0; }