Compile fixes.
[SquirrelJME.git] / nanocoat / tests / testCircleBufferUnderflow.c
blob61c83db1427f9555ab09009ea52ec556ebea4ed5
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_SUB_COUNT 3
23 #define TEST_TOTAL_COUNT 6
25 static const sjme_jubyte testRight[TEST_SUB_COUNT] =
27 'a', 'b', 'c'
30 static const sjme_jubyte testLeft[TEST_SUB_COUNT] =
32 '0', '1', '2'
35 static const sjme_jubyte testEverything[TEST_TOTAL_COUNT] =
37 '0', '1', '2', 'a', 'b', 'c'
40 /**
41 * Tests under-flowing a circle buffer.
43 * @since 2024/08/25
45 SJME_TEST_DECLARE(testCircleBufferUnderflow)
47 sjme_circleBuffer* buffer;
48 sjme_jubyte everything[TEST_TOTAL_COUNT];
50 /* Setup new buffer. */
51 buffer = NULL;
52 if (sjme_error_is(test->error = sjme_circleBuffer_new(
53 test->pool, &buffer,
54 SJME_CIRCLE_BUFFER_QUEUE, TEST_BUF_SIZE)) ||
55 buffer == NULL)
56 return sjme_unit_fail(test, "Could not make buffer.");
58 /* Push to back of buffer. */
59 if (sjme_error_is(test->error = sjme_circleBuffer_push(buffer,
60 testRight, TEST_SUB_COUNT,
61 SJME_CIRCLE_BUFFER_TAIL)))
62 return sjme_unit_fail(test, "Could not push to tail?");
64 /* Push to front of buffer. */
65 if (sjme_error_is(test->error = sjme_circleBuffer_push(buffer,
66 testLeft, TEST_SUB_COUNT,
67 SJME_CIRCLE_BUFFER_HEAD)))
68 return sjme_unit_fail(test, "Could not push to head?");
70 /* Get everything from the buffer. */
71 memset(everything, 0, sizeof(everything));
72 if (sjme_error_is(test->error = sjme_circleBuffer_get(buffer,
73 everything, TEST_TOTAL_COUNT,
74 SJME_CIRCLE_BUFFER_HEAD, 0)))
75 return sjme_unit_fail(test, "Could not read head everything?");
76 sjme_message_hexDump(everything, TEST_TOTAL_COUNT);
77 sjme_message_hexDump(testEverything, TEST_TOTAL_COUNT);
78 sjme_unit_equalI(test,
79 0, memcmp(everything, testEverything, TEST_TOTAL_COUNT),
80 "Everything head data mismatched?");
82 /* Get everything from the buffer, again from the opposite end. */
83 memset(everything, 0xFE, sizeof(everything));
84 if (sjme_error_is(test->error = sjme_circleBuffer_get(buffer,
85 everything, TEST_TOTAL_COUNT,
86 SJME_CIRCLE_BUFFER_TAIL, TEST_TOTAL_COUNT)))
87 return sjme_unit_fail(test, "Could not read tail everything?");
88 sjme_message_hexDump(everything, TEST_TOTAL_COUNT);
89 sjme_message_hexDump(testEverything, TEST_TOTAL_COUNT);
90 sjme_unit_equalI(test,
91 0, memcmp(everything, testEverything, TEST_TOTAL_COUNT),
92 "Everything tail data mismatched?");
94 /* Get half the buffer. */
95 memset(everything, 0, sizeof(everything));
96 if (sjme_error_is(test->error = sjme_circleBuffer_get(buffer,
97 everything, TEST_SUB_COUNT,
98 SJME_CIRCLE_BUFFER_HEAD, 0)))
99 return sjme_unit_fail(test, "Could not read head half head?");
100 sjme_unit_equalI(test,
101 0, memcmp(everything, testLeft, TEST_SUB_COUNT),
102 "Half head data mismatched?");
104 /* Get half the buffer, again. */
105 memset(everything, 0, sizeof(everything));
106 if (sjme_error_is(test->error = sjme_circleBuffer_get(buffer,
107 everything, TEST_SUB_COUNT,
108 SJME_CIRCLE_BUFFER_TAIL, TEST_SUB_COUNT)))
109 return sjme_unit_fail(test, "Could not read half tail?");
110 sjme_unit_equalI(test,
111 0, memcmp(everything, testRight, TEST_SUB_COUNT),
112 "Half tail data mismatched?");
114 /* Destroy the buffer. */
115 if (sjme_error_is(test->error = sjme_circleBuffer_destroy(buffer)))
116 return sjme_unit_fail(test, "Could not destroy buffer?");
118 /* Pass! */
119 return SJME_TEST_RESULT_PASS;