diff --git a/SConstruct b/SConstruct index 1ecc470..922222e 100644 --- a/SConstruct +++ b/SConstruct @@ -11,7 +11,7 @@ version_info = { 'version_revision': revision, #version_abi is the abi version of the shared library. Only bump it when #backwards compatibility breaks. Strive to not break the ABI. - 'version_abi': '1.0.0' + 'version_abi': '1.0.0' } version_info['version_str'] = "%d.%d.%d.%s" % ( version_info['version_major'], @@ -20,7 +20,7 @@ version_info['version_str'] = "%d.%d.%d.%s" % ( version_info['version_revision']) -AddOption('--build_type', +AddOption('--build_type', dest = 'build_type', type = 'choice', nargs = 1, @@ -87,7 +87,7 @@ test_xml = GetOption('test_xml') prefix = GetOption('prefix') lib_suffix = '' if not (build_type in ['debug', 'release']): - print "Error: expected 'debug' or 'release', found: " + build_type + print("Error: expected 'debug' or 'release', found: " + build_type) Exit(1) def InstallPerm(env, dest, files, perm): @@ -100,7 +100,7 @@ def InstallPerm(env, dest, files, perm): env = Environment(tools = ['default', 'textfile', 'packaging']) #allow shell environment to override compiler -if os.environ.has_key('CC'): +if os.environ.get('CC'): env['CC'] = os.environ['CC'] env.AddMethod(InstallPerm) diff --git a/include/SConscript b/include/SConscript index ae716ba..641be33 100644 --- a/include/SConscript +++ b/include/SConscript @@ -8,6 +8,6 @@ headers = ucore_env.Glob('ucore/*.h') #install targets -ucore_env.Alias('install', - ucore_env.InstallPerm(os.path.join(prefix, 'include', 'ucore'), headers, 0644)) +ucore_env.Alias('install', + ucore_env.InstallPerm(os.path.join(prefix, 'include', 'ucore'), headers, 0o0644)) diff --git a/include/ucore/ringbuf.h b/include/ucore/ringbuf.h new file mode 100644 index 0000000..dc9b47f --- /dev/null +++ b/include/ucore/ringbuf.h @@ -0,0 +1,98 @@ +#ifndef UC_RINGBUG_H_ +#define UC_RINGBUG_H_ +#include +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct UCRingBuf UCRingBuf; +struct UCRingBuf { + char *base; + uint32_t sz; + uint32_t read_idx; + uint32_t write_idx; +}; + +/** + * Creates a new ringbuffer of at least @sz length. + * The length will be rounded up to the neares page size + * + * The ringbuffer is implementd by mapping the same physical memory + * twice into virtual memory adjacent to eachother, avoiding + * a splitting up read/writes when the ringbuffer wraps around. + * + * This means accessing rb.base[rb.len] up to rb.base[rb.len * 2 - 1] + * is accessing the same memory as rb.base[0] up to rb.base[rb.len -1] + * + * @param rb UCRingBuf to initialize + * @param sz minimum length of the ring buffer + * @returns 0 on success + */ +int uc_ringbuf_init(UCRingBuf *rb, uint32_t sz); + +/** Free resources used by the ring buffer. + * + * @return 0 on success + */ +int uc_ringbuf_destroy(UCRingBuf *rb); + +/** + * Write data to the ringbuffer. The number of bytes to write must fit + * in the free space of the ringbuffer + * + * @param rb UCRingBuf to write to. + * @param data data to write + * @param sz number of bytes to write from data + * @return 0 on success + */ +int uc_ringbuf_write(UCRingBuf *rb, const void *restrict data, uint32_t sz); + +/** Read data from the ringbuffer + * + * @param rb UCRingBuf to write to. + * @param data buffer to copy data into + * @param sz number of bytes to read, must not exceed available data + * in the buffer + * @return 0 on success + */ +int uc_ringbuf_read(UCRingBuf *rb, void *restrict data, uint32_t sz); + + +/** Inspect how much free space is available for writing to the ring buffer + * + * @param rb UCRingBuf to inspect + * @returns number of free bytes + */ +uint32_t uc_ringbuf_available(const UCRingBuf *rb); + +/** + * Length of available data in the ringbuffer + * + * @param rb UCRingBuf get length of + * + * @return length of data available to read + */ +static inline uint32_t uc_ringbuf_len(const UCRingBuf *rb) +{ + return rb->write_idx - rb->read_idx; +} + +/** + * Get a pointer to the first unread byte + * Caller is responsible for reading no more than the available + * data + * + * @param rb UCRingBuf to peek into + * + * @returns pointer to first unread byte +*/ +static inline void *uc_ringbuf_peek(const UCRingBuf *rb) +{ + return &rb->base[rb->read_idx]; +} + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/bitvec.c b/src/bitvec.c index cd73536..6fb5892 100644 --- a/src/bitvec.c +++ b/src/bitvec.c @@ -18,22 +18,21 @@ static inline int bit_index(int b) } static inline int bit_offset(int b) -{ +{ return b % (sizeof(uc_bv_integer) * CHAR_BIT); } struct UCBitVec *uc_bv_new(size_t nbits) { - struct UCBitVec *v = malloc(sizeof *v); + size_t vec_len = UC_BV_LEN(nbits); + struct UCBitVec *v; + void *mem = malloc(sizeof *v + vec_len * sizeof(uc_bv_integer)); + v = mem; if (v == NULL) return NULL; - v->vec_len = UC_BV_LEN(nbits); - v->vec = malloc(v->vec_len * sizeof(uc_bv_integer)); - if (v->vec == NULL) { - free(v); - return NULL; - } + v->vec_len = vec_len; + v->vec = mem + sizeof *v; uc_bv_clr_all(v); @@ -43,24 +42,23 @@ struct UCBitVec *uc_bv_new(size_t nbits) void uc_bv_free(struct UCBitVec *v) { if (v) { - free(v->vec); free(v); } } void uc_bv_clr_bit(struct UCBitVec *v,int b) -{ +{ v->vec[bit_index(b)] &= ~(1UL << (bit_offset(b))); } int uc_bv_get_bit(const struct UCBitVec *v,int b) -{ +{ return !!(v->vec[bit_index(b)] & (1UL << bit_offset(b))); } void uc_bv_set_bit(struct UCBitVec *v,int b) -{ - v->vec[bit_index(b)] |= 1UL << (bit_offset(b)); +{ + v->vec[bit_index(b)] |= 1UL << (bit_offset(b)); } void uc_bv_set_bits_from_array(struct UCBitVec *v,const char *array,size_t array_len) diff --git a/src/fd_utils.c b/src/fd_utils.c index e14fb12..6862472 100644 --- a/src/fd_utils.c +++ b/src/fd_utils.c @@ -7,6 +7,11 @@ #include #include "ucore/fd_utils.h" +/* bsd variants use TCP_KEEPALIVE */ +#if defined(TCP_KEEPALIVE) && !defined(TCP_KEEPIDLE) +#define TCP_KEEPIDLE TCP_KEEPALIVE +#endif + int uc_set_nonblocking(int fd) { int flags = fcntl(fd,F_GETFL,NULL); diff --git a/src/iomux_epoll.c b/src/iomux_epoll.c index d18e227..d5ae10f 100644 --- a/src/iomux_epoll.c +++ b/src/iomux_epoll.c @@ -1,12 +1,13 @@ +#ifdef __linux__ #include #include #include -#include #include #include +#include #include "iomux_impl.h" -//epoll (linux specific) IO mux. +//epoll (linux specific) IO mux. //We stuff the struct IOMuxFD pointer in the event.data.ptr slot //for the kernel to keep track of, so we don't have to. ///we do not use edge triggered mode @@ -29,10 +30,10 @@ static inline uint32_t mux_2_epoll_events(unsigned int events) { uint32_t epoll_events = 0; - if (events & MUX_EV_READ) + if (events & MUX_EV_READ) epoll_events |= EPOLLIN; - if (events & MUX_EV_WRITE) + if (events & MUX_EV_WRITE) epoll_events |= EPOLLOUT; return epoll_events; @@ -47,7 +48,7 @@ static inline unsigned int epoll_2_mux_events(uint32_t events) if (events & (EPOLLIN | EPOLLHUP | EPOLLERR)) mux_events |= MUX_EV_READ; - if (events & EPOLLOUT) + if (events & EPOLLOUT) mux_events |= MUX_EV_WRITE; return mux_events; @@ -58,10 +59,10 @@ static int iomux_epoll_update_events(struct IOMux *mux_, struct IOMuxFD *fd) struct IOMuxEpoll *mux = mux_->instance; struct epoll_event e_event = {0, {0}}; int rc; - + e_event.events = mux_2_epoll_events(fd->what); e_event.data.ptr = fd; - + rc = epoll_ctl(mux->epoll_fd, EPOLL_CTL_MOD, fd->fd, &e_event); assert(rc == 0); if (rc != 0) @@ -117,7 +118,7 @@ static int iomux_epoll_unregister_fd(struct IOMux *mux_, struct IOMuxFD *fd) break; } } - + mux->num_descriptors--; return rc; @@ -145,10 +146,10 @@ static int iomux_epoll_run(struct IOMux *mux_, struct timeval *timeout) //epoll takes the timeout in miliseconds if (timeout) timeout_ms = timeval_to_ms(timeout); - else + else timeout_ms = -1; - do { + do { rc = epoll_wait(mux->epoll_fd, mux->pending_events, EPOLL_WAIT_EVENTS, timeout_ms); @@ -173,14 +174,14 @@ static int iomux_epoll_run(struct IOMux *mux_, struct timeval *timeout) //we want to deliver only one event at a time to simplify //application programming. - if (events & MUX_EV_READ && fd != NULL) { + if (events & MUX_EV_READ && fd != NULL) { assert(fd->callback != NULL); fd->callback(mux_, fd, MUX_EV_READ); } //now re-check, in case the IOMuxFD was removed fd = mux->pending_events[i].data.ptr; - if (events & MUX_EV_WRITE && fd != NULL) { + if (events & MUX_EV_WRITE && fd != NULL) { assert(fd->callback != NULL); fd->callback(mux_, fd, MUX_EV_WRITE); } @@ -211,7 +212,7 @@ static const struct IOMuxOps epoll_ops = { .unregister_fd_impl = iomux_epoll_unregister_fd, .update_events_impl = iomux_epoll_update_events, }; - + int iomux_epoll_init(struct IOMux *mux) { struct IOMuxEpoll *mux_epoll = calloc(1, sizeof *mux_epoll); @@ -230,3 +231,12 @@ int iomux_epoll_init(struct IOMux *mux) return 0; } +#else +#include +#include "iomux_impl.h" + +int iomux_epoll_init(struct IOMux *mux) +{ + return ENOSYS; +} +#endif diff --git a/src/iomux_impl.c b/src/iomux_impl.c index ae3a083..12c62a3 100644 --- a/src/iomux_impl.c +++ b/src/iomux_impl.c @@ -54,8 +54,8 @@ void iomux_delete(struct IOMux *mux) } //convert the difference between future and now -static inline void future_to_interval(const struct timeval *future, - const struct timeval *now, +static inline void future_to_interval(const struct timeval *future, + const struct timeval *now, struct timeval *result) { if (timercmp(future, now, >)) { @@ -66,7 +66,7 @@ static inline void future_to_interval(const struct timeval *future, } } -static int iomux_run_timers(struct IOMux *mux, struct timeval *timeout) +static int iomux_next_timer_timeout(struct IOMux *mux, struct timeval *timeout) { struct timeval first_timer; @@ -78,12 +78,12 @@ static int iomux_run_timers(struct IOMux *mux, struct timeval *timeout) first_timer.tv_sec, first_timer.tv_usec, timeout->tv_sec, timeout->tv_usec); */ - + assert(timeout->tv_sec >= 0); assert(timeout->tv_usec >= 0); return 1; - } + } return 0; } @@ -95,7 +95,7 @@ int iomux_run(struct IOMux *mux) struct timeval timeout; struct timeval *timeoutp; - if (iomux_run_timers(mux, &timeout)) { + if (iomux_next_timer_timeout(mux, &timeout)) { timeoutp = &timeout; } else { timeoutp = NULL; @@ -147,7 +147,7 @@ int iomux_update_events(struct IOMux *mux, struct IOMuxFD *fd, unsigned int what assert(fd != NULL); assert(fd->callback != NULL); assert(fd->fd >= 0); - + if (fd->what != what) { fd->what = what; rc = mux->ops.update_events_impl(mux, fd); diff --git a/src/ringbuf.c b/src/ringbuf.c new file mode 100644 index 0000000..3f47693 --- /dev/null +++ b/src/ringbuf.c @@ -0,0 +1,93 @@ +#ifdef __linux +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include "ucore/ringbuf.h" + + +int uc_ringbuf_init(UCRingBuf *rb, uint32_t sz) +{ + + uint32_t pagesz = getpagesize(); + // Round up to page size + sz = ((sz + pagesz - 1) / pagesz) * pagesz; + /* Reserve contiguous address space*/ + void *base = mmap(NULL, sz * 2, PROT_NONE, MAP_POPULATE|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); + if (base == NULL) { + return -errno; + } + /* MAP_PRIVATE doesn't work */ + base = mmap(base, sz * 1, PROT_READ|PROT_WRITE, MAP_POPULATE|MAP_ANONYMOUS|MAP_FIXED|MAP_SHARED, -1, 0); + if (base == MAP_FAILED) { + return -errno; + } + void *mbase = mremap(base, sz * 1, sz, MREMAP_FIXED|MREMAP_MAYMOVE|MREMAP_DONTUNMAP, base + sz); + if (mbase == MAP_FAILED) { + munmap(base, sz); + return -errno; + + } + + rb->base = base; + rb->sz = sz; + rb->read_idx = 0; + rb->write_idx = 0; + + return 0; +} + +int uc_ringbuf_destroy(UCRingBuf *rb) +{ + int rc = munmap(rb->base, rb->sz); + if (rc != 0) { + return -errno; + } + + rc = munmap(rb->base + rb->sz, rb->sz); + if (rc != 0) { + return -errno; + } + + rb->base = NULL; + + return 0; +} + +uint32_t uc_ringbuf_available(const UCRingBuf *rb) +{ + return rb->sz - rb->write_idx + rb->read_idx; +} + +int uc_ringbuf_write(UCRingBuf *rb, const void *restrict data, uint32_t len) +{ + if (rb->write_idx - rb->read_idx + len > rb->sz) { + return -ENOMEM; + } + memcpy(&rb->base[rb->write_idx], data, len); + rb->write_idx += len; + + return 0; +} + +int uc_ringbuf_read(UCRingBuf *rb, void *restrict data, uint32_t len) +{ + if (uc_ringbuf_len(rb) < len) { + return -ENOMEM; + } + + memcpy(data, &rb->base[rb->read_idx], len); + + rb->read_idx += len; + if (rb->read_idx == rb->write_idx) { + rb->read_idx = rb->write_idx = 0; + } else if (rb->read_idx > rb->sz) { + rb->read_idx -= rb->sz; + rb->write_idx -= rb->sz; + } + return 0; +} + +#endif diff --git a/src/salloc.c b/src/salloc.c index 6ec0767..8e2b17d 100644 --- a/src/salloc.c +++ b/src/salloc.c @@ -21,27 +21,32 @@ struct Chunk { struct SAlloc { size_t chunksz; //data size of each chunk Chunk *first; //head of the chunk list - Chunk *current; + Chunk *current; }; -static Chunk * -add_chunk(SAlloc *p) +static inline Chunk * +alloc_chunk(size_t sz) { Chunk *newchunk; - newchunk = malloc(sizeof *newchunk + p->chunksz - (sizeof *newchunk - offsetof(Chunk, data.data))); - if (newchunk == NULL) - return NULL; - - if (p->current != NULL) - p->current->next = newchunk; - - newchunk->next = NULL; - newchunk->firstfree = 0; - + newchunk = malloc(sizeof *newchunk + sz - (sizeof *newchunk - offsetof(Chunk, data.data))); return newchunk; } +static Chunk * +add_chunk(SAlloc *p, Chunk *c) +{ + if (c == NULL) + return c; + + p->current->next = c; + + c->next = NULL; + c->firstfree = 0; + + return c; +} + static Chunk * next_chunk(SAlloc *p) { @@ -50,8 +55,10 @@ next_chunk(SAlloc *p) if (p->current->next) { newchunk = p->current->next; newchunk->firstfree = 0; - } else - newchunk = add_chunk(p); + } else { + Chunk *c = alloc_chunk(p->chunksz); + newchunk = add_chunk(p, c); + } if (newchunk) p->current = newchunk; @@ -63,19 +70,16 @@ SAlloc * uc_new_salloc(size_t chunksz) { SAlloc *p; - Chunk *first; - p = malloc(sizeof *p); - if (p == NULL) - return NULL; - - p->chunksz = chunksz; - p->current = NULL; - - if ((first = add_chunk(p)) == NULL) { - free(p); + Chunk *first; + first = alloc_chunk(sizeof *p + chunksz); + if (first == NULL) { return NULL; } - + first->next = NULL; + first->firstfree = sizeof *p; + + p = (SAlloc *)&first->data.data[0]; + p->chunksz = chunksz; p->first = p->current = first; return p; @@ -86,7 +90,9 @@ uc_reset_salloc(SAlloc *p) { Chunk *tmp; - for(tmp = p->first; tmp; tmp = tmp->next) + tmp = p->first; + tmp->firstfree = sizeof *p; + for(tmp = tmp->next; tmp; tmp = tmp->next) tmp->firstfree = 0; p->current = p->first; @@ -97,9 +103,9 @@ uc_s_alloc(SAlloc *p,size_t sz) { Chunk *chunk; void *newmem; - + chunk = p->current; - if (chunk->firstfree + sz > p->chunksz) { + if (chunk->firstfree + sz > p->chunksz) { if (sz > p->chunksz) return NULL; @@ -107,7 +113,7 @@ uc_s_alloc(SAlloc *p,size_t sz) if (chunk == NULL) return NULL; } - + newmem = &chunk->data.data[chunk->firstfree]; chunk->firstfree += sz; @@ -119,17 +125,17 @@ uc_s_allocz(SAlloc *p,size_t sz) { void *newmem = uc_s_alloc(p, sz); - if (newmem != NULL) + if (newmem != NULL) memset(newmem, 0, sz); return newmem; } -static void +static void free_chunks(Chunk *chunk) { Chunk *next; - + for (next = NULL; chunk != NULL; chunk = next) { next = chunk->next; free(chunk); @@ -140,7 +146,5 @@ void uc_free_salloc(SAlloc *p) { free_chunks(p->first); - free(p); - } diff --git a/test/SConscript b/test/SConscript index 680c23d..37b6141 100644 --- a/test/SConscript +++ b/test/SConscript @@ -1,20 +1,27 @@ -import platform +import platform Import('env', 'build_type', 'prefix', 'lib_suffix', 'version_info', 'test_xml') test_env = env.Clone() test_env.Append(LIBPATH = ['#build/src']); -test_env.Append(LIBS = ['ucore' + lib_suffix, 'check','m','rt']) +test_env.Append(LIBS = ['ucore' + lib_suffix, 'check','m']) system = platform.system() -#NetBSD needs /usr/pkg/... +#NetBSD needs /usr/pkg/... if 'netbsd' in system.lower(): test_env.Append(LIBPATH = ['/usr/pkg/lib']) test_env.Append(LINKFLAGS = ['-Wl,-rpath,/usr/pkg/lib']) test_env.Append(CPPPATH = ['/usr/pkg/include']) - +if 'darwin' in system.lower(): + test_env.Append(LIBPATH = ['/opt/homebrew/lib']) + test_env.Append(LINKFLAGS = ['-Wl,-rpath,/opt/homebrew/lib']) + test_env.Append(CPPPATH = ['/opt/homebrew/include']) + +if 'darwin' not in system.lower(): + test_env.Append(LIBS = ['rt']) + tests = test_env.Glob('test_*.c') @@ -27,8 +34,8 @@ if test_xml: test_env.Append(LINKFLAGS = ['-Wl,-rpath,\\$$ORIGIN/../src']) prog = test_env.Program('test_runner', tests) -cmd = test_env.Command(target='test_phony' , - source = None, +cmd = test_env.Command(target='test_phony' , + source = None, action= test_env.Dir('.').abspath + '/' + test_executable) test_env.Depends(cmd, prog) test_env.AlwaysBuild(cmd) diff --git a/test/test_bcd.c b/test/test_bcd.c index 96c24ba..65732b1 100644 --- a/test/test_bcd.c +++ b/test/test_bcd.c @@ -3,6 +3,7 @@ START_TEST (test_2bcd_1234567890) +{ char ascii[] = "1234567890"; unsigned char bcd[5]; size_t len = uc_ascii2bcd(ascii, bcd, 0); @@ -13,26 +14,32 @@ START_TEST (test_2bcd_1234567890) fail_if(bcd[2] != 0x65,"was 0x%x",bcd[2]); fail_if(bcd[3] != 0x87,"was 0x%x",bcd[3]); fail_if(bcd[4] != 0x09,"was 0x%x",bcd[4]); +} END_TEST START_TEST (test_2bcd_empty) +{ char ascii[] = ""; unsigned char bcd[1]; size_t len = uc_ascii2bcd(ascii, bcd, 3); fail_if(len != 0, "len is %zu", len); +} END_TEST START_TEST (test_2bcd_1_filler) +{ char ascii[] = "1"; unsigned char bcd[1]; size_t len = uc_ascii2bcd(ascii, bcd, 0xa); fail_if(len != 1, "len is %zu", len); fail_if(bcd[0] != 0xa1,"was 0x%x",bcd[0]); +} END_TEST START_TEST (test_2bcd_2_filler) +{ char ascii[] = "123"; unsigned char bcd[2]; size_t len = uc_ascii2bcd(ascii, bcd, 0xf); @@ -41,9 +48,11 @@ START_TEST (test_2bcd_2_filler) fail_if(bcd[0] != 0x21,"was 0x%x",bcd[0]); fail_if(bcd[1] != 0xf3,"was 0x%x",bcd[1]); fail_if(UC_BCD_LEN(strlen(ascii)) != len); +} END_TEST START_TEST (test_2bcd_3_filler) +{ char ascii[] = "123"; unsigned char bcd[2]; @@ -52,18 +61,22 @@ START_TEST (test_2bcd_3_filler) fail_if(len != 2, "len is %zu", len); fail_if(bcd[0] != 0x21,"was 0x%x",bcd[0]); fail_if(bcd[1] != 0x03,"was 0x%x",bcd[1]); +} END_TEST START_TEST (test_2bcd_non_digits) +{ char ascii[] = "a,b{ }="; unsigned char bcd[10]; size_t len = uc_ascii2bcd(ascii, bcd, 0x0); fail_if(len != 0, "len is %zu", len); +} END_TEST START_TEST (test_2bcd_phoneno) +{ char ascii[] = "0047-3233-4"; unsigned char bcd[24]; @@ -75,9 +88,11 @@ START_TEST (test_2bcd_phoneno) fail_if(bcd[2] != 0x23,"was 0x%x",bcd[1]); fail_if(bcd[3] != 0x33,"was 0x%x",bcd[1]); fail_if(bcd[4] != 0xf4,"was 0x%x",bcd[1]); +} END_TEST START_TEST (test_2bcd_binary) +{ char ascii[] = {0x32,0x01,0xAF,0xFF,0x00}; unsigned char bcd[24]; @@ -85,9 +100,11 @@ START_TEST (test_2bcd_binary) fail_if(len != 1, "len is %zu", len); fail_if(bcd[0] != 0xf2,"0 was 0x%x",bcd[0]); +} END_TEST START_TEST (test_2hex_1) +{ unsigned char bcd[] = {0x21, 0x43 }; char ascii[sizeof bcd * 2 + 1]; @@ -100,9 +117,11 @@ START_TEST (test_2hex_1) fail_if(ascii[3] != '4',"3 was %c",ascii[3]); fail_if(ascii[4] != 0, "4 was %c",ascii[4]); fail_if(UC_ASCII_LEN(sizeof bcd) != len); +} END_TEST START_TEST (test_2hex_2) +{ unsigned char bcd[] = {0xF0, 0xFF, 0x00}; char ascii[sizeof bcd * 2 + 1]; @@ -116,9 +135,11 @@ START_TEST (test_2hex_2) fail_if(ascii[4] != '0', "4 was %c",ascii[4]); fail_if(ascii[5] != '0', "5 was %c",ascii[5]); fail_if(ascii[6] != 0, "6 was %c",ascii[6]); +} END_TEST START_TEST (test_2hex_empty) +{ unsigned char bcd[] = {0xF0, 0xFF, 0x00}; char ascii[sizeof bcd * 2 + 1]; @@ -127,6 +148,7 @@ START_TEST (test_2hex_empty) fail_if(len != 0, "len is %zu", len); fail_if(ascii[0] != 0, "0 was %c",ascii[0]); +} END_TEST Suite *bcd_suite(void) diff --git a/test/test_bitvec.c b/test/test_bitvec.c index 0da7670..11fe28e 100644 --- a/test/test_bitvec.c +++ b/test/test_bitvec.c @@ -3,15 +3,18 @@ START_TEST (test_bitvec_1) +{ uc_bv_integer s[3] = {0}; struct UCBitVec v = UC_BV_STATIC_INIT(s); size_t i; for (i = 0; i < sizeof s * 8; i++) fail_if(uc_bv_get_bit(&v, i), "bit %zu is not 0", i); +} END_TEST START_TEST (test_bitvec_2) +{ uc_bv_integer s[3] = {0}; struct UCBitVec v = UC_BV_STATIC_INIT(s); size_t i; @@ -21,9 +24,11 @@ START_TEST (test_bitvec_2) for (i = 0; i < sizeof s * 8; i++) fail_if(uc_bv_get_bit(&v, i) != 1 , "bit %zu is not 1", i); +} END_TEST START_TEST (test_bitvec_clearbit) +{ uc_bv_integer s[3] = {-1, -1, -1}; struct UCBitVec v = UC_BV_STATIC_INIT(s); size_t i; @@ -37,9 +42,11 @@ START_TEST (test_bitvec_clearbit) for (i = 0; i < sizeof(uc_bv_integer) * 8; i++) fail_if(uc_bv_get_bit(&v, i) != 0 , "bit %zu is not 0", i); +} END_TEST START_TEST (test_bitvec_set_bits_from_array) +{ struct UCBitVec *v = uc_bv_new(5); char a[] = {1, 0,1 ,0, 1}; @@ -51,10 +58,12 @@ START_TEST (test_bitvec_set_bits_from_array) fail_if(uc_bv_get_bit(v, 4) != 1 , "bit 4 is wrong"); uc_bv_free(v); +} END_TEST START_TEST (test_bitvec_setall_clearall) +{ struct UCBitVec *v = uc_bv_new(511); size_t i; @@ -70,6 +79,7 @@ START_TEST (test_bitvec_setall_clearall) uc_bv_free(v); +} END_TEST Suite *bitvec_suite(void) diff --git a/test/test_buffer.c b/test/test_buffer.c index 64cc8da..3f09ad4 100644 --- a/test/test_buffer.c +++ b/test/test_buffer.c @@ -4,6 +4,7 @@ START_TEST (test_new_gbuf) +{ GBuf *buf = uc_new_gbuf(11); fail_if(buf == NULL); @@ -13,9 +14,11 @@ START_TEST (test_new_gbuf) fail_if(buf->used != 0); uc_gbuf_unref(buf); +} END_TEST START_TEST (test_gbuf_grow) +{ GBuf *buf = uc_new_gbuf(33); int rc; @@ -29,9 +32,11 @@ START_TEST (test_gbuf_grow) fail_if(buf->used != 0); uc_gbuf_unref(buf); +} END_TEST START_TEST (test_gbuf_append1) +{ const char *data = "lorum_ ipson 1234567890"; int rc; GBuf *buf = uc_new_gbuf(11); @@ -44,9 +49,11 @@ START_TEST (test_gbuf_append1) fail_if(strcmp(data, buf->buf) != 0); uc_gbuf_unref(buf); +} END_TEST START_TEST (test_gbuf_printf1) +{ int rc; GBuf *buf = uc_new_gbuf(10); @@ -58,9 +65,11 @@ START_TEST (test_gbuf_printf1) fail_if(strcmp("test 1", buf->buf) != 0); uc_gbuf_unref(buf); +} END_TEST START_TEST (test_gbuf_printf2) +{ int rc; GBuf *buf = uc_new_gbuf(8); @@ -72,9 +81,11 @@ START_TEST (test_gbuf_printf2) fail_if(strcmp("test 1 test 2 test 3", buf->buf) != 0); uc_gbuf_unref(buf); +} END_TEST START_TEST (test_gbuf_printf3) +{ int rc; GBuf *buf = uc_new_gbuf(8); @@ -92,9 +103,11 @@ START_TEST (test_gbuf_printf3) fail_if(strcmp("test 1 test 2 test 3 test 4 test 5", buf->buf) != 0); uc_gbuf_unref(buf); +} END_TEST START_TEST (test_gbuf_printf_empty_string) +{ int rc; GBuf *buf = uc_new_gbuf(1); const char *fmt = ""; //tricks gcc to not warn about empty format string @@ -106,6 +119,7 @@ START_TEST (test_gbuf_printf_empty_string) fail_if(buf->used != 0); uc_gbuf_unref(buf); +} END_TEST diff --git a/test/test_clock.c b/test/test_clock.c index 2d06d54..25b0bfb 100644 --- a/test/test_clock.c +++ b/test/test_clock.c @@ -4,6 +4,7 @@ START_TEST (test_settable_clock) +{ struct UCoreSettableClock my_clock; struct timeval tv; struct timeval tv_tmp; @@ -34,9 +35,11 @@ START_TEST (test_settable_clock) fail_if(tv.tv_sec != 123456789); fail_if(tv.tv_usec != 44); +} END_TEST START_TEST (test_cached_clock) +{ struct UCoreCachedClock my_clock; struct timeval tv1,tv2; @@ -60,6 +63,7 @@ START_TEST (test_cached_clock) fail_if(!timercmp(&tv1, &tv2, !=)); +} END_TEST diff --git a/test/test_containerof.c b/test/test_containerof.c index 351bd50..6dd1098 100644 --- a/test/test_containerof.c +++ b/test/test_containerof.c @@ -15,6 +15,7 @@ struct Outer { }; START_TEST (test_container_of) +{ struct Outer outer = { .tag1 = 1, .tag2 = 2, @@ -29,6 +30,7 @@ START_TEST (test_container_of) fail_if(outerp != &outer); +} END_TEST Suite *container_of_suite(void) diff --git a/test/test_dbuf.c b/test/test_dbuf.c index 317f2ba..2b63aa5 100644 --- a/test/test_dbuf.c +++ b/test/test_dbuf.c @@ -5,6 +5,7 @@ START_TEST (test_dbuf) +{ DBuf buf; int rc; @@ -59,9 +60,11 @@ START_TEST (test_dbuf) uc_dbuf_free(&buf); +} END_TEST START_TEST (test_dbuf_take_fail) +{ DBuf buf; int rc; @@ -80,10 +83,12 @@ START_TEST (test_dbuf_take_fail) fail_if(rc == 0); uc_dbuf_free(&buf); +} END_TEST START_TEST (test_dbuf_take_all) +{ DBuf buf; int rc; @@ -104,9 +109,11 @@ START_TEST (test_dbuf_take_all) fail_if(uc_dbuf_remaining(&buf) != 10); uc_dbuf_free(&buf); +} END_TEST START_TEST (test_dbuf_add_ensure_take) +{ DBuf *buf; int rc; @@ -155,6 +162,7 @@ START_TEST (test_dbuf_add_ensure_take) uc_dbuf_free(buf); free(buf); +} END_TEST diff --git a/test/test_dstr.c b/test/test_dstr.c index 1c5e1a1..97637f9 100644 --- a/test/test_dstr.c +++ b/test/test_dstr.c @@ -6,6 +6,7 @@ START_TEST (test_dstr_basics) +{ struct DStr str; int rc; @@ -46,9 +47,11 @@ START_TEST (test_dstr_basics) //just test that this doesn't crash: uc_dstr_init(&str); uc_dstr_destroy(&str); +} END_TEST START_TEST (test_dstr_init) +{ struct DStr str; int rc; @@ -65,9 +68,11 @@ START_TEST (test_dstr_init) ck_assert_str_eq(uc_dstr_str(&str), "He"); uc_dstr_destroy(&str); +} END_TEST START_TEST (test_dstr_own) +{ struct DStr str; uc_dstr_init_str(&str, "init"); @@ -76,8 +81,10 @@ START_TEST (test_dstr_own) ck_assert_str_eq(uc_dstr_str(&str), "Hello !"); uc_dstr_destroy(&str); +} END_TEST START_TEST (test_dstr_own_steal) +{ struct DStr str; char *s; @@ -93,9 +100,11 @@ START_TEST (test_dstr_own_steal) free(s); uc_dstr_destroy(&str); +} END_TEST START_TEST (test_dstr_trim) +{ struct DStr str = UC_DSTR_INITIALIZER; uc_dstr_append_str(&str, " \t \nHello !\r\n"); @@ -118,9 +127,11 @@ START_TEST (test_dstr_trim) uc_dstr_destroy(&str); +} END_TEST START_TEST (test_dstr_replace) +{ struct DStr str; size_t rc; @@ -136,9 +147,11 @@ START_TEST (test_dstr_replace) uc_dstr_destroy(&str); +} END_TEST START_TEST (test_dstr_filter) +{ struct DStr str; size_t rc; @@ -151,9 +164,11 @@ START_TEST (test_dstr_filter) uc_dstr_destroy(&str); +} END_TEST START_TEST (test_dstr_sprintf) +{ struct DStr str; size_t rc; @@ -168,9 +183,11 @@ START_TEST (test_dstr_sprintf) uc_dstr_destroy(&str); +} END_TEST START_TEST (test_dstr_copy) +{ struct DStr str; struct DStr copy; size_t rc; @@ -195,6 +212,7 @@ START_TEST (test_dstr_copy) uc_dstr_destroy(&str); uc_dstr_destroy(©); +} END_TEST Suite *dstr_suite(void) diff --git a/test/test_heapsort.c b/test/test_heapsort.c index 3668593..aece89d 100644 --- a/test/test_heapsort.c +++ b/test/test_heapsort.c @@ -52,6 +52,7 @@ static int cmp_stuff_array(const struct Stuff *a, const struct Stuff *b, size_t } START_TEST (test_heapsort_odd) +{ struct Stuff unordered[] = { {"2", 2}, {"1", 1}, @@ -72,9 +73,11 @@ START_TEST (test_heapsort_odd) cmp_stuff, swap_stuff, NULL); print_stuff(unordered, ARRAY_SIZE(unordered)); fail_if(cmp_stuff_array(unordered, ordered, ARRAY_SIZE(unordered)) != 0); +} END_TEST START_TEST (test_heapsort_even) +{ struct Stuff unordered[] = { {"1", 1}, {"7", 7}, @@ -96,9 +99,11 @@ START_TEST (test_heapsort_even) uc_heapsort(unordered, ARRAY_SIZE(unordered), sizeof(struct Stuff), cmp_stuff, swap_stuff, NULL); fail_if(cmp_stuff_array(unordered, ordered, ARRAY_SIZE(unordered)) != 0); +} END_TEST START_TEST (test_heapsort_zero_one) +{ struct Stuff unordered[] = { {"1", 1}, {"7", 7}, @@ -128,6 +133,7 @@ START_TEST (test_heapsort_zero_one) uc_heapsort(unordered, 1, sizeof(struct Stuff), cmp_stuff, swap_stuff, NULL); fail_if(cmp_stuff_array(unordered, same, ARRAY_SIZE(unordered)) != 0); +} END_TEST Suite *heapsort_suite(void) diff --git a/test/test_hex.c b/test/test_hex.c index 1e60b6c..03b6982 100644 --- a/test/test_hex.c +++ b/test/test_hex.c @@ -4,6 +4,7 @@ START_TEST (test_uc_hex_encode_1) +{ uint8_t binary[] = {0xff}; char hex[3] = ""; @@ -11,9 +12,11 @@ START_TEST (test_uc_hex_encode_1) fail_if(strcmp(hex,"FF") != 0); +} END_TEST START_TEST (test_uc_hex_encode_2) +{ uint8_t binary[] = {127, 128, 129}; char hex[sizeof binary * 2 + 1] = ""; @@ -21,9 +24,11 @@ START_TEST (test_uc_hex_encode_2) fail_if(strcmp(hex,"7F8081") != 0, "was %s", hex); +} END_TEST START_TEST (test_uc_hex_encode_3) +{ uint8_t binary[] = {0, 1, 9, 11}; char hex[sizeof binary * 2 + 1] = ""; @@ -31,9 +36,11 @@ START_TEST (test_uc_hex_encode_3) fail_if(strcmp(hex,"0001090B") != 0, "was %s", hex); +} END_TEST START_TEST (test_uc_hex_encode_zero_len) +{ uint8_t binary[1] = {0xff}; char hex[3] = "aa"; @@ -42,9 +49,11 @@ START_TEST (test_uc_hex_encode_zero_len) fail_if(hex[0] != 0); fail_if(hex[1] != 'a'); +} END_TEST START_TEST (test_uc_hex_decode_1) +{ char hex[] = "1F"; uint8_t binary[2] = {0x11,0x22}; uint8_t *res; @@ -55,9 +64,11 @@ START_TEST (test_uc_hex_decode_1) fail_if(binary[1] != 0x22); fail_if(res != binary + 1, "binary %p, res %p", &binary[0], res); +} END_TEST START_TEST (test_uc_hex_decode_2) +{ char hex[] = "00FF80"; uint8_t binary[4] = {0x11, 0x11, 0x11}; uint8_t *res; @@ -69,9 +80,11 @@ START_TEST (test_uc_hex_decode_2) fail_if(binary[2] != 0x80); fail_if(res != binary + 3, "binary %p, res %p", &binary[0], res); +} END_TEST START_TEST (test_uc_hex_decode_zero_length) +{ char hex[] = "00FF80"; uint8_t binary[4] = {0x11, 0x11, 0x11}; uint8_t *res; @@ -83,9 +96,11 @@ START_TEST (test_uc_hex_decode_zero_length) fail_if(binary[2] != 0x11); fail_if(res != binary); +} END_TEST START_TEST (test_uc_hex_decode_empty_string) +{ char hex[] = ""; uint8_t binary[] = {0x11, 0x11, 0x11}; uint8_t *res; @@ -97,8 +112,10 @@ START_TEST (test_uc_hex_decode_empty_string) fail_if(binary[2] != 0x11); fail_if(res != binary, "length %zu\n", res - binary); +} END_TEST START_TEST (test_uc_hex_decode_spaces) +{ char hex[] = " 11 1213"; uint8_t binary[3]; uint8_t *res; @@ -110,9 +127,11 @@ START_TEST (test_uc_hex_decode_spaces) fail_if(binary[2] != 0x13); fail_if(res != binary+3, "length %zu\n", res - binary); +} END_TEST START_TEST (test_uc_hex_decode_spaces2) +{ char hex[] = "1112 13\r\n"; uint8_t binary[3]; uint8_t *res; @@ -124,9 +143,11 @@ START_TEST (test_uc_hex_decode_spaces2) fail_if(binary[2] != 0x13); fail_if(res != binary+3, "length %zu\n", res - binary); +} END_TEST START_TEST (test_uc_hex_decode_spaces_empty) +{ char hex[] = " \r\n "; uint8_t binary[] = {0x11,0x12}; uint8_t *res; @@ -137,9 +158,11 @@ START_TEST (test_uc_hex_decode_spaces_empty) fail_if(binary[1] != 0x12); fail_if(res != binary, "length %zu\n", res - binary); +} END_TEST START_TEST (test_uc_hex_decode_odd_length) +{ char hex[] = "33FF80"; uint8_t binary[4] = {0x11, 0x11, 0x11}; uint8_t *res; @@ -151,9 +174,11 @@ START_TEST (test_uc_hex_decode_odd_length) fail_if(binary[2] != 0x11); fail_if(res != &binary[1]); +} END_TEST START_TEST (test_uc_hex_decode_invalid) +{ char hex[] = "TEST "; uint8_t binary[4] = {0x11, 0x11, 0x11}; uint8_t *res; @@ -165,9 +190,11 @@ START_TEST (test_uc_hex_decode_invalid) fail_if(binary[2] != 0x11); fail_if(res != NULL); +} END_TEST START_TEST (test_uc_hex_encode_delim_1) +{ uint8_t binary[4] = {0xFF, 0x00, 0x7F, 0x0A}; char hex[17] = ""; char *res; @@ -178,9 +205,11 @@ START_TEST (test_uc_hex_encode_delim_1) *res = 0; fail_if(res != hex + 12); +} END_TEST START_TEST (test_uc_hex_encode_delim_2) +{ uint8_t binary[] = {0xFF, 0x00}; char hex[8] = ""; char *res; @@ -190,9 +219,11 @@ START_TEST (test_uc_hex_encode_delim_2) fail_if(strcmp(hex, "FF \n00 ") != 0, "hex was '%s'", hex); fail_if(res != hex + 7, "sz was %zu", res - hex); +} END_TEST START_TEST (test_uc_hex_encode_delim_zero_len) +{ uint8_t binary[] = {0xFF, 0x00}; char hex[8] = "......"; char *res; @@ -202,10 +233,12 @@ START_TEST (test_uc_hex_encode_delim_zero_len) fail_if(strcmp(hex, "") != 0, "hex was '%s'", hex); fail_if(res != hex); +} END_TEST START_TEST (test_uc_hex_decode_lowercase) +{ char hex[] = "1f"; uint8_t binary[2] = {0x11,0x22}; uint8_t *res; @@ -216,6 +249,7 @@ START_TEST (test_uc_hex_decode_lowercase) fail_if(binary[1] != 0x22); fail_if(res != binary + 1, "binary %p, res %p", &binary[0], res); +} END_TEST Suite *hex_suite(void) { diff --git a/test/test_htable.c b/test/test_htable.c index c5a5add..0fba7ca 100644 --- a/test/test_htable.c +++ b/test/test_htable.c @@ -40,6 +40,7 @@ struct MyString *my_find(struct UHTable *table, const char *str, size_t hash) START_TEST (test_htable1) +{ struct MyString str[5] = { {"One", {0,NULL}}, {"Two", {0,NULL}}, @@ -67,9 +68,11 @@ START_TEST (test_htable1) uc_htable_destroy(&table); +} END_TEST START_TEST (test_htable_hash_collision) +{ //same as test1 but with a bad hash to force equal hash struct MyString str[5] = { {"...", {0,NULL}}, @@ -97,9 +100,11 @@ START_TEST (test_htable_hash_collision) uc_htable_destroy(&table); +} END_TEST START_TEST (test_htable_resize) +{ struct MyString str[5] = { {"One", {0,NULL}}, {"Two", {0,NULL}}, @@ -132,9 +137,11 @@ START_TEST (test_htable_resize) uc_htable_destroy(&table); +} END_TEST START_TEST (test_htable_fail_init_resize) +{ int rc; struct UHTable table; rc = uc_htable_init(&table, 0); @@ -148,9 +155,11 @@ START_TEST (test_htable_fail_init_resize) uc_htable_destroy(&table); +} END_TEST START_TEST (test_htable_has_node) +{ struct MyString str1 = {"One", {0,NULL}}; struct MyString str2 = {"Two", {0,NULL}}; @@ -171,9 +180,11 @@ START_TEST (test_htable_has_node) uc_htable_destroy(&table); +} END_TEST START_TEST (test_htable_remove) +{ struct MyString str1 = {"One", {0,NULL}}; struct MyString str2 = {"One", {0,NULL}}; struct MyString str3 = {"Other", {0,NULL}}; @@ -216,9 +227,11 @@ START_TEST (test_htable_remove) uc_htable_destroy(&table); +} END_TEST START_TEST (test_htable_remove_foreach) +{ struct MyString str1 = {"One", {0,NULL}}; struct MyString str2 = {"One", {0,NULL}}; struct MyString str3 = {"Other", {0,NULL}}; @@ -254,9 +267,11 @@ START_TEST (test_htable_remove_foreach) uc_htable_destroy(&table); +} END_TEST START_TEST (test_htable_clear) +{ struct MyString str1 = {"One", {0,NULL}}; struct UHTable table; @@ -276,9 +291,11 @@ START_TEST (test_htable_clear) uc_htable_destroy(&table); +} END_TEST START_TEST (test_htable_foreach) +{ struct MyString str1 = {"One", {0,NULL}}; struct MyString str2 = {"One", {0,NULL}}; struct MyString str3 = {"Other",{0,NULL}}; @@ -318,9 +335,11 @@ START_TEST (test_htable_foreach) uc_htable_destroy(&table); +} END_TEST START_TEST (test_htable_foreach_safe) +{ struct MyString str1 = {"One", {0,NULL}}; struct MyString str2 = {"One", {0,NULL}}; struct MyString str3 = {"Other",{0,NULL}}; @@ -353,6 +372,7 @@ START_TEST (test_htable_foreach_safe) uc_htable_destroy(&table); +} END_TEST Suite *htable_suite(void) diff --git a/test/test_human_bytesz.c b/test/test_human_bytesz.c index 832155b..54e33ec 100644 --- a/test/test_human_bytesz.c +++ b/test/test_human_bytesz.c @@ -3,6 +3,7 @@ START_TEST (test_human_bytesz_bytes) +{ char result[128]; char *rc; @@ -13,9 +14,11 @@ START_TEST (test_human_bytesz_bytes) rc = uc_human_bytesz_bin(999, result, sizeof result); fail_if(rc == NULL); ck_assert_str_eq(result, "999 b"); +} END_TEST START_TEST (test_human_bytesz_bytes_0) +{ char result[128]; char *rc; @@ -26,9 +29,11 @@ START_TEST (test_human_bytesz_bytes_0) rc = uc_human_bytesz_bin(0, result, sizeof result); fail_if(rc == NULL); ck_assert_str_eq(result, "0 b"); +} END_TEST START_TEST (test_human_bytesz_kilobytes) +{ char result[128]; char *rc; @@ -39,9 +44,11 @@ START_TEST (test_human_bytesz_kilobytes) rc = uc_human_bytesz_bin(64000, result, sizeof result); fail_if(rc == NULL); ck_assert_str_eq(result, "62.50 KiB"); +} END_TEST START_TEST (test_human_bytesz_megabytes) +{ char result[128]; char *rc; @@ -52,9 +59,11 @@ START_TEST (test_human_bytesz_megabytes) rc = uc_human_bytesz_bin(6409000, result, sizeof result); fail_if(rc == NULL); ck_assert_str_eq(result, "6.11 MiB"); +} END_TEST START_TEST (test_human_bytesz_gigabytes) +{ char result[128]; char *rc; @@ -65,9 +74,11 @@ START_TEST (test_human_bytesz_gigabytes) rc = uc_human_bytesz_bin(126999400000, result, sizeof result); fail_if(rc == NULL); ck_assert_str_eq(result, "118.28 GiB"); +} END_TEST START_TEST (test_human_bytesz_terrabytes) +{ char result[128]; char *rc; @@ -78,9 +89,11 @@ START_TEST (test_human_bytesz_terrabytes) rc = uc_human_bytesz_bin(12453400030000, result, sizeof result); fail_if(rc == NULL); ck_assert_str_eq(result, "11.33 TiB"); +} END_TEST START_TEST (test_human_bytesz_petabytes) +{ char result[128]; char *rc; @@ -91,6 +104,7 @@ START_TEST (test_human_bytesz_petabytes) rc = uc_human_bytesz_bin(~0ULL, result, sizeof result); fail_if(rc == NULL); ck_assert_str_eq(result, "16384.00 PiB"); +} END_TEST diff --git a/test/test_iomux.c b/test/test_iomux.c index bc150e9..8d4c368 100644 --- a/test/test_iomux.c +++ b/test/test_iomux.c @@ -67,6 +67,7 @@ static void simple_wcb(struct IOMux *mux, struct IOMuxFD *fd, unsigned int event START_TEST (test_iomux_pipe_simple) +{ struct IOMux *mux; struct IOMuxFD rfd; struct IOMuxFD wfd; @@ -106,6 +107,7 @@ START_TEST (test_iomux_pipe_simple) iomux_delete(mux); +} END_TEST diff --git a/test/test_iomux_signal.c b/test/test_iomux_signal.c index 2d34e05..029c84a 100644 --- a/test/test_iomux_signal.c +++ b/test/test_iomux_signal.c @@ -46,6 +46,7 @@ static void usr2_handler(struct IOMux *mux, int signo, void *cookie) } START_TEST (test_iomux_signal_simple) +{ struct IOMux *mux = iomux_create(g_iomux_type); @@ -87,6 +88,7 @@ START_TEST (test_iomux_signal_simple) ck_assert_int_eq(g_usr1_seen, 1); ck_assert_int_eq(g_usr2_seen, 1); +} END_TEST diff --git a/test/test_logging.c b/test/test_logging.c index d6a8a28..126c90a 100644 --- a/test/test_logging.c +++ b/test/test_logging.c @@ -31,6 +31,7 @@ static void logging_rm_subdir(void) } START_TEST (test_logfile_location) +{ struct UCLogModule m = { .id = 0, @@ -62,9 +63,11 @@ START_TEST (test_logfile_location) rc = stat(FILE_NAME, &st); fail_if(rc != 0, "stat failed: %s", strerror(errno)); fail_if(st.st_size != 380, "was %ld", (long)st.st_size); +} END_TEST START_TEST (test_logfile_delete_destination) +{ struct UCLogModule m = { .id = 0, @@ -110,9 +113,11 @@ START_TEST (test_logfile_delete_destination) fail_if(rc != 0, "stat failed: %s", strerror(errno)); fail_if(st.st_size != 380, "was %ld", (long)st.st_size); +} END_TEST START_TEST (test_logfile_no_location) +{ struct UCLogModule m = { .id = 0, @@ -140,9 +145,11 @@ START_TEST (test_logfile_no_location) rc = stat(FILE_NAME, &st); fail_if(rc != 0, "stat failed: %s", strerror(errno)); fail_if(st.st_size != 52*2, "was %ld", (long)st.st_size); +} END_TEST START_TEST (test_logfile_loglevel_surpressed_dest) +{ struct UCLogModule m = { .id = 0, @@ -172,9 +179,11 @@ START_TEST (test_logfile_loglevel_surpressed_dest) fail_if(rc != 0, "stat failed: %s", strerror(errno)); fail_if(st.st_size != 0, "was %ld", (long)st.st_size); +} END_TEST START_TEST (test_logfile_loglevel_surpressed_module) +{ struct UCLogModule m = { .id = 0, @@ -204,9 +213,11 @@ START_TEST (test_logfile_loglevel_surpressed_module) fail_if(rc != 0, "stat failed: %s", strerror(errno)); fail_if(st.st_size != 0, "was %ld", (long)st.st_size); +} END_TEST START_TEST (test_logfile_reopen_files) +{ struct UCLogModule m = { .id = 0, @@ -247,9 +258,11 @@ START_TEST (test_logfile_reopen_files) fail_if(rc != 0, "2. stat failed: %s", strerror(errno)); fail_if(st.st_size != 77, "was %ld", (long)st.st_size);//should only one line there now +} END_TEST START_TEST (test_logfile_remove_dest) +{ struct UCLogModule m = { .id = 0, @@ -301,9 +314,11 @@ START_TEST (test_logfile_remove_dest) fail_if(rc != 0, "2. stat failed: %s", strerror(errno)); fail_if(st.st_size != 77*2, "was %ld", (long)st.st_size);//should be same size +} END_TEST START_TEST (test_logfile_full_filesystem) +{ //logging to a full filesystem shouldn't crash struct UCLogModule m = { .id = 0, @@ -331,9 +346,11 @@ START_TEST (test_logfile_full_filesystem) UC_LOGF(UC_LL_INFO, 0, "Lorum ipson %d", 1); } +} END_TEST START_TEST (test_logfile_invalid_file) +{ //check behavior when a log file cannot be opened struct UCLogModule m = { .id = 0, @@ -352,9 +369,11 @@ START_TEST (test_logfile_invalid_file) dest = uc_log_new_file("PATH/to/nonexisting.log", UC_LL_DEBUG, 1); fail_if(dest != NULL); +} END_TEST START_TEST (test_logfile_invalid_file_after_reopen) +{ //check behavior when a log file cannot be re-opened struct UCLogModule m = { .id = 0, @@ -391,9 +410,11 @@ START_TEST (test_logfile_invalid_file_after_reopen) UC_LOGF(UC_LL_WARNING, 0, "Lorum ipson %d", 1); UC_LOGF(UC_LL_INFO, 0, "Lorum ipson %d", 1); +} END_TEST START_TEST (test_logfile_raw) +{ struct UCLogModule m = { .id = 0, @@ -421,9 +442,11 @@ START_TEST (test_logfile_raw) rc = stat(FILE_NAME, &st); fail_if(rc != 0, "stat failed: %s", strerror(errno)); fail_if(st.st_size != 14*2, "was %ld", (long)st.st_size); +} END_TEST START_TEST (test_logfile_dest_mask) +{ struct UCLogModule m[] = { { @@ -467,9 +490,11 @@ START_TEST (test_logfile_dest_mask) rc = stat(FILE_NAME, &st); fail_if(rc != 0, "stat failed: %s", strerror(errno)); fail_if(st.st_size != 14*2, "was %ld", (long)st.st_size); +} END_TEST START_TEST (test_loglevel_module_none) +{ struct UCLogModule m = { .id = 0, @@ -496,9 +521,11 @@ START_TEST (test_loglevel_module_none) rc = stat(FILE_NAME, &st); fail_if(rc != 0, "stat failed: %s", strerror(errno)); fail_if(st.st_size != 0, "was %ld", (long)st.st_size); +} END_TEST START_TEST (test_loglevel_dest_none) +{ struct UCLogModule m = { .id = 0, @@ -525,6 +552,7 @@ START_TEST (test_loglevel_dest_none) rc = stat(FILE_NAME, &st); fail_if(rc != 0, "stat failed: %s", strerror(errno)); fail_if(st.st_size != 0, "was %ld", (long)st.st_size); +} END_TEST diff --git a/test/test_mbuf.c b/test/test_mbuf.c index fc37e43..bc54fc1 100644 --- a/test/test_mbuf.c +++ b/test/test_mbuf.c @@ -5,6 +5,7 @@ START_TEST (test_mbuf_new) +{ struct MBuf *mbuf; mbuf = uc_mbuf_new(110); @@ -22,9 +23,11 @@ START_TEST (test_mbuf_new) uc_mbuf_free(mbuf); +} END_TEST START_TEST (test_mbuf_new_inline) +{ struct MBuf *mbuf; mbuf = uc_mbuf_new_inline(110); @@ -44,9 +47,11 @@ START_TEST (test_mbuf_new_inline) uc_mbuf_free(mbuf); +} END_TEST START_TEST (test_mbuf_new_headroom) +{ struct MBuf *mbuf; mbuf = uc_mbuf_new_hr(110,20); @@ -64,9 +69,11 @@ START_TEST (test_mbuf_new_headroom) uc_mbuf_free(mbuf); +} END_TEST START_TEST (test_mbuf_put) +{ struct MBuf *mbuf; uint8_t *data; @@ -82,9 +89,11 @@ START_TEST (test_mbuf_put) uc_mbuf_free(mbuf); +} END_TEST START_TEST (test_mbuf_put_fail) +{ struct MBuf *mbuf; uint8_t *data; @@ -96,9 +105,11 @@ START_TEST (test_mbuf_put_fail) uc_mbuf_free(mbuf); +} END_TEST START_TEST (test_mbuf_push) +{ struct MBuf *mbuf; uint8_t *data; @@ -114,10 +125,12 @@ START_TEST (test_mbuf_push) uc_mbuf_free(mbuf); +} END_TEST START_TEST (test_mbuf_push_fail) +{ struct MBuf *mbuf; uint8_t *data; @@ -129,9 +142,11 @@ START_TEST (test_mbuf_push_fail) uc_mbuf_free(mbuf); +} END_TEST START_TEST (test_mbuf_pull) +{ struct MBuf *mbuf; uint8_t *data1; uint8_t *data2; @@ -151,9 +166,11 @@ START_TEST (test_mbuf_pull) uc_mbuf_free(mbuf); +} END_TEST START_TEST (test_mbuf_pull_fail) +{ struct MBuf *mbuf; uint8_t *data1; uint8_t *data2; @@ -172,9 +189,11 @@ START_TEST (test_mbuf_pull_fail) uc_mbuf_free(mbuf); +} END_TEST START_TEST (test_mbuf_copy) +{ struct MBuf *mbuf; uint8_t *data1; struct MBuf *copy; @@ -196,9 +215,11 @@ START_TEST (test_mbuf_copy) uc_mbuf_free(mbuf); uc_mbuf_free(copy); +} END_TEST START_TEST (test_mbuf_zero) +{ struct MBuf *mbuf; uint8_t *data1; @@ -215,9 +236,11 @@ START_TEST (test_mbuf_zero) uc_mbuf_free(mbuf); +} END_TEST START_TEST (test_mbuf_static) +{ uint8_t data[1024]; struct MBuf mbuf; @@ -245,9 +268,11 @@ START_TEST (test_mbuf_static) uc_mbuf_free(&mbuf); +} END_TEST START_TEST (test_mbuf_extend_tailroom) +{ struct MBuf *mbuf; uint8_t *data; void *filler; @@ -281,9 +306,11 @@ START_TEST (test_mbuf_extend_tailroom) uc_mbuf_free(mbuf); free(filler); +} END_TEST START_TEST (test_mbuf_extend_tailroom_wrong_type) +{ struct MBuf *mbuf; int rc; @@ -295,9 +322,11 @@ START_TEST (test_mbuf_extend_tailroom_wrong_type) uc_mbuf_free(mbuf); +} END_TEST START_TEST (test_mbuf_queue) +{ struct MBuf *m1; struct MBuf *m2; struct MBuf *q; @@ -337,6 +366,7 @@ START_TEST (test_mbuf_queue) uc_mbuf_free(m1); uc_mbuf_free(m2); +} END_TEST Suite *mbuf_suite(void) diff --git a/test/test_pack.c b/test/test_pack.c index 02336ab..522b67e 100644 --- a/test/test_pack.c +++ b/test/test_pack.c @@ -3,6 +3,7 @@ START_TEST (test_pack16_le_1) +{ uint16_t v = 0x1234; uint8_t r[2]; @@ -10,9 +11,11 @@ START_TEST (test_pack16_le_1) fail_if(r[0] != 0x34,"was 0x%x",r[0]); fail_if(r[1] != 0x12,"was 0x%x",r[1]); +} END_TEST START_TEST (test_pack16_le_2) +{ uint16_t v = 0xFEEF; uint8_t r[2]; @@ -20,9 +23,11 @@ START_TEST (test_pack16_le_2) fail_if(r[0] != 0xEF,"was 0x%x",r[0]); fail_if(r[1] != 0xFE,"was 0x%x",r[1]); +} END_TEST START_TEST (test_pack16_be_1) +{ uint16_t v = 0x1234; uint8_t r[2]; @@ -30,9 +35,11 @@ START_TEST (test_pack16_be_1) fail_if(r[0] != 0x12,"was 0x%x",r[0]); fail_if(r[1] != 0x34,"was 0x%x",r[1]); +} END_TEST START_TEST (test_pack16_be_2) +{ uint16_t v = 0xFEEF; uint8_t r[2]; @@ -40,45 +47,55 @@ START_TEST (test_pack16_be_2) fail_if(r[0] != 0xFE,"was 0x%x",r[0]); fail_if(r[1] != 0xEF,"was 0x%x",r[1]); +} END_TEST START_TEST (test_unpack16_le_1) +{ uint16_t v; uint8_t r[2] = {0x12, 0x34}; v = uc_unpack_16_le(r); fail_if(v != 0x3412,"was 0x%x",v); +} END_TEST START_TEST (test_unpack16_le_2) +{ uint16_t v; uint8_t r[2] = {0xFE, 0xEF}; v = uc_unpack_16_le(r); fail_if(v != 0xEFFE,"was 0x%x",v); +} END_TEST START_TEST (test_unpack16_be_1) +{ uint16_t v; uint8_t r[2] = {0x12, 0x34}; v = uc_unpack_16_be(r); fail_if(v != 0x1234,"was 0x%x",v); +} END_TEST START_TEST (test_unpack16_be_2) +{ uint16_t v; uint8_t r[2] = {0xFE, 0xEF}; v = uc_unpack_16_be(r); fail_if(v != 0xFEEF,"was 0x%x",v); +} END_TEST START_TEST (test_pack24_le_1) +{ uint32_t v = 0x123456; uint8_t r[3]; @@ -87,9 +104,11 @@ START_TEST (test_pack24_le_1) fail_if(r[0] != 0x56,"was 0x%x",r[0]); fail_if(r[1] != 0x34,"was 0x%x",r[1]); fail_if(r[2] != 0x12,"was 0x%x",r[2]); +} END_TEST START_TEST (test_pack24_le_2) +{ uint32_t v = 0xF8F7F6; uint8_t r[3]; @@ -98,9 +117,11 @@ START_TEST (test_pack24_le_2) fail_if(r[0] != 0xF6,"was 0x%x",r[0]); fail_if(r[1] != 0xF7,"was 0x%x",r[1]); fail_if(r[2] != 0xF8,"was 0x%x",r[2]); +} END_TEST START_TEST (test_pack24_be_1) +{ uint32_t v = 0x123456; uint8_t r[3]; @@ -109,9 +130,11 @@ START_TEST (test_pack24_be_1) fail_if(r[0] != 0x12,"was 0x%x",r[0]); fail_if(r[1] != 0x34,"was 0x%x",r[1]); fail_if(r[2] != 0x56,"was 0x%x",r[2]); +} END_TEST START_TEST (test_pack24_be_2) +{ uint32_t v = 0xF8F7F6; uint8_t r[3]; @@ -120,9 +143,11 @@ START_TEST (test_pack24_be_2) fail_if(r[0] != 0xF8,"was 0x%x",r[0]); fail_if(r[1] != 0xF7,"was 0x%x",r[1]); fail_if(r[2] != 0xF6,"was 0x%x",r[2]); +} END_TEST START_TEST (test_pack32_le_1) +{ uint32_t v = 0x12345678; uint8_t r[4]; @@ -132,9 +157,11 @@ START_TEST (test_pack32_le_1) fail_if(r[1] != 0x56,"was 0x%x",r[1]); fail_if(r[2] != 0x34,"was 0x%x",r[2]); fail_if(r[3] != 0x12,"was 0x%x",r[3]); +} END_TEST START_TEST (test_pack32_le_2) +{ uint32_t v = 0xF8F7F6F4; uint8_t r[4]; @@ -144,9 +171,11 @@ START_TEST (test_pack32_le_2) fail_if(r[1] != 0xF6,"was 0x%x",r[1]); fail_if(r[2] != 0xF7,"was 0x%x",r[2]); fail_if(r[3] != 0xF8,"was 0x%x",r[3]); +} END_TEST START_TEST (test_pack32_be_1) +{ uint32_t v = 0x12345678; uint8_t r[4]; @@ -156,9 +185,11 @@ START_TEST (test_pack32_be_1) fail_if(r[2] != 0x56,"was 0x%x",r[2]); fail_if(r[1] != 0x34,"was 0x%x",r[1]); fail_if(r[0] != 0x12,"was 0x%x",r[0]); +} END_TEST START_TEST (test_pack32_be_2) +{ uint32_t v = 0xF8F7F6F4; uint8_t r[4]; @@ -168,81 +199,99 @@ START_TEST (test_pack32_be_2) fail_if(r[2] != 0xF6,"was 0x%x",r[2]); fail_if(r[1] != 0xF7,"was 0x%x",r[1]); fail_if(r[0] != 0xF8,"was 0x%x",r[0]); +} END_TEST START_TEST (test_unpack24_le_1) +{ uint32_t v; uint8_t r[3] = {0x12, 0x34, 0x56 }; v = uc_unpack_24_le(r); fail_if(v != 0x563412,"was 0x%x",v); +} END_TEST START_TEST (test_unpack24_le_2) +{ uint32_t v; uint8_t r[4] = {0xF8, 0xF7, 0xF6}; v = uc_unpack_24_le(r); fail_if(v != 0xF6F7F8,"was 0x%x",v); +} END_TEST START_TEST (test_unpack24_be_1) +{ uint32_t v; uint8_t r[3] = {0x12, 0x34, 0x56 }; v = uc_unpack_24_be(r); fail_if(v != 0x123456,"was 0x%x",v); +} END_TEST START_TEST (test_unpack24_be_2) +{ uint32_t v; uint8_t r[4] = {0xF8, 0xF7, 0xF6}; v = uc_unpack_24_be(r); fail_if(v != 0xF8F7F6,"was 0x%x",v); +} END_TEST START_TEST (test_unpack32_le_1) +{ uint32_t v; uint8_t r[4] = {0x12, 0x34, 0x56, 0x78}; v = uc_unpack_32_le(r); fail_if(v != 0x78563412,"was 0x%x",v); +} END_TEST START_TEST (test_unpack32_le_2) +{ uint32_t v; uint8_t r[4] = {0xF8, 0xF7, 0xF6, 0xF4 }; v = uc_unpack_32_le(r); fail_if(v != 0xF4F6F7F8,"was 0x%x",v); +} END_TEST START_TEST (test_unpack32_be_1) +{ uint32_t v; uint8_t r[4] = {0x12, 0x34, 0x56, 0x78}; v = uc_unpack_32_be(r); fail_if(v != 0x12345678,"was 0x%x",v); +} END_TEST START_TEST (test_unpack32_be_2) +{ uint32_t v; uint8_t r[4] = {0xF8, 0xF7, 0xF6, 0xF4 }; v = uc_unpack_32_be(r); fail_if(v != 0xF8F7F6F4,"was 0x%x",v); +} END_TEST START_TEST (test_pack64_le_1) +{ uint64_t v = 0x1122334455667788ULL; uint8_t r[8] = {}; @@ -256,9 +305,11 @@ START_TEST (test_pack64_le_1) fail_if(r[5] != 0x33,"was 0x%x",r[5]); fail_if(r[6] != 0x22,"was 0x%x",r[6]); fail_if(r[7] != 0x11,"was 0x%x",r[7]); +} END_TEST START_TEST (test_pack64_le_2) +{ uint64_t v = 0xFFFEFDFCFBFAF9F8ULL; uint8_t r[8] = {}; @@ -272,28 +323,34 @@ START_TEST (test_pack64_le_2) fail_if(r[5] != 0xFD,"was 0x%x",r[5]); fail_if(r[6] != 0xFE,"was 0x%x",r[6]); fail_if(r[7] != 0xFF,"was 0x%x",r[7]); +} END_TEST START_TEST (test_unpack64_le_1) +{ uint64_t v; uint8_t r[8] = {0x11 ,0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; v = uc_unpack_64_le(r); fail_if(v != 0x8877665544332211ULL,"was 0x%llx",v); +} END_TEST START_TEST (test_unpack64_le_2) +{ uint64_t v; uint8_t r[8] = {0xFF ,0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xF9, 0xF8}; v = uc_unpack_64_le(r); fail_if(v != 0xF8F9FAFBFCFDFEFFULL,"was 0x%llx",v); +} END_TEST START_TEST (test_pack64_be_1) +{ uint64_t v = 0x1122334455667788ULL; uint8_t r[8] = {}; @@ -307,9 +364,11 @@ START_TEST (test_pack64_be_1) fail_if(r[5] != 0x66,"was 0x%x",r[5]); fail_if(r[6] != 0x77,"was 0x%x",r[6]); fail_if(r[7] != 0x88,"was 0x%x",r[7]); +} END_TEST START_TEST (test_pack64_be_2) +{ uint64_t v = 0xFFFEFDFCFBFAF9F8ULL; uint8_t r[8] = {}; @@ -323,24 +382,29 @@ START_TEST (test_pack64_be_2) fail_if(r[5] != 0xFA,"was 0x%x",r[5]); fail_if(r[6] != 0xF9,"was 0x%x",r[6]); fail_if(r[7] != 0xF8,"was 0x%x",r[7]); +} END_TEST START_TEST (test_unpack64_be_1) +{ uint64_t v; uint8_t r[8] = {0x11 ,0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; v = uc_unpack_64_be(r); fail_if(v != 0x1122334455667788ULL,"was 0x%llx",v); +} END_TEST START_TEST (test_unpack64_be_2) +{ uint64_t v; uint8_t r[8] = {0xFF ,0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xF9, 0xF8}; v = uc_unpack_64_be(r); fail_if(v != 0xFFFEFDFCFBFAF9F8ULL,"was 0x%llx",v); +} END_TEST Suite *pack_suite(void) diff --git a/test/test_ratelimit.c b/test/test_ratelimit.c index 0fb4f2c..20aa887 100644 --- a/test/test_ratelimit.c +++ b/test/test_ratelimit.c @@ -3,6 +3,7 @@ START_TEST (test_ratelimit_1) +{ struct RateLimit r; long now = 10; int i; @@ -12,9 +13,11 @@ START_TEST (test_ratelimit_1) fail_if(!uc_ratelimit_allow(&r, now), "i = %d", i); } fail_if(uc_ratelimit_allow(&r, now)); +} END_TEST START_TEST (test_ratelimit_2) +{ struct RateLimit r; long now = 10; @@ -27,9 +30,11 @@ START_TEST (test_ratelimit_2) fail_if(!uc_ratelimit_allow(&r, now)); fail_if(uc_ratelimit_allow(&r, now)); +} END_TEST START_TEST (test_ratelimit_3) +{ struct RateLimit r; long now = 10; int i; @@ -63,9 +68,11 @@ START_TEST (test_ratelimit_3) fail_if(uc_ratelimit_allow(&r, now)); +} END_TEST START_TEST (test_ratelimit_4) +{ struct RateLimit r; long now = 10; int i; @@ -86,9 +93,11 @@ START_TEST (test_ratelimit_4) } fail_if(uc_ratelimit_allow(&r, now)); +} END_TEST START_TEST (test_ratelimit_5) +{ struct RateLimit r; long now = 1400000000; @@ -111,9 +120,11 @@ START_TEST (test_ratelimit_5) fail_if(!uc_ratelimit_allow(&r, now)); fail_if(uc_ratelimit_allow(&r, now)); +} END_TEST START_TEST (test_ratelimit_time_backward) +{ struct RateLimit r; long now = 1400000000; @@ -126,6 +137,7 @@ START_TEST (test_ratelimit_time_backward) fail_if(uc_ratelimit_allow(&r, now)); +} END_TEST diff --git a/test/test_read_file.c b/test/test_read_file.c index 29e9924..67f353b 100644 --- a/test/test_read_file.c +++ b/test/test_read_file.c @@ -9,6 +9,7 @@ START_TEST (test_read_file_non_existing_file) +{ char *content; size_t len; int saved_errno; @@ -19,9 +20,11 @@ START_TEST (test_read_file_non_existing_file) fail_if(content != NULL); fail_if(saved_errno == 0); +} END_TEST START_TEST (test_read_file_simple) +{ char *content; const char *filename = "test_read_file_simple"; size_t len; @@ -43,9 +46,11 @@ START_TEST (test_read_file_simple) fail_if(strlen(content) != 5); fail_if(strcmp("hello", content) != 0); free(content); +} END_TEST START_TEST (test_read_file_greater_than_max) +{ char *content; const char *filename = "test_read_file_simple"; size_t len; @@ -66,9 +71,11 @@ START_TEST (test_read_file_greater_than_max) fail_if(content != NULL); fail_if(saved_errno != EMSGSIZE, "was %d", saved_errno); +} END_TEST START_TEST (test_read_file_boundary) +{ char *content; const char *filename = "test_read_file_boundary"; size_t len; @@ -91,9 +98,11 @@ START_TEST (test_read_file_boundary) fail_if(len != sizeof buf); fail_if(strcmp("foobar", &content[1022]) != 0); free(content); +} END_TEST START_TEST (test_read_file_devnull) +{ char *content; size_t len = 123; @@ -102,9 +111,11 @@ START_TEST (test_read_file_devnull) fail_if(len != 0); free(content); +} END_TEST START_TEST (test_read_file_too_big) +{ char *content; const char *filename = "test_read_file_too_big"; size_t len; @@ -124,9 +135,11 @@ START_TEST (test_read_file_too_big) fail_if(content != NULL); unlink(filename); +} END_TEST START_TEST (test_read_file_big) +{ char *content; const char *filename = "test_read_file_big"; char buf[4096 * 16]; @@ -150,6 +163,7 @@ START_TEST (test_read_file_big) free(content); unlink(filename); +} END_TEST Suite *read_file_suite(void) diff --git a/test/test_restart_file.c b/test/test_restart_file.c index 55749dc..0a2b056 100644 --- a/test/test_restart_file.c +++ b/test/test_restart_file.c @@ -10,7 +10,7 @@ #include "ucore/restart_counter.h" #define FILE_NAME "restart_counter_test" -//Note that these tests are comewhat fragile as they depend on the +//Note that these tests are comewhat fragile as they depend on the //file sizes that are created, which depends on the (relative)paths of this file. //for cleaning the created log files.. static void logging_rm_files(void) @@ -20,6 +20,7 @@ static void logging_rm_files(void) START_TEST (test_restart_counter) +{ UCRCResult rc; struct UCRestartCounter cnt; @@ -33,9 +34,11 @@ START_TEST (test_restart_counter) ck_assert_int_eq(1, uc_restart_counter_get(&cnt)); +} END_TEST START_TEST (test_restart_counter_existing) +{ UCRCResult rc; struct UCRestartCounter cnt; @@ -50,9 +53,11 @@ START_TEST (test_restart_counter_existing) fail_if(rc != UC_RC_OK); ck_assert_int_eq(100000, uc_restart_counter_get(&cnt)); +} END_TEST START_TEST (test_restart_counter_inaccessible_file) +{ UCRCResult rc; struct UCRestartCounter cnt; @@ -60,8 +65,10 @@ START_TEST (test_restart_counter_inaccessible_file) rc = uc_restart_counter_init(&cnt, "/should/not/exist/abc", 0); fail_if(rc != UC_RC_ERR_FILE_ACCESS_ERROR); +} END_TEST START_TEST (test_restart_counter_garble) +{ UCRCResult rc; struct UCRestartCounter cnt; @@ -78,9 +85,11 @@ START_TEST (test_restart_counter_garble) fail_if(rc != UC_RC_OK, "%d", rc); ck_assert_int_eq(1, uc_restart_counter_get(&cnt)); +} END_TEST START_TEST (test_restart_counter_full_filesystem) +{ UCRCResult rc; struct UCRestartCounter cnt; @@ -88,9 +97,11 @@ START_TEST (test_restart_counter_full_filesystem) rc = uc_restart_counter_init(&cnt, "/dev/full", 0); fail_if(rc != UC_RC_ERR_UNSPECIFIED); +} END_TEST - +#ifndef __APPLE__ START_TEST (test_restart_counter_lock_file) +{ UCRCResult rc; struct UCRestartCounter cnt; @@ -126,8 +137,9 @@ START_TEST (test_restart_counter_lock_file) _exit(0); } +} END_TEST - +#endif Suite *restart_counter_suite(void) { @@ -143,8 +155,9 @@ Suite *restart_counter_suite(void) } else { puts("Cannot access /dev/full. Not testing test_logfile_full_filesystem"); } - +#ifndef __APPLE__ tcase_add_test(tc1, test_restart_counter_lock_file); +#endif tcase_add_checked_fixture(tc1, logging_rm_files, logging_rm_files); suite_add_tcase(s, tc1); diff --git a/test/test_ringbuf.c b/test/test_ringbuf.c new file mode 100644 index 0000000..a11b430 --- /dev/null +++ b/test/test_ringbuf.c @@ -0,0 +1,48 @@ +#include +#include +#include "ucore/ringbuf.h" + +START_TEST (test_ringbuf) +{ + + UCRingBuf rb; + int rc; + int pgsize = getpagesize(); + rc = uc_ringbuf_init(&rb, pgsize); + ck_assert_int_eq(rc, 0); + ck_assert_int_eq(uc_ringbuf_available(&rb), pgsize); + + + for (int i = 0 ; i < pgsize; i++) { + unsigned char data = i % 256; + rc = uc_ringbuf_write(&rb, &data, 1); + ck_assert_int_eq(rc, 0); + + } + ck_assert_int_eq(uc_ringbuf_available(&rb), 0); + + + for (int i = 0; i < pgsize; i++) { + unsigned char data = 0; + rc = uc_ringbuf_read(&rb, &data, 1); + ck_assert_int_eq(rc, 0); + ck_assert_int_eq(data, i % 256); + } + ck_assert_int_eq(uc_ringbuf_available(&rb), pgsize); + +} + +Suite *ringbuf_suite(void) +{ + Suite *s = suite_create("ringbuf"); + TCase *tc1 = tcase_create("ringbuf"); + + tcase_add_test(tc1, test_ringbuf); +#ifndef __APPLE__ +#endif + + suite_add_tcase(s, tc1); + + return s; +} + diff --git a/test/test_runner.c b/test/test_runner.c index afd7e96..27f0e79 100644 --- a/test/test_runner.c +++ b/test/test_runner.c @@ -35,6 +35,7 @@ extern Suite *restart_counter_suite(void); extern Suite *iomux_suite(void); extern Suite *iomux_signal_suite(void); extern Suite *string_suite(void); +extern Suite *ringbuf_suite(void); static suite_func suites[] = { bitvec_suite, @@ -65,6 +66,7 @@ static suite_func suites[] = { iomux_suite, iomux_signal_suite, string_suite + ringbuf_suite }; @@ -107,7 +109,7 @@ main (int argc, char *argv[]) size_t i; SRunner *sr = srunner_create (NULL); - if (xml_output) + if (xml_output) srunner_set_xml(sr, "result.xml"); //set the environment variable CK_FORK=no which means don't fork(). @@ -133,10 +135,10 @@ main (int argc, char *argv[]) number_run = srunner_ntests_run(sr); number_failed = srunner_ntests_failed (sr); srunner_free (sr); - + if (!number_run) puts("ERROR: no tests were run"); - if (number_failed) + if (number_failed) printf("ERROR: %d tests failed\n", number_failed); return (number_failed != 0) ; diff --git a/test/test_sat_math.c b/test/test_sat_math.c index da55529..1248919 100644 --- a/test/test_sat_math.c +++ b/test/test_sat_math.c @@ -3,6 +3,7 @@ START_TEST (test_sat_uc_addu8) +{ uint8_t a; uint8_t b; @@ -49,9 +50,11 @@ START_TEST (test_sat_uc_addu8) a = 100; b = 100; fail_if(uc_sat_addu8(a,b) != 200); +} END_TEST START_TEST (test_sat_uc_subu8) +{ uint8_t a; uint8_t b; @@ -82,9 +85,11 @@ START_TEST (test_sat_uc_subu8) a = 100; b = 100; fail_if(uc_sat_subu8(a,b) != 0); +} END_TEST START_TEST (test_sat_uc_multu8) +{ uint8_t a; uint8_t b; @@ -120,10 +125,12 @@ START_TEST (test_sat_uc_multu8) b = 10; fail_if(uc_sat_multu8(a,b) != 100); +} END_TEST START_TEST (test_sat_uc_addu16) +{ uint16_t a; uint16_t b; @@ -170,9 +177,11 @@ START_TEST (test_sat_uc_addu16) a = 100; b = 100; fail_if(uc_sat_addu16(a,b) != 200); +} END_TEST START_TEST (test_sat_uc_subu16) +{ uint16_t a; uint16_t b; @@ -203,9 +212,11 @@ START_TEST (test_sat_uc_subu16) a = 100; b = 100; fail_if(uc_sat_subu16(a,b) != 0); +} END_TEST START_TEST (test_sat_uc_multu16) +{ uint16_t a; uint16_t b; @@ -241,9 +252,11 @@ START_TEST (test_sat_uc_multu16) b = 10; fail_if(uc_sat_multu16(a,b) != 100); +} END_TEST START_TEST (test_sat_uc_addu32) +{ uint32_t a; uint32_t b; @@ -290,9 +303,11 @@ START_TEST (test_sat_uc_addu32) a = 0x1fffffff; b = 0x80000000; fail_if(uc_sat_addu32(a,b) != 0x9fffffff); +} END_TEST START_TEST (test_sat_uc_subu32) +{ uint32_t a; uint32_t b; @@ -323,9 +338,11 @@ START_TEST (test_sat_uc_subu32) a = 100; b = 100; fail_if(uc_sat_subu32(a,b) != 0); +} END_TEST START_TEST (test_sat_uc_multu32) +{ uint32_t a; uint32_t b; @@ -361,8 +378,10 @@ START_TEST (test_sat_uc_multu32) b = 10; fail_if(uc_sat_multu32(a,b) != 100); +} END_TEST START_TEST (test_sat_uc_addu64) +{ uint64_t a; uint64_t b; @@ -409,9 +428,11 @@ START_TEST (test_sat_uc_addu64) a = 0x1fffffffffffffffULL; b = 0x8000000000000000ULL; fail_if(uc_sat_addu64(a,b) != 0x9fffffffffffffffULL); +} END_TEST START_TEST (test_sat_uc_subu64) +{ uint64_t a; uint64_t b; @@ -442,9 +463,11 @@ START_TEST (test_sat_uc_subu64) a = 100; b = 100; fail_if(uc_sat_subu64(a,b) != 0); +} END_TEST START_TEST (test_sat_uc_multu64) +{ uint64_t a; uint64_t b; @@ -480,6 +503,7 @@ START_TEST (test_sat_uc_multu64) b = 10; fail_if(uc_sat_multu64(a,b) != 100); +} END_TEST Suite *sat_math_suite(void) { diff --git a/test/test_seq.c b/test/test_seq.c index 8fc068e..5f73ac3 100644 --- a/test/test_seq.c +++ b/test/test_seq.c @@ -3,6 +3,7 @@ START_TEST (test_uc_seq_lt) +{ fail_if(uc_seq_lt(0, 1) != 1); fail_if(uc_seq_lt(0x7fffffffU, 0x80000000U) != 1); fail_if(uc_seq_lt(0xfffffffeU, 2) != 1); @@ -15,9 +16,11 @@ START_TEST (test_uc_seq_lt) fail_if(uc_seq_lt(0, 0) != 0); fail_if(uc_seq_lt(1, 1) != 0); fail_if(uc_seq_lt(0xffffffffU, 0xffffffffU) != 0); +} END_TEST START_TEST (test_uc_seq_gt) +{ fail_if(uc_seq_gt(1, 0) != 1); fail_if(uc_seq_gt(0x80000000U,0x7fffffffU) != 1); fail_if(uc_seq_gt(2, 0xfffffffeU) != 1); @@ -30,9 +33,11 @@ START_TEST (test_uc_seq_gt) fail_if(uc_seq_gt(0, 0) != 0); fail_if(uc_seq_gt(1, 1) != 0); fail_if(uc_seq_gt(0xffffffffU, 0xffffffffU) != 0); +} END_TEST START_TEST (test_uc_seq_leq) +{ fail_if(uc_seq_leq(0, 1) != 1); fail_if(uc_seq_leq(0x7fffffffU, 0x80000000U) != 1); fail_if(uc_seq_leq(0xfffffffeU, 2) != 1); @@ -47,9 +52,11 @@ START_TEST (test_uc_seq_leq) fail_if(uc_seq_leq(0x80000000U,0x7fffffffU) != 0); fail_if(uc_seq_leq(2, 0xfffffffeU) != 0); fail_if(uc_seq_leq(0, 0xffffffffU) != 0); +} END_TEST START_TEST (test_uc_seq_geq) +{ fail_if(uc_seq_geq(1, 0) != 1); fail_if(uc_seq_geq(0x80000000U,0x7fffffffU) != 1); fail_if(uc_seq_geq(2, 0xfffffffeU) != 1); @@ -65,6 +72,7 @@ START_TEST (test_uc_seq_geq) fail_if(uc_seq_geq(0xfffffffeU, 2) != 0); fail_if(uc_seq_geq(0xffffffffU, 0) != 0); +} END_TEST diff --git a/test/test_sprintb.c b/test/test_sprintb.c index c6fef06..53f5b9d 100644 --- a/test/test_sprintb.c +++ b/test/test_sprintb.c @@ -9,6 +9,7 @@ // long long 64 bit START_TEST (test_sprintb) +{ char result[128]; char *rc; @@ -24,9 +25,11 @@ START_TEST (test_sprintb) rc = uc_sprintb(result, 1); ck_assert_str_eq(rc, "00000000000000000000000000000001"); +} END_TEST START_TEST (test_sprintbc) +{ char result[128]; char *rc; @@ -42,9 +45,11 @@ START_TEST (test_sprintbc) rc = uc_sprintbc(result, 1); ck_assert_str_eq(rc, "00000001"); +} END_TEST START_TEST (test_sprintbs) +{ char result[128]; char *rc; @@ -60,9 +65,11 @@ START_TEST (test_sprintbs) rc = uc_sprintbs(result, 1); ck_assert_str_eq(rc, "0000000000000001"); +} END_TEST START_TEST (test_sprintbll) +{ char result[128]; char *rc; @@ -78,6 +85,7 @@ START_TEST (test_sprintbll) rc = uc_sprintbll(result, 1); ck_assert_str_eq(rc, "0000000000000000000000000000000000000000000000000000000000000001"); +} END_TEST Suite *sprintb_suite(void) diff --git a/test/test_strv.c b/test/test_strv.c index 311665c..44a9976 100644 --- a/test/test_strv.c +++ b/test/test_strv.c @@ -5,6 +5,7 @@ START_TEST (test_strv_append) +{ struct UCStrv strv; int rc; @@ -22,9 +23,11 @@ START_TEST (test_strv_append) uc_strv_destroy(&strv); +} END_TEST START_TEST (test_strv_clear) +{ struct UCStrv strv; int rc; @@ -38,9 +41,11 @@ START_TEST (test_strv_clear) fail_if(strv.cnt != 0); uc_strv_destroy(&strv); +} END_TEST START_TEST (test_strv_nocopy) +{ struct UCStrv strv; char *str = strdup("string"); int rc; @@ -54,9 +59,11 @@ START_TEST (test_strv_nocopy) uc_strv_destroy(&strv); +} END_TEST START_TEST (test_strv_null_terminate) +{ struct UCStrv strv; int rc; @@ -73,9 +80,11 @@ START_TEST (test_strv_null_terminate) uc_strv_destroy(&strv); +} END_TEST START_TEST (test_strv_append_all_copy) +{ struct UCStrv src; struct UCStrv dst; int rc; @@ -97,9 +106,11 @@ START_TEST (test_strv_append_all_copy) uc_strv_destroy(&src); uc_strv_destroy(&dst); +} END_TEST START_TEST (test_strv_append_all_nocopy) +{ struct UCStrv src; struct UCStrv dst; int rc; @@ -121,6 +132,7 @@ START_TEST (test_strv_append_all_nocopy) uc_strv_destroy(&dst); free(src.strings); +} END_TEST START_TEST (test_strv_reverse) diff --git a/test/test_threadqueue.c b/test/test_threadqueue.c index a8d5db0..50754be 100644 --- a/test/test_threadqueue.c +++ b/test/test_threadqueue.c @@ -12,6 +12,7 @@ struct thr_msg { }; START_TEST (test_thread_queue_length) +{ struct uc_threadqueue queue; int i; @@ -26,9 +27,11 @@ START_TEST (test_thread_queue_length) fail_if(uc_thread_queue_length(&queue) != 51); +} END_TEST START_TEST (test_thread_queue_one_thread) +{ struct uc_threadqueue queue; int i; @@ -52,9 +55,11 @@ START_TEST (test_thread_queue_one_thread) } fail_if(uc_thread_queue_destroy(&queue, NULL, NULL) != 0); +} END_TEST START_TEST (test_thread_queue_priority_msg) +{ struct uc_threadqueue queue; int i; @@ -79,6 +84,7 @@ START_TEST (test_thread_queue_priority_msg) fail_if(uc_thread_queue_destroy(&queue, NULL, NULL) != 0); +} END_TEST @@ -94,6 +100,7 @@ static void test_thread_queue_walk_func(struct uc_threadmsg *msg, void *cookie) } START_TEST (test_thread_queue_walk) +{ struct uc_threadqueue queue; int i; @@ -111,6 +118,7 @@ START_TEST (test_thread_queue_walk) uc_thread_queue_walk(&queue, test_thread_queue_walk_func, NULL); +} END_TEST #define READER_WRITER_NUM_MSG 20000 @@ -133,6 +141,7 @@ static void *test_thread_queue_reader_writer_func(void *arg) } START_TEST (test_thread_queue_reader_writer) +{ struct uc_threadqueue queue; int i; @@ -154,9 +163,11 @@ START_TEST (test_thread_queue_reader_writer) fail_if(uc_thread_queue_destroy(&queue, NULL, NULL) != 0); +} END_TEST START_TEST (test_thread_queue_reader_writer_len_1) +{ struct uc_threadqueue queue; int i; @@ -176,14 +187,16 @@ START_TEST (test_thread_queue_reader_writer_len_1) pthread_join(tid, NULL); +} END_TEST START_TEST (test_thread_queue_zero_len) +{ struct uc_threadqueue queue; fail_if(uc_thread_queue_init(&queue, 0) == 0); //thread_queue_init should fail - +} END_TEST void *test_thread_queue_multiple_writers_func(void *arg) @@ -203,6 +216,7 @@ void *test_thread_queue_multiple_writers_func(void *arg) } START_TEST (test_thread_queue_multiple_writers) +{ struct uc_threadqueue queue; int i; @@ -232,9 +246,11 @@ START_TEST (test_thread_queue_multiple_writers) fail_if(count_max != 3); +} END_TEST START_TEST (test_thread_queue_timeout1) +{ struct uc_threadqueue queue; struct timespec timeout = {0, 1000}; @@ -245,6 +261,7 @@ START_TEST (test_thread_queue_timeout1) fail_if(uc_thread_queue_tryget(&queue, &timeout, &msg) != ETIMEDOUT); uc_thread_queue_destroy(&queue, NULL, NULL); +} END_TEST void *thread_test_thread_queue_timeout2(void *arg) @@ -264,6 +281,7 @@ void *thread_test_thread_queue_timeout2(void *arg) return NULL; } START_TEST (test_thread_queue_timeout2) +{ struct uc_threadqueue queue; pthread_t tid1; @@ -280,9 +298,12 @@ START_TEST (test_thread_queue_timeout2) uc_thread_queue_destroy(&queue, NULL, NULL); +} END_TEST START_TEST (test_thread_queue_tryadd_timeout_0_0) +{ + struct uc_threadqueue queue; int i; @@ -298,10 +319,12 @@ START_TEST (test_thread_queue_tryadd_timeout_0_0) fail_if(uc_thread_queue_tryadd(&queue, &timeout, &msg[2].msg) != ETIMEDOUT); fail_if(uc_thread_queue_destroy(&queue, NULL, NULL) != 0); - +} END_TEST START_TEST (test_thread_queue_tryadd_timeout_0_100) +{ + struct uc_threadqueue queue; int i; @@ -318,9 +341,11 @@ START_TEST (test_thread_queue_tryadd_timeout_0_100) fail_if(uc_thread_queue_destroy(&queue, NULL, NULL) != 0); +} END_TEST START_TEST (test_thread_queue_tryadd_no_timeout_0_100) +{ struct uc_threadqueue queue; int i; @@ -336,7 +361,7 @@ START_TEST (test_thread_queue_tryadd_no_timeout_0_100) fail_if(uc_thread_queue_tryadd(&queue, &timeout, &msg[2].msg) != 0); fail_if(uc_thread_queue_destroy(&queue, NULL, NULL) != 0); - +} END_TEST void clear_cb(struct uc_threadmsg *msg, void *cookie) @@ -347,6 +372,7 @@ void clear_cb(struct uc_threadmsg *msg, void *cookie) } START_TEST (test_thread_queue_clear) +{ struct uc_threadqueue queue; int i; @@ -365,7 +391,7 @@ START_TEST (test_thread_queue_clear) fail_if(uc_thread_queue_length(&queue) != 0); fail_if(uc_thread_queue_destroy(&queue, NULL, NULL) != 0); - +} END_TEST diff --git a/test/test_ticket_lock.c b/test/test_ticket_lock.c index 6287e5c..b7fd69a 100644 --- a/test/test_ticket_lock.c +++ b/test/test_ticket_lock.c @@ -29,6 +29,7 @@ static void *update_thread(void *arg) #define NR_THREADS 5 START_TEST(test_ticket_lock1) +{ struct ThreadArg threads[NR_THREADS]; int i; int lrc; @@ -53,6 +54,7 @@ START_TEST(test_ticket_lock1) ck_assert_int_eq((NR_THREADS * (NR_THREADS + 1)) / 2, g_counter); +} END_TEST Suite *ticket_lock_suite(void) diff --git a/test/test_timers.c b/test/test_timers.c index 36e5f31..88d4c19 100644 --- a/test/test_timers.c +++ b/test/test_timers.c @@ -43,6 +43,7 @@ static void common_init(struct UCoreSettableClock *clk, } START_TEST (test_timers_simple) +{ struct UCoreSettableClock clk; struct TimerTest tt; struct UCTimers timers; @@ -80,9 +81,11 @@ START_TEST (test_timers_simple) ck_assert_int_eq(uc_timers_count(&timers), 0); fail_if(uc_timer_running(&timer) != 0); +} END_TEST START_TEST (test_timers_add_from_callback) +{ struct UCoreSettableClock clk; struct TimerTest tt; struct UCTimers timers; @@ -114,9 +117,11 @@ START_TEST (test_timers_add_from_callback) fail_if(uc_timer_running(&timer) == 0); ck_assert_int_eq(uc_timers_count(&timers), 1); +} END_TEST START_TEST (test_timers_remove_from_callback) +{ struct UCoreSettableClock clk; struct TimerTest tt; struct UCTimers timers; @@ -158,9 +163,11 @@ START_TEST (test_timers_remove_from_callback) rc = uc_timers_first(&timers, &tv); fail_if(rc == 0); +} END_TEST START_TEST (test_timers_add_remove) +{ struct UCoreSettableClock clk; struct TimerTest tt; struct UCTimers timers; @@ -193,6 +200,7 @@ START_TEST (test_timers_add_remove) ck_assert_int_eq(uc_timers_count(&timers), 0); fail_if(uc_timers_first(&timers, &first) == 0); +} END_TEST diff --git a/test/test_val_str.c b/test/test_val_str.c index 4474482..c930a82 100644 --- a/test/test_val_str.c +++ b/test/test_val_str.c @@ -3,6 +3,7 @@ START_TEST (test_val_str_search) +{ struct UCValStr vals[] = { UC_VS_ENTRY(1), UC_VS_ENTRY(5), @@ -14,9 +15,11 @@ START_TEST (test_val_str_search) ck_assert_str_eq(uc_val_2_str(5 , vals) , "5"); fail_if(uc_val_2_str(10, vals) != NULL); +} END_TEST START_TEST (test_val_str_vs_search) +{ struct UCValStr vals[] = { UC_VS_ENTRY(1), UC_VS_ENTRY(5), @@ -28,9 +31,11 @@ START_TEST (test_val_str_vs_search) ck_assert_str_eq(uc_val_2_str_vs(5 , vals)->str , "5"); fail_if(uc_val_2_str(10, vals) != NULL); +} END_TEST START_TEST (test_val_str_bsearch_odd) +{ struct UCValStr vals[] = { UC_VS_ENTRY(1), UC_VS_ENTRY(2), @@ -49,9 +54,11 @@ START_TEST (test_val_str_bsearch_odd) fail_if(uc_val_2_str_bs(0 , vals , len) != NULL); fail_if(uc_val_2_str_bs(55 , vals , len) != NULL); fail_if(uc_val_2_str_bs(999 , vals , len) != NULL); +} END_TEST START_TEST (test_val_str_bsearch_even) +{ struct UCValStr vals[] = { UC_VS_ENTRY(1), UC_VS_ENTRY(2), @@ -68,9 +75,11 @@ START_TEST (test_val_str_bsearch_even) fail_if(uc_val_2_str_bs(0 , vals , len) != NULL); fail_if(uc_val_2_str_bs(55 , vals , len) != NULL); fail_if(uc_val_2_str_bs(999 , vals , len) != NULL); +} END_TEST START_TEST (test_str_str_search) +{ struct UCStrStr vals[] = { {"1", "A"}, {"2", "B"}, @@ -82,9 +91,11 @@ START_TEST (test_str_str_search) ck_assert_str_eq(uc_str_2_str("3" , vals) , "C"); fail_if(uc_str_2_str("10", vals) != NULL); +} END_TEST START_TEST (test_str_str_ci_search) +{ struct UCStrStr vals[] = { {"A", "1"}, {"az","2"}, @@ -96,6 +107,7 @@ START_TEST (test_str_str_ci_search) ck_assert_str_eq(uc_str_2_str_ci("C" , vals) , "3"); fail_if(uc_str_2_str_ci("aza", vals) != NULL); +} END_TEST Suite *val_str_suite(void) {