Implement test for reading streams.
[SquirrelJME.git] / nanocoat / tests / testStream.c
blob393cf0152c9bf2c74932a2968723c50d0032af37
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_IS_ERROR(sjme_stream_inputOpenMemory(test->pool,
42 &inputStream, testData, NUM_BYTES)) ||
43 inputStream == NULL)
44 return sjme_unitFail(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 = 0;
55 if (SJME_IS_ERROR(sjme_stream_inputRead(inputStream,
56 &readCount, buf, READ_BUF)))
57 sjme_unitFail(test, "Failed read?");
59 /* EOF? */
60 if (readCount == -1)
61 break;
63 /* Should have read said bytes. */
64 sjme_unitEqualI(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_unitEqualI(test, valAt++, buf[i],
70 "Incorrectly read value?");
73 /* There should have been this many cycles. */
74 sjme_unitEqualI(test, cycles, NUM_BYTES / READ_BUF,
75 "Incorrect number of read cycles?");
77 /* Success! */
78 return SJME_TEST_RESULT_PASS;