Make all NanoCoat types closeable; Make unrefing closeable the default.
[SquirrelJME.git] / nanocoat / tests / testStreamWriteBlockBA.c
blob44a262ddb649366fb05d7d874ad75cbee8da4481
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 "mock.h"
13 #include "proto.h"
14 #include "test.h"
15 #include "unit.h"
16 #include "sjme/stream.h"
18 #define CHUNK_SIZE 4
20 #define NUM_CHUNKS 8
22 #define TOTAL_BYTES (CHUNK_SIZE * NUM_CHUNKS)
24 static sjme_errorCode finishStreamWriteBlockBA(
25 sjme_attrInNotNull sjme_stream_output stream,
26 sjme_attrInNotNull sjme_stream_resultByteArray* result,
27 sjme_attrInNullable sjme_pointer data)
29 sjme_jint i;
30 sjme_test* test;
32 /* Recover test. */
33 test = (sjme_test*)result->whatever;
35 sjme_unit_equalP(test, data, test,
36 "Test data was not passed?");
38 /* Each chunk should match! */
39 for (i = 0; i < NUM_CHUNKS; i++)
40 sjme_unit_equalI(test,
41 0, memcmp(test->global, &result->array[(CHUNK_SIZE * i)],
42 CHUNK_SIZE),
43 "Chunk %d did not match?", i);
45 /* Success! */
46 return SJME_ERROR_NONE;
49 /**
50 * Tests writing to a block of memory.
52 * @since 2024/01/09
54 SJME_TEST_DECLARE(testStreamWriteBlockBA)
56 sjme_stream_output stream;
57 sjme_jint i;
58 sjme_jubyte chunk[CHUNK_SIZE];
60 /* Set global. */
61 test->global = chunk;
63 /* Initialize input chunk data. */
64 for (i = 0; i < CHUNK_SIZE; i++)
65 chunk[i] = i + 1;
67 /* Open stream. */
68 stream = NULL;
69 if (sjme_error_is(sjme_stream_outputOpenByteArray(test->pool,
70 &stream, 2, finishStreamWriteBlockBA,
71 test)) || stream == NULL)
72 return sjme_unit_fail(test, "Could not open output stream.");
74 /* Write the buffer sequence for each chunk. */
75 for (i = 0; i < NUM_CHUNKS; i++)
76 if (sjme_error_is(sjme_stream_outputWrite(stream,
77 &chunk, sizeof(chunk))))
78 return sjme_unit_fail(test, "Could not write chunk %d?", i);
80 /* Close stream. */
81 if (sjme_error_is(sjme_closeable_close(
82 SJME_AS_CLOSEABLE(stream))))
83 return sjme_unit_fail(test, "Could not close output stream.");
85 /* Success! */
86 return SJME_TEST_RESULT_PASS;