blob: 6c0ea8432f63e4c6314c70631d7945b22ac233fe [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2015 Google Inc. All rights reserved.
3// http://ceres-solver.org/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors may be
14// used to endorse or promote products derived from this software without
15// specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28//
29// Author: sameeragarwal@google.com (Sameer Agarwal)
30
31#include "ceres/array_utils.h"
32
Austin Schuh70cc9552019-01-21 19:46:48 -080033#include <cmath>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080034#include <limits>
Austin Schuh70cc9552019-01-21 19:46:48 -080035#include <vector>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080036
Austin Schuh70cc9552019-01-21 19:46:48 -080037#include "gtest/gtest.h"
38
39namespace ceres {
40namespace internal {
41
42using std::vector;
43
44TEST(ArrayUtils, IsArrayValid) {
45 double x[3];
46 x[0] = 0.0;
47 x[1] = 1.0;
48 x[2] = 2.0;
49 EXPECT_TRUE(IsArrayValid(3, x));
50 x[1] = std::numeric_limits<double>::infinity();
51 EXPECT_FALSE(IsArrayValid(3, x));
52 x[1] = std::numeric_limits<double>::quiet_NaN();
53 EXPECT_FALSE(IsArrayValid(3, x));
54 x[1] = std::numeric_limits<double>::signaling_NaN();
55 EXPECT_FALSE(IsArrayValid(3, x));
56 EXPECT_TRUE(IsArrayValid(1, NULL));
57 InvalidateArray(3, x);
58 EXPECT_FALSE(IsArrayValid(3, x));
59}
60
61TEST(ArrayUtils, FindInvalidIndex) {
62 double x[3];
63 x[0] = 0.0;
64 x[1] = 1.0;
65 x[2] = 2.0;
66 EXPECT_EQ(FindInvalidValue(3, x), 3);
67 x[1] = std::numeric_limits<double>::infinity();
68 EXPECT_EQ(FindInvalidValue(3, x), 1);
69 x[1] = std::numeric_limits<double>::quiet_NaN();
70 EXPECT_EQ(FindInvalidValue(3, x), 1);
71 x[1] = std::numeric_limits<double>::signaling_NaN();
72 EXPECT_EQ(FindInvalidValue(3, x), 1);
73 EXPECT_EQ(FindInvalidValue(1, NULL), 1);
74 InvalidateArray(3, x);
75 EXPECT_EQ(FindInvalidValue(3, x), 0);
76}
77
78TEST(MapValuesToContiguousRange, ContiguousEntries) {
79 vector<int> array;
80 array.push_back(0);
81 array.push_back(1);
82 vector<int> expected = array;
83 MapValuesToContiguousRange(array.size(), &array[0]);
84 EXPECT_EQ(array, expected);
85 array.clear();
86
87 array.push_back(1);
88 array.push_back(0);
89 expected = array;
90 MapValuesToContiguousRange(array.size(), &array[0]);
91 EXPECT_EQ(array, expected);
92}
93
94TEST(MapValuesToContiguousRange, NonContiguousEntries) {
95 vector<int> array;
96 array.push_back(0);
97 array.push_back(2);
98 vector<int> expected;
99 expected.push_back(0);
100 expected.push_back(1);
101 MapValuesToContiguousRange(array.size(), &array[0]);
102 EXPECT_EQ(array, expected);
103}
104
105TEST(MapValuesToContiguousRange, NonContiguousRepeatingEntries) {
106 vector<int> array;
107 array.push_back(3);
108 array.push_back(1);
109 array.push_back(0);
110 array.push_back(0);
111 array.push_back(0);
112 array.push_back(5);
113 vector<int> expected;
114 expected.push_back(2);
115 expected.push_back(1);
116 expected.push_back(0);
117 expected.push_back(0);
118 expected.push_back(0);
119 expected.push_back(3);
120 MapValuesToContiguousRange(array.size(), &array[0]);
121 EXPECT_EQ(array, expected);
122}
123
124} // namespace internal
125} // namespace ceres