Adjust sourcize.c to use _snprintf if using MSVC.
[SquirrelJME.git] / nanocoat / tests / testCircleBufferStack.c
blobfc03b5ae9c77442387a127596886183d171d1cac
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 "test.h"
13 #include "proto.h"
14 #include "mock.h"
15 #include "unit.h"
17 #include "sjme/circleBuffer.h"
19 #define TEST_BUF_SIZE 32
21 #define TEST_COUNT 25
23 /**
24 * Tests using a circle buffer as a stack.
26 * @since 2024/08/25
28 SJME_TEST_DECLARE(testCircleBufferStack)
30 sjme_circleBuffer* buffer;
31 sjme_circleBuffer_seekEnd seek;
32 sjme_jint whichSeek, val;
33 sjme_jubyte byteVal;
35 for (whichSeek = 0; whichSeek < 2; whichSeek++)
37 /* Which seek are we doing? */
38 seek = (whichSeek == 0 ? SJME_CIRCLE_BUFFER_TAIL :
39 SJME_CIRCLE_BUFFER_HEAD);
41 /* Setup new buffer. */
42 buffer = NULL;
43 if (sjme_error_is(test->error = sjme_circleBuffer_new(
44 test->pool,
45 &buffer,
46 SJME_CIRCLE_BUFFER_QUEUE, TEST_BUF_SIZE)) ||
47 buffer == NULL)
48 return sjme_unit_fail(test, "Could not make buffer.");
50 /* Push values onto the given end. */
51 for (val = 0; val < TEST_COUNT; val++)
53 /* Push single value. */
54 byteVal = val;
55 if (sjme_error_is(test->error = sjme_circleBuffer_push(buffer,
56 &byteVal, 1, seek)))
57 return sjme_unit_fail(test, "Could not push %d?", val);
60 /* All should have been pushed. */
61 sjme_unit_equalI(test, TEST_COUNT, buffer->ready,
62 "Buffer ready did not have everything?");
64 /* Pop all values */
65 for (val = TEST_COUNT - 1; val >= 0; val--)
67 /* Push single value. */
68 byteVal = 255;
69 if (sjme_error_is(test->error = sjme_circleBuffer_pop(buffer,
70 &byteVal, 1, seek)))
71 return sjme_unit_fail(test, "Could not pop %d?", val);
73 /* Must be the same value. */
74 sjme_unit_equalI(test, byteVal, val,
75 "Value popped did not match?");
78 /* All should have been popped. */
79 sjme_unit_equalI(test, 0, buffer->ready,
80 "Not everything was removed from the buffer?");
82 /* Destroy the buffer. */
83 if (sjme_error_is(test->error = sjme_circleBuffer_destroy(buffer)))
84 return sjme_unit_fail(test, "Could not destroy buffer?");
87 /* Success! */
88 return SJME_TEST_RESULT_PASS;