bitbuffer.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:24k
源码类别:

Windows CE

开发平台:

C/C++

  1. /* test_libFLAC - Unit tester for libFLAC
  2.  * Copyright (C) 2000,2001,2002,2003,2004,2005  Josh Coalson
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  */
  18. #include "FLAC/assert.h"
  19. #include "private/bitbuffer.h" /* from the libFLAC private include area */
  20. #include <stdio.h>
  21. #include <string.h> /* for memcmp() */
  22. /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
  23. #ifdef _MSC_VER
  24. #define FLAC__U64L(x) x
  25. #else
  26. #define FLAC__U64L(x) x##LLU
  27. #endif
  28. /*
  29.  * WATCHOUT!  Since FLAC__BitBuffer is a private structure, we use a copy of
  30.  * the definition here to get at the internals.  Make sure this is kept up
  31.  * to date with what is in ../libFLAC/bitbuffer.c
  32.  */
  33. struct FLAC__BitBuffer {
  34. FLAC__blurb *buffer;
  35. unsigned capacity; /* in blurbs */
  36. unsigned blurbs, bits;
  37. unsigned total_bits; /* must always == FLAC__BITS_PER_BLURB*blurbs+bits */
  38. unsigned consumed_blurbs, consumed_bits;
  39. unsigned total_consumed_bits; /* must always == FLAC__BITS_PER_BLURB*consumed_blurbs+consumed_bits */
  40. FLAC__uint16 read_crc16;
  41. #if FLAC__BITS_PER_BLURB == 32
  42. unsigned crc16_align;
  43. #endif
  44. FLAC__blurb save_head, save_tail;
  45. };
  46. static FLAC__bool dummy_read_callback(FLAC__byte buffer[], unsigned *bytes, void *client_data)
  47. {
  48. (void)buffer, (void)bytes, (void)client_data;
  49. return true;
  50. }
  51. FLAC__bool test_bitbuffer()
  52. {
  53. FLAC__BitBuffer *bb, *bb_zero, *bb_one, *bbcopy;
  54. FLAC__bool ok;
  55. unsigned i, j;
  56. static FLAC__byte test_pattern1[19] = { 0xaa, 0xf0, 0xaa, 0xbe, 0xaa, 0xaa, 0xaa, 0xa8, 0x30, 0x0a, 0xaa, 0xaa, 0xaa, 0xad, 0xea, 0xdb, 0xee, 0xfa, 0xce };
  57. FLAC__ASSERT(FLAC__BITS_PER_BLURB == 8);
  58. printf("n+++ libFLAC unit test: bitbuffernn");
  59. /*
  60.  * test new -> delete
  61.  */
  62. printf("testing new... ");
  63. bb = FLAC__bitbuffer_new();
  64. if(0 == bb) {
  65. printf("FAILED, returned NULLn");
  66. return false;
  67. }
  68. printf("OKn");
  69. printf("testing delete... ");
  70. FLAC__bitbuffer_delete(bb);
  71. printf("OKn");
  72. /*
  73.  * test new -> init -> delete
  74.  */
  75. printf("testing new... ");
  76. bb = FLAC__bitbuffer_new();
  77. if(0 == bb) {
  78. printf("FAILED, returned NULLn");
  79. return false;
  80. }
  81. printf("OKn");
  82. printf("testing init... ");
  83. FLAC__bitbuffer_init(bb);
  84. if(0 == bb) {
  85. printf("FAILED, returned NULLn");
  86. return false;
  87. }
  88. printf("OKn");
  89. printf("testing delete... ");
  90. FLAC__bitbuffer_delete(bb);
  91. printf("OKn");
  92. /*
  93.  * test new -> init -> clear -> delete
  94.  */
  95. printf("testing new... ");
  96. bb = FLAC__bitbuffer_new();
  97. if(0 == bb) {
  98. printf("FAILED, returned NULLn");
  99. return false;
  100. }
  101. printf("OKn");
  102. printf("testing init... ");
  103. FLAC__bitbuffer_init(bb);
  104. if(0 == bb) {
  105. printf("FAILED, returned NULLn");
  106. return false;
  107. }
  108. printf("OKn");
  109. printf("testing clear... ");
  110. FLAC__bitbuffer_clear(bb);
  111. if(0 == bb) {
  112. printf("FAILED, returned NULLn");
  113. return false;
  114. }
  115. printf("OKn");
  116. printf("testing delete... ");
  117. FLAC__bitbuffer_delete(bb);
  118. printf("OKn");
  119. /*
  120.  * test normal usage
  121.  */
  122. printf("testing new... ");
  123. bb = FLAC__bitbuffer_new();
  124. bb_zero = FLAC__bitbuffer_new();
  125. bb_one = FLAC__bitbuffer_new();
  126. bbcopy = FLAC__bitbuffer_new();
  127. if(0 == bb || 0 == bb_zero || 0 == bb_one || 0 == bbcopy) {
  128. printf("FAILED, returned NULLn");
  129. return false;
  130. }
  131. printf("OKn");
  132. printf("testing init... ");
  133. ok = FLAC__bitbuffer_init(bb) && FLAC__bitbuffer_init(bb_zero) && FLAC__bitbuffer_init(bb_one) && FLAC__bitbuffer_init(bbcopy);
  134. printf("%sn", ok?"OK":"FAILED");
  135. if(!ok)
  136. return false;
  137. printf("testing clear... ");
  138. ok = FLAC__bitbuffer_clear(bb) && FLAC__bitbuffer_clear(bb_zero) && FLAC__bitbuffer_clear(bb_one) && FLAC__bitbuffer_clear(bbcopy);
  139. printf("%sn", ok?"OK":"FAILED");
  140. if(!ok)
  141. return false;
  142. printf("setting up bb_one... ");
  143. ok = FLAC__bitbuffer_write_raw_uint32(bb_one, 1, 7) && FLAC__bitbuffer_read_raw_uint32(bb_one, &i, 6, dummy_read_callback, 0);
  144. printf("%sn", ok?"OK":"FAILED");
  145. if(!ok)
  146. return false;
  147. FLAC__bitbuffer_dump(bb_one, stdout);
  148. printf("capacity = %un", bb->capacity);
  149. printf("testing zeroes, raw_uint32*... ");
  150. ok =
  151. FLAC__bitbuffer_write_raw_uint32(bb, 0x1, 1) &&
  152. FLAC__bitbuffer_write_raw_uint32(bb, 0x1, 2) &&
  153. FLAC__bitbuffer_write_raw_uint32(bb, 0xa, 5) &&
  154. FLAC__bitbuffer_write_raw_uint32(bb, 0xf0, 8) &&
  155. FLAC__bitbuffer_write_raw_uint32(bb, 0x2aa, 10) &&
  156. FLAC__bitbuffer_write_raw_uint32(bb, 0xf, 4) &&
  157. FLAC__bitbuffer_write_raw_uint32(bb, 0xaaaaaaaa, 32) &&
  158. FLAC__bitbuffer_write_zeroes(bb, 4) &&
  159. FLAC__bitbuffer_write_raw_uint32(bb, 0x3, 2) &&
  160. FLAC__bitbuffer_write_zeroes(bb, 8) &&
  161. FLAC__bitbuffer_write_raw_uint64(bb, FLAC__U64L(0xaaaaaaaadeadbeef), 64) &&
  162. FLAC__bitbuffer_write_raw_uint32(bb, 0xace, 12)
  163. ;
  164. if(!ok) {
  165. printf("FAILEDn");
  166. FLAC__bitbuffer_dump(bb, stdout);
  167. return false;
  168. }
  169. if(bb->blurbs != sizeof(test_pattern1)) {
  170. printf("FAILED byte count %u != %un", bb->blurbs, (unsigned)sizeof(test_pattern1));
  171. FLAC__bitbuffer_dump(bb, stdout);
  172. return false;
  173. }
  174. if(bb->bits != 0) {
  175. printf("FAILED bit count %u != 0n", bb->bits);
  176. FLAC__bitbuffer_dump(bb, stdout);
  177. return false;
  178. }
  179. if(bb->total_bits != 8*bb->blurbs+bb->bits) {
  180. printf("FAILED total_bits count %u != %u (%u:%u)n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
  181. FLAC__bitbuffer_dump(bb, stdout);
  182. return false;
  183. }
  184. if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0) {
  185. printf("FAILED pattern matchn");
  186. FLAC__bitbuffer_dump(bb, stdout);
  187. return false;
  188. }
  189. printf("OKn");
  190. FLAC__bitbuffer_dump(bb, stdout);
  191. printf("testing raw_uint32 some more... ");
  192. ok = FLAC__bitbuffer_write_raw_uint32(bb, 0x3d, 6);
  193. if(!ok) {
  194. printf("FAILEDn");
  195. FLAC__bitbuffer_dump(bb, stdout);
  196. return false;
  197. }
  198. if(bb->blurbs != sizeof(test_pattern1)) {
  199. printf("FAILED byte count %u != %un", bb->blurbs, (unsigned)sizeof(test_pattern1));
  200. FLAC__bitbuffer_dump(bb, stdout);
  201. return false;
  202. }
  203. if(bb->bits != 6) {
  204. printf("FAILED bit count %u != 6n", bb->bits);
  205. FLAC__bitbuffer_dump(bb, stdout);
  206. return false;
  207. }
  208. if(bb->total_bits != 8*bb->blurbs+bb->bits) {
  209. printf("FAILED total_bits count %u != %u (%u:%u)n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
  210. FLAC__bitbuffer_dump(bb, stdout);
  211. return false;
  212. }
  213. if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb->buffer[bb->blurbs] != 0x3d) {
  214. printf("FAILED pattern matchn");
  215. FLAC__bitbuffer_dump(bb, stdout);
  216. return false;
  217. }
  218. printf("OKn");
  219. FLAC__bitbuffer_dump(bb, stdout);
  220. printf("testing concatenate_aligned (bb_zero)... ");
  221. ok = FLAC__bitbuffer_concatenate_aligned(bb, bb_zero);
  222. if(!ok) {
  223. printf("FAILEDn");
  224. FLAC__bitbuffer_dump(bb, stdout);
  225. return false;
  226. }
  227. if(bb->blurbs != sizeof(test_pattern1)) {
  228. printf("FAILED byte count %u != %un", bb->blurbs, (unsigned)sizeof(test_pattern1));
  229. FLAC__bitbuffer_dump(bb, stdout);
  230. return false;
  231. }
  232. if(bb->bits != 6) {
  233. printf("FAILED bit count %u != 6n", bb->bits);
  234. FLAC__bitbuffer_dump(bb, stdout);
  235. return false;
  236. }
  237. if(bb->total_bits != 8*bb->blurbs+bb->bits) {
  238. printf("FAILED total_bits count %u != %u (%u:%u)n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
  239. FLAC__bitbuffer_dump(bb, stdout);
  240. return false;
  241. }
  242. if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb->buffer[bb->blurbs] != 0x3d) {
  243. printf("FAILED pattern matchn");
  244. FLAC__bitbuffer_dump(bb, stdout);
  245. return false;
  246. }
  247. printf("OKn");
  248. FLAC__bitbuffer_dump(bb, stdout);
  249. printf("testing concatenate_aligned (bb_one)... ");
  250. ok = FLAC__bitbuffer_concatenate_aligned(bb, bb_one);
  251. if(!ok) {
  252. printf("FAILEDn");
  253. FLAC__bitbuffer_dump(bb, stdout);
  254. return false;
  255. }
  256. if(bb->blurbs != sizeof(test_pattern1)) {
  257. printf("FAILED byte count %u != %un", bb->blurbs, (unsigned)sizeof(test_pattern1));
  258. FLAC__bitbuffer_dump(bb, stdout);
  259. return false;
  260. }
  261. if(bb->bits != 7) {
  262. printf("FAILED bit count %u != 7n", bb->bits);
  263. FLAC__bitbuffer_dump(bb, stdout);
  264. return false;
  265. }
  266. if(bb->total_bits != 8*bb->blurbs+bb->bits) {
  267. printf("FAILED total_bits count %u != %u (%u:%u)n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
  268. FLAC__bitbuffer_dump(bb, stdout);
  269. return false;
  270. }
  271. if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb->buffer[bb->blurbs] != 0x7b) {
  272. printf("FAILED pattern matchn");
  273. FLAC__bitbuffer_dump(bb, stdout);
  274. return false;
  275. }
  276. printf("OKn");
  277. FLAC__bitbuffer_dump(bb, stdout);
  278. printf("testing concatenate_aligned (bb_one again)... ");
  279. (void)FLAC__bitbuffer_write_raw_uint32(bb_one, 1, 1);
  280. (void)FLAC__bitbuffer_read_raw_uint32(bb_one, &i, 1, dummy_read_callback, 0);
  281. ok = FLAC__bitbuffer_concatenate_aligned(bb, bb_one);
  282. if(!ok) {
  283. printf("FAILEDn");
  284. FLAC__bitbuffer_dump(bb, stdout);
  285. return false;
  286. }
  287. if(bb->blurbs != sizeof(test_pattern1)+1) {
  288. printf("FAILED byte count %u != %un", bb->blurbs, (unsigned)sizeof(test_pattern1)+1);
  289. FLAC__bitbuffer_dump(bb, stdout);
  290. return false;
  291. }
  292. if(bb->bits != 0) {
  293. printf("FAILED bit count %u != 0n", bb->bits);
  294. FLAC__bitbuffer_dump(bb, stdout);
  295. return false;
  296. }
  297. if(bb->total_bits != 8*bb->blurbs+bb->bits) {
  298. printf("FAILED total_bits count %u != %u (%u:%u)n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
  299. FLAC__bitbuffer_dump(bb, stdout);
  300. return false;
  301. }
  302. if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb->buffer[bb->blurbs-1] != 0xf7) {
  303. printf("FAILED pattern matchn");
  304. FLAC__bitbuffer_dump(bb, stdout);
  305. return false;
  306. }
  307. printf("OKn");
  308. FLAC__bitbuffer_dump(bb, stdout);
  309. printf("testing concatenate_aligned (bb_four)... ");
  310. (void)FLAC__bitbuffer_clear(bb_one);
  311. (void)FLAC__bitbuffer_write_raw_uint32(bb_one, 8, 4);
  312. ok = FLAC__bitbuffer_concatenate_aligned(bb, bb_one);
  313. if(!ok) {
  314. printf("FAILEDn");
  315. FLAC__bitbuffer_dump(bb, stdout);
  316. return false;
  317. }
  318. if(bb->blurbs != sizeof(test_pattern1)+1) {
  319. printf("FAILED byte count %u != %un", bb->blurbs, (unsigned)sizeof(test_pattern1)+1);
  320. FLAC__bitbuffer_dump(bb, stdout);
  321. return false;
  322. }
  323. if(bb->bits != 4) {
  324. printf("FAILED bit count %u != 4n", bb->bits);
  325. FLAC__bitbuffer_dump(bb, stdout);
  326. return false;
  327. }
  328. if(bb->total_bits != 8*bb->blurbs+bb->bits) {
  329. printf("FAILED total_bits count %u != %u (%u:%u)n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
  330. FLAC__bitbuffer_dump(bb, stdout);
  331. return false;
  332. }
  333. if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb->buffer[bb->blurbs] != 0x08) {
  334. printf("FAILED pattern matchn");
  335. FLAC__bitbuffer_dump(bb, stdout);
  336. return false;
  337. }
  338. printf("OKn");
  339. FLAC__bitbuffer_dump(bb, stdout);
  340. printf("testing concatenate_aligned (bb_eight)... ");
  341. (void)FLAC__bitbuffer_read_raw_uint32(bb_one, &i, 4, dummy_read_callback, 0);
  342. (void)FLAC__bitbuffer_write_raw_uint32(bb_one, 0xaa, 8);
  343. ok = FLAC__bitbuffer_concatenate_aligned(bb, bb_one);
  344. if(!ok) {
  345. printf("FAILEDn");
  346. FLAC__bitbuffer_dump(bb, stdout);
  347. return false;
  348. }
  349. if(bb->blurbs != sizeof(test_pattern1)+2) {
  350. printf("FAILED byte count %u != %un", bb->blurbs, (unsigned)sizeof(test_pattern1)+2);
  351. FLAC__bitbuffer_dump(bb, stdout);
  352. return false;
  353. }
  354. if(bb->bits != 4) {
  355. printf("FAILED bit count %u != 4n", bb->bits);
  356. FLAC__bitbuffer_dump(bb, stdout);
  357. return false;
  358. }
  359. if(bb->total_bits != 8*bb->blurbs+bb->bits) {
  360. printf("FAILED total_bits count %u != %u (%u:%u)n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
  361. FLAC__bitbuffer_dump(bb, stdout);
  362. return false;
  363. }
  364. if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb->buffer[bb->blurbs-1] != 0x8a || bb->buffer[bb->blurbs] != 0x0a) {
  365. printf("FAILED pattern matchn");
  366. FLAC__bitbuffer_dump(bb, stdout);
  367. return false;
  368. }
  369. printf("OKn");
  370. FLAC__bitbuffer_dump(bb, stdout);
  371. printf("testing concatenate_aligned (bb_seventeen)... ");
  372. (void)FLAC__bitbuffer_write_raw_uint32(bb_one, 0x155, 9);
  373. ok = FLAC__bitbuffer_concatenate_aligned(bb, bb_one);
  374. if(!ok) {
  375. printf("FAILEDn");
  376. FLAC__bitbuffer_dump(bb, stdout);
  377. return false;
  378. }
  379. if(bb->blurbs != sizeof(test_pattern1)+4) {
  380. printf("FAILED byte count %u != %un", bb->blurbs, (unsigned)sizeof(test_pattern1)+4);
  381. FLAC__bitbuffer_dump(bb, stdout);
  382. return false;
  383. }
  384. if(bb->bits != 5) {
  385. printf("FAILED bit count %u != 5n", bb->bits);
  386. FLAC__bitbuffer_dump(bb, stdout);
  387. return false;
  388. }
  389. if(bb->total_bits != 8*bb->blurbs+bb->bits) {
  390. printf("FAILED total_bits count %u != %u (%u:%u)n", bb->total_bits, 8*bb->blurbs+bb->bits, bb->blurbs, bb->bits);
  391. FLAC__bitbuffer_dump(bb, stdout);
  392. return false;
  393. }
  394. if(memcmp(bb->buffer, test_pattern1, sizeof(FLAC__byte)*sizeof(test_pattern1)) != 0 || bb->buffer[bb->blurbs-3] != 0x8a || bb->buffer[bb->blurbs-2] != 0xaa || bb->buffer[bb->blurbs-1] != 0xaa || bb->buffer[bb->blurbs] != 0x15) {
  395. printf("FAILED pattern matchn");
  396. FLAC__bitbuffer_dump(bb, stdout);
  397. return false;
  398. }
  399. printf("OKn");
  400. FLAC__bitbuffer_dump(bb, stdout);
  401. printf("testing utf8_uint32(0x00000000)... ");
  402. FLAC__bitbuffer_clear(bb);
  403. FLAC__bitbuffer_write_utf8_uint32(bb, 0x00000000);
  404. ok = bb->total_bits == 8 && bb->buffer[0] == 0;
  405. printf("%sn", ok?"OK":"FAILED");
  406. if(!ok) {
  407. FLAC__bitbuffer_dump(bb, stdout);
  408. return false;
  409. }
  410. printf("testing utf8_uint32(0x0000007F)... ");
  411. FLAC__bitbuffer_clear(bb);
  412. FLAC__bitbuffer_write_utf8_uint32(bb, 0x0000007F);
  413. ok = bb->total_bits == 8 && bb->buffer[0] == 0x7F;
  414. printf("%sn", ok?"OK":"FAILED");
  415. if(!ok) {
  416. FLAC__bitbuffer_dump(bb, stdout);
  417. return false;
  418. }
  419. printf("testing utf8_uint32(0x00000080)... ");
  420. FLAC__bitbuffer_clear(bb);
  421. FLAC__bitbuffer_write_utf8_uint32(bb, 0x00000080);
  422. ok = bb->total_bits == 16 && bb->buffer[0] == 0xC2 && bb->buffer[1] == 0x80;
  423. printf("%sn", ok?"OK":"FAILED");
  424. if(!ok) {
  425. FLAC__bitbuffer_dump(bb, stdout);
  426. return false;
  427. }
  428. printf("testing utf8_uint32(0x000007FF)... ");
  429. FLAC__bitbuffer_clear(bb);
  430. FLAC__bitbuffer_write_utf8_uint32(bb, 0x000007FF);
  431. ok = bb->total_bits == 16 && bb->buffer[0] == 0xDF && bb->buffer[1] == 0xBF;
  432. printf("%sn", ok?"OK":"FAILED");
  433. if(!ok) {
  434. FLAC__bitbuffer_dump(bb, stdout);
  435. return false;
  436. }
  437. printf("testing utf8_uint32(0x00000800)... ");
  438. FLAC__bitbuffer_clear(bb);
  439. FLAC__bitbuffer_write_utf8_uint32(bb, 0x00000800);
  440. ok = bb->total_bits == 24 && bb->buffer[0] == 0xE0 && bb->buffer[1] == 0xA0 && bb->buffer[2] == 0x80;
  441. printf("%sn", ok?"OK":"FAILED");
  442. if(!ok) {
  443. FLAC__bitbuffer_dump(bb, stdout);
  444. return false;
  445. }
  446. printf("testing utf8_uint32(0x0000FFFF)... ");
  447. FLAC__bitbuffer_clear(bb);
  448. FLAC__bitbuffer_write_utf8_uint32(bb, 0x0000FFFF);
  449. ok = bb->total_bits == 24 && bb->buffer[0] == 0xEF && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF;
  450. printf("%sn", ok?"OK":"FAILED");
  451. if(!ok) {
  452. FLAC__bitbuffer_dump(bb, stdout);
  453. return false;
  454. }
  455. printf("testing utf8_uint32(0x00010000)... ");
  456. FLAC__bitbuffer_clear(bb);
  457. FLAC__bitbuffer_write_utf8_uint32(bb, 0x00010000);
  458. ok = bb->total_bits == 32 && bb->buffer[0] == 0xF0 && bb->buffer[1] == 0x90 && bb->buffer[2] == 0x80 && bb->buffer[3] == 0x80;
  459. printf("%sn", ok?"OK":"FAILED");
  460. if(!ok) {
  461. FLAC__bitbuffer_dump(bb, stdout);
  462. return false;
  463. }
  464. printf("testing utf8_uint32(0x001FFFFF)... ");
  465. FLAC__bitbuffer_clear(bb);
  466. FLAC__bitbuffer_write_utf8_uint32(bb, 0x001FFFFF);
  467. ok = bb->total_bits == 32 && bb->buffer[0] == 0xF7 && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF && bb->buffer[3] == 0xBF;
  468. printf("%sn", ok?"OK":"FAILED");
  469. if(!ok) {
  470. FLAC__bitbuffer_dump(bb, stdout);
  471. return false;
  472. }
  473. printf("testing utf8_uint32(0x00200000)... ");
  474. FLAC__bitbuffer_clear(bb);
  475. FLAC__bitbuffer_write_utf8_uint32(bb, 0x00200000);
  476. ok = bb->total_bits == 40 && bb->buffer[0] == 0xF8 && bb->buffer[1] == 0x88 && bb->buffer[2] == 0x80 && bb->buffer[3] == 0x80 && bb->buffer[4] == 0x80;
  477. printf("%sn", ok?"OK":"FAILED");
  478. if(!ok) {
  479. FLAC__bitbuffer_dump(bb, stdout);
  480. return false;
  481. }
  482. printf("testing utf8_uint32(0x03FFFFFF)... ");
  483. FLAC__bitbuffer_clear(bb);
  484. FLAC__bitbuffer_write_utf8_uint32(bb, 0x03FFFFFF);
  485. ok = bb->total_bits == 40 && bb->buffer[0] == 0xFB && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF && bb->buffer[3] == 0xBF && bb->buffer[4] == 0xBF;
  486. printf("%sn", ok?"OK":"FAILED");
  487. if(!ok) {
  488. FLAC__bitbuffer_dump(bb, stdout);
  489. return false;
  490. }
  491. printf("testing utf8_uint32(0x04000000)... ");
  492. FLAC__bitbuffer_clear(bb);
  493. FLAC__bitbuffer_write_utf8_uint32(bb, 0x04000000);
  494. ok = bb->total_bits == 48 && bb->buffer[0] == 0xFC && bb->buffer[1] == 0x84 && bb->buffer[2] == 0x80 && bb->buffer[3] == 0x80 && bb->buffer[4] == 0x80 && bb->buffer[5] == 0x80;
  495. printf("%sn", ok?"OK":"FAILED");
  496. if(!ok) {
  497. FLAC__bitbuffer_dump(bb, stdout);
  498. return false;
  499. }
  500. printf("testing utf8_uint32(0x7FFFFFFF)... ");
  501. FLAC__bitbuffer_clear(bb);
  502. FLAC__bitbuffer_write_utf8_uint32(bb, 0x7FFFFFFF);
  503. ok = bb->total_bits == 48 && bb->buffer[0] == 0xFD && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF && bb->buffer[3] == 0xBF && bb->buffer[4] == 0xBF && bb->buffer[5] == 0xBF;
  504. printf("%sn", ok?"OK":"FAILED");
  505. if(!ok) {
  506. FLAC__bitbuffer_dump(bb, stdout);
  507. return false;
  508. }
  509. printf("testing utf8_uint64(0x0000000000000000)... ");
  510. FLAC__bitbuffer_clear(bb);
  511. FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000000000000);
  512. ok = bb->total_bits == 8 && bb->buffer[0] == 0;
  513. printf("%sn", ok?"OK":"FAILED");
  514. if(!ok) {
  515. FLAC__bitbuffer_dump(bb, stdout);
  516. return false;
  517. }
  518. printf("testing utf8_uint64(0x000000000000007F)... ");
  519. FLAC__bitbuffer_clear(bb);
  520. FLAC__bitbuffer_write_utf8_uint64(bb, 0x000000000000007F);
  521. ok = bb->total_bits == 8 && bb->buffer[0] == 0x7F;
  522. printf("%sn", ok?"OK":"FAILED");
  523. if(!ok) {
  524. FLAC__bitbuffer_dump(bb, stdout);
  525. return false;
  526. }
  527. printf("testing utf8_uint64(0x0000000000000080)... ");
  528. FLAC__bitbuffer_clear(bb);
  529. FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000000000080);
  530. ok = bb->total_bits == 16 && bb->buffer[0] == 0xC2 && bb->buffer[1] == 0x80;
  531. printf("%sn", ok?"OK":"FAILED");
  532. if(!ok) {
  533. FLAC__bitbuffer_dump(bb, stdout);
  534. return false;
  535. }
  536. printf("testing utf8_uint64(0x00000000000007FF)... ");
  537. FLAC__bitbuffer_clear(bb);
  538. FLAC__bitbuffer_write_utf8_uint64(bb, 0x00000000000007FF);
  539. ok = bb->total_bits == 16 && bb->buffer[0] == 0xDF && bb->buffer[1] == 0xBF;
  540. printf("%sn", ok?"OK":"FAILED");
  541. if(!ok) {
  542. FLAC__bitbuffer_dump(bb, stdout);
  543. return false;
  544. }
  545. printf("testing utf8_uint64(0x0000000000000800)... ");
  546. FLAC__bitbuffer_clear(bb);
  547. FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000000000800);
  548. ok = bb->total_bits == 24 && bb->buffer[0] == 0xE0 && bb->buffer[1] == 0xA0 && bb->buffer[2] == 0x80;
  549. printf("%sn", ok?"OK":"FAILED");
  550. if(!ok) {
  551. FLAC__bitbuffer_dump(bb, stdout);
  552. return false;
  553. }
  554. printf("testing utf8_uint64(0x000000000000FFFF)... ");
  555. FLAC__bitbuffer_clear(bb);
  556. FLAC__bitbuffer_write_utf8_uint64(bb, 0x000000000000FFFF);
  557. ok = bb->total_bits == 24 && bb->buffer[0] == 0xEF && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF;
  558. printf("%sn", ok?"OK":"FAILED");
  559. if(!ok) {
  560. FLAC__bitbuffer_dump(bb, stdout);
  561. return false;
  562. }
  563. printf("testing utf8_uint64(0x0000000000010000)... ");
  564. FLAC__bitbuffer_clear(bb);
  565. FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000000010000);
  566. ok = bb->total_bits == 32 && bb->buffer[0] == 0xF0 && bb->buffer[1] == 0x90 && bb->buffer[2] == 0x80 && bb->buffer[3] == 0x80;
  567. printf("%sn", ok?"OK":"FAILED");
  568. if(!ok) {
  569. FLAC__bitbuffer_dump(bb, stdout);
  570. return false;
  571. }
  572. printf("testing utf8_uint64(0x00000000001FFFFF)... ");
  573. FLAC__bitbuffer_clear(bb);
  574. FLAC__bitbuffer_write_utf8_uint64(bb, 0x00000000001FFFFF);
  575. ok = bb->total_bits == 32 && bb->buffer[0] == 0xF7 && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF && bb->buffer[3] == 0xBF;
  576. printf("%sn", ok?"OK":"FAILED");
  577. if(!ok) {
  578. FLAC__bitbuffer_dump(bb, stdout);
  579. return false;
  580. }
  581. printf("testing utf8_uint64(0x0000000000200000)... ");
  582. FLAC__bitbuffer_clear(bb);
  583. FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000000200000);
  584. ok = bb->total_bits == 40 && bb->buffer[0] == 0xF8 && bb->buffer[1] == 0x88 && bb->buffer[2] == 0x80 && bb->buffer[3] == 0x80 && bb->buffer[4] == 0x80;
  585. printf("%sn", ok?"OK":"FAILED");
  586. if(!ok) {
  587. FLAC__bitbuffer_dump(bb, stdout);
  588. return false;
  589. }
  590. printf("testing utf8_uint64(0x0000000003FFFFFF)... ");
  591. FLAC__bitbuffer_clear(bb);
  592. FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000003FFFFFF);
  593. ok = bb->total_bits == 40 && bb->buffer[0] == 0xFB && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF && bb->buffer[3] == 0xBF && bb->buffer[4] == 0xBF;
  594. printf("%sn", ok?"OK":"FAILED");
  595. if(!ok) {
  596. FLAC__bitbuffer_dump(bb, stdout);
  597. return false;
  598. }
  599. printf("testing utf8_uint64(0x0000000004000000)... ");
  600. FLAC__bitbuffer_clear(bb);
  601. FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000004000000);
  602. ok = bb->total_bits == 48 && bb->buffer[0] == 0xFC && bb->buffer[1] == 0x84 && bb->buffer[2] == 0x80 && bb->buffer[3] == 0x80 && bb->buffer[4] == 0x80 && bb->buffer[5] == 0x80;
  603. printf("%sn", ok?"OK":"FAILED");
  604. if(!ok) {
  605. FLAC__bitbuffer_dump(bb, stdout);
  606. return false;
  607. }
  608. printf("testing utf8_uint64(0x000000007FFFFFFF)... ");
  609. FLAC__bitbuffer_clear(bb);
  610. FLAC__bitbuffer_write_utf8_uint64(bb, 0x000000007FFFFFFF);
  611. ok = bb->total_bits == 48 && bb->buffer[0] == 0xFD && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF && bb->buffer[3] == 0xBF && bb->buffer[4] == 0xBF && bb->buffer[5] == 0xBF;
  612. printf("%sn", ok?"OK":"FAILED");
  613. if(!ok) {
  614. FLAC__bitbuffer_dump(bb, stdout);
  615. return false;
  616. }
  617. printf("testing utf8_uint64(0x0000000080000000)... ");
  618. FLAC__bitbuffer_clear(bb);
  619. FLAC__bitbuffer_write_utf8_uint64(bb, 0x0000000080000000);
  620. ok = bb->total_bits == 56 && bb->buffer[0] == 0xFE && bb->buffer[1] == 0x82 && bb->buffer[2] == 0x80 && bb->buffer[3] == 0x80 && bb->buffer[4] == 0x80 && bb->buffer[5] == 0x80 && bb->buffer[6] == 0x80;
  621. printf("%sn", ok?"OK":"FAILED");
  622. if(!ok) {
  623. FLAC__bitbuffer_dump(bb, stdout);
  624. return false;
  625. }
  626. printf("testing utf8_uint64(0x0000000FFFFFFFFF)... ");
  627. FLAC__bitbuffer_clear(bb);
  628. FLAC__bitbuffer_write_utf8_uint64(bb, FLAC__U64L(0x0000000FFFFFFFFF));
  629. ok = bb->total_bits == 56 && bb->buffer[0] == 0xFE && bb->buffer[1] == 0xBF && bb->buffer[2] == 0xBF && bb->buffer[3] == 0xBF && bb->buffer[4] == 0xBF && bb->buffer[5] == 0xBF && bb->buffer[6] == 0xBF;
  630. printf("%sn", ok?"OK":"FAILED");
  631. if(!ok) {
  632. FLAC__bitbuffer_dump(bb, stdout);
  633. return false;
  634. }
  635. printf("testing grow... ");
  636. FLAC__bitbuffer_clear(bb);
  637. FLAC__bitbuffer_write_raw_uint32(bb, 0xa, 4);
  638. j = bb->capacity;
  639. for(i = 0; i < j; i++)
  640. FLAC__bitbuffer_write_raw_uint32(bb, 0xaa, 8);
  641. ok = bb->total_bits = i*8+4 && bb->buffer[0] == 0xaa && bb->buffer[i] == 0xa;
  642. printf("%sn", ok?"OK":"FAILED");
  643. if(!ok) {
  644. FLAC__bitbuffer_dump(bb, stdout);
  645. return false;
  646. }
  647. printf("capacity = %un", bb->capacity);
  648. printf("testing clone... ");
  649. ok = FLAC__bitbuffer_clone(bbcopy, bb);
  650. if(!ok) {
  651. printf("FAILEDn");
  652. FLAC__bitbuffer_dump(bb, stdout);
  653. FLAC__bitbuffer_dump(bbcopy, stdout);
  654. return false;
  655. }
  656. if(bb->blurbs != bbcopy->blurbs) {
  657. printf("FAILED byte count %u != %un", bb->blurbs, bbcopy->blurbs);
  658. FLAC__bitbuffer_dump(bb, stdout);
  659. FLAC__bitbuffer_dump(bbcopy, stdout);
  660. return false;
  661. }
  662. if(bb->bits != bbcopy->bits) {
  663. printf("FAILED bit count %u != %un", bb->bits, bbcopy->bits);
  664. FLAC__bitbuffer_dump(bb, stdout);
  665. FLAC__bitbuffer_dump(bbcopy, stdout);
  666. return false;
  667. }
  668. if(bb->total_bits != bbcopy->total_bits) {
  669. printf("FAILED total_bits count %u != %un", bb->total_bits, bbcopy->total_bits);
  670. FLAC__bitbuffer_dump(bb, stdout);
  671. FLAC__bitbuffer_dump(bbcopy, stdout);
  672. return false;
  673. }
  674. if(memcmp(bb->buffer, bbcopy->buffer, sizeof(FLAC__byte)*bb->capacity) != 0) {
  675. printf("FAILED pattern matchn");
  676. FLAC__bitbuffer_dump(bb, stdout);
  677. FLAC__bitbuffer_dump(bbcopy, stdout);
  678. return false;
  679. }
  680. printf("OKn");
  681. printf("testing free... ");
  682. FLAC__bitbuffer_free(bb);
  683. FLAC__bitbuffer_free(bbcopy);
  684. printf("OKn");
  685. printf("testing delete... ");
  686. FLAC__bitbuffer_delete(bb);
  687. FLAC__bitbuffer_delete(bb_zero);
  688. FLAC__bitbuffer_delete(bb_one);
  689. FLAC__bitbuffer_delete(bbcopy);
  690. printf("OKn");
  691. printf("nPASSED!n");
  692. return true;
  693. }