Line data Source code
1 : #include <gmock/gmock.h>
2 : #include <gtest/gtest.h>
3 : #include <cassert>
4 : #include <cfloat>
5 :
6 : extern "C"
7 : {
8 : #include "dist.h"
9 : }
10 : #define DOUBLE_NEAR(x) DoubleNear((x), 4.0 * DBL_EPSILON)
11 :
12 : using namespace testing;
13 :
14 : class TestThatDDIST2 : public Test
15 : {
16 : public:
17 : int n = 10, incx = 1, incy = 1, kvec = 2;
18 : double *xvector = NULL;
19 : double *yvector = NULL;
20 :
21 7 : void SetUp() override
22 : {
23 7 : xvector = (double *)calloc((size_t)n, (size_t)sizeof(double));
24 : // if (xvector == NULL)
25 : //{
26 : // printf("Failed to allocate xvector memory!\n");
27 : // assert(xvector);
28 : //}
29 :
30 7 : yvector = (double *)calloc((size_t)n, (size_t)sizeof(double));
31 : // if (yvector == NULL)
32 : //{
33 : // printf("Failed to allocate yvector memory!\n");
34 : // assert(yvector);
35 : //}
36 7 : }
37 :
38 7 : void TearDown() override
39 : {
40 7 : free(yvector);
41 7 : free(xvector);
42 7 : }
43 : };
44 :
45 2 : TEST_F(TestThatDDIST2, ReturnsZeroWheneverTheDimensionNIsNotPositive)
46 : {
47 1 : n = 0;
48 1 : EXPECT_THAT(ddist2(n, xvector, incx, yvector, incy, kvec), DOUBLE_NEAR(0.0));
49 1 : n = -1;
50 1 : EXPECT_THAT(ddist2(n, xvector, incx, yvector, incy, kvec), DOUBLE_NEAR(0.0));
51 1 : n = -10;
52 1 : EXPECT_THAT(ddist2(n, xvector, incx, yvector, incy, kvec), DOUBLE_NEAR(0.0));
53 1 : }
54 :
55 2 : TEST_F(TestThatDDIST2,
56 : ReturnsZeroWheneverKVECEqualsAnyIntegerExceptTwoAndINCXEqualsZero)
57 : {
58 1 : kvec = 1;
59 1 : incx = 0;
60 1 : EXPECT_THAT(ddist2(n, xvector, incx, yvector, incy, kvec), DOUBLE_NEAR(0.0));
61 1 : }
62 :
63 2 : TEST_F(TestThatDDIST2,
64 : ReturnsZeroWheneverKVECEqualsTwoAndINCXEqualsZeroAndINCYEqualsZero)
65 : {
66 1 : kvec = 2;
67 1 : incx = 0;
68 1 : incy = 0;
69 1 : EXPECT_THAT(ddist2(n, xvector, incx, yvector, incy, kvec), DOUBLE_NEAR(0.0));
70 1 : }
71 :
72 2 : TEST_F(TestThatDDIST2,
73 : ReturnsZeroWheneverXVectorIsZeroAndYVectorIsZeroAtDefaultInputs)
74 : {
75 1 : EXPECT_THAT(ddist2(n, xvector, incx, yvector, incy, kvec), DOUBLE_NEAR(0.0));
76 1 : }
77 :
78 2 : TEST_F(TestThatDDIST2, ReturnsDistanceForSpecifiedXVectorAndYVector)
79 : {
80 1 : xvector[ 2 ] = 3.0;
81 1 : yvector[ 2 ] = 2.999999999999981;
82 :
83 1 : EXPECT_THAT(ddist2(n, xvector, incx, yvector, incy, kvec),
84 : DOUBLE_NEAR(1.898481372109018e-14));
85 :
86 1 : xvector[ 2 ] = 3.0;
87 1 : yvector[ 2 ] = 2.999999999999981;
88 1 : xvector[ 7 ] = 5.0;
89 1 : yvector[ 7 ] = 2.0;
90 1 : EXPECT_THAT(ddist2(n, xvector, incx, yvector, incy, kvec), DOUBLE_NEAR(3.0));
91 1 : }
92 :
93 2 : TEST_F(TestThatDDIST2, ReturnsDistanceForSpecifiedXVector)
94 : {
95 1 : xvector[ 2 ] = 3.0;
96 1 : EXPECT_THAT(ddist2(n, xvector, incx, yvector, incy, kvec), DOUBLE_NEAR(3.0));
97 :
98 1 : xvector[ 2 ] = 3.0;
99 1 : xvector[ 7 ] = 3.0;
100 1 : EXPECT_THAT(ddist2(n, xvector, incx, yvector, incy, kvec),
101 : DOUBLE_NEAR(4.242640687119286));
102 1 : }
103 :
104 2 : TEST_F(TestThatDDIST2, HasNoUnderflowNoOverflow)
105 : {
106 1 : const double cutlo = 4.44089e-16;
107 1 : const double cuthi = 1.30438e19;
108 :
109 1 : xvector[ 0 ] = 1.0;
110 1 : xvector[ 1 ] = 1.0;
111 1 : xvector[ 2 ] = cutlo;
112 1 : xvector[ 3 ] = 1.0;
113 1 : xvector[ 4 ] = 1.0;
114 1 : xvector[ 5 ] = cuthi;
115 1 : xvector[ 6 ] = 1.0;
116 1 : xvector[ 7 ] = cutlo;
117 1 : xvector[ 8 ] = 1.0;
118 :
119 1 : yvector[ 0 ] = 1.0;
120 1 : yvector[ 1 ] = 1.0;
121 1 : yvector[ 2 ] = cuthi;
122 1 : yvector[ 3 ] = 1.0;
123 1 : yvector[ 4 ] = 1.0;
124 1 : yvector[ 5 ] = cutlo;
125 1 : yvector[ 6 ] = 1.0;
126 1 : yvector[ 7 ] = cuthi;
127 1 : yvector[ 8 ] = 1.0;
128 1 : EXPECT_THAT(ddist2(n - 1, xvector, incx, yvector, incy, kvec),
129 : DOUBLE_NEAR(2.259252432376692e+19));
130 1 : }
131 :
132 : class TestThatDNRM2 : public Test
133 : {
134 : public:
135 : int n = 10, incx = 1;
136 : double *xvector = NULL;
137 :
138 5 : void SetUp() override
139 : {
140 5 : xvector = (double *)calloc((size_t)n, (size_t)sizeof(double));
141 : // if (xvector == NULL)
142 : //{
143 : // printf("Failed to allocate xvector memory!\n");
144 : // assert(xvector);
145 : //}
146 5 : }
147 :
148 5 : void TearDown() override { free(xvector); }
149 : };
150 :
151 2 : TEST_F(TestThatDNRM2, ReturnsZeroWheneverTheDimensionNIsNotPositive)
152 : {
153 1 : n = 0;
154 1 : EXPECT_THAT(dnrm2(n, xvector, incx), DOUBLE_NEAR(0.0));
155 1 : n = -1;
156 1 : EXPECT_THAT(dnrm2(n, xvector, incx), DOUBLE_NEAR(0.0));
157 1 : n = -10;
158 1 : EXPECT_THAT(dnrm2(n, xvector, incx), DOUBLE_NEAR(0.0));
159 1 : }
160 :
161 2 : TEST_F(TestThatDNRM2, ReturnsZeroWheneverINCXEqualsZero)
162 : {
163 1 : incx = 0;
164 1 : EXPECT_THAT(dnrm2(n, xvector, incx), DOUBLE_NEAR(0.0));
165 1 : }
166 :
167 2 : TEST_F(TestThatDNRM2, ReturnsZeroWheneverXVectorIsZeroAtDefaultInputs)
168 : {
169 1 : EXPECT_THAT(dnrm2(n, xvector, incx), DOUBLE_NEAR(0.0));
170 1 : }
171 :
172 2 : TEST_F(TestThatDNRM2, ReturnsDistanceForSpecifiedXVector)
173 : {
174 1 : xvector[ 2 ] = 3.0;
175 1 : EXPECT_THAT(dnrm2(n, xvector, incx), DOUBLE_NEAR(3.0));
176 :
177 1 : xvector[ 2 ] = 3.0;
178 1 : xvector[ 7 ] = 3.0;
179 1 : EXPECT_THAT(dnrm2(n, xvector, incx), DOUBLE_NEAR(4.242640687119286));
180 1 : }
181 :
182 2 : TEST_F(TestThatDNRM2, HasNoUnderflowNoOverflow)
183 : {
184 1 : const double cutlo = 4.44089e-16;
185 1 : const double cuthi = 1.30438e19;
186 :
187 1 : xvector[ 0 ] = 1.0;
188 1 : xvector[ 1 ] = 1.0;
189 1 : xvector[ 2 ] = cutlo;
190 1 : xvector[ 3 ] = 1.0;
191 1 : xvector[ 4 ] = 1.0;
192 1 : xvector[ 5 ] = cuthi;
193 1 : xvector[ 6 ] = 1.0;
194 1 : xvector[ 7 ] = cutlo;
195 1 : xvector[ 8 ] = 1.0;
196 :
197 1 : EXPECT_THAT(dnrm2(n - 1, xvector, incx), DOUBLE_NEAR(1.30438e+19));
198 1 : }
|