1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
16 #include "sjme/bitStream.h"
18 /** Test bit sequence. */
19 static const sjme_jubyte testData
[132] =
21 29, 21, 227U, 128U, 144U, 64, 129U,
22 5, 48, 64, 3, 112, 0, 30, 0,
23 16, 0, 17, 0, 36, 0, 152U,
24 0, 0, 5, 0, 84, 0, 0, 11,
25 0, 224U, 2, 0, 128U, 1, 0,
26 144U, 1, 0, 64, 3, 0, 128U,
27 13, 0, 0, 112, 0, 0, 64,
28 7, 0, 0, 240U, 0, 0, 0, 62,
30 179U, 80, 12, 14, 1, 18, 40,
31 64, 3, 12, 128U, 5, 224U,
32 0, 240U, 0, 8, 0, 16, 1,
33 64, 2, 0, 50, 0, 160U, 0,
34 0, 84, 0, 0, 13, 0, 128U,
38 0, 192U, 5, 0, 0, 240U,
44 * Tests writing to a bit stream.
48 SJME_TEST_DECLARE(testBitStreamWrite
)
50 sjme_stream_output baos
;
51 sjme_bitStream_output output
;
52 sjme_bitStream_order order
;
53 sjme_stream_resultByteArray result
;
56 /* Output target byte array. */
58 memset(&result
, 0, sizeof(result
));
59 if (sjme_error_is(test
->error
= sjme_stream_outputOpenByteArrayTo(
61 512, &result
)) || baos
== NULL
)
62 return sjme_unit_fail(test
, "Could not open output byte array.");
64 /* Open the bit stream. */
66 if (sjme_error_is(test
->error
= sjme_bitStream_outputOpenStream(
68 baos
, SJME_JNI_TRUE
)) ||
70 return sjme_unit_fail(test
, "Could not open bit stream?");
73 for (i
= 1; i
<= 64; i
++)
75 /* Zero is really 32. */
76 actual
= ((i
% 32) == 0 ? 32 : i
% 32);
78 /* Write in both orders. */
79 order
= (i
<= 32 ? SJME_BITSTREAM_LSB
: SJME_BITSTREAM_MSB
);
82 if (sjme_error_is(test
->error
= sjme_bitStream_outputWrite(
85 return sjme_unit_fail(test
, "Could not write %d bits?", actual
);
88 /* Close the bit stream. */
89 if (sjme_error_is(test
->error
= sjme_closeable_close(
90 SJME_AS_CLOSEABLE(output
))))
91 return sjme_unit_fail(test
, "Could not close the bit stream?");
93 /* There should be data. */
94 sjme_unit_notEqualP(test
, NULL
, result
.array
,
95 "There was no output array?");
97 /* Should result in a match. */
98 sjme_message_hexDump(result
.array
, result
.length
);
99 sjme_unit_equalI(test
, sizeof(testData
), result
.length
,
100 "Length did not match?");
101 sjme_unit_equalI(test
,
102 0, memcmp(testData
, result
.array
, result
.length
),
103 "Written contents did not match?");
105 /* Free the array. */
106 if (sjme_error_is(test
->error
= sjme_alloc_free(result
.array
)))
107 return sjme_unit_fail(test
, "Could not free the array?");
110 return SJME_TEST_RESULT_PASS
;