X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fobjset%2Ftest%2Frun.c;fp=ccan%2Fobjset%2Ftest%2Frun.c;h=c4737da7f7c1be76105956e7f15f2cda48a940c9;hb=756749b2d337334b23deffcfe75b4f731f8f78d1;hp=0000000000000000000000000000000000000000;hpb=e6ed30e6896a8cc2df523c3a7e343252856142b4;p=ccan diff --git a/ccan/objset/test/run.c b/ccan/objset/test/run.c new file mode 100644 index 00000000..c4737da7 --- /dev/null +++ b/ccan/objset/test/run.c @@ -0,0 +1,97 @@ +#include +#include + +struct objset_charp { + OBJSET_MEMBERS(char *); +}; + +struct objset_int { + OBJSET_MEMBERS(int *); +}; + +int main(void) +{ + struct objset_charp osetc; + struct objset_int oseti; + struct objset_iter i; + int i1 = 1, i2 = 2; + char c1 = 1, c2 = 2; + + /* This is how many tests you plan to run */ + plan_tests(46); + + objset_init(&osetc); + objset_init(&oseti); + ok1(objset_empty(&osetc)); + ok1(objset_empty(&oseti)); + ok1(objset_get(&oseti, &i1) == NULL); + ok1(objset_get(&oseti, &i2) == NULL); + ok1(objset_get(&osetc, &c1) == NULL); + ok1(objset_get(&osetc, &c2) == NULL); + + ok1(!objset_del(&oseti, &i1)); + ok1(!objset_del(&oseti, &i2)); + ok1(!objset_del(&osetc, &c1)); + ok1(!objset_del(&osetc, &c2)); + + objset_add(&oseti, &i1); + ok1(!objset_empty(&oseti)); + ok1(objset_get(&oseti, &i1) == &i1); + ok1(objset_get(&oseti, &i2) == NULL); + + objset_add(&osetc, &c1); + ok1(!objset_empty(&osetc)); + ok1(objset_get(&osetc, &c1) == &c1); + ok1(objset_get(&osetc, &c2) == NULL); + + objset_add(&oseti, &i2); + ok1(!objset_empty(&oseti)); + ok1(objset_get(&oseti, &i1) == &i1); + ok1(objset_get(&oseti, &i2) == &i2); + + objset_add(&osetc, &c2); + ok1(!objset_empty(&osetc)); + ok1(objset_get(&osetc, &c1) == &c1); + ok1(objset_get(&osetc, &c2) == &c2); + + ok1((objset_first(&oseti, &i) == &i1 + && objset_next(&oseti, &i) == &i2) + || (objset_first(&oseti, &i) == &i2 + && objset_next(&oseti, &i) == &i1)); + ok1(objset_next(&oseti, &i) == NULL); + + ok1((objset_first(&osetc, &i) == &c1 + && objset_next(&osetc, &i) == &c2) + || (objset_first(&osetc, &i) == &c2 + && objset_next(&osetc, &i) == &c1)); + ok1(objset_next(&osetc, &i) == NULL); + + ok1(objset_del(&oseti, &i1)); + ok1(!objset_del(&oseti, &i1)); + ok1(objset_del(&osetc, &c1)); + ok1(!objset_del(&osetc, &c1)); + + ok1(objset_first(&oseti, &i) == &i2); + ok1(objset_next(&oseti, &i) == NULL); + ok1(objset_first(&osetc, &i) == &c2); + ok1(objset_next(&osetc, &i) == NULL); + + objset_clear(&oseti); + ok1(objset_first(&oseti, &i) == NULL); + ok1(objset_empty(&oseti)); + ok1(objset_get(&oseti, &i1) == NULL); + ok1(objset_get(&oseti, &i2) == NULL); + ok1(!objset_del(&oseti, &i1)); + ok1(!objset_del(&oseti, &i2)); + + objset_clear(&osetc); + ok1(objset_first(&osetc, &i) == NULL); + ok1(objset_empty(&osetc)); + ok1(objset_get(&osetc, &c1) == NULL); + ok1(objset_get(&osetc, &c2) == NULL); + ok1(!objset_del(&osetc, &c1)); + ok1(!objset_del(&osetc, &c2)); + + /* This exits depending on whether all tests passed */ + return exit_status(); +}