|
| 1 | +/* -------------------------------------------------------------------------- * |
| 2 | + * OpenSim: testCommonUtilities.cpp * |
| 3 | + * -------------------------------------------------------------------------- * |
| 4 | + * The OpenSim API is a toolkit for musculoskeletal modeling and simulation. * |
| 5 | + * See http://opensim.stanford.edu and the NOTICE file for more information. * |
| 6 | + * OpenSim is developed at Stanford University and supported by the US * |
| 7 | + * National Institutes of Health (U54 GM072970, R24 HD065690) and by DARPA * |
| 8 | + * through the Warrior Web program. * |
| 9 | + * * |
| 10 | + * Copyright (c) 2005-2024 Stanford University and the Authors * |
| 11 | + * Author(s): Alexander Beattie * |
| 12 | + * * |
| 13 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may * |
| 14 | + * not use this file except in compliance with the License. You may obtain a * |
| 15 | + * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. * |
| 16 | + * * |
| 17 | + * Unless required by applicable law or agreed to in writing, software * |
| 18 | + * distributed under the License is distributed on an "AS IS" BASIS, * |
| 19 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * |
| 20 | + * See the License for the specific language governing permissions and * |
| 21 | + * limitations under the License. * |
| 22 | + * -------------------------------------------------------------------------- */ |
| 23 | + |
| 24 | +#include <OpenSim/Common/CommonUtilities.h> |
| 25 | + |
| 26 | +#include <algorithm> // std::transform |
| 27 | +#include <limits> // std::numeric_limits |
| 28 | +#include <vector> |
| 29 | +#include <numeric> // std::iota |
| 30 | +#include <cmath> // std::fma |
| 31 | + |
| 32 | +#include <catch2/catch_all.hpp> |
| 33 | + |
| 34 | +using namespace OpenSim; |
| 35 | + |
| 36 | +constexpr double tol = std::numeric_limits<double>::epsilon() * 10; |
| 37 | + |
| 38 | +TEST_CASE("createVectorLinspaceInterval produces correct output for integers", "[createVectorLinspaceInterval]") { |
| 39 | + auto result = createVectorLinspaceInterval<int>(5, 0, 2); |
| 40 | + std::vector<int> expected = {0, 2, 4, 6, 8}; |
| 41 | + REQUIRE_THAT(result, Catch::Matchers::Equals(expected)); |
| 42 | +} |
| 43 | + |
| 44 | +TEST_CASE("createVectorLinspaceInterval produces correct output for floats", "[createVectorLinspaceInterval]") { |
| 45 | + auto result = createVectorLinspaceInterval<float>(5, 1.0f, 0.5f); |
| 46 | + std::vector<float> expected = {1.0f, 1.5f, 2.0f, 2.5f, 3.0f}; |
| 47 | + REQUIRE_THAT(result, Catch::Matchers::Equals(expected)); |
| 48 | +} |
| 49 | + |
| 50 | +TEST_CASE("createVectorLinspaceInterval handles negative step size", "[createVectorLinspaceInterval]") { |
| 51 | + auto result = createVectorLinspaceInterval<double>(5, 10.0, -2.0); |
| 52 | + std::vector<double> expected = {10.0, 8.0, 6.0, 4.0, 2.0}; |
| 53 | + REQUIRE_THAT(result, Catch::Matchers::Equals(expected)); |
| 54 | +} |
| 55 | + |
| 56 | +TEST_CASE("createVectorLinspaceInterval handles zero length", "[createVectorLinspaceInterval]") { |
| 57 | + auto result = createVectorLinspaceInterval<double>(0, 0.0, 1.0); |
| 58 | + std::vector<double> expected = {}; |
| 59 | + REQUIRE_THAT(result, Catch::Matchers::Equals(expected)); |
| 60 | +} |
| 61 | + |
| 62 | +TEST_CASE("createVectorLinspaceInterval handles large step size", "[createVectorLinspaceInterval]") { |
| 63 | + auto result = createVectorLinspaceInterval<int>(5, 0, 100); |
| 64 | + std::vector<int> expected = {0, 100, 200, 300, 400}; |
| 65 | + REQUIRE_THAT(result, Catch::Matchers::Equals(expected)); |
| 66 | +} |
| 67 | + |
| 68 | +TEST_CASE("createVectorLinspaceInterval handles large length", "[createVectorLinspaceInterval]") { |
| 69 | + auto result = createVectorLinspaceInterval<double>(100, 0.0, 0.1); |
| 70 | + std::vector<double> expected(100); |
| 71 | + std::iota(expected.begin(), expected.end(), 0.0); |
| 72 | + std::transform(expected.begin(), expected.end(), expected.begin(), |
| 73 | + [](double value) { return value * 0.1; }); |
| 74 | + REQUIRE_THAT(result, Catch::Matchers::Equals(expected)); |
| 75 | +} |
| 76 | + |
| 77 | +TEST_CASE("createVectorLinspaceInterval produces correct output for small spacing with 10 elements", "[createVectorLinspaceInterval]") { |
| 78 | + auto result = createVectorLinspaceInterval<double>(10, 0.0, 0.001); |
| 79 | + std::vector<double> expected = {0.0, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009}; |
| 80 | + // Check that the sizes of the vectors are the same |
| 81 | + REQUIRE(result.size() == expected.size()); |
| 82 | + |
| 83 | + // Check that each result is within the specified tolerance of the expected value |
| 84 | + for (size_t i = 0; i < result.size(); ++i) { |
| 85 | + REQUIRE_THAT(result[i], Catch::Matchers::WithinAbs(expected[i], tol)); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +TEST_CASE("createVectorLinspaceInterval produces correct output for small spacing", "[createVectorLinspaceInterval]") { |
| 90 | + std::vector<double> expected(100); |
| 91 | + std::iota(expected.begin(), expected.end(), 0.0); |
| 92 | + std::transform(expected.begin(), expected.end(), expected.begin(), |
| 93 | + [](double value) { return value * 0.001; }); |
| 94 | + auto result = createVectorLinspaceInterval<double>(100, 0.0, 0.001); |
| 95 | + // Check that the sizes of the vectors are the same |
| 96 | + REQUIRE(result.size() == expected.size()); |
| 97 | + |
| 98 | + // Check that each result is within the specified tolerance of the expected value |
| 99 | + for (size_t i = 0; i < result.size(); ++i) { |
| 100 | + REQUIRE_THAT(result[i], Catch::Matchers::WithinAbs(expected[i], tol)); |
| 101 | + } |
| 102 | +} |
0 commit comments