diff options
author | Brad Bishop <bradleyb@us.ibm.com> | 2014-06-30 22:10:16 -0500 |
---|---|---|
committer | Patrick Williams <iawillia@us.ibm.com> | 2014-07-02 22:49:29 -0500 |
commit | bf4630076762d9c20c16c80c1c791f352b046dd1 (patch) | |
tree | efc67bad010a28fd1abf5aeeefda2a969514fd97 /clib/test | |
download | ffs-bf4630076762d9c20c16c80c1c791f352b046dd1.tar.gz ffs-bf4630076762d9c20c16c80c1c791f352b046dd1.zip |
Port FFS tools over from Building Block repository.
Diffstat (limited to 'clib/test')
-rw-r--r-- | clib/test/array.c | 70 | ||||
-rw-r--r-- | clib/test/checksum.c | 117 | ||||
-rw-r--r-- | clib/test/dispatch.c | 63 | ||||
-rw-r--r-- | clib/test/ecc.c | 220 | ||||
-rw-r--r-- | clib/test/err.c | 73 | ||||
-rw-r--r-- | clib/test/exc_throw.c | 47 | ||||
-rw-r--r-- | clib/test/exception.c | 73 | ||||
-rw-r--r-- | clib/test/heap.c | 38 | ||||
-rw-r--r-- | clib/test/list.c | 71 | ||||
-rw-r--r-- | clib/test/map.c | 76 | ||||
-rw-r--r-- | clib/test/mq.c | 74 | ||||
-rw-r--r-- | clib/test/slab.c | 51 | ||||
-rw-r--r-- | clib/test/splay.c | 96 | ||||
-rw-r--r-- | clib/test/table.c | 102 | ||||
-rw-r--r-- | clib/test/tree.c | 84 | ||||
-rw-r--r-- | clib/test/vector.c | 83 | ||||
-rw-r--r-- | clib/test/watch.c | 44 |
17 files changed, 1382 insertions, 0 deletions
diff --git a/clib/test/array.c b/clib/test/array.c new file mode 100644 index 0000000..825db06 --- /dev/null +++ b/clib/test/array.c @@ -0,0 +1,70 @@ +/* + * Copyright (c) International Business Machines Corp., 2014 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <clib/array.h> +#include <clib/array_iter.h> + +#define SIZE 2550 + +int main(const int argc, const char * argv[]) { + array_t a; + array_init(&a, 4, 1024); + +printf("size[%d]\n", array_size(&a)); +printf("pages[%d]\n", array_pages(&a)); +printf("capacity[%d]\n", array_capacity(&a)); +printf("low[%u] high[%u]\n", array_low(&a), array_high(&a)); +exit(1); + + array_dump(&a, stdout); + + array_put(&a, 52, (uint32_t[]){52}); + array_put(&a, 53, (uint32_t[]){53}); + array_put(&a, 167, (uint32_t[]){167}); + array_put(&a, 223, (uint32_t[]){223}); + array_put(&a, 78, (uint32_t[]){78}); + array_put(&a, 205, (uint32_t[]){205}); + array_put(&a, 183, (uint32_t[]){183}); + array_put(&a, 150, (uint32_t[]){150}); + array_put(&a, 90, (uint32_t[]){90}); + array_put(&a, 66, (uint32_t[]){66}); + array_put(&a, 91, (uint32_t[]){91}); + array_put(&a, 45, (uint32_t[]){45}); + array_put(&a, 211, (uint32_t[]){211}); + + uint32_t arr[] = {985,986,987,988,990,991,992,993,994}; + array_put(&a, 985, arr, 9); + + array_iter_t it; + array_iter_init(&it, &a, AI_FLAG_FWD); + + uint32_t * j; + array_for_each(&it, j) { + printf("arr[%d]\n", *j); + } + + array_dump(&a, stdout); + array_delete(&a); + + return 0; +} diff --git a/clib/test/checksum.c b/clib/test/checksum.c new file mode 100644 index 0000000..c615cca --- /dev/null +++ b/clib/test/checksum.c @@ -0,0 +1,117 @@ +/* + * Copyright (c) International Business Machines Corp., 2014 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <stdio.h> +#include <stdlib.h> + +#include <clib/checksum.h> +#include <clib/exception.h> + +int main(void) +{ + unsigned int i; + uint8_t *buf = malloc(32); + uint8_t align[8] = { 0x01, 0x02, 0x04, 0x08, + 0x10, 0x20, 0x40, 0x80 }; + uint8_t unaligned1[9] = { 0x11, 0x11, 0x11, 0x11, + 0x22, 0x22, 0x22, 0x22, 0x33 }; + uint8_t unaligned2[10] = { 0x11, 0x11, 0x11, 0x11, + 0x22, 0x22, 0x22, 0x22, 0x33, 0x33 }; + uint8_t unaligned3[11] = { 0x11, 0x11, 0x11, 0x11, + 0x22, 0x22, 0x22, 0x22, 0x33, 0x33, 0x33 }; + + memcpy(buf, align, sizeof(align)); + uint32_t csum = memcpy_checksum(NULL, buf, sizeof(align)); + if (csum != 0x11224488) { + printf("fail %d a:%08x e:%08x\n", __LINE__, csum, 0x11224488); + return 1; + } + + memcpy(buf, unaligned1, sizeof(unaligned1)); + csum = memcpy_checksum(NULL, buf, sizeof(unaligned1)); + if (csum != 0x00333333) { + printf("fail %d a:%08x e:%08x\n", __LINE__, csum, 0x00333333); + return 1; + } + + memcpy(buf, unaligned2, sizeof(unaligned2)); + csum = memcpy_checksum(NULL, buf, sizeof(unaligned2)); + if (csum != 0x00003333) { + printf("fail %d a:%08x e:%08x\n", __LINE__, csum, 0x00003333); + return 1; + } + + memcpy(buf, unaligned3, sizeof(unaligned3)); + csum = memcpy_checksum(NULL, buf, sizeof(unaligned3)); + if (csum != 0x00000033) { + printf("fail %d a:%08x e:%08x\n", __LINE__, csum, 0x00000033); + return 1; + } + + char dst[16]; + csum = memcpy_checksum(dst, align, sizeof(align)); + if (csum != 0x11224488) { + printf("fail %d a:%08x e:%08x\n", __LINE__, csum, 0x11224488); + return 1; + } + + csum = memcpy_checksum(dst, unaligned1, sizeof(unaligned1)); + for (i = 0; i < sizeof(unaligned1); i++) { + if (dst[i] != unaligned1[i]) + printf("fail %d byte %i a:%08x e:%08x\n", + __LINE__, i, dst[i], unaligned1[i]); + } + if (csum != 0x00333333) { + printf("fail %d a:%08x e:%08x\n", __LINE__, csum, 0x00333333); + return 1; + } + + csum = memcpy_checksum(dst, unaligned2, sizeof(unaligned2)); + for (i = 0; i < sizeof(unaligned2); i++) { + if (dst[i] != unaligned2[i]) + printf("fail %d byte %i a:%08x e:%08x\n", + __LINE__, i, dst[i], unaligned2[i]); + } + if (csum != 0x00003333) { + printf("fail %d a:%08x e:%08x\n", __LINE__, csum, 0x00003333); + return 1; + } + + csum = memcpy_checksum(dst, unaligned3, sizeof(unaligned3)); + for (i = 0; i < sizeof(unaligned3); i++) { + if (dst[i] != unaligned3[i]) + printf("fail %d byte %i a:%08x e:%08x\n", + __LINE__, i, dst[i], unaligned3[i]); + } + if (csum != 0x00000033) { + printf("fail %d a:%08x e:%08x\n", __LINE__, csum, 0x00000033); + return 1; + } + +#if 0 + exception_t ex; + try { + (void)memcpy_checksum(NULL, &align[1], sizeof(align) - 1); + } else (ex) { + printf("Hey, what's this in the weeds? It's a baby, awesome!\n"); + } end_try; + +#endif + + return 0; +} diff --git a/clib/test/dispatch.c b/clib/test/dispatch.c new file mode 100644 index 0000000..8851c55 --- /dev/null +++ b/clib/test/dispatch.c @@ -0,0 +1,63 @@ +/* + * Copyright (c) International Business Machines Corp., 2014 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <clib/watch.h> +#include <clib/dispatch.h> + +int callback(dispatch_event_t * ev, void * ptr) { + char buf[256]; + int rc; + + rc = read(ev->fd, buf, sizeof buf); + printf("rc[%d]\n", rc); + + printf("fd[%x]\n", ev->fd); + printf("events[%x]\n", ev->events); + + return 0; +} + +int main(const int argc, const char * argv[]) { + watch_t w; + watch_init(&w); + + watch_callback_t wc = { + .data = NULL, + .func = NULL, + }; + + watch_add(&w, "/dev/mqueue", IN_CREATE | IN_ATTRIB, &wc); + + dispatch_t d; + dispatch_init(&d); + + dispatch_callback_t dc = { + .data = NULL, + .func = callback, + }; + + dispatch_add(&d, watch_fileno(&w), EPOLLIN, &dc); + dispatch_wait(&d); + + return 0; +} diff --git a/clib/test/ecc.c b/clib/test/ecc.c new file mode 100644 index 0000000..90760d7 --- /dev/null +++ b/clib/test/ecc.c @@ -0,0 +1,220 @@ +/* + * Copyright (c) International Business Machines Corp., 2014 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <clib/assert.h> +#include <clib/type.h> +#include <clib/misc.h> + +#include <clib/ecc.h> +int test_sfc_ecc() +{ + uint64_t in[] = { 0x3131313131313131, 0x3232323232323232, + 0x3333333333333333, 0x3434343434343434, + 0x3535353535353535, 0x3636363636363636, + 0x3737373737373737, 0x3838383838383838, + 0x3132333435363738, 0x3938373635343332, + }; + printf("Function: test_sfc_ecc\n"); + printf("IN [\n"); + for (uint i=0; i<sizeof(in); i++) { + if (i && i % 8 == 0) printf(" "); + printf("%2.2x", ((uint8_t *)in)[i]); + } + printf("]\n"); + + uint8_t out_ecc[sizeof in + sizeof in / 8]; + int rc = sfc_ecc_inject(out_ecc, sizeof out_ecc, in, sizeof in); + printf("sz_in(%d) sz_out(%d) rc(%d)\n", sizeof in, sizeof out_ecc, rc); + + printf("OUT_ECC [\n"); + for (int i=0; i<rc; i++) { + if (i && i % 8 == 0) printf(" "); + printf("%2.2x", ((uint8_t *)out_ecc)[i]); + } + printf("]\n"); + + uint8_t keep_out_ecc[sizeof in + sizeof in / 8]; + memcpy(keep_out_ecc, out_ecc, sizeof keep_out_ecc); + printf("sz_in(%d) sz_keep_out(%d) sz_out(%d)\n", sizeof in, sizeof keep_out_ecc, sizeof out_ecc); + + printf("KEEP_OUT_ECC [\n"); + for (int i=0; i<rc; i++) { + if (i && i % 8 == 0) printf(" "); + printf("%2.2x", ((uint8_t *)out_ecc)[i]); + } + printf("]\n"); + + + + uint8_t out_no_ecc[sizeof in]; + int myrc = sfc_ecc_remove(out_no_ecc, sizeof out_no_ecc, + out_ecc, sizeof out_ecc); + printf("ECC_STATUS:%d \n",myrc); + printf("OUT_NO_ECC [\n"); + for (uint i=0; i<sizeof(out_no_ecc); i++) { + if (i && i % 8 == 0) printf(" "); + printf("%2.2x", ((uint8_t *)out_no_ecc)[i]); + } + printf("]\n"); + + for (uint x=0; x<sizeof(out_ecc);x++) + { + for (int c=0; c<8; c++) + { + out_ecc[x] ^= 1<<c; + printf("OUT_CORRUPTED_ECC[%d,%d] [\n",x,c); + for (int i=0; i<rc; i++) { + if (i && i % 8 == 0) printf(" "); + printf("%2.2x", ((uint8_t *)out_ecc)[i]); + } + printf("]\n"); + myrc = sfc_ecc_remove(out_no_ecc, sizeof out_no_ecc, + out_ecc, sizeof out_ecc); + printf("ECC_STATUS:%d \n",myrc); + printf("OUT_NO_ECC [\n"); + for (uint i=0; i<sizeof(out_no_ecc); i++) { + if (i && i % 8 == 0) printf(" "); + printf("%2.2x", ((uint8_t *)out_no_ecc)[i]); + } + printf("]\n"); + + printf("IN_OUT_ECC [\n"); + for (uint i=0; i<sizeof(out_ecc); i++) { + if (i && i % 8 == 0) printf(" "); + printf("%2.2x", ((uint8_t *)out_ecc)[i]); + } + printf("]\n\n"); + if (myrc == 0) + { + printf("For SFC Uncorrectable event\n"); + memcpy(out_ecc, keep_out_ecc, sizeof out_ecc); + + //return 1; + } + } + } + + return 0; +} + +int test_p8_ecc() +{ + uint64_t in[] = { 0x3131313131313131, 0x3333333333333333,}; + printf("Function: test_p8_ecc\n"); + printf("IN [\n"); + for (uint i=0; i<sizeof(in); i++) { + if (i && i % 8 == 0) printf(" "); + printf("%2.2x", ((uint8_t *)in)[i]); + } + printf("]\n"); + + uint8_t out_ecc[sizeof in + sizeof in / 8]; + int rc = p8_ecc_inject(out_ecc, sizeof out_ecc, in, sizeof in); + printf("sz_in(%d) sz_out(%d) rc(%d)\n", sizeof in, sizeof out_ecc, rc); + + printf("OUT_ECC [\n"); + for (int i=0; i<rc; i++) { + if (i && i % 8 == 0) printf(" "); + printf("%2.2x", ((uint8_t *)out_ecc)[i]); + } + printf("]\n"); + + uint8_t out_no_ecc[sizeof in]; + ecc_status_t myrc = p8_ecc_remove(out_no_ecc, sizeof out_no_ecc, + out_ecc, sizeof out_ecc); + printf("ECC_STATUS:%d \n",myrc); + printf("OUT_NO_ECC [\n"); + for (uint i=0; i<sizeof(out_no_ecc); i++) { + if (i && i % 8 == 0) printf(" "); + printf("%2.2x", ((uint8_t *)out_no_ecc)[i]); + } + printf("]\n"); + + for (uint x=0; x<sizeof(out_ecc);x++) + { + for (int c=0; c<8; c++) + { + out_ecc[x] ^= 1<<c; + printf("OUT_CORRUPTED_ECC[%d,%d] [\n",x,c); + for (int i=0; i<rc; i++) { + if (i && i % 8 == 0) printf(" "); + printf("%2.2x", ((uint8_t *)out_ecc)[i]); + } + printf("]\n"); + myrc = p8_ecc_remove(out_no_ecc, sizeof out_no_ecc, + out_ecc, sizeof out_ecc); + printf("ECC_STATUS:%d \n",myrc); + printf("OUT_NO_ECC [\n"); + for (uint i=0; i<sizeof(out_no_ecc); i++) { + if (i && i % 8 == 0) printf(" "); + printf("%2.2x", ((uint8_t *)out_no_ecc)[i]); + } + printf("]\n"); + + printf("IN_OUT_ECC [\n"); + for (uint i=0; i<sizeof(out_ecc); i++) { + if (i && i % 8 == 0) printf(" "); + printf("%2.2x", ((uint8_t *)out_ecc)[i]); + } + printf("]\n\n"); + if (myrc == UNCORRECTABLE) + { + printf("Uncorrectable event\n"); + return 1; + } + } + } + + return 0; +} + +int main (void) { + + uint64_t in[] = { 0x3131313131313131, 0x3232323232323232, + 0x3333333333333333, 0x3434343434343434, }; + + printf("IN [\n"); + for (uint i=0; i<sizeof(in); i++) { + if (i && i % 8 == 0) printf(" "); + printf("%2.2x", ((uint8_t *)in)[i]); + } + printf("]\n"); + + uint8_t out[sizeof in + sizeof in / 8]; + + int rc = sfc_ecc_inject(out, sizeof out, in, sizeof in); + + printf("sz_in(%d) sz_out(%d) rc(%d)\n", sizeof in, sizeof out, rc); + + printf("OUT [\n"); + for (int i=0; i<rc; i++) { + if (i && i % 8 == 0) printf(" "); + printf("%2.2x", ((uint8_t *)out)[i]); + } + printf("]\n\n"); + + sfc_ecc_dump(stdout, 0, out, rc); + test_sfc_ecc(); + test_p8_ecc(); + + return 0; +} diff --git a/clib/test/err.c b/clib/test/err.c new file mode 100644 index 0000000..9ad6420 --- /dev/null +++ b/clib/test/err.c @@ -0,0 +1,73 @@ +/* + * Copyright (c) International Business Machines Corp., 2014 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <errno.h> +#include <string.h> +#include <libgen.h> + +#include <clib/assert.h> +#include <clib/version.h> +#include <clib/err.h> + +extern char * program_invocation_short_name; + +#define FOO_MAJOR 0x01 +#define FOO_MINOR 0x00 +#define FOO_PATCH 0x00 +#define FOO_VER VER(FOO_MAJOR, FOO_MINOR, FOO_PATCH) + +#define FOO_UNEXPECTED(f, ...) ({ \ + UNEXPECTED(f, ##__VA_ARGS__); \ + VERSION(FOO_VER, "%s", program_invocation_short_name); \ + }) + + +int main (int argc, char * argv[]) { + ERRNO(EINVAL); + FOO_UNEXPECTED("cannot frob the ka-knob"); + + goto error; + + if (false) { + err_t * err = NULL; +error: + while ((err = err_get()) != NULL) { + switch (err_type(err)) { + case ERR_VERSION: + fprintf(stderr, "%s: %s : %s(%d) : v%d.%02d.%04d %.*s\n", + basename((char *)argv[0]), + err_type_name(err), basename(err_file(err)), err_line(err), + VER_TO_MAJOR(err_code(err)), VER_TO_MINOR(err_code(err)), + VER_TO_PATCH(err_code(err)), + err_size(err), (char *)err_data(err)); + break; + default: + fprintf(stderr, "%s: %s : %s(%d) : (code=%d) %.*s\n", + basename((char *)argv[0]), + err_type_name(err), basename(err_file(err)), err_line(err), + err_code(err), err_size(err), (char *)err_data(err)); + } + } + } + + return 0; +} + diff --git a/clib/test/exc_throw.c b/clib/test/exc_throw.c new file mode 100644 index 0000000..de06241 --- /dev/null +++ b/clib/test/exc_throw.c @@ -0,0 +1,47 @@ +/* + * Copyright (c) International Business Machines Corp., 2014 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include <stdio.h> + +#include <clib/exception.h> + +#define EXCEPTION_FOO 10 + +void foo() { + throw(EXCEPTION_FOO, ex.data, ex.size); +} + +int main() { + exception_t ex; + + try { + printf("try block: BEFORE foo\n"); + foo(); + printf("try block: AFTER foo <-- should not get here\n"); + } catch (EXCEPTION_FOO, ex) { + printf("main: CAUGHT %s(%d) EXCEPTION_FOO data[%d]\n", + ex.file, ex.line, *(int *)ex.data); + } else (ex) { + printf("main: CAUGHT %s(%d) data[%d]\n", + ex.file, ex.line, *(int *)ex.data); + } end + + return 0; +} + diff --git a/clib/test/exception.c b/clib/test/exception.c new file mode 100644 index 0000000..8a8e16f --- /dev/null +++ b/clib/test/exception.c @@ -0,0 +1,73 @@ +/* + * Copyright (c) International Business Machines Corp., 2014 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include <stdio.h> + +#include <clib/exception.h> +#include <clib/memory_leak_detection.h> + +#define EXCEPTION_FOO 1 +#define EXCEPTION_BAR 2 +#define EXCEPTION_TAZ 3 + +int bar(int i) { + int * data = MALLOC(sizeof *data); + *data = i; + + throw_bytes(EXCEPTION_BAR, data, sizeof *data); + + return 0; +} + +int foo(int i) { + + exception_t ex; + try { + printf("%s: %d\n", __func__, __LINE__); + bar(i); + } catch (EXCEPTION_BAR, ex) { + printf("foo: CAUGHT %s(%d) EXCEPTION_BAR data[%d]\n", + ex.file, ex.line, *(int *)ex.data); + throw_bytes(EXCEPTION_FOO, ex.data, ex.size); + } end_try + + throw_bytes(EXCEPTION_FOO, "this is a test", 14); + + printf("%s: %d\n", __func__, __LINE__); + + return 0; +} + +int main(void) { + exception_t ex; + + try { + printf("%s: %d\n", __func__, __LINE__); + foo(1); + printf("try block: AFTER foo <-- should not get here\n"); + } catch (EXCEPTION_FOO, ex) { + printf("main: CAUGHT %s(%d) EXCEPTION_FOO data[%d]\n", + ex.file, ex.line, *(int *)ex.data); + } else (ex) { + printf("main: CAUGHT %s(%d) data[%d]\n", + ex.file, ex.line, *(int *)ex.data); + } end_try + + return 0; +} diff --git a/clib/test/heap.c b/clib/test/heap.c new file mode 100644 index 0000000..db7008e --- /dev/null +++ b/clib/test/heap.c @@ -0,0 +1,38 @@ +/* + * Copyright (c) International Business Machines Corp., 2014 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <clib/heap.h> + +int main(const int argc, const char * argv[]) { + heap_t h; + heap_init(h, 0, 0); + + void * p5 = heap_alloc(&h, 5); + void * p9 = heap_alloc(&h, 9); + void * p13 = heap_alloc(&h, 13); + + heap_free(&h, p13); + heap_free(&h, p5); + heap_free(&h, p9); + + return 0; +} diff --git a/clib/test/list.c b/clib/test/list.c new file mode 100644 index 0000000..a93f6a7 --- /dev/null +++ b/clib/test/list.c @@ -0,0 +1,71 @@ +/* + * Copyright (c) International Business Machines Corp., 2014 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <clib/assert.h> +#include <clib/slab.h> +#include <clib/list.h> +#include <clib/list_iter.h> + +struct data { + list_node_t node; + int i; + float f; +}; +typedef struct data data_t; + +int main (void) { +// slab_t s = INIT_SLAB; +// slab_init(&s, "my_slab", sizeof(data_t), 0); + + list_t l = INIT_LIST; + list_init(&l); + + int i; + for (i=0; i<10; i++) { +// data_t * d = (data_t *)slab_alloc(&s); + data_t * d = (data_t *)malloc(sizeof(*d)); + + d->i = i; + d->f = (float)i; + + list_add_tail(&l, &d->node); + } + + list_iter_t it; + list_iter_init(&it, &l, LI_FLAG_FWD); + + data_t * d; + list_for_each(&it, d, node) { + printf("i: %d f: %f\n", d->i, d->f); + } + + while (list_empty(&l) == false) { + data_t * d = container_of(list_remove_tail(&l), data_t, node); + printf("i: %d f: %f\n", d->i, d->f); + } + +// slab_dump(&s, stdout); +// slab_delete(&s); + + return 0; +} + diff --git a/clib/test/map.c b/clib/test/map.c new file mode 100644 index 0000000..2501a32 --- /dev/null +++ b/clib/test/map.c @@ -0,0 +1,76 @@ +/* + * Copyright (c) International Business Machines Corp., 2014 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <clib/assert.h> +#include <clib/slab.h> +#include <clib/type.h> +#include <clib/map.h> + +struct data { + map_node_t node; + int i; + float f; +}; +typedef struct data data_t; + +int main (void) { + slab_t s = INIT_SLAB; + slab_init(&s, "my_slab", sizeof(data_t), 0); + + int compare(const int i1, const int i2) { + return i1 - i2; + } + + map_t m; + map_init(&m, (compare_f)compare); + + int i; + for (i=0; i<100; i++) { + data_t * d = (data_t *)slab_alloc(&s); + + printf("i[%d]\n", i); + + d->i = i; + d->f = (float)i; + + map_node_init(&d->node, (const void *)(d->i)); + map_insert(&m, &d->node); + } + +#if 1 + i = 6; + map_node_t * n = map_find(&m, (const void *)(i)); + map_remove(&m, n); + n = map_find(&m, (const void *)i); + + i = 2; + map_find(&m, (const void *)i); + i = 8; + map_find(&m, (const void *)i); +#endif + + map_dump(&m, stdout); + slab_delete(&s); + + return 0; +} + diff --git a/clib/test/mq.c b/clib/test/mq.c new file mode 100644 index 0000000..aaa9eab --- /dev/null +++ b/clib/test/mq.c @@ -0,0 +1,74 @@ +/* + * Copyright (c) International Business Machines Corp., 2014 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <clib/vector.h> +#include <clib/vector_iter.h> + +#include <clib/misc.h> +#include <clib/mq.h> + +#define SIZE 2550 + +int main(void) { + vector_t a = INIT_VECTOR; + vector_init(&a, "my_vector", 4, 1024); + + vector_put(&a, 52, (uint32_t[]){52}); + vector_put(&a, 53, (uint32_t[]){53}); + vector_put(&a, 167, (uint32_t[]){167}); + vector_put(&a, 223, (uint32_t[]){223}); + vector_put(&a, 78, (uint32_t[]){78}); + vector_put(&a, 205, (uint32_t[]){205}); + vector_put(&a, 183, (uint32_t[]){183}); + vector_put(&a, 150, (uint32_t[]){150}); + vector_put(&a, 90, (uint32_t[]){90}); + vector_put(&a, 66, (uint32_t[]){66}); + vector_put(&a, 91, (uint32_t[]){91}); + vector_put(&a, 45, (uint32_t[]){45}); + vector_put(&a, 211, (uint32_t[]){211}); + uint32_t arr[] = {55,56,57,58,59,60,61,62,63}; + vector_put(&a, 985, arr, 9); + + vector_iter_t it; + vector_iter_init(&it, &a, VI_FLAG_FWD); + + uint32_t * j; + vector_for_each(&it, j) { + printf("XXX i[%d]\n", *j); + } + + vector_dump(&a, stdout); + + mqueue_t mq = INIT_MQUEUE; + mqueue_init(&mq, "dbs"); + mqueue_create(&mq, gettid()); + + vector_send(&a, &mq); + vector_delete(&a); + + sleep(1); + + mqueue_delete(&mq); + + return 0; +} diff --git a/clib/test/slab.c b/clib/test/slab.c new file mode 100644 index 0000000..721c98f --- /dev/null +++ b/clib/test/slab.c @@ -0,0 +1,51 @@ +/* + * Copyright (c) International Business Machines Corp., 2014 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <clib/slab.h> + +#define SIZE 6500 +#define ALLOC 16 +#define PAGE 4096 + +int main(void) { + slab_t s = INIT_SLAB; + slab_init(&s, "my_slab", ALLOC, PAGE); + + void * ptr[SIZE] = {NULL,}; + + int i; + for (i=0; i<SIZE; i++) { + ptr[i] = slab_alloc(&s); + memset(ptr[i], i % 256, ALLOC); + } + + slab_dump(&s, stdout); + + for (i=0; i<SIZE; i++) { + slab_free(&s, ptr[i]); + } + + slab_dump(&s, stdout); + slab_delete(&s); + + return 0; +} diff --git a/clib/test/splay.c b/clib/test/splay.c new file mode 100644 index 0000000..c62b87b --- /dev/null +++ b/clib/test/splay.c @@ -0,0 +1,96 @@ +/* + * Copyright (c) International Business Machines Corp., 2014 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <clib/assert.h> +#include <clib/slab.h> +#include <clib/type.h> +#include <clib/tree.h> + +struct data { + tree_node_t node; + int i; + float f; +}; +typedef struct data data_t; + +static inline int hash(int key) { + key = ~key + (key << 15); + key = key ^ (key >> 12); + key = key + (key << 2); + key = key ^ (key >> 4); + key = key * 2057; + key = key ^ (key >> 16); + + return key; +} + +int main (void) { + slab_t s = INIT_SLAB; + slab_init(&s, "my_slab", sizeof(data_t), 4096); + + int compare(const int i1, const int i2) { + return i1 - i2; + } + + tree_t l = INIT_TREE; + tree_init(&l, (compare_f)compare); + + int i; + for (i=0; i<10000; i++) { + data_t * d = (data_t *)slab_alloc(&s); + + d->i = hash(i); + d->f = (float)i; + + tree_node_init(&d->node, (const void *)d->i); + splay_insert(&l, &d->node); + } + + tree_dump(&l, stdout); + + i = hash(6); + tree_node_t * n = tree_find(&l, (const void *)i); + printf("n[%p]\n", n); + + data_t * d = container_of(n, data_t, node); + printf("d->i[%d]\n", d->i); + + splay_remove(&l, n); + n = tree_find(&l, (const void *)i); + printf("n[%p]\n", n); + + i = 2; + splay_find(&l, (const void *)i); + i = 8; + splay_find(&l, (const void *)i); + +#if 0 + slab_dump(&s, stdout); + tree_delete(&l, d, node); +#endif + + tree_dump(&l, stdout); + slab_delete(&s); + + return 0; +} + diff --git a/clib/test/table.c b/clib/test/table.c new file mode 100644 index 0000000..62253df --- /dev/null +++ b/clib/test/table.c @@ -0,0 +1,102 @@ +/* + * Copyright (c) International Business Machines Corp., 2014 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <clib/table.h> +#include <clib/table_iter.h> + +#define COLS 5 + +int main(void) { + table_t t = INIT_TABLE; + table_init(&t, "table", COLS); + + const char * str = "column4 is a really long assed string"; + + table_name(&t, 0, "column0"); + table_name(&t, 1, "column1"); + table_name(&t, 2, "column2"); + table_name(&t, 3, "column3"); + table_name(&t, 4, str); + + printf("%s\n", table_name(&t, 4)); + printf("cols: %d\n", table_columns(&t)); + printf("rows: %d\n", table_rows(&t)); + + table_dump(&t, stdout); + + table_iter_t it; + table_iter_init(&it, &t, TI_FLAG_FWD); + + size_t r, c; + + value_t * val; + table_for_each(&it, val) { + for (c=0; c<COLS; c++) + value_dump(val+c, stdout); + } + printf("xxx =================\n"); + + value_t v[COLS]; + + for (r=0; r<COLS; r++) { + for (c=0; c<COLS; c++) { + printf("t[%d][%d] --> %d\n", r, c, r * COLS + c); + value_i32(v+c, r * COLS + c); + value_dump(v+c, stdout); + } + table_row(&t, r, v); + } + table_dump(&t, stdout); + printf("yyy =================\n"); + + table_for_each(&it, val) { + for (c=0; c<COLS; c++) + value_dump(val+c, stdout); + } + printf("zzz =================\n"); + + FILE * f = fopen("table.bin", "w+"); + table_serialize(&t); + table_save(&t, f); + table_delete(&t); + fclose(f); + + printf("111 =================\n"); + + f = fopen("table.bin", "r+"); + table_load(&t, f); + table_deserialize(&t); + table_dump(&t, stdout); + fclose(f); + + table_iter_init(&it, &t, TI_FLAG_FWD); + table_for_each(&it, val) { + for (c=0; c<COLS; c++) + value_dump(val+c, stdout); + } + printf("aaa =================\n"); + + table_dump(&t, stdout); + table_delete(&t); + + return 0; +} diff --git a/clib/test/tree.c b/clib/test/tree.c new file mode 100644 index 0000000..52f6c20 --- /dev/null +++ b/clib/test/tree.c @@ -0,0 +1,84 @@ +/* + * Copyright (c) International Business Machines Corp., 2014 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <clib/assert.h> +#include <clib/type.h> +#include <clib/slab.h> + +#include <clib/tree.h> +#include <clib/tree_iter.h> + +struct data { + tree_node_t node; + int i; + float f; +}; +typedef struct data data_t; + +int main (void) { + slab_t s = INIT_SLAB; + slab_init(&s, "my_slab", sizeof(data_t), 4096); + + tree_t t = INIT_TREE; + tree_init(&t, default_compare); + + int i; + for (i=0; i<25; i++) { + data_t * d = (data_t *)slab_alloc(&s); + + printf("insert i[%d] --> %p\n", i, d); + d->i = i; + d->f = (float)i; /* key */ + + tree_node_init(&d->node, (const void *)(d->i)); + tree_insert(&t, &d->node); + } + +#if 1 + i = 6; + tree_node_t * n = tree_find(&t, (const void *)(i)); + tree_remove(&t, n); + n = tree_find(&t, (const void *)i); + + i = 2; + tree_find(&t, (const void *)i); + i = 8; + tree_find(&t, (const void *)i); +#endif + + tree_dump(&t, stdout); + + data_t * d; + + tree_iter_t it; + tree_iter_init(&it, &t, TI_FLAG_FWD); + + tree_for_each(&it, d, node) { + printf("depth first (FWD) i[%d] f[%f]\n", d->i, d->f); + } + +// tree_dump(&t, stdout); + slab_delete(&s); + + return 0; +} + diff --git a/clib/test/vector.c b/clib/test/vector.c new file mode 100644 index 0000000..2925fbe --- /dev/null +++ b/clib/test/vector.c @@ -0,0 +1,83 @@ +/* + * Copyright (c) International Business Machines Corp., 2014 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <clib/vector.h> +#include <clib/vector_iter.h> + +int main(void) { + vector_t a = INIT_VECTOR; + vector_init(&a, "foo", 4, 1024); + + vector_size(&a, 1000); + + vector_put(&a, 0, (uint32_t[]){0xffffffff}); + vector_put(&a, 1, (uint32_t[]){0xffffffff}); + vector_put(&a, 52, (uint32_t[]){52}); + vector_put(&a, 53, (uint32_t[]){53}); + vector_put(&a, 167, (uint32_t[]){167}); + vector_put(&a, 223, (uint32_t[]){223}); + vector_put(&a, 78, (uint32_t[]){78}); + vector_put(&a, 205, (uint32_t[]){205}); + vector_put(&a, 183, (uint32_t[]){183}); + vector_put(&a, 150, (uint32_t[]){150}); + vector_put(&a, 90, (uint32_t[]){90}); + vector_put(&a, 66, (uint32_t[]){66}); + vector_put(&a, 91, (uint32_t[]){91}); + vector_put(&a, 45, (uint32_t[]){45}); + vector_put(&a, 211, (uint32_t[]){211}); + + uint32_t arr[] = {985,986,987,988,990,991,992,993,994}; + vector_put(&a, 985, arr, 9); + + vector_dump(&a, stdout); + + vector_size(&a, 200); + + vector_dump(&a, stdout); + + FILE *f = fopen("vector.bin", "w+"); + vector_save(&a, f); + fclose(f); + + vector_delete(&a); + + f = fopen("vector.bin", "r"); + vector_load(&a, f); + fclose(f); + +#if 1 + vector_iter_t it; + vector_iter_init(&it, &a, VI_FLAG_FWD); + + uint32_t * j; + vector_for_each(&it, j) { + printf("arr[%d] = %d\n", it.idx, *j); + } +#endif + + vector_dump(&a, stdout); + + vector_delete(&a); + + return 0; +} diff --git a/clib/test/watch.c b/clib/test/watch.c new file mode 100644 index 0000000..07e68cf --- /dev/null +++ b/clib/test/watch.c @@ -0,0 +1,44 @@ +/* + * Copyright (c) International Business Machines Corp., 2014 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <clib/watch.h> + +int callback(watch_event_t * we) { + printf("hello\n"); + return 0; +} + +int main(const int argc, const char * argv[]) { + watch_t w; + watch_init(&w); + + watch_callback_t cb = { + .data = NULL, + .func = callback, + }; + + watch_add(&w, "/dev/mqueue", IN_CREATE | IN_ATTRIB, &cb); + watch_wait(&w); + + return 0; +} |