Permalink
Browse files

Wrote a test function for buffer.c.

  • Loading branch information...
1 parent 2c027ea commit 31f5d62cac640dbe7c5ea726c1e86ebbefe39c26 @ingramj committed Sep 27, 2011
Showing with 34 additions and 1 deletion.
  1. +30 −0 buffer.c
  2. +1 −0 rescheme.c
  3. +2 −0 rescheme.h
  4. +1 −1 stack.c
View
@@ -1,6 +1,7 @@
#include "rescheme.h"
#include <assert.h>
+#include <string.h>
#define _RS_BUF_GROWBY 128
@@ -69,3 +70,32 @@ const char *rs_buf_cstr(struct rs_buf *buf)
buf->buf[buf->off] = '\0';
return (const char *) buf->buf;
}
+
+
+void rs_buf_test(void)
+{
+ struct rs_buf buf;
+ rs_buf_init(&buf);
+
+ // Make sure the buffer is empty.
+ assert(strcmp(rs_buf_cstr(&buf), "") == 0);
+
+ char str[] = "This is the test string.\n";
+
+ // Push the characters of str into buf, excluding the final '\0'.
+ for (int i = 0; i < (int) strlen(str); i++) {
+ if (rs_buf_push(&buf, str[i]) == NULL) {
+ rs_fatal("could not push to buffer:");
+ }
+ }
+ const char *bufstr = rs_buf_cstr(&buf);
+
+ // Make sure the returned string is nul-terminated.
+ assert(bufstr[strlen(str)+1] == '\0');
+
+ // Make sure the returned string matches the original string.
+ assert(strcmp(bufstr, str) == 0);
+
+ rs_buf_reset(&buf);
+ TRACE("passed");
+}
View
@@ -6,6 +6,7 @@ int main(void)
printf("ReScheme v0.3\n");
#ifdef DEBUG
+ rs_buf_test();
rs_stack_test();
#endif
View
@@ -179,6 +179,8 @@ struct rs_buf *rs_buf_push(struct rs_buf *buf, char c);
*/
const char *rs_buf_cstr(struct rs_buf *buf);
+/* Run a basic test of the buffer functions. */
+void rs_buf_test(void);
/**** stack.c - generic stack data structure. ****/
View
@@ -52,5 +52,5 @@ void rs_stack_test(void)
}
}
assert(stack == NULL);
- TRACE("passed.");
+ TRACE("passed");
}

0 comments on commit 31f5d62

Please sign in to comment.