Adjust sourcize.c to use _snprintf if using MSVC.
[SquirrelJME.git] / nanocoat / tests / testBitStreamWrite.c
blob08e3a8228345c7662cfc4d1dde6cb9759a0a518a
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
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 // -------------------------------------------------------------------------*/
10 #include <string.h>
12 #include "test.h"
13 #include "proto.h"
14 #include "mock.h"
15 #include "unit.h"
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,
29 0, 0, 0, 32, 0, 0, 0,
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,
35 14, 0, 128U, 1, 0, 0,
36 19, 0, 0, 44, 0, 0,
37 96, 3, 0, 0, 14, 0,
38 0, 192U, 5, 0, 0, 240U,
39 0, 0, 0, 248U, 0, 0,
40 0, 4
43 /**
44 * Tests writing to a bit stream.
46 * @since 2024/08/27
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;
54 sjme_jint i, actual;
56 /* Output target byte array. */
57 baos = NULL;
58 memset(&result, 0, sizeof(result));
59 if (sjme_error_is(test->error = sjme_stream_outputOpenByteArrayTo(
60 test->pool, &baos,
61 512, &result)) || baos == NULL)
62 return sjme_unit_fail(test, "Could not open output byte array.");
64 /* Open the bit stream. */
65 output = NULL;
66 if (sjme_error_is(test->error = sjme_bitStream_outputOpenStream(
67 test->pool, &output,
68 baos, SJME_JNI_TRUE)) ||
69 output == NULL)
70 return sjme_unit_fail(test, "Could not open bit stream?");
72 /* Write all bits. */
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);
81 /* Write value. */
82 if (sjme_error_is(test->error = sjme_bitStream_outputWrite(
83 output, order,
84 actual, actual)))
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?");
109 /* Success! */
110 return SJME_TEST_RESULT_PASS;