00001 #include <ctype.h>
00002 #include <u/libu.h>
00003
00004 #define RB_SZ 4096
00005
00006 int test_suite_rb_register (u_test_t *t);
00007
00008 static int rw (u_test_case_t *tc, int malloc_based, int fast);
00009 #ifdef U_RB_CAN_MMAP
00010 static int test_rw (u_test_case_t *tc);
00011 static int test_rw_fast (u_test_case_t *tc);
00012 #endif
00013
00014 static int rw (u_test_case_t *tc, int malloc_based, int fast)
00015 {
00016 enum { BUF_SZ = 1024 };
00017 int opts;
00018 u_rb_t *rb = NULL;
00019 size_t i, obuf_sz;
00020 char ibuf[BUF_SZ], obuf[BUF_SZ], *obuf_ptr, c;
00021
00022 opts = fast ? U_RB_OPT_USE_CONTIGUOUS_MEM : U_RB_OPT_NONE;
00023 opts |= malloc_based ? U_RB_OPT_IMPL_MALLOC : U_RB_OPT_NONE;
00024
00025 u_test_err_if (u_rb_create(RB_SZ, opts, &rb));
00026
00027 c = '*';
00028 memset(ibuf, c, sizeof ibuf);
00029
00030
00031 for (i = 0; i < 4; i++)
00032 u_test_err_if (u_rb_write(rb, ibuf, sizeof ibuf) != sizeof ibuf);
00033
00034
00035 for (i = 0, c = 0; c < 127; c++)
00036 {
00037 if (!isprint(c))
00038 continue;
00039
00040
00041
00042 if (fast)
00043 {
00044 obuf_sz = BUF_SZ;
00045 obuf_ptr = u_rb_fast_read(rb, &obuf_sz);
00046 u_test_err_if (obuf_ptr == NULL || obuf_sz != BUF_SZ);
00047 }
00048 else
00049 {
00050 u_test_err_if (u_rb_read(rb, obuf, sizeof obuf) != sizeof obuf);
00051 obuf_ptr = obuf;
00052 }
00053
00054
00055 if (++i > 4)
00056 {
00057 u_test_err_ifm (obuf_ptr[0] != (c - 4) ||
00058 obuf_ptr[BUF_SZ - 1] != (c - 4),
00059 "expecting \'%c\', got \'%c\'", c - 4, obuf_ptr[0]);
00060 }
00061 else
00062 {
00063 u_test_err_ifm (obuf_ptr[0] != '*' || obuf_ptr[BUF_SZ - 1] != '*',
00064 "expecting \'%c\', got \'%c\'", '*', obuf_ptr[0]);
00065 }
00066
00067
00068 memset(ibuf, c, sizeof ibuf);
00069 u_test_err_if (u_rb_write(rb, ibuf, sizeof ibuf) != sizeof ibuf);
00070 }
00071
00072 u_rb_free(rb);
00073
00074 return U_TEST_SUCCESS;
00075 err:
00076 if (rb)
00077 u_rb_free(rb);
00078 return U_TEST_FAILURE;
00079 }
00080
00081
00082 #ifdef U_RB_CAN_MMAP
00083 static int test_rw (u_test_case_t *tc) { return rw(tc, 0, 0); }
00084 static int test_rw_fast (u_test_case_t *tc) { return rw(tc, 0, 1); }
00085 #endif
00086 static int test_rw_malloc (u_test_case_t *tc) { return rw(tc, 1, 0); }
00087 static int test_rw_fast_malloc (u_test_case_t *tc) { return rw(tc, 1, 1); }
00088
00089 int test_suite_rb_register (u_test_t *t)
00090 {
00091 u_test_suite_t *ts = NULL;
00092
00093 con_err_if (u_test_suite_new("Ring Buffer", &ts));
00094
00095 #ifdef U_RB_CAN_MMAP
00096 con_err_if (u_test_case_register("Read-write (mmap)", test_rw, ts));
00097 con_err_if (u_test_case_register("Read-write fast (mmap)",
00098 test_rw_fast, ts));
00099 #endif
00100 con_err_if (u_test_case_register("Read-write (malloc)",
00101 test_rw_malloc, ts));
00102 con_err_if (u_test_case_register("Read-write fast (malloc)",
00103 test_rw_fast_malloc, ts));
00104
00105 return u_test_suite_add(ts, t);
00106 err:
00107 u_test_suite_free(ts);
00108 return ~0;
00109 }