You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.1 KiB

  1. #ifdef TEST
  2. #include "unity.h"
  3. #include "../src/interestCalculator.c"
  4. void setUp(void)
  5. {
  6. }
  7. void tearDown(void)
  8. {
  9. }
  10. void test_initiateInterest(void) {
  11. /* Arrange */
  12. int length = 10;
  13. float startAmount[] = {34, 233, 4400, 1600, 245, 34544, 3767.32, 1422, 5435, 8199};
  14. float yearlyInterest=12;
  15. float durationInYears=2.5;
  16. float monthlyInterest=8;
  17. float durationInMonths=3.9;
  18. float resultsYearly[length];
  19. float resultsMonthly[length];
  20. float expectedYearly[]={44.2,302.9,5720,2080,318.5,44907.2,4897.516,1848.6,7065.5,10658.7};
  21. float expectedMonthly[]={44.608,305.696,5772.8,2099.2,321.44,45321.73,4942.724,1865.664,7130.72,10757.09};
  22. /* Act */
  23. for (int i = 0; i < length; i++) {
  24. resultsYearly[i]=initiateInterest(startAmount[i],yearlyInterest/100, durationInYears);
  25. }
  26. for (int i = 0; i < length; i++) {
  27. resultsMonthly[i]=initiateInterest(startAmount[i],monthlyInterest/100, durationInMonths);
  28. }
  29. /* Assert*/
  30. for (int i = 0; i < length; i++) {
  31. TEST_ASSERT_EQUAL_FLOAT(expectedYearly[i], resultsYearly[i]);
  32. TEST_ASSERT_EQUAL_FLOAT(expectedMonthly[i], resultsMonthly[i]);
  33. }
  34. }
  35. void test_initiateInterestFalse(void) {
  36. /* Arrange */
  37. int length = 10;
  38. float startAmount[] = {34, 233, 4400, 1600, 245, 34544, 3767.32, 1422, 5435, 8199};
  39. float yearlyInterest=12;
  40. float durationInYears=2.5;
  41. float monthlyInterest=8;
  42. float durationInMonths=3.9;
  43. float resultsYearly[length];
  44. float resultsMonthly[length];
  45. float expectedYearly[]={0,0,0,0,0,0,0,0,0,0};
  46. float expectedMonthly[]={0,0,0,0,0,0,0,0,0,0};
  47. /* Act */
  48. for (int i = 0; i < length; i++) {
  49. resultsYearly[i]=initiateInterest(startAmount[i],yearlyInterest/100, durationInYears);
  50. }
  51. for (int i = 0; i < length; i++) {
  52. resultsMonthly[i]=initiateInterest(startAmount[i],monthlyInterest/100, durationInMonths);
  53. }
  54. /* Assert*/
  55. for (int i = 0; i < length; i++) {
  56. TEST_ASSERT_FALSE(expectedYearly[i] == resultsYearly[i]);
  57. TEST_ASSERT_FALSE(expectedMonthly[i] == resultsMonthly[i]);
  58. }
  59. }
  60. #endif // TEST