1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
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 // -------------------------------------------------------------------------*/
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
] =
30 static const sjme_jubyte testLeft
[TEST_SUB_COUNT
] =
35 static const sjme_jubyte testEverything
[TEST_TOTAL_COUNT
] =
37 '0', '1', '2', 'a', 'b', 'c'
41 * Tests under-flowing a circle buffer.
45 SJME_TEST_DECLARE(testCircleBufferUnderflow
)
47 sjme_circleBuffer
* buffer
;
48 sjme_jubyte everything
[TEST_TOTAL_COUNT
];
50 /* Setup new buffer. */
52 if (sjme_error_is(test
->error
= sjme_circleBuffer_new(
54 SJME_CIRCLE_BUFFER_QUEUE
, TEST_BUF_SIZE
)) ||
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?");
119 return SJME_TEST_RESULT_PASS
;