/* * 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 = dice_expression_new(); int error = 0; assert_non_null(e); assert_false(dice_expression_parse(e, "", &error)); assert_int_equal(error, 1); dice_expression_free(e); } static void test_expr_parse_simple(void **arg) { dice_expression_t e = dice_expression_new(); int error = 0, i = 0; int64_t result = 0; assert_non_null(e); assert_true(dice_expression_parse(e, "4d6", &error)); assert_int_equal(error, 0); for (i = 0; i < 100000; i++) { assert_true(dice_expression_roll(e, &result)); assert_true(result >= 4 && result <= 32); } dice_expression_free(e); } static void test_expr_parse_modifier(void **arg) { dice_expression_t e = dice_expression_new(); int error = 0, i = 0; int64_t result = 0; assert_non_null(e); assert_true(dice_expression_parse(e, "1d20+3+5", &error)); assert_int_equal(error, 0); for (i = 0; i < 100000; i++) { assert_true(dice_expression_roll(e, &result)); assert_true(result >= 9 && result <= 28); } dice_expression_free(e); } static void test_expr_parse_complex(void **arg) { dice_expression_t e = dice_expression_new(); int error = 0, i = 0; int64_t result = 0; assert_non_null(e); assert_true(dice_expression_parse(e, "1d8+(3+5)+1d6+5d4", &error)); assert_int_equal(error, 0); for (i = 0; i < 100000; i++) { assert_true(dice_expression_roll(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); }