Skip to content

Test commit #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
377 changes: 377 additions & 0 deletions ek6798-TestAllocator.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,377 @@
// -------------------------------------
// projects/allocator/TestAllocator1.c++
// Copyright (C) 2015
// Glenn P. Downing
// -------------------------------------

// --------
// includes
// --------

#include <algorithm> // count
#include <memory> // allocator

#include "gtest/gtest.h"

#include "Allocator.h"

// --------------
// TestAllocator1
// --------------

template <typename A>
struct TestAllocator1 : testing::Test {
// --------
// typedefs
// --------

typedef A allocator_type;
typedef typename A::value_type value_type;
typedef typename A::size_type size_type;
typedef typename A::pointer pointer;};

typedef testing::Types<
std::allocator<int>,
std::allocator<double>,
my_allocator<int, 100>,
my_allocator<double, 100>>
my_types_1;

TYPED_TEST_CASE(TestAllocator1, my_types_1);

TYPED_TEST(TestAllocator1, test_1) {
typedef typename TestFixture::allocator_type allocator_type;
typedef typename TestFixture::value_type value_type;
typedef typename TestFixture::size_type size_type;
typedef typename TestFixture::pointer pointer;

allocator_type x;
const size_type s = 1;
const value_type v = 2;
const pointer p = x.allocate(s);
if (p != nullptr) {
x.construct(p, v);
ASSERT_EQ(v, *p);
x.destroy(p);
x.deallocate(p, s);}
}

TYPED_TEST(TestAllocator1, test_10) {
typedef typename TestFixture::allocator_type allocator_type;
typedef typename TestFixture::value_type value_type;
typedef typename TestFixture::size_type size_type;
typedef typename TestFixture::pointer pointer;

allocator_type x;
const size_type s = 10;
const value_type v = 2;
const pointer b = x.allocate(s);
if (b != nullptr) {
pointer e = b + s;
pointer p = b;
try {
while (p != e) {
x.construct(p, v);
++p;}}
catch (...) {
while (b != p) {
--p;
x.destroy(p);}
x.deallocate(b, s);
throw;}
ASSERT_EQ(s, std::count(b, e, v));
while (b != e) {
--e;
x.destroy(e);}
x.deallocate(b, s);}
}

// --------------
// TestAllocator2
// --------------

TEST(TestAllocator2, const_index) {
const my_allocator<int, 100> x;
ASSERT_EQ(x[0], 0);}

TEST(TestAllocator2, index) {
my_allocator<int, 100> x;
ASSERT_EQ(x[0], 0);}

// --------------
// TestAllocator3
// --------------

template <typename A>
struct TestAllocator3 : testing::Test {
// --------
// typedefs
// --------

typedef A allocator_type;
typedef typename A::value_type value_type;
typedef typename A::size_type size_type;
typedef typename A::pointer pointer;};

typedef testing::Types<
my_allocator<int, 100>,
my_allocator<double, 100>>
my_types_2;

TYPED_TEST_CASE(TestAllocator3, my_types_2);

TYPED_TEST(TestAllocator3, test_1) {
typedef typename TestFixture::allocator_type allocator_type;
typedef typename TestFixture::value_type value_type;
typedef typename TestFixture::size_type size_type;
typedef typename TestFixture::pointer pointer;

allocator_type x;
const size_type s = 1;
const value_type v = 2;
const pointer p = x.allocate(s);
if (p != nullptr) {
x.construct(p, v);
ASSERT_EQ(v, *p);
x.destroy(p);
x.deallocate(p, s);}}

TYPED_TEST(TestAllocator3, test_10) {
typedef typename TestFixture::allocator_type allocator_type;
typedef typename TestFixture::value_type value_type;
typedef typename TestFixture::size_type size_type;
typedef typename TestFixture::pointer pointer;

allocator_type x;
const size_type s = 10;
const value_type v = 2;
const pointer b = x.allocate(s);
if (b != nullptr) {
pointer e = b + s;
pointer p = b;
try {
while (p != e) {
x.construct(p, v);
++p;}}
catch (...) {
while (b != p) {
--p;
x.destroy(p);}
x.deallocate(b, s);
throw;}
ASSERT_EQ(s, std::count(b, e, v));
while (b != e) {
--e;
x.destroy(e);}
x.deallocate(b, s);}}

// --------------
// TestAllocator4
// --------------
TEST(TestAllocator4, const_index) {
const my_allocator<double, 2000> x;
ASSERT_EQ(x[0], 0);
}

TEST(TestAllocator4, const_index2) {
const my_allocator<double, 4200> x;
ASSERT_EQ(x[0], 0);
}

// --------------
// TestAllocator5
// --------------
template <typename A>
struct TestAllocator5 : testing::Test {
// --------
// typedefs
// --------

typedef A allocator_type;
typedef typename A::value_type value_type;
typedef typename A::size_type size_type;
typedef typename A::pointer pointer;};

typedef testing::Types<
my_allocator<int, 200>,
my_allocator<double, 200>>
my_types_3;

TYPED_TEST_CASE(TestAllocator5, my_types_3);

TYPED_TEST(TestAllocator5, test_1) {
typedef typename TestFixture::allocator_type allocator_type;
typedef typename TestFixture::value_type value_type;
typedef typename TestFixture::size_type size_type;
typedef typename TestFixture::pointer pointer;

allocator_type x;
const size_type s = 1;
const value_type v = 42;
const pointer p = x.allocate(s);
if (p != nullptr) {
x.construct(p, v);
ASSERT_EQ(v, *p);
x.destroy(p);
x.deallocate(p, s);}}

TYPED_TEST(TestAllocator5, test_10) {
typedef typename TestFixture::allocator_type allocator_type;
typedef typename TestFixture::value_type value_type;
typedef typename TestFixture::size_type size_type;
typedef typename TestFixture::pointer pointer;

allocator_type x;
const size_type s = 21;
const value_type v = 42;
const pointer b = x.allocate(s);
if (b != nullptr) {
pointer e = b + s;
pointer p = b;
try {
while (p != e) {
x.construct(p, v);
++p;}}
catch (...) {
while (b != p) {
--p;
x.destroy(p);}
x.deallocate(b, s);
throw;}
ASSERT_EQ(s, std::count(b, e, v));
while (b != e) {
--e;
x.destroy(e);}
x.deallocate(b, s);}}

// --------------
// TestAllocator6
// --------------
template <typename A>
struct TestAllocator6 : testing::Test {
// --------
// typedefs
// --------

typedef A allocator_type;
typedef typename A::value_type value_type;
typedef typename A::size_type size_type;
typedef typename A::pointer pointer;};

typedef testing::Types<
my_allocator<int, 50>,
my_allocator<double, 50>>
my_types_4;

TYPED_TEST_CASE(TestAllocator6, my_types_4);

TYPED_TEST(TestAllocator6, test_1) {
typedef typename TestFixture::allocator_type allocator_type;
typedef typename TestFixture::value_type value_type;
typedef typename TestFixture::size_type size_type;
typedef typename TestFixture::pointer pointer;

allocator_type x;
const size_type s = 1;
const value_type v = 42;
const pointer p = x.allocate(s);
if (p != nullptr) {
x.construct(p, v);
ASSERT_EQ(v, *p);
x.destroy(p);
x.deallocate(p, s);}}

TYPED_TEST(TestAllocator6, test_10) {
typedef typename TestFixture::allocator_type allocator_type;
typedef typename TestFixture::value_type value_type;
typedef typename TestFixture::size_type size_type;
typedef typename TestFixture::pointer pointer;

allocator_type x;
const size_type s = 6;
const value_type v = 42;
const pointer b = x.allocate(s);
if (b != nullptr) {
pointer e = b + s;
pointer p = b;
try {
while (p != e) {
x.construct(p, v);
++p;}}
catch (...) {
while (b != p) {
--p;
x.destroy(p);}
x.deallocate(b, s);
throw;}
ASSERT_EQ(s, std::count(b, e, v));
while (b != e) {
--e;
x.destroy(e);}
x.deallocate(b, s);}}

// --------------
// TestAllocator7
// --------------
template <typename A>
struct TestAllocator7 : testing::Test {
// --------
// typedefs
// --------

typedef A allocator_type;
typedef typename A::value_type value_type;
typedef typename A::size_type size_type;
typedef typename A::pointer pointer;};

typedef testing::Types<
my_allocator<int, 2008>,
my_allocator<double, 4008>>
my_types_5;

TYPED_TEST_CASE(TestAllocator7, my_types_5);

TYPED_TEST(TestAllocator7, test_1) {
typedef typename TestFixture::allocator_type allocator_type;
typedef typename TestFixture::value_type value_type;
typedef typename TestFixture::size_type size_type;
typedef typename TestFixture::pointer pointer;

allocator_type x;
const size_type s = 1;
const value_type v = 42;
const pointer p = x.allocate(s);
if (p != nullptr) {
x.construct(p, v);
ASSERT_EQ(v, *p);
x.destroy(p);
x.deallocate(p, s);}}

TYPED_TEST(TestAllocator7, test_10) {
typedef typename TestFixture::allocator_type allocator_type;
typedef typename TestFixture::value_type value_type;
typedef typename TestFixture::size_type size_type;
typedef typename TestFixture::pointer pointer;

allocator_type x;
const size_type s = 500;
const value_type v = 42;
const pointer b = x.allocate(s);
if (b != nullptr) {
pointer e = b + s;
pointer p = b;
try {
while (p != e) {
x.construct(p, v);
++p;}}
catch (...) {
while (b != p) {
--p;
x.destroy(p);}
x.deallocate(b, s);
throw;}
ASSERT_EQ(s, std::count(b, e, v));
while (b != e) {
--e;
x.destroy(e);}
x.deallocate(b, s);}}
Loading