Collect all of the various spread out 3rd party licenses to a single file.
[SquirrelJME.git] / nanocoat / tests / testStreamOverRead.c
blob63e20a56c902ae0683d06a72213ee8833159ea0e
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"
17 #define NUM_BYTES 16
19 #define READ_BYTES 50
21 static const sjme_jubyte testBytes[NUM_BYTES] =
23 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
24 12, 13, 14, 15, 16, 17,
27 /**
28 * Tests reading more bytes than what is available.
30 * @since 2023/12/31
32 SJME_TEST_DECLARE(testStreamOverRead)
34 sjme_stream_input inputStream;
35 sjme_jint readCount;
36 sjme_jint available;
37 sjme_jubyte buf[READ_BYTES];
39 /* Open stream. */
40 if (sjme_error_is(sjme_stream_inputOpenMemory(test->pool,
41 &inputStream, testBytes, NUM_BYTES)))
42 return sjme_unit_fail(test, "Could not open input stream.");
44 /* Get available bytes. */
45 available = -2;
46 if (sjme_error_is(sjme_stream_inputAvailable(inputStream,
47 &available)) || available < 0)
48 return sjme_unit_fail(test, "Could not get available bytes?");
50 /* The number of available bytes should be the full size. */
51 sjme_unit_equalI(test, available, NUM_BYTES,
52 "Available bytes incorrect?");
54 /* Read way too many bytes. */
55 memset(buf, 0, sizeof(buf));
56 readCount = -2;
57 if (sjme_error_is(sjme_stream_inputRead(inputStream,
58 &readCount, buf, READ_BYTES)))
59 return sjme_unit_fail(test, "Failed to read bytes?");
61 /* Should be the bytes in the buffer, not the read attempt. */
62 sjme_unit_equalI(test, readCount, NUM_BYTES,
63 "Read count incorrect?");
65 /* Test that the actual bytes are correct. */
66 sjme_unit_equalI(test, 0, memcmp(buf, testBytes, readCount),
67 "Read bytes are not correct?");
69 /* Close the stream. */
70 if (sjme_error_is(sjme_stream_inputClose(inputStream)))
71 return sjme_unit_fail(test, "Could not close stream?");
73 /* Success! */
74 return SJME_TEST_RESULT_PASS;