libdice/tests/test_expr_parse.c

106 lines
2.8 KiB
C
Raw Normal View History

2018-02-17 11:27:15 +01:00
/*
* This file is part of libdice.
*
* Copyright (C) 2018 Florian Stinglmayr <florian@n0la.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <dice.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static void test_expr_parse_none(void **arg)
{
dice_expression_t e = dice_expression_new();
2018-02-17 11:27:15 +01:00
int error = 0;
assert_non_null(e);
assert_false(dice_expression_parse(e, "", &error));
2018-02-17 11:27:15 +01:00
assert_int_equal(error, 1);
dice_expression_free(e);
2018-02-17 11:27:15 +01:00
}
static void test_expr_parse_simple(void **arg)
{
dice_expression_t e = dice_expression_new();
2018-02-17 11:27:15 +01:00
int error = 0, i = 0;
int64_t result = 0;
assert_non_null(e);
assert_true(dice_expression_parse(e, "4d6", &error));
2018-02-17 11:27:15 +01:00
assert_int_equal(error, 0);
for (i = 0; i < 100000; i++) {
assert_true(dice_expression_roll(e, &result));
2018-02-17 11:27:15 +01:00
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();
2018-02-17 11:27:15 +01:00
int error = 0, i = 0;
int64_t result = 0;
assert_non_null(e);
assert_true(dice_expression_parse(e, "1d20+3+5", &error));
2018-02-17 11:27:15 +01:00
assert_int_equal(error, 0);
for (i = 0; i < 100000; i++) {
assert_true(dice_expression_roll(e, &result));
2018-02-17 11:27:15 +01:00
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();
2018-02-17 11:27:15 +01:00
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));
2018-02-17 11:27:15 +01:00
assert_int_equal(error, 0);
for (i = 0; i < 100000; i++) {
assert_true(dice_expression_roll(e, &result));
2018-02-17 11:27:15 +01:00
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);
}