add tiny expression library and make it a dice expression parser

Use tiny expression library (from Github) and expand it to provide
dice expression parsing. This needs more testing obviously.
This commit is contained in:
2018-02-17 10:52:46 +01:00
parent 25227d5c6c
commit 92b909158c
9 changed files with 853 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
bin_PROGRAMS = test_dice_simple_roll \
test_dice_parse \
test_dice_evaluate
test_dice_evaluate \
test_expr_parse
AM_CFLAGS = -I../lib ${CMOCKA_CFLAGS}
AM_LDFLAGS = ${CMOCKA_LIBS} ../libdice.la

View File

@@ -6,6 +6,10 @@
#include <string.h>
#include <stdlib.h>
/* private library function
*/
int dice_consumed(dice_t d);
static void test_dice_parse_none(void **data)
{
dice_t d = dice_parse("");
@@ -31,6 +35,8 @@ static void test_dice_parse_amount_sides(void **data)
assert_true(dice_get(d, DICEOPTION_SIDES, &i));
assert_int_equal(i, 10);
assert_int_equal(dice_consumed(d), 4);
dice_free(d);
}
@@ -44,6 +50,27 @@ static void test_dice_parse_sides(void **data)
assert_true(dice_get(d, DICEOPTION_SIDES, &i));
assert_int_equal(i, 12);
assert_int_equal(dice_consumed(d), 3);
dice_free(d);
}
static void test_dice_parse_big(void **data)
{
char const *dice_str = "1000d120000";
dice_t d = dice_parse(dice_str);
int i = 0;
assert_non_null(d);
assert_true(dice_get(d, DICEOPTION_SIDES, &i));
assert_int_equal(i, 120000);
assert_true(dice_get(d, DICEOPTION_AMOUNT, &i));
assert_int_equal(i, 1000);
assert_int_equal(dice_consumed(d), strlen(dice_str));
dice_free(d);
}
@@ -54,6 +81,7 @@ int main(int ac, char **av)
cmocka_unit_test(test_dice_parse_amount),
cmocka_unit_test(test_dice_parse_amount_sides),
cmocka_unit_test(test_dice_parse_sides),
cmocka_unit_test(test_dice_parse_big),
};
return cmocka_run_group_tests(tests, NULL, NULL);