commit 7b33f058c273c8173e20d44f6b3e44a1a561b86d
parent 818b520e038f667730618f02cae7a6c9b0247ebc
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Wed, 1 Apr 2015 11:39:56 +0200
Test the smc_solve function
Diffstat:
2 files changed, 117 insertions(+), 0 deletions(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -91,6 +91,7 @@ if(NOT NO_TEST)
endfunction(new_test)
new_test(test_smc_device)
+ new_test(test_smc_solve m)
endif()
################################################################################
diff --git a/src/test_smc_solve.c b/src/test_smc_solve.c
@@ -0,0 +1,116 @@
+/* Copyright (C) |Meso|Star> 2015 (contact@meso-star.com)
+ *
+ * This software is a computer program whose purpose is to manage the
+ * statistical estimation of a function.
+ *
+ * This software is governed by the CeCILL 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
+ * 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 license and that you accept its terms. */
+
+#include "smc.h"
+#include "test_smc_utils.h"
+
+#include <rsys/math.h>
+
+#include <math.h>
+
+static void
+rcp_x(void* value, void* ctx)
+{
+ float* result = value;
+ /* samp in [1, 2] */
+ float samp = ((float)rand() / (float)RAND_MAX) + 1.f;
+ CHECK(ctx, NULL);
+ (void)ctx;
+ *result = 1.f / samp;
+}
+
+static void
+cos_x(void* value, void* ctx)
+{
+ float* result = value;
+ /* samp in [PI/4, 3PI/4] */
+ float samp = ((float)rand() / (float)RAND_MAX) * (float)(2.0*PI/4.0 + PI/4.0);
+ CHECK(ctx, (void*)0xC0DE);
+ (void)ctx;
+ *result = (float)cos(samp);
+}
+
+int
+main(int argc, char** argv)
+{
+ struct mem_allocator allocator;
+ struct smc_device* dev;
+ struct smc_estimator* estimator;
+ struct smc_estimator_status status;
+ (void)argc, (void)argv;
+
+ mem_init_proxy_allocator(&allocator, &mem_default_allocator);
+
+ CHECK(smc_device_create(NULL, &allocator, &dev), RES_OK);
+
+ CHECK(smc_solve(NULL, NULL, NULL, NULL, NULL), RES_BAD_ARG);
+ CHECK(smc_solve(dev, NULL, NULL, NULL, NULL), RES_BAD_ARG);
+ CHECK(smc_solve(NULL, rcp_x, NULL, NULL, NULL), RES_BAD_ARG);
+ CHECK(smc_solve(dev, rcp_x, NULL, NULL, NULL), RES_BAD_ARG);
+ CHECK(smc_solve(NULL, NULL, &smc_float, NULL, NULL), RES_BAD_ARG);
+ CHECK(smc_solve(dev, NULL, &smc_float, NULL, NULL), RES_BAD_ARG);
+ CHECK(smc_solve(NULL, rcp_x, &smc_float, NULL, NULL), RES_BAD_ARG);
+ CHECK(smc_solve(dev, rcp_x, &smc_float, NULL, NULL), RES_BAD_ARG);
+
+ CHECK(smc_solve(NULL, NULL, NULL, NULL, &estimator), RES_BAD_ARG);
+ CHECK(smc_solve(dev, NULL, NULL, NULL, &estimator), RES_BAD_ARG);
+ CHECK(smc_solve(NULL, rcp_x, NULL, NULL, &estimator), RES_BAD_ARG);
+ CHECK(smc_solve(dev, rcp_x, NULL, NULL, &estimator), RES_BAD_ARG);
+ CHECK(smc_solve(NULL, NULL, &smc_float, NULL, &estimator), RES_BAD_ARG);
+ CHECK(smc_solve(dev, NULL, &smc_float, NULL, &estimator), RES_BAD_ARG);
+ CHECK(smc_solve(NULL, rcp_x, &smc_float, NULL, &estimator), RES_BAD_ARG);
+ CHECK(smc_solve(dev, rcp_x, &smc_float, NULL, &estimator), RES_OK);
+
+ CHECK(smc_estimator_get_status(NULL, NULL), RES_BAD_ARG);
+ CHECK(smc_estimator_get_status(estimator, NULL), RES_BAD_ARG);
+ CHECK(smc_estimator_get_status(NULL, &status), RES_BAD_ARG);
+ CHECK(smc_estimator_get_status(estimator, &status), RES_OK);
+ CHECK(eq_eps
+ ((float)(log(2.f) - log(1.f)),
+ SMC_FLOAT(status.expected_value),
+ SMC_FLOAT(status.standard_error)), 1);
+ CHECK(smc_estimator_ref_put(estimator), RES_OK);
+
+ CHECK(smc_solve(dev, cos_x, &smc_float, (void*)0xC0DE, &estimator), RES_OK);
+ CHECK(eq_eps
+ ((float)(sin(3.0*PI/4.0) - sin(PI/4.0)),
+ SMC_FLOAT(status.expected_value),
+ SMC_FLOAT(status.standard_error)), 1);
+ CHECK(smc_device_ref_put(dev), RES_OK);
+
+ CHECK(smc_estimator_ref_put(estimator), RES_OK);
+
+ check_memory_allocator(&allocator);
+ mem_shutdown_proxy_allocator(&allocator);
+ CHECK(mem_allocated_size(), 0);
+ return 0;
+}
+