2
0

bert_tests.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * bert_tests.c - Tests for the BER tester.
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2004 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 bert_tests_page BERT tests
  27. \section bert_tests_page_sec_1 What does it do?
  28. These tests exercise each of the BERT standards supported by the BERT module.
  29. */
  30. #if defined(HAVE_CONFIG_H)
  31. #include "config.h"
  32. #endif
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <assert.h>
  37. #include <sndfile.h>
  38. #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
  39. #include "spandsp.h"
  40. /* Use a local random generator, so the results are consistent across platforms */
  41. static int my_rand(void)
  42. {
  43. static int rndnum = 1234567;
  44. return (rndnum = 1664525U*rndnum + 1013904223U);
  45. }
  46. /*- End of function --------------------------------------------------------*/
  47. static void reporter(void *user_data, int reason, bert_results_t *results)
  48. {
  49. int channel;
  50. channel = (int) (intptr_t) user_data;
  51. printf("%d: BERT report '%s' ", channel, bert_event_to_str(reason));
  52. switch (reason)
  53. {
  54. case BERT_REPORT_REGULAR:
  55. printf("%d bits, %d bad bits, %d resyncs", results->total_bits, results->bad_bits, results->resyncs);
  56. break;
  57. }
  58. printf("\r");
  59. }
  60. /*- End of function --------------------------------------------------------*/
  61. int8_t test[0x800000];
  62. int main(int argc, char *argv[])
  63. {
  64. bert_state_t *tx_bert;
  65. bert_state_t *rx_bert;
  66. bert_state_t *bert;
  67. bert_results_t bert_results;
  68. int i;
  69. int bit;
  70. int zeros;
  71. int max_zeros;
  72. int failed;
  73. tx_bert = bert_init(NULL, 0, BERT_PATTERN_ZEROS, 300, 20);
  74. rx_bert = bert_init(NULL, 0, BERT_PATTERN_ZEROS, 300, 20);
  75. for (i = 0; i < 511*2; i++)
  76. {
  77. bit = bert_get_bit(tx_bert);
  78. bert_put_bit(rx_bert, bit);
  79. }
  80. bert_result(rx_bert, &bert_results);
  81. printf("Zeros: Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
  82. if (bert_results.bad_bits || bert_results.total_bits != 950)
  83. {
  84. printf("Test failed.\n");
  85. exit(2);
  86. }
  87. bert_free(tx_bert);
  88. bert_free(rx_bert);
  89. tx_bert = bert_init(NULL, 0, BERT_PATTERN_ONES, 300, 20);
  90. rx_bert = bert_init(NULL, 0, BERT_PATTERN_ONES, 300, 20);
  91. for (i = 0; i < 511*2; i++)
  92. {
  93. bit = bert_get_bit(tx_bert);
  94. bert_put_bit(rx_bert, bit);
  95. }
  96. bert_result(rx_bert, &bert_results);
  97. printf("Ones: Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
  98. if (bert_results.bad_bits || bert_results.total_bits != 950)
  99. {
  100. printf("Test failed.\n");
  101. exit(2);
  102. }
  103. bert_free(tx_bert);
  104. bert_free(rx_bert);
  105. tx_bert = bert_init(NULL, 0, BERT_PATTERN_1_TO_7, 300, 20);
  106. rx_bert = bert_init(NULL, 0, BERT_PATTERN_1_TO_7, 300, 20);
  107. for (i = 0; i < 511*2; i++)
  108. {
  109. bit = bert_get_bit(tx_bert);
  110. bert_put_bit(rx_bert, bit);
  111. }
  112. bert_result(rx_bert, &bert_results);
  113. printf("1 to 7: Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
  114. if (bert_results.bad_bits || bert_results.total_bits != 950)
  115. {
  116. printf("Test failed.\n");
  117. exit(2);
  118. }
  119. bert_free(tx_bert);
  120. bert_free(rx_bert);
  121. tx_bert = bert_init(NULL, 0, BERT_PATTERN_1_TO_3, 300, 20);
  122. rx_bert = bert_init(NULL, 0, BERT_PATTERN_1_TO_3, 300, 20);
  123. for (i = 0; i < 511*2; i++)
  124. {
  125. bit = bert_get_bit(tx_bert);
  126. bert_put_bit(rx_bert, bit);
  127. }
  128. bert_result(rx_bert, &bert_results);
  129. printf("1 to 3: Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
  130. if (bert_results.bad_bits || bert_results.total_bits != 950)
  131. {
  132. printf("Test failed.\n");
  133. exit(2);
  134. }
  135. bert_free(tx_bert);
  136. bert_free(rx_bert);
  137. tx_bert = bert_init(NULL, 0, BERT_PATTERN_1_TO_1, 300, 20);
  138. rx_bert = bert_init(NULL, 0, BERT_PATTERN_1_TO_1, 300, 20);
  139. for (i = 0; i < 511*2; i++)
  140. {
  141. bit = bert_get_bit(tx_bert);
  142. bert_put_bit(rx_bert, bit);
  143. }
  144. bert_result(rx_bert, &bert_results);
  145. printf("1 to 1: Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
  146. if (bert_results.bad_bits || bert_results.total_bits != 950)
  147. {
  148. printf("Test failed.\n");
  149. exit(2);
  150. }
  151. bert_free(tx_bert);
  152. bert_free(rx_bert);
  153. tx_bert = bert_init(NULL, 0, BERT_PATTERN_3_TO_1, 300, 20);
  154. rx_bert = bert_init(NULL, 0, BERT_PATTERN_3_TO_1, 300, 20);
  155. for (i = 0; i < 511*2; i++)
  156. {
  157. bit = bert_get_bit(tx_bert);
  158. bert_put_bit(rx_bert, bit);
  159. }
  160. bert_result(rx_bert, &bert_results);
  161. printf("3 to 1: Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
  162. if (bert_results.bad_bits || bert_results.total_bits != 950)
  163. {
  164. printf("Test failed.\n");
  165. exit(2);
  166. }
  167. bert_free(tx_bert);
  168. bert_free(rx_bert);
  169. tx_bert = bert_init(NULL, 0, BERT_PATTERN_7_TO_1, 300, 20);
  170. rx_bert = bert_init(NULL, 0, BERT_PATTERN_7_TO_1, 300, 20);
  171. for (i = 0; i < 511*2; i++)
  172. {
  173. bit = bert_get_bit(tx_bert);
  174. bert_put_bit(rx_bert, bit);
  175. }
  176. bert_result(rx_bert, &bert_results);
  177. printf("7 to 1: Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
  178. if (bert_results.bad_bits || bert_results.total_bits != 950)
  179. {
  180. printf("Test failed.\n");
  181. exit(2);
  182. }
  183. bert_free(tx_bert);
  184. bert_free(rx_bert);
  185. tx_bert = bert_init(NULL, 0, BERT_PATTERN_ITU_O153_9, 300, 20);
  186. rx_bert = bert_init(NULL, 0, BERT_PATTERN_ITU_O153_9, 300, 20);
  187. for (i = 0; i < 0x200; i++)
  188. test[i] = 0;
  189. max_zeros = 0;
  190. zeros = 0;
  191. for (i = 0; i < 511*2; i++)
  192. {
  193. bit = bert_get_bit(tx_bert);
  194. if (bit)
  195. {
  196. if (zeros > max_zeros)
  197. max_zeros = zeros;
  198. zeros = 0;
  199. }
  200. else
  201. {
  202. zeros++;
  203. }
  204. bert_put_bit(rx_bert, bit);
  205. test[tx_bert->tx.reg]++;
  206. }
  207. failed = false;
  208. if (test[0] != 0)
  209. {
  210. printf("XXX %d %d\n", 0, test[0]);
  211. failed = true;
  212. }
  213. for (i = 1; i < 0x200; i++)
  214. {
  215. if (test[i] != 2)
  216. {
  217. printf("XXX %d %d\n", i, test[i]);
  218. failed = true;
  219. }
  220. }
  221. bert_result(rx_bert, &bert_results);
  222. printf("O.153(9): Bad bits %d/%d, max zeros %d\n", bert_results.bad_bits, bert_results.total_bits, max_zeros);
  223. if (bert_results.bad_bits || bert_results.total_bits != 986 || failed)
  224. {
  225. printf("Test failed.\n");
  226. exit(2);
  227. }
  228. bert_free(tx_bert);
  229. bert_free(rx_bert);
  230. tx_bert = bert_init(NULL, 0, BERT_PATTERN_ITU_O152_11, 300, 20);
  231. rx_bert = bert_init(NULL, 0, BERT_PATTERN_ITU_O152_11, 300, 20);
  232. for (i = 0; i < 0x800; i++)
  233. test[i] = 0;
  234. max_zeros = 0;
  235. zeros = 0;
  236. for (i = 0; i < 2047*2; i++)
  237. {
  238. bit = bert_get_bit(tx_bert);
  239. if (bit)
  240. {
  241. if (zeros > max_zeros)
  242. max_zeros = zeros;
  243. zeros = 0;
  244. }
  245. else
  246. {
  247. zeros++;
  248. }
  249. bert_put_bit(rx_bert, bit);
  250. test[tx_bert->tx.reg]++;
  251. }
  252. failed = false;
  253. if (test[0] != 0)
  254. {
  255. printf("XXX %d %d\n", 0, test[0]);
  256. failed = true;
  257. }
  258. for (i = 1; i < 0x800; i++)
  259. {
  260. if (test[i] != 2)
  261. {
  262. printf("XXX %d %d\n", i, test[i]);
  263. failed = true;
  264. }
  265. }
  266. bert_result(rx_bert, &bert_results);
  267. printf("O.152(11): Bad bits %d/%d, max zeros %d\n", bert_results.bad_bits, bert_results.total_bits, max_zeros);
  268. if (bert_results.bad_bits || bert_results.total_bits != 4052 || failed)
  269. {
  270. printf("Test failed.\n");
  271. exit(2);
  272. }
  273. bert_free(tx_bert);
  274. bert_free(rx_bert);
  275. tx_bert = bert_init(NULL, 0, BERT_PATTERN_ITU_O151_15, 300, 20);
  276. rx_bert = bert_init(NULL, 0, BERT_PATTERN_ITU_O151_15, 300, 20);
  277. for (i = 0; i < 0x8000; i++)
  278. test[i] = 0;
  279. max_zeros = 0;
  280. zeros = 0;
  281. for (i = 0; i < 32767*2; i++)
  282. {
  283. bit = bert_get_bit(tx_bert);
  284. if (bit)
  285. {
  286. if (zeros > max_zeros)
  287. max_zeros = zeros;
  288. zeros = 0;
  289. }
  290. else
  291. {
  292. zeros++;
  293. }
  294. bert_put_bit(rx_bert, bit);
  295. test[tx_bert->tx.reg]++;
  296. }
  297. failed = false;
  298. if (test[0] != 0)
  299. {
  300. printf("XXX %d %d\n", 0, test[0]);
  301. failed = true;
  302. }
  303. for (i = 1; i < 0x8000; i++)
  304. {
  305. if (test[i] != 2)
  306. {
  307. printf("XXX %d %d\n", i, test[i]);
  308. failed = true;
  309. }
  310. }
  311. bert_result(rx_bert, &bert_results);
  312. printf("O.151(15): Bad bits %d/%d, max zeros %d\n", bert_results.bad_bits, bert_results.total_bits, max_zeros);
  313. if (bert_results.bad_bits || bert_results.total_bits != 65480 || failed)
  314. {
  315. printf("Test failed.\n");
  316. exit(2);
  317. }
  318. bert_free(tx_bert);
  319. bert_free(rx_bert);
  320. tx_bert = bert_init(NULL, 0, BERT_PATTERN_ITU_O151_20, 300, 20);
  321. rx_bert = bert_init(NULL, 0, BERT_PATTERN_ITU_O151_20, 300, 20);
  322. for (i = 0; i < 0x100000; i++)
  323. test[i] = 0;
  324. max_zeros = 0;
  325. zeros = 0;
  326. for (i = 0; i < 1048575*2; i++)
  327. {
  328. bit = bert_get_bit(tx_bert);
  329. if (bit)
  330. {
  331. if (zeros > max_zeros)
  332. max_zeros = zeros;
  333. zeros = 0;
  334. }
  335. else
  336. {
  337. zeros++;
  338. }
  339. bert_put_bit(rx_bert, bit);
  340. test[tx_bert->tx.reg]++;
  341. }
  342. failed = false;
  343. if (test[0] != 0)
  344. {
  345. printf("XXX %d %d\n", 0, test[0]);
  346. failed = true;
  347. }
  348. for (i = 1; i < 0x100000; i++)
  349. {
  350. if (test[i] != 2)
  351. printf("XXX %d %d\n", i, test[i]);
  352. }
  353. bert_result(rx_bert, &bert_results);
  354. printf("O.151(20): Bad bits %d/%d, max zeros %d\n", bert_results.bad_bits, bert_results.total_bits, max_zeros);
  355. if (bert_results.bad_bits || bert_results.total_bits != 2097066 || failed)
  356. {
  357. printf("Test failed.\n");
  358. exit(2);
  359. }
  360. bert_free(tx_bert);
  361. bert_free(rx_bert);
  362. tx_bert = bert_init(NULL, 0, BERT_PATTERN_ITU_O151_23, 300, 20);
  363. rx_bert = bert_init(NULL, 0, BERT_PATTERN_ITU_O151_23, 300, 20);
  364. for (i = 0; i < 0x800000; i++)
  365. test[i] = 0;
  366. max_zeros = 0;
  367. zeros = 0;
  368. for (i = 0; i < 8388607*2; i++)
  369. {
  370. bit = bert_get_bit(tx_bert);
  371. if (bit)
  372. {
  373. if (zeros > max_zeros)
  374. max_zeros = zeros;
  375. zeros = 0;
  376. }
  377. else
  378. {
  379. zeros++;
  380. }
  381. bert_put_bit(rx_bert, bit);
  382. test[tx_bert->tx.reg]++;
  383. }
  384. failed = false;
  385. if (test[0] != 0)
  386. {
  387. printf("XXX %d %d\n", 0, test[0]);
  388. failed = true;
  389. }
  390. for (i = 1; i < 0x800000; i++)
  391. {
  392. if (test[i] != 2)
  393. printf("XXX %d %d\n", i, test[i]);
  394. }
  395. bert_result(rx_bert, &bert_results);
  396. printf("O.151(23): Bad bits %d/%d, max zeros %d\n", bert_results.bad_bits, bert_results.total_bits, max_zeros);
  397. if (bert_results.bad_bits || bert_results.total_bits != 16777136 || failed)
  398. {
  399. printf("Test failed.\n");
  400. exit(2);
  401. }
  402. bert_free(tx_bert);
  403. bert_free(rx_bert);
  404. tx_bert = bert_init(NULL, 0, BERT_PATTERN_QBF, 300, 20);
  405. rx_bert = bert_init(NULL, 0, BERT_PATTERN_QBF, 300, 20);
  406. for (i = 0; i < 100000; i++)
  407. {
  408. bit = bert_get_bit(tx_bert);
  409. bert_put_bit(rx_bert, bit);
  410. }
  411. bert_result(rx_bert, &bert_results);
  412. printf("QBF: Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
  413. if (bert_results.bad_bits || bert_results.total_bits != 100000)
  414. {
  415. printf("Test failed.\n");
  416. exit(2);
  417. }
  418. bert_free(tx_bert);
  419. bert_free(rx_bert);
  420. /* Test the mechanism for categorising the error rate into <10^x bands */
  421. /* TODO: The result of this test is not checked automatically */
  422. bert = bert_init(NULL, 15000000, BERT_PATTERN_ITU_O152_11, 300, 20);
  423. bert_set_report(bert, 100000, reporter, (intptr_t) 0);
  424. for (;;)
  425. {
  426. if ((bit = bert_get_bit(bert)) == SIG_STATUS_END_OF_DATA)
  427. {
  428. bert_result(bert, &bert_results);
  429. printf("Rate test: %d bits, %d bad bits, %d resyncs\n", bert_results.total_bits, bert_results.bad_bits, bert_results.resyncs);
  430. if (bert_results.total_bits != 15000000 - 42
  431. ||
  432. bert_results.bad_bits != 58
  433. ||
  434. bert_results.resyncs != 0)
  435. {
  436. printf("Tests failed\n");
  437. exit(2);
  438. }
  439. break;
  440. //bert = bert_init(NULL, 15000000, BERT_PATTERN_ITU_O152_11, 300, 20);
  441. //bert_set_report(bert, 100000, reporter, (intptr_t) 0);
  442. //continue;
  443. }
  444. if ((my_rand() & 0x3FFFF) == 0)
  445. bit ^= 1;
  446. //if ((my_rand() & 0xFFF) == 0)
  447. // bert_put_bit(bert, bit);
  448. bert_put_bit(bert, bit);
  449. }
  450. bert_free(bert);
  451. printf("Tests passed.\n");
  452. return 0;
  453. }
  454. /*- End of function --------------------------------------------------------*/
  455. /*- End of file ------------------------------------------------------------*/