fix16_str_unittests.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <fix16.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <string.h>
  5. #include <stdbool.h>
  6. #include "unittests.h"
  7. int main()
  8. {
  9. int status = 0;
  10. {
  11. COMMENT("Testing fix16_to_str corner cases");
  12. char buf[13];
  13. fix16_to_str(fix16_from_dbl(1234.5678), buf, 4);
  14. printf("1234.5678 = %s\n", buf);
  15. TEST(strcmp(buf, "1234.5678") == 0);
  16. fix16_to_str(fix16_from_dbl(-1234.5678), buf, 4);
  17. printf("-1234.5678 = %s\n", buf);
  18. TEST(strcmp(buf, "-1234.5678") == 0);
  19. fix16_to_str(0, buf, 0);
  20. TEST(strcmp(buf, "0") == 0);
  21. fix16_to_str(fix16_from_dbl(0.9), buf, 0);
  22. TEST(strcmp(buf, "1") == 0);
  23. fix16_to_str(1, buf, 5);
  24. printf("(fix16_t)1 = %s\n", buf);
  25. TEST(strcmp(buf, "0.00002") == 0);
  26. fix16_to_str(-1, buf, 5);
  27. printf("(fix16_t)-1 = %s\n", buf);
  28. TEST(strcmp(buf, "-0.00002") == 0);
  29. fix16_to_str(65535, buf, 5);
  30. printf("(fix16_t)65535 = %s\n", buf);
  31. TEST(strcmp(buf, "0.99998") == 0);
  32. fix16_to_str(65535, buf, 4);
  33. printf("(fix16_t)65535 = %s\n", buf);
  34. TEST(strcmp(buf, "1.0000") == 0);
  35. fix16_to_str(fix16_maximum, buf, 5);
  36. printf("fix16_maximum = %s\n", buf);
  37. TEST(strcmp(buf, "32767.99998") == 0);
  38. fix16_to_str(fix16_minimum, buf, 5);
  39. printf("fix16_minimum = %s\n", buf);
  40. TEST(strcmp(buf, "-32768.00000") == 0);
  41. }
  42. {
  43. COMMENT("Testing fix16_from_str corner cases");
  44. TEST(fix16_from_str("1234.5678") == fix16_from_dbl(1234.5678));
  45. TEST(fix16_from_str("-1234.5678") == fix16_from_dbl(-1234.5678));
  46. TEST(fix16_from_str(" +1234,56780 ") == fix16_from_dbl(1234.5678));
  47. TEST(fix16_from_str("0") == 0);
  48. TEST(fix16_from_str("1") == fix16_one);
  49. TEST(fix16_from_str("1.0") == fix16_one);
  50. TEST(fix16_from_str("1.0000000000") == fix16_one);
  51. TEST(fix16_from_str("0.00002") == 1);
  52. TEST(fix16_from_str("0.99998") == 65535);
  53. TEST(fix16_from_str("32767.99998") == fix16_maximum);
  54. TEST(fix16_from_str("-32768.00000") == fix16_minimum);
  55. }
  56. {
  57. COMMENT("Extended testing for whole range");
  58. fix16_t value = fix16_minimum;
  59. char testbuf[13];
  60. char goodbuf[13];
  61. bool ok = true;
  62. while (value < fix16_maximum)
  63. {
  64. double fvalue = fix16_to_dbl(value);
  65. /* Turns out we have to jump through some hoops to round
  66. doubles perfectly for printing:
  67. http://stackoverflow.com/questions/994764/rounding-doubles-5-sprintf
  68. */
  69. fvalue = round(fvalue * 100000.)/100000.;
  70. snprintf(goodbuf, 13, "%0.5f", fvalue);
  71. fix16_to_str(value, testbuf, 5);
  72. if (strcmp(goodbuf, testbuf) != 0)
  73. {
  74. printf("Value (fix16_t)%d gave %s, should be %s\n", value, testbuf, goodbuf);
  75. ok = false;
  76. }
  77. fix16_t roundtrip = fix16_from_str(testbuf);
  78. if (roundtrip != value)
  79. {
  80. printf("Roundtrip failed: (fix16_t)%d -> %s -> (fix16_t)%d\n", value, testbuf, roundtrip);
  81. ok = false;
  82. }
  83. value += 0x10001;
  84. }
  85. TEST(ok);
  86. }
  87. if (status != 0)
  88. fprintf(stdout, "\n\nSome tests FAILED!\n");
  89. return status;
  90. }