Browse Source

math: fix uninitialised variable in arraynd.

undefined
Sam Hocevar 10 years ago
parent
commit
90f0420382
2 changed files with 27 additions and 18 deletions
  1. +18
    -17
      src/lol/math/arraynd.h
  2. +9
    -1
      test/unit/arraynd.cpp

+ 18
- 17
src/lol/math/arraynd.h View File

@@ -107,25 +107,20 @@ public:
typedef array<T...> super;
typedef typename super::element_t element_t;

inline arraynd() :
super(),
m_sizes()
inline arraynd()
{
}

inline arraynd(vec_t<size_t, N> sizes, element_t e = element_t()) :
super(),
m_sizes(sizes)
inline arraynd(vec_t<size_t, N> sizes, element_t e = element_t())
: m_sizes(sizes)
{
SetSize(m_sizes, e);
FixSizes(e);
}

inline arraynd(arraynd_initializer<element_t, N> initializer) :
super(),
m_sizes()
inline arraynd(arraynd_initializer<element_t, N> initializer)
{
initializer.FillSizes(&m_sizes[0]);
SetSize(m_sizes);
FixSizes();
initializer.FillValues(&super::operator[](0), &m_sizes[0], 1);
}

@@ -210,12 +205,8 @@ public:
* FIXME: data gets scrambled; should we care? */
inline void SetSize(vec_t<size_t, N> sizes, element_t e = element_t())
{
size_t total_size = 1;

for (auto size : sizes)
total_size *= size;

this->Resize(total_size, e);
m_sizes = sizes;
FixSizes(e);
}

inline vec_t<size_t, N> GetSize() const
@@ -230,6 +221,16 @@ public:
inline int Bytes() const { return super::Bytes(); }

private:
inline void FixSizes(element_t e = element_t())
{
size_t total_size = 1;

for (auto size : m_sizes)
total_size *= size;

this->Resize(total_size, e);
}

vec_t<size_t, N> m_sizes;
};



+ 9
- 1
test/unit/arraynd.cpp View File

@@ -27,16 +27,24 @@ LOLUNIT_FIXTURE(ArrayNDTest)
LOLUNIT_TEST(Array2D)
{
arraynd<2, int> a;
a.SetSize(vec_t<size_t, 2>(1, 2));
a.SetSize(vec_t<size_t, 2>(2, 2));

/* Non-const accessors */
a[0][0] = 1;
a[0][1] = 2;
a[1][0] = 3;
a[1][1] = 4;
LOLUNIT_ASSERT_EQUAL(a[0][0], 1);
LOLUNIT_ASSERT_EQUAL(a[0][1], 2);
LOLUNIT_ASSERT_EQUAL(a[1][0], 3);
LOLUNIT_ASSERT_EQUAL(a[1][1], 4);

/* Const accessors */
arraynd<2, int> const &b = a;
LOLUNIT_ASSERT_EQUAL(b[0][0], 1);
LOLUNIT_ASSERT_EQUAL(b[0][1], 2);
LOLUNIT_ASSERT_EQUAL(b[1][0], 3);
LOLUNIT_ASSERT_EQUAL(b[1][1], 4);
}

LOLUNIT_TEST(ArrayNDCreate)


Loading…
Cancel
Save