Collect all of the various spread out 3rd party licenses to a single file.
[SquirrelJME.git] / nanocoat / tests / testStream.c
blob46d7268155a451aadeac0ca1d8b1439b969051b7
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_BUF 4
21 static const sjme_jubyte testData[NUM_BYTES] =
23 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
24 13, 14, 15, 16, 17
27 /**
28 * Tests that streams work.
30 * @since 2023/12/30
32 SJME_TEST_DECLARE(testStream)
34 sjme_stream_input inputStream;
35 sjme_jint readCount, cycles;
36 sjme_jubyte buf[READ_BUF];
37 sjme_jubyte valAt, i;
39 /* Setup input stream. */
40 inputStream = NULL;
41 if (sjme_error_is(sjme_stream_inputOpenMemory(test->pool,
42 &inputStream, testData, NUM_BYTES)) ||
43 inputStream == NULL)
44 return sjme_unit_fail(test, "Could not open input stream.");
46 /* Read until EOF. */
47 valAt = 2;
48 for (cycles = 0;; cycles++)
50 /* Clear read buffer. */
51 memset(buf, 0, sizeof(buf));
53 /* Read in more data. */
54 readCount = -2;
55 if (sjme_error_is(sjme_stream_inputRead(inputStream,
56 &readCount, buf, READ_BUF)) || readCount < -1)
57 sjme_unit_fail(test, "Failed read?");
59 /* EOF? */
60 if (readCount == -1)
61 break;
63 /* Should have read said bytes. */
64 sjme_unit_equalI(test, READ_BUF, readCount,
65 "Did not read correct number of bytes?");
67 /* Values in buffer should match. */
68 for (i = 0; i < READ_BUF; i++)
69 sjme_unit_equalI(test, valAt++, buf[i],
70 "Incorrectly read value?");
73 /* There should have been this many cycles. */
74 sjme_unit_equalI(test, cycles, NUM_BYTES / READ_BUF,
75 "Incorrect number of read cycles?");
77 /* Close the stream. */
78 if (sjme_error_is(sjme_stream_inputClose(inputStream)))
79 return sjme_unit_fail(test, "Could not close stream?");
81 /* Success! */
82 return SJME_TEST_RESULT_PASS;