Mercurial > molko
changeset 287:75d2fdf96064
util: nrand no longer contains upper range
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 04 Jan 2021 11:34:18 +0100 |
parents | 3991779aaba9 |
children | cc0f02ae9005 |
files | libmlk-core/core/util.c tests/CMakeLists.txt tests/test-util.c |
diffstat | 3 files changed, 66 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/libmlk-core/core/util.c Wed Dec 23 15:37:48 2020 +0100 +++ b/libmlk-core/core/util.c Mon Jan 04 11:34:18 2021 +0100 @@ -48,5 +48,5 @@ { assert(upper <= RAND_MAX); - return (rand() % (upper - lower + 1)) + lower; + return (rand() % (upper - lower)) + lower; }
--- a/tests/CMakeLists.txt Wed Dec 23 15:37:48 2020 +0100 +++ b/tests/CMakeLists.txt Mon Jan 04 11:34:18 2021 +0100 @@ -40,3 +40,5 @@ SOURCES test-tileset.c FLAGS DIRECTORY="${tests_SOURCE_DIR}/assets/maps/" ) + +molko_define_test(TARGET util SOURCES test-util.c)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test-util.c Mon Jan 04 11:34:18 2021 +0100 @@ -0,0 +1,63 @@ +/* + * test-util.c -- test utilities + * + * Copyright (c) 2020 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <stdbool.h> + +#define GREATEST_USE_ABBREVS 0 +#include <greatest.h> + +#include <core/util.h> + +GREATEST_TEST +nrand_simple(void) +{ + bool found[10] = {false}; + + /* Only try from [2..5], util_nrand does not include upper range. */ + for (int i = 0; i < 100000; ++i) + found[util_nrand(2, 6)] = true; + + GREATEST_ASSERT_EQ(false, found[0]); + GREATEST_ASSERT_EQ(false, found[1]); + GREATEST_ASSERT_EQ(true, found[2]); + GREATEST_ASSERT_EQ(true, found[3]); + GREATEST_ASSERT_EQ(true, found[4]); + GREATEST_ASSERT_EQ(true, found[5]); + GREATEST_ASSERT_EQ(false, found[7]); + GREATEST_ASSERT_EQ(false, found[8]); + GREATEST_ASSERT_EQ(false, found[9]); + + GREATEST_PASS(); +} + +GREATEST_SUITE(suite_nrand) +{ + GREATEST_RUN_TEST(nrand_simple); +} + +GREATEST_MAIN_DEFS(); + +int +main(int argc, char **argv) +{ + GREATEST_MAIN_BEGIN(); + GREATEST_RUN_SUITE(suite_nrand); + GREATEST_MAIN_END(); + + return 0; +}