diff --git a/ek6798-TestAllocator.c++ b/ek6798-TestAllocator.c++ new file mode 100644 index 0000000..f1521eb --- /dev/null +++ b/ek6798-TestAllocator.c++ @@ -0,0 +1,377 @@ +// ------------------------------------- +// projects/allocator/TestAllocator1.c++ +// Copyright (C) 2015 +// Glenn P. Downing +// ------------------------------------- + +// -------- +// includes +// -------- + +#include // count +#include // allocator + +#include "gtest/gtest.h" + +#include "Allocator.h" + +// -------------- +// TestAllocator1 +// -------------- + +template +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, + std::allocator, + my_allocator, + my_allocator> + 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 x; + ASSERT_EQ(x[0], 0);} + +TEST(TestAllocator2, index) { + my_allocator x; + ASSERT_EQ(x[0], 0);} + +// -------------- +// TestAllocator3 +// -------------- + +template +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, + my_allocator> + 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 x; + ASSERT_EQ(x[0], 0); +} + +TEST(TestAllocator4, const_index2) { + const my_allocator x; + ASSERT_EQ(x[0], 0); +} + +// -------------- +// TestAllocator5 +// -------------- +template +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, + my_allocator> + 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 +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, + my_allocator> + 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 +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, + my_allocator> + 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);}} diff --git a/ek6798-TestAllocator.out b/ek6798-TestAllocator.out new file mode 100644 index 0000000..7971034 --- /dev/null +++ b/ek6798-TestAllocator.out @@ -0,0 +1,124 @@ +==23527== Memcheck, a memory error detector +==23527== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. +==23527== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info +==23527== Command: ./TestAllocator +==23527== +Running main() from gtest_main.cc +[==========] Running 28 tests from 14 test cases. +[----------] Global test environment set-up. +[----------] 2 tests from TestAllocator1/0, where TypeParam = std::allocator +[ RUN ] TestAllocator1/0.test_1 +[ OK ] TestAllocator1/0.test_1 (9 ms) +[ RUN ] TestAllocator1/0.test_10 +[ OK ] TestAllocator1/0.test_10 (4 ms) +[----------] 2 tests from TestAllocator1/0 (21 ms total) + +[----------] 2 tests from TestAllocator1/1, where TypeParam = std::allocator +[ RUN ] TestAllocator1/1.test_1 +[ OK ] TestAllocator1/1.test_1 (4 ms) +[ RUN ] TestAllocator1/1.test_10 +[ OK ] TestAllocator1/1.test_10 (4 ms) +[----------] 2 tests from TestAllocator1/1 (8 ms total) + +[----------] 2 tests from TestAllocator1/2, where TypeParam = my_allocator +[ RUN ] TestAllocator1/2.test_1 +[ OK ] TestAllocator1/2.test_1 (7 ms) +[ RUN ] TestAllocator1/2.test_10 +[ OK ] TestAllocator1/2.test_10 (3 ms) +[----------] 2 tests from TestAllocator1/2 (10 ms total) + +[----------] 2 tests from TestAllocator1/3, where TypeParam = my_allocator +[ RUN ] TestAllocator1/3.test_1 +[ OK ] TestAllocator1/3.test_1 (7 ms) +[ RUN ] TestAllocator1/3.test_10 +[ OK ] TestAllocator1/3.test_10 (3 ms) +[----------] 2 tests from TestAllocator1/3 (10 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 = my_allocator +[ RUN ] TestAllocator3/0.test_1 +[ OK ] TestAllocator3/0.test_1 (3 ms) +[ RUN ] TestAllocator3/0.test_10 +[ OK ] TestAllocator3/0.test_10 (3 ms) +[----------] 2 tests from TestAllocator3/0 (6 ms total) + +[----------] 2 tests from TestAllocator3/1, where TypeParam = my_allocator +[ RUN ] TestAllocator3/1.test_1 +[ OK ] TestAllocator3/1.test_1 (3 ms) +[ RUN ] TestAllocator3/1.test_10 +[ OK ] TestAllocator3/1.test_10 (3 ms) +[----------] 2 tests from TestAllocator3/1 (6 ms total) + +[----------] 2 tests from TestAllocator4 +[ RUN ] TestAllocator4.const_index +[ OK ] TestAllocator4.const_index (3 ms) +[ RUN ] TestAllocator4.const_index2 +[ OK ] TestAllocator4.const_index2 (3 ms) +[----------] 2 tests from TestAllocator4 (7 ms total) + +[----------] 2 tests from TestAllocator5/0, where TypeParam = my_allocator +[ RUN ] TestAllocator5/0.test_1 +[ OK ] TestAllocator5/0.test_1 (7 ms) +[ RUN ] TestAllocator5/0.test_10 +[ OK ] TestAllocator5/0.test_10 (3 ms) +[----------] 2 tests from TestAllocator5/0 (11 ms total) + +[----------] 2 tests from TestAllocator5/1, where TypeParam = my_allocator +[ RUN ] TestAllocator5/1.test_1 +[ OK ] TestAllocator5/1.test_1 (7 ms) +[ RUN ] TestAllocator5/1.test_10 +[ OK ] TestAllocator5/1.test_10 (3 ms) +[----------] 2 tests from TestAllocator5/1 (10 ms total) + +[----------] 2 tests from TestAllocator6/0, where TypeParam = my_allocator +[ RUN ] TestAllocator6/0.test_1 +[ OK ] TestAllocator6/0.test_1 (7 ms) +[ RUN ] TestAllocator6/0.test_10 +[ OK ] TestAllocator6/0.test_10 (3 ms) +[----------] 2 tests from TestAllocator6/0 (10 ms total) + +[----------] 2 tests from TestAllocator6/1, where TypeParam = my_allocator +[ RUN ] TestAllocator6/1.test_1 +[ OK ] TestAllocator6/1.test_1 (6 ms) +[ RUN ] TestAllocator6/1.test_10 +[ OK ] TestAllocator6/1.test_10 (1 ms) +[----------] 2 tests from TestAllocator6/1 (8 ms total) + +[----------] 2 tests from TestAllocator7/0, where TypeParam = my_allocator +[ RUN ] TestAllocator7/0.test_1 +[ OK ] TestAllocator7/0.test_1 (7 ms) +[ RUN ] TestAllocator7/0.test_10 +[ OK ] TestAllocator7/0.test_10 (2 ms) +[----------] 2 tests from TestAllocator7/0 (9 ms total) + +[----------] 2 tests from TestAllocator7/1, where TypeParam = my_allocator +[ RUN ] TestAllocator7/1.test_1 +[ OK ] TestAllocator7/1.test_1 (7 ms) +[ RUN ] TestAllocator7/1.test_10 +[ OK ] TestAllocator7/1.test_10 (1 ms) +[----------] 2 tests from TestAllocator7/1 (8 ms total) + +[----------] Global test environment tear-down +[==========] 28 tests from 14 test cases ran. (154 ms total) +[ PASSED ] 28 tests. +==23527== +==23527== HEAP SUMMARY: +==23527== in use at exit: 0 bytes in 0 blocks +==23527== total heap usage: 1,234 allocs, 1,234 frees, 160,736 bytes allocated +==23527== +==23527== All heap blocks were freed -- no leaks are possible +==23527== +==23527== For counts of detected and suppressed errors, rerun with: -v +==23527== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) +File 'TestAllocator.c++' +Lines executed:78.53% of 177 +Branches executed:61.35% of 828 +Taken at least once:32.85% of 828 +Calls executed:49.27% of 751 +Creating 'TestAllocator.c++.gcov'