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/stream.h"
22 #define TOTAL_BYTES (CHUNK_SIZE * NUM_CHUNKS)
25 * Tests writing to a block of memory.
29 SJME_TEST_DECLARE(testStreamWriteBlock
)
31 sjme_stream_output stream
;
33 sjme_jubyte chunk
[CHUNK_SIZE
];
36 /* Initialize input chunk data. */
37 for (i
= 0; i
< CHUNK_SIZE
; i
++)
40 /* Allocate resultant buffer. */
41 buf
= sjme_alloca(TOTAL_BYTES
);
43 return sjme_unit_fail(test
, "Could not allocate output buffer?");
46 memset(buf
, 0, TOTAL_BYTES
);
48 /* Open stream onto the buffer. */
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
);
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
);
72 return SJME_TEST_RESULT_PASS
;