diff --git a/zc2443-TestAllocator.c++ b/zc2443-TestAllocator.c++ new file mode 100644 index 0000000..b6ffae3 --- /dev/null +++ b/zc2443-TestAllocator.c++ @@ -0,0 +1,280 @@ +// ------------------------------------- +// 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 +// -------------- + +// zachTest +TEST(TestAllocator2, const_index) { + const my_allocator x; + ASSERT_EQ(x[0], 92);} + +// zachTest +TEST(TestAllocator2, index) { + my_allocator x; + x[0] = 5; + ASSERT_EQ(x[0], 5);} + +// zachTest +TEST(TestAllocator3, index) { + my_allocator x; + x.allocate(1); + x[0] = 5; + ASSERT_EQ(x[0], 5);} + +// zachTest +TEST(TestAllocator3, const_index) { + const my_allocator x; + ASSERT_EQ(x[0], 4);} + +// zachTest +TEST(TestAllocator4, index) { + my_allocator x; + x.allocate(1); + ASSERT_EQ(x[0], -4);} + +// zachTest +TEST(TestAllocator5, index) { + my_allocator x; + x.allocate(1); + ASSERT_EQ(x[0], -12);} + +// zachTest +TEST(TestAllocator6, index) { + my_allocator x; + int * p = x.allocate(1); + x.deallocate(p, 10); + ASSERT_EQ(x[0], 12);} + +// zachTest +TEST(TestAllocator7, index) { + my_allocator x; + int * p = x.allocate(1); + x.deallocate(p, 10); + x.allocate(1); + ASSERT_EQ(x[0], -12);} + +// zachTest +TEST(TestAllocator8, index) { + my_allocator x; + int * p = x.allocate(1); + x.deallocate(p, 10); + p = x.allocate(1); + x.deallocate(p, 10); + ASSERT_EQ(x[0], 12);} + +// zachTest +TEST(TestAllocator9, index) { + my_allocator x; + int * p = x.allocate(10); + ASSERT_EQ(p, nullptr); +} + + +// zachTest +TEST(TestAllocator10, indexAndValid) { + my_allocator x; + x[0] = 23; + ASSERT_FALSE(x.valid());} + +// zachTest +TEST(TestAllocator11, indexAndValid) { + my_allocator x; + x[16] = 23; + ASSERT_FALSE(x.valid());} + +TEST(TestAllocator12, allocateTest) { + my_allocator x; + x.allocate(1); + x.allocate(1); + ASSERT_EQ(x[0], -4);} + +TEST(TestAllocator13, allocateTest) { + my_allocator x; + x.allocate(1); + x.allocate(1); + ASSERT_EQ(x[8], -4);} + +TEST(TestAllocator14, allocateTest) { + my_allocator x; + x.allocate(1); + x.allocate(1); + ASSERT_EQ(x[12], -10);} + +TEST(TestAllocator15, allocateTest) { + my_allocator x; + x.allocate(1); + x.allocate(1); + ASSERT_EQ(x[26], -10);} + + +TEST(TestAllocator16, deallocateTest) { + my_allocator x; + x.allocate(5); + ASSERT_EQ(x[0], -22);} + +TEST(TestAllocator17, deallocateTest) { + my_allocator x; + x.deallocate(x.allocate(5), 10); + ASSERT_EQ(x[0], 22);} + +TEST(TestAllocator18, allocateTest) { + my_allocator x; + ASSERT_EQ(x[0], 10000 - 8);} + +TEST(TestAllocator19, allocateTest) { + my_allocator x; + ASSERT_EQ(x[0], x[10000 - 4]);} + + +// -------------- +// 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);}} + diff --git a/zc2443-TestAllocator.out b/zc2443-TestAllocator.out new file mode 100644 index 0000000..58d2aaa --- /dev/null +++ b/zc2443-TestAllocator.out @@ -0,0 +1,168 @@ +==5407== Memcheck, a memory error detector +==5407== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. +==5407== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info +==5407== Command: ./TestAllocator +==5407== +Running main() from gtest_main.cc +[==========] Running 32 tests from 24 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 (20 ms) +[ RUN ] TestAllocator1/0.test_10 +[ OK ] TestAllocator1/0.test_10 (8 ms) +[----------] 2 tests from TestAllocator1/0 (47 ms total) + +[----------] 2 tests from TestAllocator1/1, where TypeParam = std::allocator +[ RUN ] TestAllocator1/1.test_1 +[ OK ] TestAllocator1/1.test_1 (8 ms) +[ RUN ] TestAllocator1/1.test_10 +[ OK ] TestAllocator1/1.test_10 (6 ms) +[----------] 2 tests from TestAllocator1/1 (16 ms total) + +[----------] 2 tests from TestAllocator1/2, where TypeParam = my_allocator +[ RUN ] TestAllocator1/2.test_1 +[ OK ] TestAllocator1/2.test_1 (14 ms) +[ RUN ] TestAllocator1/2.test_10 +[ OK ] TestAllocator1/2.test_10 (5 ms) +[----------] 2 tests from TestAllocator1/2 (21 ms total) + +[----------] 2 tests from TestAllocator1/3, where TypeParam = my_allocator +[ RUN ] TestAllocator1/3.test_1 +[ OK ] TestAllocator1/3.test_1 (14 ms) +[ RUN ] TestAllocator1/3.test_10 +[ OK ] TestAllocator1/3.test_10 (5 ms) +[----------] 2 tests from TestAllocator1/3 (22 ms total) + +[----------] 2 tests from TestAllocator2 +[ RUN ] TestAllocator2.const_index +[ OK ] TestAllocator2.const_index (3 ms) +[ RUN ] TestAllocator2.index +[ OK ] TestAllocator2.index (3 ms) +[----------] 2 tests from TestAllocator2 (9 ms total) + +[----------] 2 tests from TestAllocator3 +[ RUN ] TestAllocator3.index +[ OK ] TestAllocator3.index (3 ms) +[ RUN ] TestAllocator3.const_index +[ OK ] TestAllocator3.const_index (6 ms) +[----------] 2 tests from TestAllocator3 (12 ms total) + +[----------] 1 test from TestAllocator4 +[ RUN ] TestAllocator4.index +[ OK ] TestAllocator4.index (3 ms) +[----------] 1 test from TestAllocator4 (4 ms total) + +[----------] 1 test from TestAllocator5 +[ RUN ] TestAllocator5.index +[ OK ] TestAllocator5.index (8 ms) +[----------] 1 test from TestAllocator5 (10 ms total) + +[----------] 1 test from TestAllocator6 +[ RUN ] TestAllocator6.index +[ OK ] TestAllocator6.index (5 ms) +[----------] 1 test from TestAllocator6 (6 ms total) + +[----------] 1 test from TestAllocator7 +[ RUN ] TestAllocator7.index +[ OK ] TestAllocator7.index (3 ms) +[----------] 1 test from TestAllocator7 (5 ms total) + +[----------] 1 test from TestAllocator8 +[ RUN ] TestAllocator8.index +[ OK ] TestAllocator8.index (4 ms) +[----------] 1 test from TestAllocator8 (5 ms total) + +[----------] 1 test from TestAllocator9 +[ RUN ] TestAllocator9.index +[ OK ] TestAllocator9.index (4 ms) +[----------] 1 test from TestAllocator9 (5 ms total) + +[----------] 1 test from TestAllocator10 +[ RUN ] TestAllocator10.indexAndValid +[ OK ] TestAllocator10.indexAndValid (5 ms) +[----------] 1 test from TestAllocator10 (7 ms total) + +[----------] 1 test from TestAllocator11 +[ RUN ] TestAllocator11.indexAndValid +[ OK ] TestAllocator11.indexAndValid (6 ms) +[----------] 1 test from TestAllocator11 (7 ms total) + +[----------] 1 test from TestAllocator12 +[ RUN ] TestAllocator12.allocateTest +[ OK ] TestAllocator12.allocateTest (12 ms) +[----------] 1 test from TestAllocator12 (14 ms total) + +[----------] 1 test from TestAllocator13 +[ RUN ] TestAllocator13.allocateTest +[ OK ] TestAllocator13.allocateTest (3 ms) +[----------] 1 test from TestAllocator13 (5 ms total) + +[----------] 1 test from TestAllocator14 +[ RUN ] TestAllocator14.allocateTest +[ OK ] TestAllocator14.allocateTest (3 ms) +[----------] 1 test from TestAllocator14 (5 ms total) + +[----------] 1 test from TestAllocator15 +[ RUN ] TestAllocator15.allocateTest +[ OK ] TestAllocator15.allocateTest (3 ms) +[----------] 1 test from TestAllocator15 (5 ms total) + +[----------] 1 test from TestAllocator16 +[ RUN ] TestAllocator16.deallocateTest +[ OK ] TestAllocator16.deallocateTest (3 ms) +[----------] 1 test from TestAllocator16 (4 ms total) + +[----------] 1 test from TestAllocator17 +[ RUN ] TestAllocator17.deallocateTest +[ OK ] TestAllocator17.deallocateTest (5 ms) +[----------] 1 test from TestAllocator17 (6 ms total) + +[----------] 1 test from TestAllocator18 +[ RUN ] TestAllocator18.allocateTest +[ OK ] TestAllocator18.allocateTest (6 ms) +[----------] 1 test from TestAllocator18 (8 ms total) + +[----------] 1 test from TestAllocator19 +[ RUN ] TestAllocator19.allocateTest +[ OK ] TestAllocator19.allocateTest (3 ms) +[----------] 1 test from TestAllocator19 (4 ms total) + +[----------] 2 tests from TestAllocator3/0, where TypeParam = my_allocator +[ RUN ] TestAllocator3/0.test_1 +[ OK ] TestAllocator3/0.test_1 (5 ms) +[ RUN ] TestAllocator3/0.test_10 +[ OK ] TestAllocator3/0.test_10 (4 ms) +[----------] 2 tests from TestAllocator3/0 (12 ms total) + +[----------] 2 tests from TestAllocator3/1, where TypeParam = my_allocator +[ RUN ] TestAllocator3/1.test_1 +[ OK ] TestAllocator3/1.test_1 (4 ms) +[ RUN ] TestAllocator3/1.test_10 +[ OK ] TestAllocator3/1.test_10 (5 ms) +[----------] 2 tests from TestAllocator3/1 (11 ms total) + +[----------] Global test environment tear-down +[==========] 32 tests from 24 test cases ran. (348 ms total) +[ PASSED ] 32 tests. +==5407== +==5407== HEAP SUMMARY: +==5407== in use at exit: 72,704 bytes in 1 blocks +==5407== total heap usage: 877 allocs, 876 frees, 244,665 bytes allocated +==5407== +==5407== LEAK SUMMARY: +==5407== definitely lost: 0 bytes in 0 blocks +==5407== indirectly lost: 0 bytes in 0 blocks +==5407== possibly lost: 0 bytes in 0 blocks +==5407== still reachable: 72,704 bytes in 1 blocks +==5407== suppressed: 0 bytes in 0 blocks +==5407== Rerun with --leak-check=full to see details of leaked memory +==5407== +==5407== For counts of detected and suppressed errors, rerun with: -v +==5407== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) +File 'TestAllocator.c++' +Lines executed:92.16% of 153 +Branches executed:65.28% of 772 +Taken at least once:34.20% of 772 +Calls executed:56.18% of 817 +Creating 'TestAllocator.c++.gcov'