Make all NanoCoat types closeable; Make unrefing closeable the default.
[SquirrelJME.git] / nanocoat / tests / testStreamWriteBlock.c
blobd71aaca2b5add23a34a205514dcca70d4e47d008
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 /**
25 * Tests writing to a block of memory.
27 * @since 2024/01/09
29 SJME_TEST_DECLARE(testStreamWriteBlock)
31 sjme_stream_output stream;
32 sjme_jint i;
33 sjme_jubyte chunk[CHUNK_SIZE];
34 sjme_jubyte* buf;
36 /* Initialize input chunk data. */
37 for (i = 0; i < CHUNK_SIZE; i++)
38 chunk[i] = i + 1;
40 /* Allocate resultant buffer. */
41 buf = sjme_alloca(TOTAL_BYTES);
42 if (buf == NULL)
43 return sjme_unit_fail(test, "Could not allocate output buffer?");
45 /* Clear buffer. */
46 memset(buf, 0, TOTAL_BYTES);
48 /* Open stream onto the buffer. */
49 stream = NULL;
50 if (sjme_error_is(sjme_stream_outputOpenMemory(test->pool,
51 &stream, buf, TOTAL_BYTES)) || stream == NULL)
52 return sjme_unit_fail(test, "Could not open output stream.");
54 /* Write the buffer sequence for each chunk. */
55 for (i = 0; i < NUM_CHUNKS; i++)
56 if (sjme_error_is(sjme_stream_outputWrite(stream,
57 &chunk, sizeof(chunk))))
58 return sjme_unit_fail(test, "Could not write chunk %d?", i);
60 /* Close stream. */
61 if (sjme_error_is(sjme_closeable_close(
62 SJME_AS_CLOSEABLE(stream))))
63 return sjme_unit_fail(test, "Could not close output stream.");
65 /* Each chunk should match! */
66 for (i = 0; i < NUM_CHUNKS; i++)
67 sjme_unit_equalI(test,
68 0, memcmp(chunk, &buf[(CHUNK_SIZE * i)], CHUNK_SIZE),
69 "Chunk %d did not match?", i);
71 /* Success! */
72 return SJME_TEST_RESULT_PASS;