bitstream_tests.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * bitstream_tests.c
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2007 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. /*! \page bitstream_tests_page Bitstream tests
  26. \section bitstream_tests_page_sec_1 What does it do?
  27. \section bitstream_tests_page_sec_2 How is it used?
  28. */
  29. #if defined(HAVE_CONFIG_H)
  30. #include "config.h"
  31. #endif
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <fcntl.h>
  35. #include <string.h>
  36. #include <assert.h>
  37. #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
  38. #include "spandsp.h"
  39. uint8_t buffer[256];
  40. #define PATTERN 0x1111111
  41. #define SEQUENCE_LENGTH 17
  42. uint8_t left[] =
  43. {
  44. 0x28, /* 2 of 4, 3, 2, 1 */
  45. 0xC8, /* 1 of 6, 5, 2 of 4 */
  46. 0xAE, /* 3 of 7, 5 of 6 */
  47. 0x67, /* 4 of 8, 4 of 7 */
  48. 0x74, /* 4 of 9, 4 of 8 */
  49. 0x43, /* 3 of 10, 5 of 9 */
  50. 0x32, /* 1 of 11, 7 of 10 */
  51. 0xAA, /* 8 of 11 */
  52. 0xAE, /* 6 of 12, 2 of 11 */
  53. 0xED, /* 2 of 13, 6 of 12 */
  54. 0x99, /* 8 of 13 */
  55. 0x8E, /* 5 of 14, 3 of 13 */
  56. 0xEE, /* 8 of 14 */
  57. 0xEE, /* 7 of 15, 1 of 14 */
  58. 0xEE, /* 8 of 15 */
  59. 0xFF, /* 8 of 16 */
  60. 0xFF, /* 8 of 16 */
  61. 0x88, /* 8 of 17 */
  62. 0x88, /* 8 of 17 */
  63. 0x00 /* 1 of 17 */
  64. };
  65. uint8_t right[] =
  66. {
  67. 0xD2, /* 1, 2, 3, 2 of 4 */
  68. 0x90, /* 2 of 4, 5, 1 of 6 */
  69. 0xCA, /* 5 of 6, 3 of 7 */
  70. 0x7C, /* 4 of 7, 4 of 8 */
  71. 0x87, /* 4 of 8, 4 of 9 */
  72. 0x28, /* 5 of 9, 3 of 10 */
  73. 0x33, /* 7 of 10, 1 of 11 */
  74. 0x55, /* 8 of 11 */
  75. 0xED, /* 2 of 11, 6 of 12 */
  76. 0x2E, /* 6 of 12, 2 of 13 */
  77. 0x33, /* 8 of 13 */
  78. 0xEB, /* 3 of 13, 5 of 14 */
  79. 0xEE, /* 8 of 14 */
  80. 0xDC, /* 1 of 14, 7 of 15 */
  81. 0xDD, /* 8 of 15 */
  82. 0xFF, /* 8 of 16 */
  83. 0xFF, /* 8 of 16 */
  84. 0x10, /* 8 of 17 */
  85. 0x11, /* 8 of 17 */
  86. 0x01 /* 1 of 17 */
  87. };
  88. int main(int argc, char *argv[])
  89. {
  90. int i;
  91. bitstream_state_t state;
  92. bitstream_state_t *s;
  93. const uint8_t *r;
  94. uint8_t *w;
  95. uint8_t *cc;
  96. unsigned int x;
  97. int total_bits;
  98. s = bitstream_init(&state, true);
  99. w = buffer;
  100. total_bits = 0;
  101. for (i = 0; i < SEQUENCE_LENGTH; i++)
  102. {
  103. bitstream_put(s, &w, i*PATTERN, i + 1);
  104. total_bits += (i + 1);
  105. }
  106. bitstream_flush(s, &w);
  107. printf("%d bits written\n", total_bits);
  108. for (cc = buffer; cc < w; cc++)
  109. printf("%02X ", *cc);
  110. printf("\n");
  111. for (cc = right; cc < right + sizeof(right); cc++)
  112. printf("%02X ", *cc);
  113. printf("\n");
  114. if ((w - buffer) != sizeof(right) || memcmp(buffer, right, sizeof(right)))
  115. {
  116. printf("Test failed\n");
  117. exit(2);
  118. }
  119. s = bitstream_init(&state, true);
  120. r = buffer;
  121. for (i = 0; i < SEQUENCE_LENGTH; i++)
  122. {
  123. x = bitstream_get(s, &r, i + 1);
  124. if (x != ((PATTERN*i) & ((1 << (i + 1)) - 1)))
  125. {
  126. printf("Error 0x%X 0x%X\n", x, ((PATTERN*i) & ((1 << (i + 1)) - 1)));
  127. printf("Test failed\n");
  128. exit(2);
  129. }
  130. }
  131. s = bitstream_init(&state, false);
  132. w = buffer;
  133. total_bits = 0;
  134. for (i = 0; i < SEQUENCE_LENGTH; i++)
  135. {
  136. bitstream_put(s, &w, PATTERN*i, i + 1);
  137. total_bits += (i + 1);
  138. }
  139. bitstream_flush(s, &w);
  140. printf("%d bits written\n", total_bits);
  141. for (cc = buffer; cc < w; cc++)
  142. printf("%02X ", *cc);
  143. printf("\n");
  144. for (cc = left; cc < left + sizeof(left); cc++)
  145. printf("%02X ", *cc);
  146. printf("\n");
  147. if ((w - buffer) != sizeof(left) || memcmp(buffer, left, sizeof(left)))
  148. {
  149. printf("Test failed\n");
  150. exit(2);
  151. }
  152. s = bitstream_init(&state, false);
  153. r = buffer;
  154. for (i = 0; i < SEQUENCE_LENGTH; i++)
  155. {
  156. x = bitstream_get(s, &r, i + 1);
  157. if (x != ((PATTERN*i) & ((1 << (i + 1)) - 1)))
  158. {
  159. printf("Error 0x%X 0x%X\n", x, ((PATTERN*i) & ((1 << (i + 1)) - 1)));
  160. printf("Test failed\n");
  161. exit(2);
  162. }
  163. }
  164. printf("Tests passed.\n");
  165. return 0;
  166. }
  167. /*- End of function --------------------------------------------------------*/
  168. /*- End of file ------------------------------------------------------------*/