crc_tests.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * crc_tests.c
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2003 Steve Underwood
  9. *
  10. * All rights reserved.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2, as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. /*! \file */
  26. /*! \page crc_tests_page CRC tests
  27. \section crc_tests_page_sec_1 What does it do?
  28. The CRC tests exercise the ITU-16 and ITU-32 CRC module, and verifies
  29. correct operation.
  30. */
  31. #if defined(HAVE_CONFIG_H)
  32. #include "config.h"
  33. #endif
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include "spandsp.h"
  38. int ref_len;
  39. uint8_t buf[1000];
  40. /* Use a local random generator, so the results are consistent across platforms. We have hard coded
  41. correct results for a message sequence generated by this particular PRNG. */
  42. static int my_rand(void)
  43. {
  44. static int rndnum = 1234567;
  45. return (rndnum = 1664525U*rndnum + 1013904223U) >> 8;
  46. }
  47. /*- End of function --------------------------------------------------------*/
  48. static int cook_up_msg(uint8_t *buf)
  49. {
  50. int i;
  51. int len;
  52. /* Use medium length messages, with some randomised length variation. */
  53. /* TODO: this doesn't exercise extremely short or long messages. */
  54. len = (my_rand() & 0x3F) + 100;
  55. for (i = 0; i < len; i++)
  56. buf[i] = my_rand();
  57. return len;
  58. }
  59. /*- End of function --------------------------------------------------------*/
  60. int main(int argc, char *argv[])
  61. {
  62. int i;
  63. int j;
  64. int len;
  65. uint16_t crc16a;
  66. uint16_t crc16b;
  67. printf("CRC module tests\n");
  68. /* TODO: This doesn't check every function in the module */
  69. /* Try a few random messages through the CRC logic. */
  70. printf("Testing the CRC-16 routines\n");
  71. for (i = 0; i < 100; i++)
  72. {
  73. ref_len = cook_up_msg(buf);
  74. len = crc_itu16_append(buf, ref_len);
  75. if (!crc_itu16_check(buf, len))
  76. {
  77. printf("CRC-16 failure\n");
  78. exit(2);
  79. }
  80. }
  81. printf("Test passed.\n\n");
  82. printf("Testing the CRC-16 byte by byte and bit by bit routines\n");
  83. for (i = 0; i < 100; i++)
  84. {
  85. ref_len = cook_up_msg(buf);
  86. crc16a = 0xFFFF;
  87. crc16a = crc_itu16_calc(buf, ref_len, crc16a);
  88. crc16b = 0xFFFF;
  89. for (j = 0; j < ref_len; j++)
  90. crc16b = crc_itu16_bits(buf[j], 8, crc16b);
  91. if (crc16a != crc16b)
  92. {
  93. printf("CRC-16 failure\n");
  94. exit(2);
  95. }
  96. }
  97. printf("Test passed.\n\n");
  98. printf("Testing the CRC-32 routines\n");
  99. for (i = 0; i < 100; i++)
  100. {
  101. ref_len = cook_up_msg(buf);
  102. len = crc_itu32_append(buf, ref_len);
  103. if (!crc_itu32_check(buf, len))
  104. {
  105. printf("CRC-32 failure\n");
  106. exit(2);
  107. }
  108. }
  109. printf("Test passed.\n");
  110. return 0;
  111. }
  112. /*- End of function --------------------------------------------------------*/
  113. /*- End of file ------------------------------------------------------------*/