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.

149 lines
3.5 KiB

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