commit 24b84a59cb545064f176f3fed05822658103c954
parent d8d4008a1ebc67489f09e52e230c4ad64846cd14
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Thu, 2 Apr 2015 13:32:29 +0200
Refactor of the RNG tests
Diffstat:
4 files changed, 127 insertions(+), 172 deletions(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -82,7 +82,7 @@ rcmake_setup_devel(ssp StarSP ${VERSION} star/ssp_version.h)
# Add tests
################################################################################
if(NOT NO_TEST)
- function(new_test _name)
+ function(build_test _name)
add_executable(${_name}
${SSP_SOURCE_DIR}/${_name}.c
${SSP_SOURCE_DIR}/test_ssp_utils.h)
@@ -91,11 +91,16 @@ if(NOT NO_TEST)
foreach(_lib ${_libraries})
target_link_libraries(${_name} ${_lib})
endforeach(_lib)
+ endfunction()
+
+ function(new_test _name)
+ build_test(_name)
add_test(${_name} ${_name})
- endfunction(new_test)
+ endfunction()
- new_test(test_ssp_rng_kiss)
- new_test(test_ssp_rng_mt19937_64)
+ build_test(test_ssp_rng)
+ add_test(test_ssp_rng_kiss test_ssp_rng kiss)
+ add_test(test_ssp_rng_mt19937_64 test_ssp_rng mt19937_64)
endif()
################################################################################
diff --git a/src/test_ssp_rng.c b/src/test_ssp_rng.c
@@ -0,0 +1,118 @@
+/* Copyright (C) |Meso|Star> 2015 (contact@meso-star.com)
+ *
+ * This software is a program whose purpose is to test the spp library.
+ *
+ * This software is governed by the CeCILL-C license under French law and
+ * abiding by the rules of distribution of free software. You can use,
+ * modify and/or redistribute the software under the terms of the CeCILL-C
+ * license as circulated by CEA, CNRS and INRIA at the following URL
+ * "http://www.cecill.info".
+ *
+ * As a counterpart to the access to the source code and rights to copy,
+ * modify and redistribute granted by the license, users are provided only
+ * with a limited warranty and the software's author, the holder of the
+ * economic rights, and the successive licensors have only limited
+ * liability.
+ *
+ * In this respect, the user's attention is drawn to the risks associated
+ * with loading, using, modifying and/or developing or reproducing the
+ * software by the user in light of its specific status of free software,
+ * that may mean that it is complicated to manipulate, and that also
+ * therefore means that it is reserved for developers and experienced
+ * professionals having in-depth computer knowledge. Users are therefore
+ * encouraged to load and test the software's suitability as regards their
+ * requirements in conditions enabling the security of their systems and/or
+ * data to be ensured and, more generally, to use and operate it in the
+ * same conditions as regards security.
+ *
+ * The fact that you are presently reading this means that you have had
+ * knowledge of the CeCILL-C license and that you accept its terms. */
+
+#include "ssp.h"
+#include "test_ssp_utils.h"
+
+#include <string.h>
+
+#define NRAND 1024
+
+static void /* Really basic test */
+test_rng(struct ssp_rng_type* type)
+{
+ struct ssp_rng* rng;
+ struct mem_allocator allocator;
+ unsigned long datai0[NRAND];
+ unsigned long datai1[NRAND];
+ double dataf[NRAND];
+ int i, j;
+
+ mem_init_proxy_allocator(&allocator, &mem_default_allocator);
+
+ NCHECK(type, NULL);
+ CHECK(ssp_rng_create(NULL, NULL, NULL), RES_BAD_ARG);
+ CHECK(ssp_rng_create(&allocator, NULL, NULL), RES_BAD_ARG);
+ CHECK(ssp_rng_create(NULL, type, NULL), RES_BAD_ARG);
+ CHECK(ssp_rng_create(&allocator, type, NULL), RES_BAD_ARG);
+ CHECK(ssp_rng_create(NULL, NULL, &rng), RES_BAD_ARG);
+ CHECK(ssp_rng_create(&allocator, NULL, &rng), RES_BAD_ARG);
+ CHECK(ssp_rng_create(NULL, type, &rng), RES_OK);
+
+ CHECK(ssp_rng_ref_get(NULL), RES_BAD_ARG);
+ CHECK(ssp_rng_ref_get(rng), RES_OK);
+ CHECK(ssp_rng_ref_put(NULL), RES_BAD_ARG);
+ CHECK(ssp_rng_ref_put(rng), RES_OK);
+ CHECK(ssp_rng_ref_put(rng), RES_OK);
+
+ CHECK(ssp_rng_create(&allocator, type, &rng), RES_OK);
+ CHECK(ssp_rng_set(NULL, 0), RES_BAD_ARG);
+ CHECK(ssp_rng_set(rng, 0), RES_OK);
+
+ FOR_EACH(i, 0, NRAND) {
+ datai0[i] = ssp_rng_get(rng);
+ CHECK(datai0[i] >= ssp_rng_min(rng), 1);
+ CHECK(datai0[i] <= ssp_rng_max(rng), 1);
+ FOR_EACH(j, 0, i) {
+ NCHECK(datai0[i], datai0[j]);
+ }
+ }
+
+ CHECK(ssp_rng_set(rng, 0xDECAFBAD), RES_OK);
+ FOR_EACH(i, 0, NRAND) {
+ datai1[i] = ssp_rng_get(rng);
+ NCHECK(datai1[i], datai0[i]);
+ CHECK(datai1[i] >= ssp_rng_min(rng), 1);
+ CHECK(datai1[i] <= ssp_rng_max(rng), 1);
+ FOR_EACH(j, 0, i) {
+ NCHECK(datai1[i], datai1[j]);
+ }
+ }
+
+ FOR_EACH(i, 0, NRAND) {
+ dataf[i] = ssp_rng_get_canonical(rng);
+ CHECK(dataf[i] >= 0.0, 1);
+ CHECK(dataf[i] < 1.0, 1);
+ FOR_EACH(j, 0, i) {
+ NCHECK(dataf[i], dataf[j]);
+ }
+ }
+
+ CHECK(ssp_rng_ref_put(rng), RES_OK);
+ check_memory_allocator(&allocator);
+ mem_shutdown_proxy_allocator(&allocator);
+}
+
+int
+main(int argc, char** argv)
+{
+ if(argc <= 1) {
+ fprintf(stderr, "Usage: %s <kiss|mt19937_64>\n", argv[0]);
+ exit(0);
+ }
+ if(!strcmp(argv[1], "kiss")) {
+ test_rng(&ssp_rng_kiss);
+ } else if(!strcmp(argv[1], "mt19937_64")) {
+ test_rng(&ssp_rng_mt19937_64);
+ }
+ CHECK(mem_allocated_size(), 0);
+ return 0;
+}
+
diff --git a/src/test_ssp_rng_kiss.c b/src/test_ssp_rng_kiss.c
@@ -1,102 +0,0 @@
-/* Copyright (C) |Meso|Star> 2015 (contact@meso-star.com)
- *
- * This software is a program whose purpose is to test the spp library.
- *
- * This software is governed by the CeCILL-C license under French law and
- * abiding by the rules of distribution of free software. You can use,
- * modify and/or redistribute the software under the terms of the CeCILL-C
- * license as circulated by CEA, CNRS and INRIA at the following URL
- * "http://www.cecill.info".
- *
- * As a counterpart to the access to the source code and rights to copy,
- * modify and redistribute granted by the license, users are provided only
- * with a limited warranty and the software's author, the holder of the
- * economic rights, and the successive licensors have only limited
- * liability.
- *
- * In this respect, the user's attention is drawn to the risks associated
- * with loading, using, modifying and/or developing or reproducing the
- * software by the user in light of its specific status of free software,
- * that may mean that it is complicated to manipulate, and that also
- * therefore means that it is reserved for developers and experienced
- * professionals having in-depth computer knowledge. Users are therefore
- * encouraged to load and test the software's suitability as regards their
- * requirements in conditions enabling the security of their systems and/or
- * data to be ensured and, more generally, to use and operate it in the
- * same conditions as regards security.
- *
- * The fact that you are presently reading this means that you have had
- * knowledge of the CeCILL-C license and that you accept its terms. */
-
-#include "ssp.h"
-#include "test_ssp_utils.h"
-
-#define NRAND 1024
-
-int
-main(int argc, char** argv)
-{
- struct ssp_rng* rng;
- struct mem_allocator allocator;
- unsigned long datai0[NRAND];
- unsigned long datai1[NRAND];
- double dataf[NRAND];
- int i, j;
- (void)argc, (void)argv;
-
- mem_init_proxy_allocator(&allocator, &mem_default_allocator);
-
- CHECK(ssp_rng_create(NULL, NULL, NULL), RES_BAD_ARG);
- CHECK(ssp_rng_create(&allocator, NULL, NULL), RES_BAD_ARG);
- CHECK(ssp_rng_create(NULL, &ssp_rng_kiss, NULL), RES_BAD_ARG);
- CHECK(ssp_rng_create(&allocator, &ssp_rng_kiss, NULL), RES_BAD_ARG);
- CHECK(ssp_rng_create(NULL, NULL, &rng), RES_BAD_ARG);
- CHECK(ssp_rng_create(&allocator, NULL, &rng), RES_BAD_ARG);
- CHECK(ssp_rng_create(NULL, &ssp_rng_kiss, &rng), RES_OK);
-
- CHECK(ssp_rng_ref_get(NULL), RES_BAD_ARG);
- CHECK(ssp_rng_ref_get(rng), RES_OK);
- CHECK(ssp_rng_ref_put(NULL), RES_BAD_ARG);
- CHECK(ssp_rng_ref_put(rng), RES_OK);
- CHECK(ssp_rng_ref_put(rng), RES_OK);
-
- CHECK(ssp_rng_create(&allocator, &ssp_rng_kiss, &rng), RES_OK);
- CHECK(ssp_rng_set(NULL, 0), RES_BAD_ARG);
- CHECK(ssp_rng_set(rng, 0), RES_OK);
-
- FOR_EACH(i, 0, NRAND) datai0[i] = ssp_rng_get(rng);
- FOR_EACH(i, 0, NRAND) {
- FOR_EACH(j, 0, NRAND) {
- if(i == j) continue;
- NCHECK(datai0[i], datai0[j]);
- }
- }
- CHECK(ssp_rng_set(rng, 0xDECAFBAD), RES_OK);
- FOR_EACH(i, 0, NRAND) datai1[i] = ssp_rng_get(rng);
- FOR_EACH(i, 0, NRAND) {
- NCHECK(datai0[i], datai1[i]);
- FOR_EACH(j, 0, NRAND) {
- if(i == j) continue;
- NCHECK(datai1[i], datai1[j]);
- }
- }
- FOR_EACH(i, 0, NRAND)
- dataf[i] = ssp_rng_get_canonical(rng);
- FOR_EACH(i, 0, NRAND) {
- CHECK(dataf[i] >= 0.0, 1);
- CHECK(dataf[i] < 1.0, 1);
- FOR_EACH(j, 0, NRAND) {
- if(i == j) continue;
- NCHECK(dataf[i], dataf[j]);
- }
- }
- CHECK(ssp_rng_min(rng), 0);
- CHECK(ssp_rng_max(rng), UINT32_MAX);
- CHECK(ssp_rng_ref_put(rng), RES_OK);
-
- check_memory_allocator(&allocator);
- mem_shutdown_proxy_allocator(&allocator);
- CHECK(mem_allocated_size(), 0);
- return 0;
-}
-
diff --git a/src/test_ssp_rng_mt19937_64.c b/src/test_ssp_rng_mt19937_64.c
@@ -1,66 +0,0 @@
-/* Copyright (C) |Meso|Star> 2015 (contact@meso-star.com)
- *
- * This software is a library whose purpose is to generate [pseudo] random
- * numbers and random variates.
- *
- * This software is governed by the CeCILL-C license under French law and
- * abiding by the rules of distribution of free software. You can use,
- * modify and/or redistribute the software under the terms of the CeCILL-C
- * license as circulated by CEA, CNRS and INRIA at the following URL
- * "http://www.cecill.info".
- *
- * As a counterpart to the access to the source code and rights to copy,
- * modify and redistribute granted by the license, users are provided only
- * with a limited warranty and the software's author, the holder of the
- * economic rights, and the successive licensors have only limited
- * liability.
- *
- * In this respect, the user's attention is drawn to the risks associated
- * with loading, using, modifying and/or developing or reproducing the
- * software by the user in light of its specific status of free software,
- * that may mean that it is complicated to manipulate, and that also
- * therefore means that it is reserved for developers and experienced
- * professionals having in-depth computer knowledge. Users are therefore
- * encouraged to load and test the software's suitability as regards their
- * requirements in conditions enabling the security of their systems and/or
- * data to be ensured and, more generally, to use and operate it in the
- * same conditions as regards security.
- *
- * The fact that you are presently reading this means that you have had
- * knowledge of the CeCILL-C license and that you accept its terms. */
-
-#include "ssp.h"
-#include "test_ssp_utils.h"
-
-#define NRAND 1024
-
-int
-main(int argc, char** argv)
-{
- struct ssp_rng* rng;
- struct mem_allocator allocator;
- int i;
- (void)argc, (void)argv;
-
- mem_init_proxy_allocator(&allocator, &mem_default_allocator);
-
- CHECK(ssp_rng_create(&allocator, &ssp_rng_mt19937_64, &rng), RES_OK);
- CHECK(ssp_rng_set(rng, 0xDECAFBAD), RES_OK);
- FOR_EACH(i, 0, NRAND) {
- const unsigned long l = ssp_rng_get(rng);
- const double d = ssp_rng_get_canonical(rng);
- CHECK(l >= ssp_rng_min(rng), 1);
- CHECK(l <= ssp_rng_max(rng), 1);
- CHECK(d >= 0.0, 1);
- CHECK(d < 1.0, 1);
- }
-
- CHECK(ssp_rng_min(rng), 0);
- CHECK(ssp_rng_max(rng), UINT64_MAX);
- CHECK(ssp_rng_ref_put(rng), RES_OK);
-
- check_memory_allocator(&allocator);
- mem_shutdown_proxy_allocator(&allocator);
- CHECK(mem_allocated_size(), 0);
- return 0;
-}