testSparseVector.cc
Go to the documentation of this file.
1 
11 #include <iostream>
12 
13 #include <utl/SparseVector.h>
14 
15 #include <tst/Verify.h>
16 #include <cppunit/extensions/HelperMacros.h>
17 
18 using namespace std;
19 using namespace utl;
20 using namespace tst;
21 
22 #define ASSERT_CLOSE(x, y) CPPUNIT_ASSERT(Verify<CloseTo>(x, y))
23 #define ASSERT_CLOSE_EPS(x, y, eps) CPPUNIT_ASSERT(Verify<CloseTo>(x, y, eps))
24 #define ASSERT_EQUAL(x, y) CPPUNIT_ASSERT(Verify<Equal>(x, y))
25 #define ASSERT_ITERATOR_EQUAL(it, e, i, v) \
26  CPPUNIT_ASSERT(it != e); \
27  ASSERT_EQUAL(it.GetIndex(), i); \
28  ASSERT_EQUAL(it(), v)
29 
30 
34 class TestSparseVector : public CppUnit::TestFixture {
35 
36  CPPUNIT_TEST_SUITE(TestSparseVector);
37  CPPUNIT_TEST(TestAccess);
38  CPPUNIT_TEST(TestIterators);
39  CPPUNIT_TEST(TestDotProduct);
40  CPPUNIT_TEST(TestAddSubtract);
41  CPPUNIT_TEST(TestScalarMultiply);
42  CPPUNIT_TEST(TestCtor);
43  CPPUNIT_TEST_SUITE_END();
44 
45 public:
46  void
48  {
49  // 10
50  const int size = 10;
52 
54 
55  for (int i = 0; i < size; ++i)
56  v[i];
57 
59 
60  for (int i = 0; i < size; ++i)
61  v(i);
62 
63  ASSERT_EQUAL(v.GetNonEmptySize(), (unsigned int)(size));
64 
65  v.Reclaim();
66 
68 
69  v(5) = 1;
70 
72 
73  ASSERT_EQUAL(v[5], 1.);
74  }
75 
76  void
78  {
80  v(0) = 1; v(10) = 2; v(100) = 3; v(1000) = -1;
81 
83  ASSERT_ITERATOR_EQUAL(it, v.SparseEnd(), 0, 1);
84  ++it;
85  ASSERT_ITERATOR_EQUAL(it, v.SparseEnd(), 10, 2);
86  ++it;
87  ASSERT_ITERATOR_EQUAL(it, v.SparseEnd(), 100, 3);
88  ++it;
89  ASSERT_ITERATOR_EQUAL(it, v.SparseEnd(), 1000, -1);
90  ++it;
91  CPPUNIT_ASSERT(it == v.SparseEnd());
92 
93  it = v.Find(0);
94  ASSERT_ITERATOR_EQUAL(it, v.SparseEnd(), 0, 1);
95 
96  it = v.Find(10);
97  ASSERT_ITERATOR_EQUAL(it, v.SparseEnd(), 10, 2);
98 
99  it = v.Find(1000);
100  ASSERT_ITERATOR_EQUAL(it, v.SparseEnd(), 1000, -1);
101  }
102 
103  void
105  {
107  a(0) = 1; a(3) = 2; a(5) = 3; a(9) = 4;
108 
110  b(0) = 1; b(1) = 0; b(2) = 2; b(3) = 3; b(4) = 4; b(6) = 6; b(7) = 7; b(9) = 9;
111 
112  const double scalar = a * b;
113 
114  ASSERT_EQUAL(scalar, 43.);
115  }
116 
117  void
119  {
121  a(0) = 1; a(3) = 2; a(5) = 3; a(9) = 4;
122 
124  b(0) = 1; b(1) = 0; b(2) = 2; b(3) = 3; b(4) = 4; b(6) = 6; b(7) = 7; b(9) = 9;
125 
126  SparseVector<double> ta(a);
127 
128  a += b;
129 
130  // a + b
132  p(0) = 2; p(1) = 0; p(2) = 2; p(3) = 5; p(4) = 4; p(5) = 3; p(6) = 6; p(7) = 7; p(9) = 13;
133 
134  CPPUNIT_ASSERT(a == p);
135 
136  a.Reclaim();
137  p.Reclaim();
138 
139  CPPUNIT_ASSERT(a == p);
140 
141  a -= b;
142 
143  a.Reclaim();
144 
145  CPPUNIT_ASSERT(a == ta);
146 
147 #pragma clang diagnostic push
148 #pragma clang diagnostic ignored "-Wself-assign-overloaded"
149  // warning since we don't know if this is a = 0;
150  a -= a;
151 #pragma clang diagnostic pop
152 
153  a.Reclaim();
154 
155  CPPUNIT_ASSERT(a.IsEmpty());
156 
158  }
159 
160  void
162  {
164  a(0) = 1; a(3) = 2; a(5) = 3; a(9) = 4;
165 
166  a *= 2;
167 
169  b(0) = 2; b(3) = 4; b(5) = 6; b(9) = 8;
170 
171  CPPUNIT_ASSERT(a == b);
172  }
173 
174  void
176  {
177  SparseVector<int> vi(1);
178 
179  SparseVector<double> vd(vi);
180 
181  CPPUNIT_ASSERT(vi == vd);
182  }
183 
184 };
185 
186 
Sparse container class for vectorial data.
Definition: SparseVector.h:36
#define ASSERT_ITERATOR_EQUAL(it, e, i, v)
bool IsEmpty() const
Definition: SparseVector.h:112
unsigned int Reclaim()
remove &quot;empty&quot; elements (that are equal to the initializer)
Definition: SparseVector.h:124
Iterator Find(const IndexType index)
Definition: SparseVector.h:186
CPPUNIT_TEST_SUITE_REGISTRATION(testAiresShowerFile)
#define U
unsigned int GetNonEmptySize() const
Definition: SparseVector.h:82
Iterator SparseEnd()
Definition: SparseVector.h:182
Iterator SparseBegin()
Definition: SparseVector.h:178
#define ASSERT_EQUAL(x, y)

, generated on Tue Sep 26 2023.