From ade810ff1e855402d2cab12fd2faa1ad62f12615 Mon Sep 17 00:00:00 2001 From: Ayush Bishnoi Date: Thu, 1 Apr 2021 13:01:09 +0530 Subject: [PATCH 01/34] code functionality expanded, pre makefile creation --- .../inc/complex_calculator.h | 12 +- .../calculator_complex/main.c | 37 +++ .../src/complex_calculator.c | 49 +++- .../test/test_complex_calculator.c | 220 +++++++++++++++++- 4 files changed, 308 insertions(+), 10 deletions(-) diff --git a/Example_Programs/programming_concpets/calculator_complex/inc/complex_calculator.h b/Example_Programs/programming_concpets/calculator_complex/inc/complex_calculator.h index 0c8746b7..6ef6889e 100644 --- a/Example_Programs/programming_concpets/calculator_complex/inc/complex_calculator.h +++ b/Example_Programs/programming_concpets/calculator_complex/inc/complex_calculator.h @@ -32,13 +32,15 @@ typedef struct complex_t { /** * @brief computes sum of teh two complex numbers * - * @param[in] cnum1 Pointer to complex number1 - * @param[in] cnum2 Pointer to complex number2 - * @param[out] csum Pointer to store the computed result + * @param[in] c1 Pointer to complex number1 + * @param[in] c2 Pointer to complex number2 + * @param[out] result Pointer to store the computed result * @return error_t SUCCESS if operation is completed successfully. Error value otherwise. */ -error_t complex_sum(complex_t* cnum1, complex_t* cnum2, complex_t* csum); - +error_t complex_sum(complex_t* c1, complex_t* c2, complex_t* result); +error_t complex_sub(complex_t* c1, complex_t* c2, complex_t* result); +error_t complex_mul(complex_t* c1, complex_t* c2, complex_t* result); +error_t complex_div(complex_t* c1, complex_t* c2, complex_t* result); /** * @brief TODO Add the remaining functions diff --git a/Example_Programs/programming_concpets/calculator_complex/main.c b/Example_Programs/programming_concpets/calculator_complex/main.c index 5833eb24..68e6ca25 100644 --- a/Example_Programs/programming_concpets/calculator_complex/main.c +++ b/Example_Programs/programming_concpets/calculator_complex/main.c @@ -4,4 +4,41 @@ int main() { //TODO Write all the logic for Calculator app printf("Complex calculator "); + static complex_t a = {0, 0}; + static complex_t b = {0, 0}; + static complex_t c = {0, 0}; + int choice; + while (1) + { + printf("To add complex numbers press 1.\n"); + printf("To subtract complex numbers press 2.\n"); + printf("To multiply complex numbers press 3.\n"); + printf("To divide complex numbers press 4.\n"); + printf("To exit press 5.\n"); + printf("Enter the number.\n"); + scanf("%d", &choice); + + if (choice == 1) + { + error_t res = complex_sum(&a, &b, &c); + } + if (choice == 2) + { + error_t res = complex_sub(&a, &b, &c); + } + if (choice == 3) + { + error_t res = complex_mul(&a, &b, &c); + } + if (choice == 4) + { + error_t res = complex_div(&a, &b, &c); + } + + if (choice == 5) + { + break; + } + } + return 0; } \ No newline at end of file diff --git a/Example_Programs/programming_concpets/calculator_complex/src/complex_calculator.c b/Example_Programs/programming_concpets/calculator_complex/src/complex_calculator.c index 8e220f55..02e21e8f 100644 --- a/Example_Programs/programming_concpets/calculator_complex/src/complex_calculator.c +++ b/Example_Programs/programming_concpets/calculator_complex/src/complex_calculator.c @@ -1,15 +1,56 @@ #include "stdlib.h" #include "complex_calculator.h" -error_t complex_sum(complex_t* cnum1, complex_t* cnum2, complex_t* csum) +error_t complex_sum(complex_t* c1, complex_t* c2, complex_t* result) { - if(NULL == cnum1 || NULL == cnum2) + if(NULL == c1 || NULL == c2) return ERROR_NULL_PTR; - csum->real = cnum1->real + cnum2->real; - csum->imaginary = cnum1->imaginary + cnum2->imaginary; + result->real = c1->real + c2->real; + result->imaginary = c1->imaginary + c2->imaginary; return SUCCESS; +} + +error_t complex_sub(complex_t *c1, complex_t *c2, complex_t *result) +{ + if (NULL == c1 || NULL == c2) + return ERROR_NULL_PTR; + + result->real = c1->real - c2->real; + result->imaginary = c1->imaginary - c2->imaginary; + + return SUCCESS; +} + +error_t complex_mul(complex_t *c1, complex_t *c2, complex_t *result) +{ + if (NULL == c1 || NULL == c2) + return ERROR_NULL_PTR; + + result->real = (c1->real * c2->real) - (c1->imaginary * c2->imaginary); + result->imaginary = (c1->real * c2->imaginary) + (c1->imaginary * c2->real); + + return SUCCESS; +} + +error_t complex_div(complex_t *c1, complex_t *c2, complex_t *result) +{ + if (c2->real == 0 && c2->imaginary == 0) + { + return ERROR_DIV_BY_ZERO; + } + + else if (c2 == NULL || c1 == NULL) + return ERROR_NULL_PTR; + + else + { + result->real = ((c1->real * c2->real) + (c1->imaginary * c2->imaginary)) / (pow(c2->real, 2) + pow(c2->imaginary, 2)); + result->imaginary = ((c1->imaginary * c2->real) - (c1->real * c2->imaginary)) / (pow(c2->real, 2) + pow(c2->imaginary, 2)); + + return SUCCESS; + } } \ No newline at end of file diff --git a/Example_Programs/programming_concpets/calculator_complex/test/test_complex_calculator.c b/Example_Programs/programming_concpets/calculator_complex/test/test_complex_calculator.c index 8c823660..d7aa165a 100644 --- a/Example_Programs/programming_concpets/calculator_complex/test/test_complex_calculator.c +++ b/Example_Programs/programming_concpets/calculator_complex/test/test_complex_calculator.c @@ -63,7 +63,208 @@ void test_negative(void) } -int main(void) +void cadd(void) +{ + c1.real = 0; + c1.imaginary = 0; + + c2.real = 0; + c2.imaginary = 0; + + TEST_ASSERT_EQUAL(SUCCESS, complex_sum(&c1, &c2, &result)); + TEST_ASSERT_EQUAL(0, result.real); + TEST_ASSERT_EQUAL(0, result.imaginary); + + c1.real = 30; + c1.imaginary = 40; + + c2.real = 20; + c2.imaginary = 80; + + TEST_ASSERT_EQUAL(SUCCESS, complex_sum(&c1, &c2, &result)); + TEST_ASSERT_EQUAL(50, result.real); + TEST_ASSERT_EQUAL(120, result.imaginary); + + c1.real = -50; + c1.imaginary = -25; + + c2.real = 80; + c2.imaginary = 85; + + TEST_ASSERT_EQUAL(SUCCESS, complex_sum(&c1, &c2, &result)); + TEST_ASSERT_EQUAL(30, result.real); + TEST_ASSERT_EQUAL(60, result.imaginary); + + c1.real = -25; + c1.imaginary = -15; + + c2.real = -75; + c2.imaginary = -95; + + TEST_ASSERT_EQUAL(SUCCESS, complex_sum(&c1, &c2, &result)); + TEST_ASSERT_EQUAL(-100, result.real); + TEST_ASSERT_EQUAL(-110, result.imaginary); +} + +void cdiff(void) +{ + c1.real = 0; + c1.imaginary = 0; + + c2.real = 0; + c2.imaginary = 0; + + TEST_ASSERT_EQUAL(SUCCESS, complex_sub(&c1, &c2, &result)); + TEST_ASSERT_EQUAL(0, result.real); + TEST_ASSERT_EQUAL(0, result.imaginary); + + c1.real = 30; + c1.imaginary = 40; + + c2.real = 20; + c2.imaginary = 80; + + TEST_ASSERT_EQUAL(SUCCESS, complex_sub(&c1, &c2, &result)); + TEST_ASSERT_EQUAL(10, result.real); + TEST_ASSERT_EQUAL(-40, result.imaginary); + + c1.real = -50; + c1.imaginary = -25; + + c2.real = 80; + c2.imaginary = 85; + + TEST_ASSERT_EQUAL(SUCCESS, complex_sub(&c1, &c2, &result)); + TEST_ASSERT_EQUAL(-130, result.real); + TEST_ASSERT_EQUAL(-110, result.imaginary); + + c1.real = -25; + c1.imaginary = -15; + + c2.real = -75; + c2.imaginary = -95; + + TEST_ASSERT_EQUAL(SUCCESS, complex_sub(&c1, &c2, &result)); + TEST_ASSERT_EQUAL(50, result.real); + TEST_ASSERT_EQUAL(80, result.imaginary); +} + +void cmul(void) +{ + c1.real = 0; + c1.imaginary = 0; + + c2.real = 0; + c2.imaginary = 0; + + TEST_ASSERT_EQUAL(SUCCESS, complex_mul(&c1, &c2, &result)); + TEST_ASSERT_EQUAL(0, result.real); + TEST_ASSERT_EQUAL(0, result.imaginary); + + c1.real = 30; + c1.imaginary = 40; + + c2.real = 20; + c2.imaginary = 80; + + TEST_ASSERT_EQUAL(SUCCESS, complex_mul(&c1, &c2, &result)); + TEST_ASSERT_EQUAL(-2600, result.real); + TEST_ASSERT_EQUAL(3200, result.imaginary); + + c1.real = -50; + c1.imaginary = -25; + + c2.real = 80; + c2.imaginary = 85; + + TEST_ASSERT_EQUAL(SUCCESS, complex_mul(&c1, &c2, &result)); + TEST_ASSERT_EQUAL(-1875, result.real); + TEST_ASSERT_EQUAL(-6250, result.imaginary); + + c1.real = -25; + c1.imaginary = -15; + + c2.real = -75; + c2.imaginary = -95; + + TEST_ASSERT_EQUAL(SUCCESS, complex_mul(&c1, &c2, &result)); + TEST_ASSERT_EQUAL(450, result.real); + TEST_ASSERT_EQUAL(3500, result.imaginary); + + c1.real = 0; + c1.imaginary = -15; + + c2.real = -75; + c2.imaginary = 0; + + TEST_ASSERT_EQUAL(SUCCESS, complex_mul(&c1, &c2, &result)); + TEST_ASSERT_EQUAL(0, result.real); + TEST_ASSERT_EQUAL(1125, result.imaginary); +} + +void cdiv(void) +{ + c1.real = 0; + c1.imaginary = 0; + + c2.real = 0; + c2.imaginary = 0; + + TEST_ASSERT_EQUAL(ERROR_DIV_BY_ZERO, complex_div(&c1, &c2, &result)); + + c1.real = 30; + c1.imaginary = 40; + + c2.real = 20; + c2.imaginary = 80; + + TEST_ASSERT_EQUAL(SUCCESS, complex_div(&c1, &c2, &result)); + TEST_ASSERT_EQUAL(0, result.real); + TEST_ASSERT_EQUAL(0, result.imaginary); + + c1.real = 55; + c1.imaginary = -3; + + c2.real = 10; + c2.imaginary = 10; + + TEST_ASSERT_EQUAL(SUCCESS, complex_div(&c1, &c2, &result)); + TEST_ASSERT_EQUAL(2, result.real); + TEST_ASSERT_EQUAL(-2, result.imaginary); + + c1.real = -50; + c1.imaginary = -25; + + c2.real = 80; + c2.imaginary = 85; + + TEST_ASSERT_EQUAL(SUCCESS, complex_div(&c1, &c2, &result)); + TEST_ASSERT_EQUAL(0, result.real); + TEST_ASSERT_EQUAL(0, result.imaginary); + + c1.real = -25; + c1.imaginary = -15; + + c2.real = -75; + c2.imaginary = -95; + + TEST_ASSERT_EQUAL(SUCCESS, complex_div(&c1, &c2, &result)); + TEST_ASSERT_EQUAL(0, result.real); + TEST_ASSERT_EQUAL(0, result.imaginary); + + c1.real = 0; + c1.imaginary = -15; + + c2.real = -75; + c2.imaginary = 0; + + TEST_ASSERT_EQUAL(SUCCESS, complex_div(&c1, &c2, &result)); + TEST_ASSERT_EQUAL(0, result.real); + TEST_ASSERT_EQUAL(0, result.imaginary); +} + +int test_main(void) + { /* Initiate the Unity Test Framework */ UNITY_BEGIN(); @@ -72,7 +273,24 @@ int main(void) RUN_TEST(test_zero); RUN_TEST(test_positive); RUN_TEST(test_negative); + RUN_TEST(cadd); + RUN_TEST(cdiff); + RUN_TEST(cmul); + RUN_TEST(cdiv); /* Close the Unity Test Framework */ return UNITY_END(); } + +// int main(void) +//{ + /* Initiate the Unity Test Framework */ + // UNITY_BEGIN(); + + /* Run Test functions */ + + + /* Close the Unity Test Framework */ + // return UNITY_END(); + +//} \ No newline at end of file From 251992442b2db2e17bbe864521fab1212f3f3a22 Mon Sep 17 00:00:00 2001 From: Ayush Bishnoi Date: Thu, 1 Apr 2021 15:30:02 +0530 Subject: [PATCH 02/34] after second seccessful build --- .../calculator_complex/complex_calculator.gcno | Bin 0 -> 2208 bytes .../calculator_complex/inc/complex_calculator.h | 5 +++++ .../calculator_complex/main.c | 2 ++ .../calculator_complex/src/complex_calculator.c | 4 +++- 4 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 Example_Programs/programming_concpets/calculator_complex/complex_calculator.gcno diff --git a/Example_Programs/programming_concpets/calculator_complex/complex_calculator.gcno b/Example_Programs/programming_concpets/calculator_complex/complex_calculator.gcno new file mode 100644 index 0000000000000000000000000000000000000000..e50c1a0c7c42663dc9ee828560881f31e3d1f51e GIT binary patch literal 2208 zcmdT_Jxc>Y5WV#)DjE?{6eUC=ii**IO0Y77R5mFDJ0+eUD2b88h=rwM<3F$#QdrrD zm7Q4H_yYt}SXkNy=gr)bh(T?yVtdxD`gpG${ivU z>R^h|QsdyD2DO%6H4d#%H^9`_rS3xyt%=SB9PV1iB|l>K82W#$OPnW6UbE)vGF&54 zBwW9BIqu>@V|@oE9U_FCAfIf=VX?;RFMEg2>90k%#>LV$qfNQ-EriBnjQ;-`<2-|W z7Tq+bu@~2`#zT5Ze#(37SL0#gjF68u #include "complex_calculator.h" + + int main() { //TODO Write all the logic for Calculator app diff --git a/Example_Programs/programming_concpets/calculator_complex/src/complex_calculator.c b/Example_Programs/programming_concpets/calculator_complex/src/complex_calculator.c index 02e21e8f..bb5f3ca8 100644 --- a/Example_Programs/programming_concpets/calculator_complex/src/complex_calculator.c +++ b/Example_Programs/programming_concpets/calculator_complex/src/complex_calculator.c @@ -1,6 +1,8 @@ -#include "stdlib.h" +#include +#include #include "complex_calculator.h" + error_t complex_sum(complex_t* c1, complex_t* c2, complex_t* result) { if(NULL == c1 || NULL == c2) From 210ac803f99d997baa23d7771ffaecd7ad6e58ca Mon Sep 17 00:00:00 2001 From: Ayush Bishnoi Date: Sun, 4 Apr 2021 21:37:58 +0530 Subject: [PATCH 03/34] test file working finally --- .../calculator_complex/Makefile | 21 +- .../complex_calculator.gcno | Bin 2208 -> 2208 bytes .../doc/Complex_Calc_Doxyfile | 2582 ----------------- .../calculator_complex/doc/Makefile | 8 - .../calculator_complex/doc/doc_main.md | 2 - .../src/complex_calculator.c | 7 + .../test/test_complex_calculator.c | 3 +- 7 files changed, 21 insertions(+), 2602 deletions(-) delete mode 100644 Example_Programs/programming_concpets/calculator_complex/doc/Complex_Calc_Doxyfile delete mode 100644 Example_Programs/programming_concpets/calculator_complex/doc/Makefile delete mode 100644 Example_Programs/programming_concpets/calculator_complex/doc/doc_main.md diff --git a/Example_Programs/programming_concpets/calculator_complex/Makefile b/Example_Programs/programming_concpets/calculator_complex/Makefile index 937ab909..a89dafcd 100644 --- a/Example_Programs/programming_concpets/calculator_complex/Makefile +++ b/Example_Programs/programming_concpets/calculator_complex/Makefile @@ -1,3 +1,5 @@ + + PROJ_NAME = Complex_Calculator TEST_PROJ_NAME = Test_$(PROJ_NAME) @@ -13,15 +15,15 @@ INC = -Iinc\ #To check if the OS is Windows or Linux and set the executable file extension and delete command accordingly ifdef OS - RM = del /q - FixPath = $(subst /,\,$1) - EXEC = exe + RM = del /q + FixPath = $(subst /,\,$1) + EXEC = exe else - ifeq ($(shell uname), Linux) - RM = rm -rf - FixPath = $1 - EXEC = out - endif + ifeq ($(shell uname), Linux) + RM = rm -rf + FixPath = $1 + EXEC = out + endif endif # Makefile will not run target command if the name with file already exists. To override, use .PHONY @@ -50,4 +52,5 @@ $(BUILD_DIR): clean: $(RM) $(call FixPath,$(BUILD_DIR)/*) make clean -C doc - rmdir $(BUILD_DIR) doc/html \ No newline at end of file + rmdir $(BUILD_DIR) doc/html + diff --git a/Example_Programs/programming_concpets/calculator_complex/complex_calculator.gcno b/Example_Programs/programming_concpets/calculator_complex/complex_calculator.gcno index e50c1a0c7c42663dc9ee828560881f31e3d1f51e..dce3a3f0c27ef55d5de263b093fa778d1e6e0bbb 100644 GIT binary patch delta 316 zcmZ1=xIj=LKQB35%h1Acu1McpMg}lo6b4d7`&T%8lF2wfQNe;!8_4GbVw;VL!i=mS zp6+Bp7VF6$7)2&0uxK#qPyWbg&1eK9g(o{OX+T*uK$h|3Kn~%_JD4;$&4IcsfY@sC zMmAxNt508gf8WfqYBCG61*ZZ~7-YQWWM1~;tRS8e$aqGj$%bs!jLJYGtT|PIqG~{_ z0n$GC0SgODZLNFy