first version of libdice
This commit is contained in:
8
tests/Makefile.am
Normal file
8
tests/Makefile.am
Normal file
@@ -0,0 +1,8 @@
|
||||
bin_PROGRAMS = test_dice_simple_roll \
|
||||
test_dice_parse \
|
||||
test_dice_evaluate
|
||||
|
||||
AM_CFLAGS = -I../lib ${CMOCKA_CFLAGS}
|
||||
AM_LDFLAGS = ${CMOCKA_LIBS} ../libdice.la
|
||||
|
||||
TESTS = $(bin_PROGRAMS)
|
||||
72
tests/test_dice_evaluate.c
Normal file
72
tests/test_dice_evaluate.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <dice.h>
|
||||
|
||||
#include <setjmp.h>
|
||||
#include <cmocka.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static void test_dice_evaluate_null(void **data)
|
||||
{
|
||||
dice_t d = NULL;
|
||||
dice_result_t *r = NULL;
|
||||
size_t rlen = 0;
|
||||
|
||||
assert_false(dice_evaluate(d, &r, &rlen));
|
||||
}
|
||||
|
||||
static void test_dice_evaluate_one(void **data)
|
||||
{
|
||||
dice_t d = NULL;
|
||||
dice_result_t *r = NULL;
|
||||
size_t rlen = 0;
|
||||
|
||||
d = dice_simple(1, 10);
|
||||
|
||||
assert_non_null(d);
|
||||
assert_true(dice_evaluate(d, &r, &rlen));
|
||||
assert_non_null(r);
|
||||
|
||||
assert_int_equal(rlen, 1);
|
||||
assert_true(r[0].result >= 1 && r[0].result <= 10);
|
||||
|
||||
dice_free(d);
|
||||
dice_result_freev(r, rlen);
|
||||
}
|
||||
|
||||
static void test_dice_evaluate_many(void **data)
|
||||
{
|
||||
dice_t d = NULL;
|
||||
dice_result_t *r = NULL;
|
||||
size_t rlen = 0;
|
||||
const int amount = 150;
|
||||
int i = 0;
|
||||
|
||||
/* your average evening playing Shadowrun...
|
||||
*/
|
||||
d = dice_simple(amount, 6);
|
||||
|
||||
assert_non_null(d);
|
||||
assert_true(dice_evaluate(d, &r, &rlen));
|
||||
assert_non_null(r);
|
||||
|
||||
assert_int_equal(rlen, amount);
|
||||
|
||||
for (i = 0; i < amount; i++) {
|
||||
assert_true(r[i].result >= 1 && r[i].result <= 6);
|
||||
}
|
||||
|
||||
dice_free(d);
|
||||
dice_result_freev(r, rlen);
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_dice_evaluate_null),
|
||||
cmocka_unit_test(test_dice_evaluate_one),
|
||||
cmocka_unit_test(test_dice_evaluate_many),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
60
tests/test_dice_parse.c
Normal file
60
tests/test_dice_parse.c
Normal file
@@ -0,0 +1,60 @@
|
||||
#include <dice.h>
|
||||
|
||||
#include <setjmp.h>
|
||||
#include <cmocka.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static void test_dice_parse_none(void **data)
|
||||
{
|
||||
dice_t d = dice_parse("");
|
||||
assert_null(d);
|
||||
}
|
||||
|
||||
static void test_dice_parse_amount(void **data)
|
||||
{
|
||||
dice_t d = dice_parse("3d");
|
||||
assert_null(d);
|
||||
}
|
||||
|
||||
static void test_dice_parse_amount_sides(void **data)
|
||||
{
|
||||
dice_t d = dice_parse("5d10");
|
||||
int i = 0;
|
||||
|
||||
assert_non_null(d);
|
||||
|
||||
assert_true(dice_get(d, DICEOPTION_AMOUNT, &i));
|
||||
assert_int_equal(i, 5);
|
||||
|
||||
assert_true(dice_get(d, DICEOPTION_SIDES, &i));
|
||||
assert_int_equal(i, 10);
|
||||
|
||||
dice_free(d);
|
||||
}
|
||||
|
||||
static void test_dice_parse_sides(void **data)
|
||||
{
|
||||
dice_t d = dice_parse("d12");
|
||||
int i = 0;
|
||||
|
||||
assert_non_null(d);
|
||||
|
||||
assert_true(dice_get(d, DICEOPTION_SIDES, &i));
|
||||
assert_int_equal(i, 12);
|
||||
|
||||
dice_free(d);
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_dice_parse_none),
|
||||
cmocka_unit_test(test_dice_parse_amount),
|
||||
cmocka_unit_test(test_dice_parse_amount_sides),
|
||||
cmocka_unit_test(test_dice_parse_sides),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
113
tests/test_dice_simple_roll.c
Normal file
113
tests/test_dice_simple_roll.c
Normal file
@@ -0,0 +1,113 @@
|
||||
#include <dice.h>
|
||||
|
||||
#include <setjmp.h>
|
||||
#include <cmocka.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static void test_dice_simple_null(void **data)
|
||||
{
|
||||
dice_t d = dice_simple(0, 0);
|
||||
assert_null(d);
|
||||
}
|
||||
|
||||
static void test_dice_simple_one(void **data)
|
||||
{
|
||||
dice_t d = dice_simple(1, 20);
|
||||
assert_non_null(d);
|
||||
dice_free(d);
|
||||
}
|
||||
|
||||
static void test_dice_simple_roll(void **data)
|
||||
{
|
||||
dice_t d = dice_simple(1, 20);
|
||||
int i = 0;
|
||||
|
||||
assert_non_null(d);
|
||||
|
||||
for (i = 0; i < 100000; i++) {
|
||||
int64_t res = dice_roll(d);
|
||||
assert_true(res >= 1 && res <= 20);
|
||||
}
|
||||
|
||||
dice_free(d);
|
||||
}
|
||||
|
||||
static void test_dice_simple_multi(void **data)
|
||||
{
|
||||
dice_t d = dice_simple(3, 6);
|
||||
int i = 0;
|
||||
|
||||
assert_non_null(d);
|
||||
|
||||
for (i = 0; i < 100000; i++) {
|
||||
int64_t res = dice_roll(d);
|
||||
assert_true(res >= 3 && res <= 18);
|
||||
}
|
||||
|
||||
dice_free(d);
|
||||
}
|
||||
|
||||
static void test_dice_simple_get(void **data)
|
||||
{
|
||||
dice_t d = dice_simple(8, 10);
|
||||
int i = 0;
|
||||
|
||||
assert_non_null(d);
|
||||
|
||||
assert_true(dice_get(d, DICEOPTION_AMOUNT, &i));
|
||||
assert_int_equal(i, 8);
|
||||
|
||||
assert_true(dice_get(d, DICEOPTION_SIDES, &i));
|
||||
assert_int_equal(i, 10);
|
||||
|
||||
dice_free(d);
|
||||
}
|
||||
|
||||
static void test_dice_simple_set(void **data)
|
||||
{
|
||||
dice_t d = dice_simple(1, 20);
|
||||
int i = 0;
|
||||
|
||||
assert_non_null(d);
|
||||
|
||||
assert_true(dice_set(d, DICEOPTION_AMOUNT, 20));
|
||||
assert_true(dice_set(d, DICEOPTION_SIDES, 4));
|
||||
|
||||
assert_true(dice_get(d, DICEOPTION_AMOUNT, &i));
|
||||
assert_int_equal(i, 20);
|
||||
|
||||
assert_true(dice_get(d, DICEOPTION_SIDES, &i));
|
||||
assert_int_equal(i, 4);
|
||||
|
||||
dice_free(d);
|
||||
}
|
||||
|
||||
static void test_dice_simple_invalid_getset(void **data)
|
||||
{
|
||||
dice_t d = dice_simple(1, 20);
|
||||
int i = 0;
|
||||
|
||||
assert_non_null(d);
|
||||
|
||||
assert_false(dice_set(d, 9001, 20));
|
||||
assert_false(dice_get(d, 9001, &i));
|
||||
|
||||
dice_free(d);
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_dice_simple_null),
|
||||
cmocka_unit_test(test_dice_simple_one),
|
||||
cmocka_unit_test(test_dice_simple_roll),
|
||||
cmocka_unit_test(test_dice_simple_multi),
|
||||
cmocka_unit_test(test_dice_simple_set),
|
||||
cmocka_unit_test(test_dice_simple_get),
|
||||
cmocka_unit_test(test_dice_simple_invalid_getset),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
Reference in New Issue
Block a user