Initial import of libucore
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
import os
|
||||
Import('env', 'build_type', 'prefix', 'version_info', 'test_xml')
|
||||
|
||||
test_env = env.Clone()
|
||||
test_env.Append(LIBPATH = ['#build/src']);
|
||||
test_env.Append(CPPPATH = ['#src'])
|
||||
test_env.Append(LIBS = ['ucore_' + build_type, 'check'])
|
||||
|
||||
tests = test_env.Glob('test_*.c')
|
||||
|
||||
test_executable = 'test_runner'
|
||||
#run test_runner --xml to produce an xml result file
|
||||
if test_xml:
|
||||
test_executable += ' --xml'
|
||||
|
||||
prog = test_env.Program('test_runner', tests)
|
||||
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)
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include "iomux.h"
|
||||
|
||||
extern struct IOMuxFD s_fd;
|
||||
|
||||
void stdin_read_cb(struct IOMux *mux, struct IOMuxFD *fd, unsigned int event)
|
||||
{
|
||||
char buf[64];
|
||||
ssize_t len = read(fd->fd, buf, sizeof buf);
|
||||
printf("Read %d bytes\n", len);
|
||||
iomux_unregister_fd(mux, fd);
|
||||
if(len > 0) {
|
||||
s_fd.what = MUX_EV_READ;
|
||||
iomux_register_fd(mux, &s_fd);
|
||||
}
|
||||
}
|
||||
|
||||
struct IOMuxFD s_fd = {
|
||||
.fd = 0,
|
||||
.what = MUX_EV_READ,
|
||||
.callback = stdin_read_cb,
|
||||
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct IOMux *mux = iomux_create(IOMUX_TYPE_DEFAULT);
|
||||
iomux_register_fd(mux, &s_fd);
|
||||
iomux_run(mux);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#include "iomux.h"
|
||||
|
||||
extern struct IOMuxFD s_fd;
|
||||
|
||||
void stdin_read_cb(struct IOMux *mux, struct IOMuxFD *fd, unsigned int event)
|
||||
{
|
||||
char buf[4096];
|
||||
ssize_t len = read(fd->fd, buf, sizeof buf);
|
||||
if(len <= 0) {
|
||||
perror("read");
|
||||
fprintf(stderr,"Unregister stdin fd %d\n", fd->fd);
|
||||
iomux_unregister_fd(mux, fd);
|
||||
close(fd->fd);
|
||||
close(fd->cookie_int);
|
||||
}
|
||||
else
|
||||
write(fd->cookie_int, buf, len);
|
||||
}
|
||||
|
||||
void pipe_read_cb(struct IOMux *mux, struct IOMuxFD *fd, unsigned int event)
|
||||
{
|
||||
char buf[4095];
|
||||
ssize_t len = read(fd->fd, buf, sizeof buf);
|
||||
if(len <= 0) {
|
||||
perror("pipe read");
|
||||
printf("Unregister pipe fd %d [stdout]\n", fd->fd);
|
||||
fprintf(stderr,"Unregister pipe fd %d[stderr]\n", fd->fd);
|
||||
iomux_unregister_fd(mux, fd);
|
||||
close(fd->cookie_int);
|
||||
fclose(fd->cookie_ptr);
|
||||
|
||||
} else {
|
||||
fwrite(buf, 1 , len, fd->cookie_ptr);
|
||||
fflush(fd->cookie_ptr);
|
||||
}
|
||||
}
|
||||
|
||||
void *writer(void *arg)
|
||||
{
|
||||
struct IOMux *mux = arg;
|
||||
iomux_run(mux);
|
||||
printf("writer mux returned\n");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct IOMux *mux1 = iomux_create(IOMUX_TYPE_DEFAULT);
|
||||
struct IOMux *mux2 = iomux_create(IOMUX_TYPE_DEFAULT);
|
||||
int pfd[2];
|
||||
int rc;
|
||||
struct IOMuxFD fd1 = {0};
|
||||
struct IOMuxFD fd2 = {0};
|
||||
pthread_t tid;
|
||||
pipe(pfd);
|
||||
|
||||
fd1.fd = 0;
|
||||
fd1.what = MUX_EV_READ;
|
||||
fd1.cookie_int = pfd[1];
|
||||
fd1.callback = stdin_read_cb;
|
||||
|
||||
fd2.fd = pfd[0];
|
||||
fd2.what = MUX_EV_READ;
|
||||
fd2.cookie_ptr = fopen("out", "wb");
|
||||
fd2.callback = pipe_read_cb;
|
||||
|
||||
|
||||
rc = iomux_register_fd(mux1, &fd1);
|
||||
if(rc != 0)
|
||||
printf("Cannot register fd1: %s\n", strerror(rc));
|
||||
rc = iomux_register_fd(mux2, &fd2);
|
||||
if(rc != 0)
|
||||
printf("Cannot register fdw: %s\n", strerror(rc));
|
||||
pthread_create(&tid,NULL,writer,mux2);
|
||||
iomux_run(mux1);
|
||||
printf("reader mux returned\n");
|
||||
pthread_join(tid, NULL);
|
||||
iomux_delete(mux1);
|
||||
iomux_delete(mux2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include "iomux.h"
|
||||
|
||||
|
||||
#define BUFLEN (4098*66)
|
||||
|
||||
struct Copy {
|
||||
struct IOMuxFD in;
|
||||
struct IOMuxFD out;
|
||||
|
||||
char buffer[BUFLEN];
|
||||
size_t w_idx;
|
||||
size_t r_idx;
|
||||
int out_running;
|
||||
};
|
||||
|
||||
|
||||
void in_cb(struct IOMux *mux, struct IOMuxFD *fd, unsigned int event)
|
||||
{
|
||||
struct Copy *copy_ctx = fd->cookie_ptr;
|
||||
size_t left = BUFLEN - copy_ctx->w_idx;
|
||||
ssize_t rc;
|
||||
left /= 2;
|
||||
if(left == 0)
|
||||
left++;
|
||||
|
||||
|
||||
rc = read(fd->fd, ©_ctx->buffer[copy_ctx->w_idx], left);
|
||||
if(rc <= 0) {
|
||||
if(rc == -1 && errno == EAGAIN) {
|
||||
return;
|
||||
} else if(rc == -1) {
|
||||
perror("read");
|
||||
}
|
||||
iomux_unregister_fd(mux, fd);
|
||||
return;
|
||||
}
|
||||
|
||||
copy_ctx->w_idx += rc;
|
||||
|
||||
if(!copy_ctx->out_running) {
|
||||
copy_ctx->out.what = MUX_EV_WRITE;
|
||||
iomux_register_fd(mux, ©_ctx->out);
|
||||
copy_ctx->out_running = 1;
|
||||
}
|
||||
|
||||
if(copy_ctx->w_idx == BUFLEN) {
|
||||
iomux_unregister_fd(mux, fd);
|
||||
}
|
||||
}
|
||||
|
||||
void out_cb(struct IOMux *mux, struct IOMuxFD *fd, unsigned int event)
|
||||
{
|
||||
struct Copy *copy_ctx = fd->cookie_ptr;
|
||||
ssize_t rc;
|
||||
size_t len = copy_ctx->w_idx - copy_ctx->r_idx;
|
||||
|
||||
rc = write(1, ©_ctx->buffer[copy_ctx->r_idx], len);
|
||||
if(rc <= 0) {
|
||||
if(rc == -1 && errno == EAGAIN) {
|
||||
return;
|
||||
} else {
|
||||
perror("write");
|
||||
iomux_unregister_fd(mux, ©_ctx->in);
|
||||
}
|
||||
iomux_unregister_fd(mux, fd);
|
||||
copy_ctx->out_running = 0;
|
||||
} else {
|
||||
copy_ctx->r_idx += rc;
|
||||
if(copy_ctx->r_idx == copy_ctx->w_idx) {
|
||||
iomux_unregister_fd(mux, fd);
|
||||
copy_ctx->out_running = 0;
|
||||
if(copy_ctx->w_idx == BUFLEN) {
|
||||
copy_ctx->w_idx = 0;
|
||||
copy_ctx->r_idx = 0;
|
||||
iomux_register_fd(mux, ©_ctx->in);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int set_nonblocking(int fd)
|
||||
{
|
||||
int flags = fcntl(fd,F_GETFL,NULL);
|
||||
if(flags < 0 ) {
|
||||
return flags;
|
||||
}
|
||||
return fcntl(fd,F_SETFL,flags | O_NONBLOCK);
|
||||
}
|
||||
|
||||
void timer_cb(struct UCTimers *timers, struct UCTimer *timer)
|
||||
{
|
||||
fprintf(stderr, "Timer %d fired\n", timer->cookie_int);
|
||||
struct UCTimer *t = calloc(1, sizeof *t);
|
||||
t->callback = timer_cb;
|
||||
t->cookie_int = timer->cookie_int + 1;
|
||||
uc_timer_add(timers, t, 5,5);
|
||||
t = calloc(1, sizeof *t);
|
||||
t->callback = timer_cb;
|
||||
t->cookie_int = timer->cookie_int + 2;
|
||||
uc_timer_add(timers, t, 5,4);
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct Copy copy_ctx = {
|
||||
.in = {
|
||||
.fd = 0,
|
||||
.what = MUX_EV_READ,
|
||||
.callback = in_cb,
|
||||
.cookie_ptr = ©_ctx,
|
||||
},
|
||||
.out = {
|
||||
.fd = 1,
|
||||
.callback = out_cb,
|
||||
.cookie_ptr = ©_ctx,
|
||||
}
|
||||
};
|
||||
struct UCTimer timer = {
|
||||
.callback = timer_cb,
|
||||
};
|
||||
set_nonblocking(0);
|
||||
set_nonblocking(1);
|
||||
|
||||
struct IOMux *mux = iomux_create(IOMUX_TYPE_EPOLL);
|
||||
iomux_register_fd(mux, ©_ctx.in);
|
||||
uc_timer_add(iomux_get_timers(mux), &timer, 5, 0);
|
||||
|
||||
iomux_run(mux);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
#include "ucore_logging.h"
|
||||
#include "ucore_utils.h"
|
||||
#include <syslog.h>
|
||||
enum {
|
||||
MAIN,
|
||||
CC,
|
||||
HO
|
||||
};
|
||||
struct uc_log_module module_arr[] = {
|
||||
{
|
||||
MAIN,
|
||||
"MAIN",
|
||||
"Main module",
|
||||
UC_LL_WARNING
|
||||
},
|
||||
{
|
||||
CC,
|
||||
"CC",
|
||||
"Call Control module",
|
||||
UC_LL_DEBUG,
|
||||
},
|
||||
{
|
||||
HO,
|
||||
"HO",
|
||||
"Handover module",
|
||||
UC_LL_DEBUG
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
struct uc_log_modules modules = {
|
||||
.mods = module_arr,
|
||||
.cnt = ARRAY_SIZE(module_arr),
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct uc_log_destination *dest = uc_log_new_stderr(UC_LL_INFO, 0);
|
||||
uc_log_init(&modules);
|
||||
uc_log_add_destination(dest);
|
||||
dest = uc_log_new_file("test.log", UC_LL_INFO, 1);
|
||||
uc_log_add_destination(dest);
|
||||
dest = uc_log_new_syslog("log.test", LOG_DAEMON, UC_LL_INFO, 0);
|
||||
uc_log_add_destination(dest);
|
||||
UC_LOGF( UC_LL_INFO, CC, "Test cc info %d\n", 2);
|
||||
UC_LOGF( UC_LL_WARNING, HO, "Test HO warning%d\n", 2);
|
||||
UC_LOGF( UC_LL_INFO, MAIN, "Test MAIN info \n" );
|
||||
UC_LOGF( UC_LL_WARNING, MAIN, "Test MAIN warning\n" );
|
||||
uc_log_module_set_loglevel(MAIN, UC_LL_INFO);
|
||||
UC_LOGF( UC_LL_INFO, MAIN, "Test MAIN warning 2\n" );
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#include <string.h>
|
||||
#include "ucore_salloc.h"
|
||||
|
||||
// Intended to be run under valgrind. Should show no errors
|
||||
|
||||
|
||||
int main(int argc, char *argv)
|
||||
{
|
||||
size_t i;
|
||||
SAlloc *s = uc_new_salloc(36 * 100);
|
||||
for(i = 0; i < 100; i++) {
|
||||
void *p = uc_s_alloc(s, 36);
|
||||
memset(p,7,36);
|
||||
}
|
||||
|
||||
uc_free_salloc(s);
|
||||
|
||||
s = uc_new_salloc(85 * 100);
|
||||
for(i = 0; i < 100 * 3; i++) {
|
||||
void *p = uc_s_alloc(s, 85);
|
||||
memset(p,7,85);
|
||||
}
|
||||
|
||||
uc_free_salloc(s);
|
||||
|
||||
return 0;
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
#include <stdio.h>
|
||||
#include "ucore_seq.h"
|
||||
|
||||
|
||||
void check_before(uint32_t a, uint32_t b)
|
||||
{
|
||||
printf("%lu before %lu = %d\n", a ,b, seq_before(a, b));
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
check_before(0, 1);
|
||||
check_before(1, 0);
|
||||
check_before(0, 0xffffffff);
|
||||
check_before(0, 0xffff);
|
||||
check_before(0, 0x1ffff);
|
||||
check_before(0xffff, 0xffffffff);
|
||||
check_before(0x7fffffffU - 1, 0xffffffff);
|
||||
check_before(0x7fffffffU + 1, 0xffffffff);
|
||||
check_before(0x7fffffffU + 1, 0x7fffffffU);
|
||||
check_before(0x7fffffffU - 1, 0x7fffffffU);
|
||||
check_before(0x7fffffffU, 0x7fffffffU - 1);
|
||||
check_before(0x7fffffffU, 0x7fffffffU + 1);
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
+151
@@ -0,0 +1,151 @@
|
||||
#include <check.h>
|
||||
#include "ucore_string.h"
|
||||
|
||||
|
||||
START_TEST (test_2bcd_1234567890)
|
||||
char ascii[] = "1234567890";
|
||||
unsigned char bcd[5];
|
||||
size_t len = uc_ascii2bcd(ascii, bcd, 0);
|
||||
|
||||
fail_if(len != 5, "len is %zu", len);
|
||||
fail_if(bcd[0] != 0x21,"was 0x%x",bcd[0]);
|
||||
fail_if(bcd[1] != 0x43,"was 0x%x",bcd[1]);
|
||||
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);
|
||||
|
||||
fail_if(len != 2, "len is %zu", len);
|
||||
fail_if(bcd[0] != 0x21,"was 0x%x",bcd[0]);
|
||||
fail_if(bcd[1] != 0xf3,"was 0x%x",bcd[1]);
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_2bcd_3_filler)
|
||||
|
||||
char ascii[] = "123";
|
||||
unsigned char bcd[2];
|
||||
size_t len = uc_ascii2bcd(ascii, bcd, 0x0);
|
||||
|
||||
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];
|
||||
size_t len = uc_ascii2bcd(ascii, bcd, 0xf);
|
||||
|
||||
fail_if(len != 5, "len is %zu", len);
|
||||
fail_if(bcd[0] != 0x00,"was 0x%x",bcd[0]);
|
||||
fail_if(bcd[1] != 0x74,"was 0x%x",bcd[1]);
|
||||
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];
|
||||
size_t len = uc_ascii2bcd(ascii, bcd, 0xf);
|
||||
|
||||
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];
|
||||
size_t len = uc_bcd2ascii(bcd, sizeof bcd, ascii);
|
||||
|
||||
fail_if(len != 4, "len is %zu", len);
|
||||
fail_if(ascii[0] != '1',"0 was %c",ascii[0]);
|
||||
fail_if(ascii[1] != '2',"1 was %c",ascii[1]);
|
||||
fail_if(ascii[2] != '3',"2 was %c",ascii[2]);
|
||||
fail_if(ascii[3] != '4',"3 was %c",ascii[3]);
|
||||
fail_if(ascii[4] != 0, "4 was %c",ascii[4]);
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_2hex_2)
|
||||
|
||||
unsigned char bcd[] = {0xF0, 0xFF, 0x00};
|
||||
char ascii[sizeof bcd * 2 + 1];
|
||||
size_t len = uc_bcd2ascii(bcd, sizeof bcd, ascii);
|
||||
|
||||
fail_if(len != 6, "len is %zu", len);
|
||||
fail_if(ascii[0] != '0',"0 was %c",ascii[0]);
|
||||
fail_if(ascii[1] != 'F',"1 was %c",ascii[1]);
|
||||
fail_if(ascii[2] != 'F',"2 was %c",ascii[2]);
|
||||
fail_if(ascii[3] != 'F',"3 was %c",ascii[3]);
|
||||
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];
|
||||
ascii[0] = 0xFF;
|
||||
size_t len = uc_bcd2ascii(bcd, 0, ascii);
|
||||
|
||||
fail_if(len != 0, "len is %zu", len);
|
||||
fail_if(ascii[0] != 0, "0 was %c",ascii[0]);
|
||||
END_TEST
|
||||
|
||||
Suite *bcd_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("bcd");
|
||||
TCase *tc = tcase_create("bcd tests");
|
||||
tcase_add_test(tc, test_2bcd_1234567890);
|
||||
tcase_add_test(tc, test_2bcd_empty);
|
||||
tcase_add_test(tc, test_2bcd_1_filler);
|
||||
tcase_add_test(tc, test_2bcd_2_filler);
|
||||
tcase_add_test(tc, test_2bcd_3_filler);
|
||||
tcase_add_test(tc, test_2bcd_non_digits);
|
||||
tcase_add_test(tc, test_2bcd_phoneno);
|
||||
tcase_add_test(tc, test_2bcd_binary);
|
||||
|
||||
tcase_add_test(tc, test_2hex_1);
|
||||
tcase_add_test(tc, test_2hex_2);
|
||||
tcase_add_test(tc, test_2hex_empty);
|
||||
|
||||
suite_add_tcase(s, tc);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
#include <check.h>
|
||||
#include "ucore_bitvec.h"
|
||||
|
||||
|
||||
START_TEST (test_bitvec_1)
|
||||
unsigned long s[3] = {0};
|
||||
struct bitvec v = BITVEC_STATIC_INIT(s);
|
||||
size_t i;
|
||||
|
||||
for(i = 0; i < sizeof s * 8; i++)
|
||||
fail_if(get_bit(&v, i), "bit %zu is not 0", i);
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_bitvec_2)
|
||||
unsigned long s[3] = {0};
|
||||
struct bitvec v = BITVEC_STATIC_INIT(s);
|
||||
size_t i;
|
||||
|
||||
for(i = 0; i < sizeof s * 8; i++)
|
||||
set_bit(&v, i);
|
||||
|
||||
for(i = 0; i < sizeof s * 8; i++)
|
||||
fail_if(get_bit(&v, i) != 1 , "bit %zu is not 1", i);
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_bitvec_clearbit)
|
||||
unsigned long s[3] = {-1, -1, -1};
|
||||
struct bitvec v = BITVEC_STATIC_INIT(s);
|
||||
size_t i;
|
||||
|
||||
for(i = 0; i < sizeof(unsigned long) * 8; i++)
|
||||
fail_if(get_bit(&v, i) != 1 , "bit %zu is not 1", i);
|
||||
|
||||
for(i = 0; i < sizeof(unsigned long) * 8; i++)
|
||||
clear_bit(&v, i);
|
||||
|
||||
for(i = 0; i < sizeof(unsigned long) * 8; i++)
|
||||
fail_if(get_bit(&v, i) != 0 , "bit %zu is not 0", i);
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_bitvec_set_bits_from_array)
|
||||
struct bitvec *v = bitvec_new(5);
|
||||
char a[] = {1, 0,1 ,0, 1};
|
||||
|
||||
set_bits_from_array(v,a , 5);
|
||||
fail_if(get_bit(v, 0) != 1 , "bit 0 is wrong");
|
||||
fail_if(get_bit(v, 1) != 0 , "bit 1 is wrong");
|
||||
fail_if(get_bit(v, 2) != 1 , "bit 2 is wrong");
|
||||
fail_if(get_bit(v, 3) != 0 , "bit 3 is wrong");
|
||||
fail_if(get_bit(v, 4) != 1 , "bit 4 is wrong");
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST (test_bitvec_setall_clearall)
|
||||
struct bitvec *v = bitvec_new(511);
|
||||
size_t i;
|
||||
|
||||
set_all(v);
|
||||
|
||||
for(i = 0; i < 511; i++)
|
||||
fail_if(get_bit(v, i) != 1 , "bit %zu is not 1", i);
|
||||
|
||||
clear_all(v);
|
||||
|
||||
for(i = 0; i < sizeof(unsigned long) * 8; i++)
|
||||
fail_if(get_bit(v, i) != 0 , "bit %zu is not 0", i);
|
||||
|
||||
END_TEST
|
||||
|
||||
Suite *bitvec_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("bitvec");
|
||||
TCase *tc = tcase_create("bitvec tests");
|
||||
tcase_add_test(tc, test_bitvec_1);
|
||||
tcase_add_test(tc, test_bitvec_2);
|
||||
tcase_add_test(tc, test_bitvec_set_bits_from_array);
|
||||
tcase_add_test(tc, test_bitvec_clearbit);
|
||||
tcase_add_test(tc, test_bitvec_setall_clearall);
|
||||
suite_add_tcase(s, tc);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
#include <check.h>
|
||||
#include "ucore_utils.h"
|
||||
|
||||
|
||||
struct Inner {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
struct Outer {
|
||||
int tag1;
|
||||
int tag2;
|
||||
struct Inner inner;
|
||||
int tag3;
|
||||
};
|
||||
|
||||
START_TEST (test_container_of)
|
||||
struct Outer outer = {
|
||||
.tag1 = 1,
|
||||
.tag2 = 2,
|
||||
.inner = {
|
||||
.x = 1234,
|
||||
.y = 5678
|
||||
}
|
||||
};
|
||||
|
||||
struct Inner *innerp = &outer.inner;
|
||||
struct Outer *outerp = CONTAINER_OF(innerp, struct Outer, inner);
|
||||
|
||||
fail_if(outerp != &outer);
|
||||
|
||||
END_TEST
|
||||
|
||||
Suite *container_of_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("container_of");
|
||||
TCase *tc = tcase_create("container_of tests");
|
||||
tcase_add_test(tc, test_container_of);
|
||||
|
||||
suite_add_tcase(s, tc);
|
||||
|
||||
return s;
|
||||
}
|
||||
+193
@@ -0,0 +1,193 @@
|
||||
#include <check.h>
|
||||
#include <string.h>
|
||||
#include "ucore_string.h"
|
||||
|
||||
|
||||
START_TEST (test_uc_hex_encode_1)
|
||||
uint8_t binary[] = {0xff};
|
||||
char hex[3] = "";
|
||||
|
||||
uc_hex_encode(binary, 1, hex);
|
||||
|
||||
fail_if(strcmp(hex,"FF") != 0);
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_uc_hex_encode_2)
|
||||
uint8_t binary[] = {127, 128, 129};
|
||||
char hex[4] = "";
|
||||
|
||||
uc_hex_encode(binary, sizeof binary, hex);
|
||||
|
||||
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[5] = "";
|
||||
|
||||
uc_hex_encode(binary, sizeof binary, hex);
|
||||
|
||||
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";
|
||||
|
||||
uc_hex_encode(binary, 0, hex);
|
||||
|
||||
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;
|
||||
|
||||
res = uc_hex_decode(hex, 2, binary);
|
||||
|
||||
fail_if(binary[0] != 0x1f);
|
||||
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;
|
||||
|
||||
res = uc_hex_decode(hex, 6, binary);
|
||||
|
||||
fail_if(binary[0] != 0x00);
|
||||
fail_if(binary[1] != 0xFF);
|
||||
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;
|
||||
|
||||
res = uc_hex_decode(hex, 0, binary);
|
||||
|
||||
fail_if(binary[0] != 0x11);
|
||||
fail_if(binary[1] != 0x11);
|
||||
fail_if(binary[2] != 0x11);
|
||||
fail_if(res != binary);
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_uc_hex_decode_empty_string)
|
||||
char hex[] = "";
|
||||
uint8_t binary[4] = {0x11, 0x11, 0x11};
|
||||
uint8_t *res;
|
||||
|
||||
res = uc_hex_decode(hex, 2, binary);
|
||||
|
||||
fail_if(binary[0] != 0x11);
|
||||
fail_if(binary[1] != 0x11);
|
||||
fail_if(binary[2] != 0x11);
|
||||
fail_if(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;
|
||||
|
||||
res = uc_hex_decode(hex, 3, binary);
|
||||
|
||||
fail_if(binary[0] != 0x33);
|
||||
fail_if(binary[1] != 0x11);
|
||||
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;
|
||||
|
||||
res = uc_hex_decode(hex, 3, binary);
|
||||
|
||||
fail_if(binary[0] != 0x11);
|
||||
fail_if(binary[1] != 0x11);
|
||||
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;
|
||||
|
||||
res = uc_hex_encode_delim(binary, sizeof binary, hex, sizeof hex, " ");
|
||||
|
||||
fail_if(strcmp(hex, "FF 00 7F 0A ") != 0, "hex was '%s'", hex);
|
||||
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;
|
||||
|
||||
res = uc_hex_encode_delim(binary, sizeof binary, hex, sizeof hex, " \n");
|
||||
|
||||
fail_if(strcmp(hex, "FF \n00 ") != 0, "hex was '%s'", hex);
|
||||
fail_if(res != hex + 8);
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_uc_hex_encode_delim_zero_len)
|
||||
uint8_t binary[] = {0xFF, 0x00};
|
||||
char hex[8] = "......";
|
||||
char *res;
|
||||
|
||||
res = uc_hex_encode_delim(binary, 0, hex, sizeof hex, " \n");
|
||||
|
||||
fail_if(strcmp(hex, "") != 0, "hex was '%s'", hex);
|
||||
fail_if(res != hex);
|
||||
|
||||
END_TEST
|
||||
|
||||
|
||||
Suite *hex_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("hex");
|
||||
TCase *tc = tcase_create("hex tests");
|
||||
tcase_add_test(tc, test_uc_hex_encode_1);
|
||||
tcase_add_test(tc, test_uc_hex_encode_2);
|
||||
tcase_add_test(tc, test_uc_hex_encode_3);
|
||||
tcase_add_test(tc, test_uc_hex_encode_zero_len);
|
||||
|
||||
tcase_add_test(tc, test_uc_hex_encode_delim_1);
|
||||
tcase_add_test(tc, test_uc_hex_encode_delim_2);
|
||||
tcase_add_test(tc, test_uc_hex_encode_delim_zero_len);
|
||||
|
||||
|
||||
tcase_add_test(tc, test_uc_hex_decode_1);
|
||||
tcase_add_test(tc, test_uc_hex_decode_2);
|
||||
tcase_add_test(tc, test_uc_hex_decode_zero_length);
|
||||
tcase_add_test(tc, test_uc_hex_decode_odd_length);
|
||||
tcase_add_test(tc, test_uc_hex_decode_invalid);
|
||||
tcase_add_test(tc, test_uc_hex_decode_empty_string);
|
||||
|
||||
|
||||
suite_add_tcase(s, tc);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,403 @@
|
||||
#include <check.h>
|
||||
#include "ucore_pack.h"
|
||||
|
||||
|
||||
START_TEST (test_pack16_le_1)
|
||||
uint16_t v = 0x1234;
|
||||
uint8_t r[2];
|
||||
|
||||
uc_pack_16_le(v,r);
|
||||
|
||||
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];
|
||||
|
||||
uc_pack_16_le(v,r);
|
||||
|
||||
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];
|
||||
|
||||
uc_pack_16_be(v,r);
|
||||
|
||||
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];
|
||||
|
||||
uc_pack_16_be(v, r);
|
||||
|
||||
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];
|
||||
|
||||
uc_pack_24_le(v,r);
|
||||
|
||||
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];
|
||||
|
||||
uc_pack_24_le(v,r);
|
||||
|
||||
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];
|
||||
|
||||
uc_pack_24_be(v,r);
|
||||
|
||||
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];
|
||||
|
||||
uc_pack_24_be(v,r);
|
||||
|
||||
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];
|
||||
|
||||
uc_pack_32_le(v,r);
|
||||
|
||||
fail_if(r[0] != 0x78,"was 0x%x",r[0]);
|
||||
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];
|
||||
|
||||
uc_pack_32_le(v,r);
|
||||
|
||||
fail_if(r[0] != 0xF4,"was 0x%x",r[0]);
|
||||
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];
|
||||
|
||||
uc_pack_32_be(v,r);
|
||||
|
||||
fail_if(r[3] != 0x78,"was 0x%x",r[3]);
|
||||
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];
|
||||
|
||||
uc_pack_32_be(v,r);
|
||||
|
||||
fail_if(r[3] != 0xF4,"was 0x%x",r[3]);
|
||||
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] = {};
|
||||
|
||||
uc_pack_64_le(v, r);
|
||||
|
||||
fail_if(r[0] != 0x88,"was 0x%x",r[0]);
|
||||
fail_if(r[1] != 0x77,"was 0x%x",r[1]);
|
||||
fail_if(r[2] != 0x66,"was 0x%x",r[2]);
|
||||
fail_if(r[3] != 0x55,"was 0x%x",r[3]);
|
||||
fail_if(r[4] != 0x44,"was 0x%x",r[4]);
|
||||
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] = {};
|
||||
|
||||
uc_pack_64_le(v, r);
|
||||
|
||||
fail_if(r[0] != 0xF8,"was 0x%x",r[0]);
|
||||
fail_if(r[1] != 0xF9,"was 0x%x",r[1]);
|
||||
fail_if(r[2] != 0xFA,"was 0x%x",r[2]);
|
||||
fail_if(r[3] != 0xFB,"was 0x%x",r[3]);
|
||||
fail_if(r[4] != 0xFC,"was 0x%x",r[4]);
|
||||
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] = {};
|
||||
|
||||
uc_pack_64_be(v, r);
|
||||
|
||||
fail_if(r[0] != 0x11,"was 0x%x",r[0]);
|
||||
fail_if(r[1] != 0x22,"was 0x%x",r[1]);
|
||||
fail_if(r[2] != 0x33,"was 0x%x",r[2]);
|
||||
fail_if(r[3] != 0x44,"was 0x%x",r[3]);
|
||||
fail_if(r[4] != 0x55,"was 0x%x",r[4]);
|
||||
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] = {};
|
||||
|
||||
uc_pack_64_be(v, r);
|
||||
|
||||
fail_if(r[0] != 0xFF,"was 0x%x",r[0]);
|
||||
fail_if(r[1] != 0xFE,"was 0x%x",r[1]);
|
||||
fail_if(r[2] != 0xFD,"was 0x%x",r[2]);
|
||||
fail_if(r[3] != 0xFC,"was 0x%x",r[3]);
|
||||
fail_if(r[4] != 0xFB,"was 0x%x",r[4]);
|
||||
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)
|
||||
{
|
||||
Suite *s = suite_create("pack");
|
||||
TCase *tc = tcase_create("pack tests");
|
||||
|
||||
tcase_add_test(tc, test_pack16_le_1);
|
||||
tcase_add_test(tc, test_pack16_le_2);
|
||||
|
||||
tcase_add_test(tc, test_pack16_be_1);
|
||||
tcase_add_test(tc, test_pack16_be_2);
|
||||
|
||||
tcase_add_test(tc, test_unpack16_le_1);
|
||||
tcase_add_test(tc, test_unpack16_le_2);
|
||||
|
||||
tcase_add_test(tc, test_unpack16_be_1);
|
||||
tcase_add_test(tc, test_unpack16_be_2);
|
||||
|
||||
tcase_add_test(tc, test_pack24_le_1);
|
||||
tcase_add_test(tc, test_pack24_le_2);
|
||||
|
||||
tcase_add_test(tc, test_pack24_be_1);
|
||||
tcase_add_test(tc, test_pack24_be_2);
|
||||
|
||||
tcase_add_test(tc, test_unpack24_le_1);
|
||||
tcase_add_test(tc, test_unpack24_le_2);
|
||||
|
||||
tcase_add_test(tc, test_unpack24_be_1);
|
||||
tcase_add_test(tc, test_unpack24_be_2);
|
||||
|
||||
tcase_add_test(tc, test_pack32_le_1);
|
||||
tcase_add_test(tc, test_pack32_le_2);
|
||||
|
||||
tcase_add_test(tc, test_pack32_be_1);
|
||||
tcase_add_test(tc, test_pack32_be_2);
|
||||
|
||||
tcase_add_test(tc, test_unpack32_le_1);
|
||||
tcase_add_test(tc, test_unpack32_le_2);
|
||||
|
||||
tcase_add_test(tc, test_unpack32_be_1);
|
||||
tcase_add_test(tc, test_unpack32_be_2);
|
||||
|
||||
tcase_add_test(tc, test_pack64_le_1);
|
||||
tcase_add_test(tc, test_pack64_le_2);
|
||||
|
||||
tcase_add_test(tc, test_unpack64_le_1);
|
||||
tcase_add_test(tc, test_unpack64_le_2);
|
||||
|
||||
tcase_add_test(tc, test_pack64_be_1);
|
||||
tcase_add_test(tc, test_pack64_be_2);
|
||||
|
||||
tcase_add_test(tc, test_unpack64_be_1);
|
||||
tcase_add_test(tc, test_unpack64_be_2);
|
||||
|
||||
suite_add_tcase(s, tc);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
#include <check.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include "ucore_read_file.h"
|
||||
|
||||
|
||||
START_TEST (test_read_file_non_existing_file)
|
||||
char *content;
|
||||
size_t len;
|
||||
int saved_errno;
|
||||
|
||||
content = uc_read_file("non existing file 123765", &len, 1000000000);
|
||||
saved_errno = errno;
|
||||
|
||||
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;
|
||||
ssize_t rc;
|
||||
|
||||
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0664);
|
||||
fail_if(fd == -1);
|
||||
|
||||
rc = write(fd, "hello", 5);
|
||||
fail_if(rc != 5, "rc was %ld", (long)rc);
|
||||
|
||||
close(fd);
|
||||
|
||||
content = uc_read_file(filename, &len, 1000000000);
|
||||
unlink(filename);
|
||||
|
||||
fail_if(content == NULL);
|
||||
fail_if(len != 5);
|
||||
fail_if(strlen(content) != 5);
|
||||
fail_if(strcmp("hello", content) != 0);
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_read_file_greater_than_max)
|
||||
char *content;
|
||||
const char *filename = "test_read_file_simple";
|
||||
size_t len;
|
||||
ssize_t rc;
|
||||
int saved_errno;
|
||||
|
||||
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0664);
|
||||
fail_if(fd == -1);
|
||||
|
||||
rc = write(fd, "hello", 5);
|
||||
fail_if(rc != 5, "rc was %ld", (long)rc);
|
||||
|
||||
close(fd);
|
||||
|
||||
content = uc_read_file(filename, &len, 4);
|
||||
saved_errno = errno;
|
||||
unlink(filename);
|
||||
|
||||
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;
|
||||
ssize_t rc;
|
||||
char buf[1030] = "";
|
||||
strcpy(&buf[1022], "foobar");
|
||||
|
||||
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0664);
|
||||
fail_if(fd == -1);
|
||||
|
||||
rc = write(fd, buf, sizeof buf);
|
||||
fail_if(rc != sizeof buf, "rc was %ld", (long)rc);
|
||||
|
||||
close(fd);
|
||||
|
||||
content = uc_read_file(filename, &len, 1000000000);
|
||||
unlink(filename);
|
||||
|
||||
fail_if(content == NULL);
|
||||
fail_if(len != sizeof buf);
|
||||
fail_if(strcmp("foobar", &content[1022]) != 0);
|
||||
free(content);
|
||||
END_TEST
|
||||
|
||||
Suite *read_file_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("read_file");
|
||||
TCase *tc = tcase_create("read_file tests");
|
||||
tcase_add_test(tc, test_read_file_non_existing_file);
|
||||
tcase_add_test(tc, test_read_file_simple);
|
||||
tcase_add_test(tc, test_read_file_greater_than_max);
|
||||
tcase_add_test(tc, test_read_file_boundary);
|
||||
|
||||
suite_add_tcase(s, tc);
|
||||
|
||||
return s;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#include <stdlib.h>
|
||||
#include <getopt.h>
|
||||
#include <stdio.h>
|
||||
#include <check.h>
|
||||
|
||||
extern Suite *bitvec_suite(void);
|
||||
extern Suite *bcd_suite(void);
|
||||
extern Suite *hex_suite(void);
|
||||
extern Suite *container_of_suite(void);
|
||||
extern Suite *read_file_suite(void);
|
||||
extern Suite *pack_suite(void);
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
int xml_output = argc > 1 && strcmp(argv[1], "--xml") == 0;
|
||||
|
||||
int number_failed = 0;
|
||||
|
||||
SRunner *sr = srunner_create (NULL);
|
||||
if(xml_output)
|
||||
srunner_set_xml(sr, "result.xml");
|
||||
|
||||
srunner_add_suite(sr, bitvec_suite());
|
||||
srunner_add_suite(sr, bcd_suite());
|
||||
srunner_add_suite(sr, hex_suite());
|
||||
srunner_add_suite(sr, container_of_suite());
|
||||
srunner_add_suite(sr, read_file_suite());
|
||||
srunner_add_suite(sr, pack_suite());
|
||||
|
||||
|
||||
srunner_run_all (sr, CK_VERBOSE);
|
||||
number_failed = srunner_ntests_failed (sr);
|
||||
srunner_free (sr);
|
||||
|
||||
if(number_failed)
|
||||
printf("ERROR: %d tests failed\n", number_failed);
|
||||
|
||||
return (number_failed != 0) ;
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "tree.h"
|
||||
|
||||
struct descriptor {
|
||||
int fd;
|
||||
SPLAY_ENTRY(descriptor) entry;
|
||||
};
|
||||
|
||||
int descriptor_cmp(struct descriptor *a, struct descriptor *b);
|
||||
|
||||
SPLAY_HEAD(descriptors, descriptor);
|
||||
SPLAY_PROTOTYPE(descriptors, descriptor, entry, descriptor_cmp);
|
||||
SPLAY_GENERATE(descriptors, descriptor, entry, descriptor_cmp);
|
||||
|
||||
struct descriptors entries;
|
||||
|
||||
int descriptor_cmp(struct descriptor *a, struct descriptor *b)
|
||||
{
|
||||
if(a->fd < b->fd)
|
||||
return -1;
|
||||
if(a->fd > b->fd)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void add(int fd)
|
||||
{
|
||||
struct descriptor *des = malloc(sizeof *des);
|
||||
des->fd = fd;
|
||||
SPLAY_INSERT(descriptors, &entries, des);
|
||||
}
|
||||
|
||||
|
||||
void find(int fd)
|
||||
{
|
||||
struct descriptor des;
|
||||
struct descriptor *found;
|
||||
|
||||
des.fd = fd;
|
||||
found = SPLAY_FIND(descriptors, &entries, &des);
|
||||
|
||||
if(found == NULL)
|
||||
printf("Not found fd %d\n", fd);
|
||||
else
|
||||
printf("Found fd %d (%d)\n", found->fd, fd);
|
||||
}
|
||||
|
||||
void rem(int fd)
|
||||
{
|
||||
struct descriptor des;
|
||||
|
||||
des.fd = fd;
|
||||
SPLAY_REMOVE(descriptors, &entries, &des);
|
||||
}
|
||||
|
||||
void iter(void){
|
||||
struct descriptor *bd;
|
||||
SPLAY_FOREACH(bd, descriptors, &entries) {
|
||||
printf("foreach fd %d\n", bd->fd);
|
||||
}
|
||||
}
|
||||
|
||||
void rem_iter(void){
|
||||
struct descriptor *bd;
|
||||
|
||||
while (!SPLAY_EMPTY(&entries)) {
|
||||
bd = SPLAY_ROOT(&entries);
|
||||
SPLAY_REMOVE(descriptors, &entries, bd);
|
||||
printf("removed fd %d\n", bd->fd);
|
||||
free(bd);
|
||||
}
|
||||
}
|
||||
|
||||
void fd_min_max(void)
|
||||
{
|
||||
struct descriptor *min_des, *max_des;
|
||||
|
||||
min_des = SPLAY_MIN(descriptors, &entries);
|
||||
max_des = SPLAY_MAX(descriptors, &entries);
|
||||
|
||||
if(min_des && max_des)
|
||||
printf("min fd = %d max fd = %d\n", min_des->fd, max_des->fd);
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
SPLAY_INIT(&entries);
|
||||
add(3);
|
||||
add(4);
|
||||
add(5);
|
||||
add(11);
|
||||
add(64);
|
||||
add(9);
|
||||
add(12);
|
||||
|
||||
find(11);
|
||||
find(13);
|
||||
|
||||
rem(11);
|
||||
find(11);
|
||||
fd_min_max();
|
||||
iter();
|
||||
rem_iter();
|
||||
fd_min_max();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "ucore_math.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
unsigned int a,N;
|
||||
if(argc < 2) {
|
||||
printf("Usage %s interger1\n",argv[0]);
|
||||
return 1;
|
||||
}
|
||||
a = atoi(argv[1]);
|
||||
N = uc_phi_32(a);
|
||||
printf("phi of %u is %u\n",a,N);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user