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.

125 lines
2.9 KiB

11 months ago
11 months ago
11 months ago
11 months ago
  1. #ifdef TEST
  2. #include "unity.h"
  3. #include "exponent.h"
  4. #include "logarithmus.h"
  5. void setUp(void)
  6. {
  7. }
  8. void tearDown(void)
  9. {
  10. }
  11. void test_DoubleBase_ForPositiveExponent(void){
  12. int r1, r2, r3;
  13. r1 = p(2.0, -2.0);
  14. r2 = p(3.0, -3.0);
  15. r3 = p(4.0, 5.0);
  16. TEST_ASSERT_EQUAL_INT(4.0, r1);
  17. TEST_ASSERT_EQUAL_INT(-27.0, r2);
  18. TEST_ASSERT_EQUAL_INT(625.0, r3);
  19. }
  20. void test_DoubleBase_ForNegativeExponent(void){
  21. float r1, r2, r3;
  22. r1 = powerD(-1.0, 2.0);
  23. r2 = powerD(-3.0, -3.0);
  24. r3 = powerD(-2.0, -2.0);
  25. TEST_ASSERT_EQUAL_FLOAT(0.5, r1);
  26. TEST_ASSERT_EQUAL_FLOAT(-0.0370370373, r2);
  27. TEST_ASSERT_EQUAL_FLOAT(0.25, r3);
  28. }
  29. void test_DoubleBase_for_positive_decimal_exponent(void){
  30. float r1, r2, r3;
  31. r1 = powerD(0.45, 3.0);
  32. r2 = powerD(0.5, 9.0);
  33. r3 = powerD(0.5, 2.0);
  34. TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.639474, r1);
  35. TEST_ASSERT_DOUBLE_WITHIN(0.0001, 3.0, r2);
  36. TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.414213, r3);
  37. }
  38. void test_DoubleDecimalBase_for_positive_decimal_exponent(void){
  39. float r1, r2, r3;
  40. r1 = powerD(0.31, 0.413);
  41. r2 = powerD(0.5, 3.13432);
  42. r3 = powerD(0.124, 10.1231);
  43. TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.760229, r1);
  44. TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.770401, r2);
  45. TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.332474, r3);
  46. }
  47. void test_DoubleBase_for_negative_decimal_exponent(void){
  48. float r1, r2, r3;
  49. r1 = powerD(-0.125, 3.0);
  50. r2 = powerD(-0.5, 2.0);
  51. r3 = powerD(-0.12923, 10.0);
  52. TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.871685, r1);
  53. TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.707106, r2);
  54. TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.742625, r3);
  55. }
  56. void test_DoubleDecimalBase_for_negative_decimal_exponent(void){
  57. float r1, r2, r3;
  58. r1 = powerD(-0.5, 6.2315);
  59. r2 = powerD(-0.1937, 9.2309);
  60. r3 = powerD(-2.2145, 22.9983);
  61. TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.400593, r1);
  62. TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.650177, r2);
  63. TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.000964, r3);
  64. }
  65. void test_exponent_edge_case(void){
  66. float r1, r2, r3, r4, r5;
  67. r1 = powerD(0, 2);
  68. r2 = powerD(0, 0);
  69. r3 = powerD(2, 0);
  70. r4 = powerD(-3, 0);
  71. r5 = powerD(0.5, -3);
  72. TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.0, r1);
  73. TEST_ASSERT_DOUBLE_WITHIN(0.0001, 1.0, r2);
  74. TEST_ASSERT_DOUBLE_WITHIN(0.0001, 0.0, r3);
  75. TEST_ASSERT_DOUBLE_WITHIN(0.0001, -1.0, r4);
  76. TEST_ASSERT_DOUBLE_WITHIN(0.0001, -1.0, r5);
  77. }
  78. void test_Exponent_with_positive_number(void){
  79. double r1, r2, r3;
  80. r1 = exponential(0.0);
  81. r2 = exponential(1.0);
  82. r3 = exponential(2.0);
  83. TEST_ASSERT_EQUAL_DOUBLE(1.0, r1);
  84. TEST_ASSERT_DOUBLE_WITHIN(0.0001, 2.718282, r2);
  85. TEST_ASSERT_DOUBLE_WITHIN(0.0001, 7.389056, r3);
  86. }
  87. void test_Exponent_with_negative_number(void){
  88. double r1, r2, r3;
  89. r1 = exponential(-1.0);
  90. r2 = exponential(-2.0);
  91. TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.367879, r1);
  92. TEST_ASSERT_DOUBLE_WITHIN(0.000001, 0.135335, r2);
  93. }
  94. #endif // TEST