Commit 5e341a5f authored by Scott Vokes's avatar Scott Vokes
Browse files

Convert remaining greatest-based unit tests.

parent 3101c91a
Loading
Loading
Loading
Loading
+1 −7
Original line number Diff line number Diff line
@@ -27,10 +27,7 @@ ECHOSRV_OBJS = \
	echosrv.o \
	util.o \

all: bus.png test_yacht echosrv bus_example

test: test_yacht
	./test_yacht
all: bus.png echosrv bus_example

%.png: %.dot
	dot -Tpng -o $@ $^
@@ -38,9 +35,6 @@ test: test_yacht
libbus.a: ${BUS_OBJS}
	ar -rcs $@ $^

test_yacht: test_yacht.o yacht.o
	${CC} -o $@ $^ ${LDFLAGS}

echosrv: ${ECHOSRV_OBJS}
	${CC} -o $@ $^ ${LDFLAGS}

src/lib/bus/greatest.h

deleted100644 → 0
+0 −661

File deleted.

Preview size limit exceeded, changes collapsed.

src/lib/threadpool/greatest.h

deleted100644 → 0
+0 −661

File deleted.

Preview size limit exceeded, changes collapsed.

+206 −0
Original line number Diff line number Diff line
@@ -17,115 +17,149 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "greatest.h"
#include "unity.h"
#include "yacht.h"

typedef struct yacht yacht;

TEST yacht_should_cleanly_init_and_free(void) {
void setUp(void) {}
void tearDown(void) {}

void test_yacht_should_cleanly_init_and_free(void) {
    yacht *y = yacht_init(8);
    ASSERT(y);
    TEST_ASSERT(y);
    yacht_free(y, NULL, NULL);
    PASS();
}

TEST yacht_should_add_and_have_accurate_membership(void) {

void yacht_should_add_and_have_accurate_membership(void) {
    uint8_t sz2 = 8;
    yacht *y = yacht_init(sz2);

    for (int i = 0; i < (1 << (sz2 - 1)); i++) {
        ASSERT(yacht_set(y, i, NULL, NULL));
        ASSERT(yacht_member(y, i));
        ASSERT(!yacht_member(y, i + 1));
        TEST_ASSERT(yacht_set(y, i, NULL, NULL));
        TEST_ASSERT(yacht_member(y, i));
        TEST_ASSERT(!yacht_member(y, i + 1));
    }

    yacht_free(y, NULL, NULL);
    PASS();
}

TEST yacht_should_add_and_remove_accurately(uint8_t sz2) {
bool yacht_should_add_and_remove_accurately(uint8_t sz2) {
    yacht *y = yacht_init(sz2);
    ASSERT(y);
    TEST_ASSERT(y);

    for (int i = 0; i < (1 << (sz2 - 1)); i++) {
        uintptr_t v = 1;
        void *old = NULL;
        ASSERT(yacht_set(y, i, (void *)v, &old));
        ASSERT_EQ(NULL, old);
        ASSERT(yacht_member(y, i));
        TEST_ASSERT(yacht_set(y, i, (void *)v, &old));
        TEST_ASSERT_EQUAL(NULL, old);
        TEST_ASSERT(yacht_member(y, i));
        void *old2 = NULL;
        ASSERT(yacht_remove(y, i, &old2));
        ASSERT_EQ((void *)1, old2);
        ASSERT(!yacht_member(y, i));
        TEST_ASSERT(yacht_remove(y, i, &old2));
        TEST_ASSERT_EQUAL((void *)1, old2);
        TEST_ASSERT(!yacht_member(y, i));
        /* Add it back, to ensure it isn't disturbed by other removes */
        ASSERT(yacht_set(y, i, NULL, NULL));
        ASSERT(yacht_member(y, i));
        TEST_ASSERT(yacht_set(y, i, NULL, NULL));
        TEST_ASSERT(yacht_member(y, i));

        for (int j = 0; j < i; j++) {
            ASSERT(yacht_member(y, j));
            TEST_ASSERT(yacht_member(y, j));
        }
    }

    yacht_free(y, NULL, NULL);
    PASS();
    return true;
}

void test_yacht_should_add_and_remove_accurately8(void) {
    yacht_should_add_and_remove_accurately(8);
}

void test_yacht_should_add_and_remove_accurately10(void) {
    yacht_should_add_and_remove_accurately(10);
}

TEST yacht_should_add_and_remove_accurately_out_of_order(uint8_t sz2) {
void test_yacht_should_add_and_remove_accurately14(void) {
    yacht_should_add_and_remove_accurately(14);
}

void yacht_should_add_and_remove_accurately_out_of_order(uint8_t sz2) {
    yacht *y = yacht_init(sz2);
    ASSERT(y);
    TEST_ASSERT(y);

    const uint64_t other_large_prime = 4294967279L; /* (2 ** 32) - 17 */

    for (int i = 0; i < (1 << (sz2 - 1)); i++) {
        int iv = i * other_large_prime;
        ASSERT(yacht_set(y, iv, NULL, NULL));
        ASSERT(yacht_member(y, iv));
        ASSERT(yacht_remove(y, iv, NULL));
        ASSERT(!yacht_member(y, iv));
        TEST_ASSERT(yacht_set(y, iv, NULL, NULL));
        TEST_ASSERT(yacht_member(y, iv));
        TEST_ASSERT(yacht_remove(y, iv, NULL));
        TEST_ASSERT(!yacht_member(y, iv));
        /* Add it back, to ensure it isn't disturbed by other removes */
        ASSERT(yacht_set(y, iv, NULL, NULL));
        ASSERT(yacht_member(y, iv));
        TEST_ASSERT(yacht_set(y, iv, NULL, NULL));
        TEST_ASSERT(yacht_member(y, iv));

        for (int j = 0; j < i; j++) {
            int jv = j * other_large_prime;
            ASSERT(yacht_member(y, jv));
            TEST_ASSERT(yacht_member(y, jv));
        }
    }

    yacht_free(y, NULL, NULL);
    PASS();
}

TEST yacht_set_should_return_old_keys(uint8_t sz2) {
void test_yacht_should_add_and_remove_accurately_out_of_order8(void) {
    yacht_should_add_and_remove_accurately_out_of_order(8);
}
void test_yacht_should_add_and_remove_accurately_out_of_order10(void) {
    yacht_should_add_and_remove_accurately_out_of_order(10);
}
void test_yacht_should_add_and_remove_accurately_out_of_order14(void) {
    yacht_should_add_and_remove_accurately_out_of_order(14);
}

void yacht_set_should_return_old_keys(uint8_t sz2) {
    yacht *y = yacht_init(sz2);
    ASSERT(y);
    TEST_ASSERT(y);

    const uint64_t other_large_prime = 4294967279L; /* (2 ** 32) - 17 */

    for (int i = 0; i < (1 << (sz2 - 1)); i++) {
        int iv = i * other_large_prime;
        uintptr_t old = 0;
        ASSERT(yacht_set(y, iv, (void *)1, (void *)&old));
        ASSERT_EQ(0, old);
        ASSERT(yacht_set(y, iv, (void *)2, (void *)&old));
        ASSERT_EQ(1, old);
        TEST_ASSERT(yacht_set(y, iv, (void *)1, (void *)&old));
        TEST_ASSERT_EQUAL(0, old);
        TEST_ASSERT(yacht_set(y, iv, (void *)2, (void *)&old));
        TEST_ASSERT_EQUAL(1, old);
        uintptr_t val = 0;
        ASSERT(yacht_get(y, iv, (void *)&val));
        ASSERT_EQ(2, val);
        TEST_ASSERT(yacht_get(y, iv, (void *)&val));
        TEST_ASSERT_EQUAL(2, val);

        for (int j = 0; j < i; j++) {
            int jv = j * other_large_prime;
            ASSERT(yacht_member(y, jv));
            TEST_ASSERT(yacht_member(y, jv));
        }
    }

    yacht_free(y, NULL, NULL);
    PASS();
}

TEST yacht_should_grow_and_add_and_remove_accurately_out_of_order(uint8_t sz2) {
void test_yacht_set_should_return_old_keys8(void) {
    yacht_set_should_return_old_keys(8);
}

void test_yacht_set_should_return_old_keys10(void) {
    yacht_set_should_return_old_keys(10);
}

void test_yacht_set_should_return_old_keys14(void) {
    yacht_set_should_return_old_keys(14);
}

void yacht_should_grow_and_add_and_remove_accurately_out_of_order(uint8_t sz2) {
    yacht *y = yacht_init(0);   /* start at default small size */
    ASSERT(y);
    TEST_ASSERT(y);

    const uint64_t other_large_prime = 4294967279L; /* (2 ** 32) - 17 */

@@ -133,53 +167,40 @@ TEST yacht_should_grow_and_add_and_remove_accurately_out_of_order(uint8_t sz2) {
        int iv = i * other_large_prime;
        uintptr_t v = 1;
        void *old = NULL;
        ASSERT(yacht_set(y, iv, (void *)v, &old));
        ASSERT_EQ(NULL, old);
        TEST_ASSERT(yacht_set(y, iv, (void *)v, &old));
        TEST_ASSERT_EQUAL(NULL, old);

        ASSERT(yacht_member(y, iv));
        TEST_ASSERT(yacht_member(y, iv));

        void *old2 = NULL;
        ASSERT(yacht_remove(y, iv, &old2));
        ASSERT_EQ((void*)v, old2);
        TEST_ASSERT(yacht_remove(y, iv, &old2));
        TEST_ASSERT_EQUAL((void*)v, old2);

        ASSERT(!yacht_member(y, iv));
        TEST_ASSERT(!yacht_member(y, iv));
        /* Add it back, to ensure it isn't disturbed by other removes */
        ASSERT(yacht_set(y, iv, NULL, NULL));
        ASSERT(yacht_member(y, iv));
        TEST_ASSERT(yacht_set(y, iv, NULL, NULL));
        TEST_ASSERT(yacht_member(y, iv));

        for (int j = 0; j < i; j++) {
            int jv = j * other_large_prime;
            ASSERT(yacht_member(y, jv));
            TEST_ASSERT(yacht_member(y, jv));
        }
    }

    yacht_free(y, NULL, NULL);
    PASS();
}

SUITE(suite) {
    RUN_TEST(yacht_should_cleanly_init_and_free);
    RUN_TEST(yacht_should_add_and_have_accurate_membership);
    RUN_TESTp(yacht_should_add_and_remove_accurately, 8);
    RUN_TESTp(yacht_should_add_and_remove_accurately, 10);
    RUN_TESTp(yacht_should_add_and_remove_accurately, 14);
    RUN_TESTp(yacht_should_add_and_remove_accurately_out_of_order, 8);
    RUN_TESTp(yacht_should_add_and_remove_accurately_out_of_order, 10);
    RUN_TESTp(yacht_should_add_and_remove_accurately_out_of_order, 14);
    RUN_TESTp(yacht_set_should_return_old_keys, 8);
    RUN_TESTp(yacht_set_should_return_old_keys, 10);
    RUN_TESTp(yacht_set_should_return_old_keys, 14);
    RUN_TESTp(yacht_should_grow_and_add_and_remove_accurately_out_of_order, 8);
    RUN_TESTp(yacht_should_grow_and_add_and_remove_accurately_out_of_order, 10);
    RUN_TESTp(yacht_should_grow_and_add_and_remove_accurately_out_of_order, 14);

}

/* Add all the definitions that need to be in the test runner's main file. */
GREATEST_MAIN_DEFS();

int main(int argc, char **argv) {
    GREATEST_MAIN_BEGIN();      /* command-line arguments, initialization. */
    RUN_SUITE(suite);
    GREATEST_MAIN_END();        /* display results */
}

void test_yacht_should_grow_and_add_and_remove_accurately_out_of_order8(void)
{
    yacht_should_grow_and_add_and_remove_accurately_out_of_order(8);
}

void test_yacht_should_grow_and_add_and_remove_accurately_out_of_order10(void)
{
    yacht_should_grow_and_add_and_remove_accurately_out_of_order(10);
}

void test_yacht_should_grow_and_add_and_remove_accurately_out_of_order14(void)
{
    yacht_should_grow_and_add_and_remove_accurately_out_of_order(14);
}