diff --git a/lib/dice.c b/lib/dice.c index 17efc87..0767812 100644 --- a/lib/dice.c +++ b/lib/dice.c @@ -1,3 +1,22 @@ +/* + * This file is part of libdice. + * + * Copyright (C) 2018 Florian Stinglmayr + * + * libdice is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * libdice 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libdice. If not, see . + */ + #include "dice.h" #include "dice_parse.h" diff --git a/lib/dice.h b/lib/dice.h index b2033fd..342092b 100644 --- a/lib/dice.h +++ b/lib/dice.h @@ -1,3 +1,22 @@ +/* + * This file is part of libdice. + * + * Copyright (C) 2018 Florian Stinglmayr + * + * libdice is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * libdice 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libdice. If not, see . + */ + #ifndef LIBDICE_DICE_H #define LIBDICE_DICE_H diff --git a/lib/diceexpr.c b/lib/diceexpr.c index bacd646..6b23547 100644 --- a/lib/diceexpr.c +++ b/lib/diceexpr.c @@ -705,11 +705,13 @@ static void pn (const te_expr *n, int depth) { } } - static void te_print(const te_expr *n) { pn(n, 0); } +/* libdice specific functions + */ + struct dice_expression_ { te_expr *expr; diff --git a/tests/test_dice_evaluate.c b/tests/test_dice_evaluate.c index b9cb4a6..d1cab75 100644 --- a/tests/test_dice_evaluate.c +++ b/tests/test_dice_evaluate.c @@ -1,3 +1,22 @@ +/* + * This file is part of libdice. + * + * Copyright (C) 2018 Florian Stinglmayr + * + * libdice is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * libdice 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libdice. If not, see . + */ + #include #include diff --git a/tests/test_dice_parse.c b/tests/test_dice_parse.c index 3d044cc..8b26e67 100644 --- a/tests/test_dice_parse.c +++ b/tests/test_dice_parse.c @@ -1,3 +1,22 @@ +/* + * This file is part of libdice. + * + * Copyright (C) 2018 Florian Stinglmayr + * + * libdice is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * libdice 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libdice. If not, see . + */ + #include #include diff --git a/tests/test_dice_simple_roll.c b/tests/test_dice_simple_roll.c index f206ee2..f8036ed 100644 --- a/tests/test_dice_simple_roll.c +++ b/tests/test_dice_simple_roll.c @@ -1,3 +1,22 @@ +/* + * This file is part of libdice. + * + * Copyright (C) 2018 Florian Stinglmayr + * + * libdice is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * libdice 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libdice. If not, see . + */ + #include #include diff --git a/tests/test_expr_parse.c b/tests/test_expr_parse.c new file mode 100644 index 0000000..4204b8f --- /dev/null +++ b/tests/test_expr_parse.c @@ -0,0 +1,107 @@ +/* + * This file is part of libdice. + * + * Copyright (C) 2018 Florian Stinglmayr + * + * libdice is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * libdice 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libdice. If not, see . + */ + +#include + +#include +#include +#include +#include +#include + +static void test_expr_parse_none(void **arg) +{ + dice_expression_t e = NULL; + int error = 0; + + e = dice_expression_parse("", &error); + + assert_null(e); + assert_int_equal(error, 1); +} + +static void test_expr_parse_simple(void **arg) +{ + dice_expression_t e = NULL; + int error = 0, i = 0; + int64_t result = 0; + + e = dice_expression_parse("4d6", &error); + + assert_non_null(e); + assert_int_equal(error, 0); + + for (i = 0; i < 100000; i++) { + assert_true(dice_expression_evaluate(e, &result)); + assert_true(result >= 4 && result <= 32); + } + + dice_expression_free(e); +} + +static void test_expr_parse_modifier(void **arg) +{ + dice_expression_t e = NULL; + int error = 0, i = 0; + int64_t result = 0; + + e = dice_expression_parse("1d20+3+5", &error); + + assert_non_null(e); + assert_int_equal(error, 0); + + for (i = 0; i < 100000; i++) { + assert_true(dice_expression_evaluate(e, &result)); + assert_true(result >= 9 && result <= 28); + } + + dice_expression_free(e); +} + +static void test_expr_parse_complex(void **arg) +{ + dice_expression_t e = NULL; + int error = 0, i = 0; + int64_t result = 0; + + e = dice_expression_parse("1d8+(3+5)+1d6+5d4", &error); + + assert_non_null(e); + assert_int_equal(error, 0); + + for (i = 0; i < 100000; i++) { + assert_true(dice_expression_evaluate(e, &result)); + assert_true(result >= 15 && result <= 42); + } + + dice_expression_free(e); +} + + +int main(int ac, char **av) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_expr_parse_none), + cmocka_unit_test(test_expr_parse_simple), + cmocka_unit_test(test_expr_parse_modifier), + cmocka_unit_test(test_expr_parse_complex), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +}