Skip to content

Instantly share code, notes, and snippets.

@daniel-kristjansson
Created June 22, 2013 19:26
Show Gist options
  • Save daniel-kristjansson/5842257 to your computer and use it in GitHub Desktop.
Save daniel-kristjansson/5842257 to your computer and use it in GitHub Desktop.
C++ array init
#include <iostream>
using namespace std;
#include <QtTest/QtTest>
class A
{
public:
A() {}
A(float) : i(0x30) {}
unsigned char a[16];
unsigned char i;
};
class B
{
public:
B() : i(0x30) {}
unsigned char a[16];
unsigned char i;
};
class C
{
public:
C() : a(), i(0x30) {}
unsigned char a[16];
unsigned char i;
};
class Testcpp_ctor_init: public QObject
{
Q_OBJECT
static const unsigned buffer_size = 8096;
char *buffer;
public:
Testcpp_ctor_init()
{
buffer = new char[buffer_size];
}
~Testcpp_ctor_init()
{
delete [] buffer;
}
private slots:
// called at the beginning of these sets of tests
void initTestCase(void)
{
}
// called at the end of these sets of tests
void cleanupTestCase(void)
{
}
// called before each test case
void init(void)
{
for (unsigned i = 0; i < buffer_size; i++)
buffer[i] = 99;
}
// called after each test case
void cleanup(void)
{
}
void basic_value_is_not_initialized_with_empty_ctor(void)
{
A *a = new (buffer) A();
QVERIFY(a->i == 99);
}
void array_value_is_not_initialized_with_empty_ctor(void)
{
A *a = new (buffer) A();
QVERIFY(a->a[0] == 99);
}
void array_value_is_initialized_with_empty_ctor_given_ctor(void)
{
C *c = new (buffer) C();
QVERIFY(c->a[0] == 0);
}
void array_value_is_not_initialized_with_int_init_ctor(void)
{
B *b = new (buffer) B();
QVERIFY(b->a[0] == 99);
}
void array_value_is_not_initialized_with_int_init_ctor_with_param(void)
{
A *a = new (buffer) A(5);
QVERIFY(a->a[0] == 99);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment