Skip to content
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
233 changes: 233 additions & 0 deletions art2589-jaz747-TestAllocator.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
// ------------------------------------
// projects/allocator/TestAllocator1.c++
// Copyright (C) 2015
// Glenn P. Downing
// ------------------------------------

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

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

#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<Allocator<int, 100>, Allocator<double, 100>> my_types_1;

TYPED_TEST_CASE(TestAllocator1, my_types_1);

// Default Constructor Tests

TYPED_TEST(TestAllocator1, test_1) {
try {
Allocator<double, 8> x;
ASSERT_EQ(true, false);
} catch (std::bad_alloc &e) {
ASSERT_EQ(true, true);
}
}

TYPED_TEST(TestAllocator1, test_2) {
try {
Allocator<int, 4> x;
ASSERT_EQ(true, false);
} catch (std::bad_alloc &e) {
ASSERT_EQ(true, true);
}
}

// Allocate Tests

TYPED_TEST(TestAllocator1, test_3) {
try {
Allocator<int, 100> x;
x.allocate(-9);
ASSERT_EQ(true, false);
} catch (std::bad_alloc &e) {
ASSERT_EQ(true, true);
}
}

TYPED_TEST(TestAllocator1, test_4) {
try {
Allocator<int, 100> x;
x.allocate(25);
ASSERT_EQ(true, false);
} catch (std::bad_alloc &e) {
ASSERT_EQ(true, true);
}
}

TYPED_TEST(TestAllocator1, test_5) {
try {
Allocator<int, 100> x;
x.allocate(13);
ASSERT_EQ(true, true);
} catch (std::bad_alloc &e) {
ASSERT_EQ(true, false);
}
}

// Deallocate Tests

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

allocator_type x;
const size_type s = 100;
const value_type v = 2;

try {
x.deallocate(nullptr, s);
ASSERT_EQ(true, false);
} catch (std::bad_alloc &e) {
ASSERT_EQ(true, true);
}
}

TYPED_TEST(TestAllocator1, test_7) {
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 = 4;
const value_type v = 2;
try {
pointer p = x.allocate(s);
x.deallocate(p, v);
ASSERT_EQ(true, true);
} catch (std::bad_alloc &e) {
ASSERT_EQ(true, false);
}
}

TYPED_TEST(TestAllocator1, test_8) {
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 = 4;
const value_type v = 2;

try {
pointer p = x.allocate(s);
x.deallocate(p, s);
x.deallocate(p, s);
} catch (std::bad_alloc &e) {
ASSERT_EQ(true, true);
}
}

// force right coalesce
TYPED_TEST(TestAllocator1, test_9) {
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 = 4;
const value_type v = 2;

try {
pointer p = x.allocate(s);
pointer q = x.allocate(s);
x.deallocate(q, s);
x.deallocate(p, s);
} catch (std::bad_alloc &e) {
ASSERT_EQ(true, false);
}
}

// Force left Coalesce
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 = 4;
const value_type v = 2;

try {
pointer p = x.allocate(s);
pointer q = x.allocate(s);
x.deallocate(p, s);
x.deallocate(q, s);
} catch (std::bad_alloc &e) {
ASSERT_EQ(true, false);
}
}

// Both sides merge test
TYPED_TEST(TestAllocator1, test_11) {
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 = 3;
const value_type v = 1;

try {
pointer p = x.allocate(s);
pointer q = x.allocate(s);
pointer r = x.allocate(s);
x.deallocate(p, s);
x.deallocate(r, s);
x.deallocate(q, s);
} catch (std::bad_alloc &e) {
ASSERT_EQ(true, false);
}
}

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

template <typename A> struct TestAllocator2 : 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<Allocator<int, 100>, Allocator<double, 100>> my_types_2;

TYPED_TEST_CASE(TestAllocator2, my_types_2);

TEST(TestAllocator2, valid) {
Allocator<int, 20> x;
assert(x.valid());
}
81 changes: 81 additions & 0 deletions art2589-jaz747-TestAllocator.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
==23786== Memcheck, a memory error detector
==23786== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==23786== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==23786== Command: ./TestAllocator
==23786==
Running main() from gtest_main.cc
[==========] Running 14 tests from 7 test cases.
[----------] Global test environment set-up.
[----------] 2 tests from TestAllocator1/0, where TypeParam = std::allocator<int>
[ RUN ] TestAllocator1/0.test_1
[ OK ] TestAllocator1/0.test_1 (9 ms)
[ RUN ] TestAllocator1/0.test_10
[ OK ] TestAllocator1/0.test_10 (3 ms)
[----------] 2 tests from TestAllocator1/0 (18 ms total)

[----------] 2 tests from TestAllocator1/1, where TypeParam = std::allocator<double>
[ RUN ] TestAllocator1/1.test_1
[ OK ] TestAllocator1/1.test_1 (5 ms)
[ RUN ] TestAllocator1/1.test_10
[ OK ] TestAllocator1/1.test_10 (2 ms)
[----------] 2 tests from TestAllocator1/1 (8 ms total)

[----------] 2 tests from TestAllocator1/2, where TypeParam = Allocator<int, 100ul>
[ RUN ] TestAllocator1/2.test_1
[ OK ] TestAllocator1/2.test_1 (2 ms)
[ RUN ] TestAllocator1/2.test_10
[ OK ] TestAllocator1/2.test_10 (1 ms)
[----------] 2 tests from TestAllocator1/2 (3 ms total)

[----------] 2 tests from TestAllocator1/3, where TypeParam = Allocator<double, 100ul>
[ RUN ] TestAllocator1/3.test_1
[ OK ] TestAllocator1/3.test_1 (3 ms)
[ RUN ] TestAllocator1/3.test_10
[ OK ] TestAllocator1/3.test_10 (1 ms)
[----------] 2 tests from TestAllocator1/3 (4 ms total)

[----------] 2 tests from TestAllocator2
[ RUN ] TestAllocator2.const_index
[ OK ] TestAllocator2.const_index (2 ms)
[ RUN ] TestAllocator2.index
[ OK ] TestAllocator2.index (2 ms)
[----------] 2 tests from TestAllocator2 (4 ms total)

[----------] 2 tests from TestAllocator3/0, where TypeParam = Allocator<int, 100ul>
[ RUN ] TestAllocator3/0.test_1
[ OK ] TestAllocator3/0.test_1 (1 ms)
[ RUN ] TestAllocator3/0.test_10
[ OK ] TestAllocator3/0.test_10 (2 ms)
[----------] 2 tests from TestAllocator3/0 (3 ms total)

[----------] 2 tests from TestAllocator3/1, where TypeParam = Allocator<double, 100ul>
[ RUN ] TestAllocator3/1.test_1
[ OK ] TestAllocator3/1.test_1 (1 ms)
[ RUN ] TestAllocator3/1.test_10
[ OK ] TestAllocator3/1.test_10 (1 ms)
[----------] 2 tests from TestAllocator3/1 (3 ms total)

[----------] Global test environment tear-down
[==========] 14 tests from 7 test cases ran. (63 ms total)
[ PASSED ] 14 tests.
==23786==
==23786== HEAP SUMMARY:
==23786== in use at exit: 0 bytes in 0 blocks
==23786== total heap usage: 710 allocs, 710 frees, 100,261 bytes allocated
==23786==
==23786== All heap blocks were freed -- no leaks are possible
==23786==
==23786== For counts of detected and suppressed errors, rerun with: -v
==23786== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
File 'Allocator.h'
Lines executed:63.16% of 19
Branches executed:33.33% of 24
Taken at least once:16.67% of 24
Calls executed:25.00% of 24
Creating 'Allocator.h.gcov'
File 'TestAllocator.c++'
Lines executed:68.06% of 72
Branches executed:42.39% of 368
Taken at least once:22.28% of 368
Calls executed:39.30% of 369
Creating 'TestAllocator.c++.gcov'