diff --git a/amd3938-TestAllocator.c++ b/amd3938-TestAllocator.c++ new file mode 100644 index 0000000..2d1fceb --- /dev/null +++ b/amd3938-TestAllocator.c++ @@ -0,0 +1,307 @@ +// ------------------------------------- +// projects/allocator/TestAllocator1.c++ +// Copyright (C) 2015 +// Glenn P. Downing +// ------------------------------------- + +// -------- +// includes +// -------- + +#include // count +#include // allocator + +#include "gtest/gtest.h" + +#include "Allocator.h" + +using namespace std; + +// -------------- +// 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], 92);} + +TEST(TestAllocator2, index) { + my_allocator x; + ASSERT_EQ(x[0], 92);} + +// -------------- +// 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);}} + +/*Alyjan's and Chris's tests*/ + +TEST(TestAllocator4, index) { + my_allocator x; + ASSERT_EQ(x[0], 992);} + +TEST(TestAllocator5, index) { + try{ + my_allocator x; + } + // the constructor should throw a bad_alloc_exception + catch(bad_alloc e){ + ASSERT_TRUE(true); + }} + +TEST(TestAllocator6, index) { + my_allocator x; + ASSERT_EQ(x[0], 992);} + +TEST(TestAllocator7, valid) { + my_allocator x; + x[0] = 20; + ASSERT_FALSE(x.valid());} + +TEST(TestAllocator8, valid) { + my_allocator x; + x[0] = 20; + x[96] = 20; + ASSERT_FALSE(x.valid());} + +TEST(TestAllocator9, valid) { + my_allocator x; + x[96] = -92; + ASSERT_FALSE(x.valid());} + +template +struct TestAllocator11 : testing::Test { + // -------- + // typedefs + // -------- + typedef typename A::pointer pointer;}; + +typedef testing::Types< + my_allocator> + our_types; + +TYPED_TEST_CASE(TestAllocator11, our_types); + +TYPED_TEST(TestAllocator11, allocate1) { + typedef typename TestFixture::pointer pointer; + my_allocator x; + try{ + pointer p = x.allocate(100); + } + // we should get an exception when we try to allocate more than n*sizeof(T)+(2*sizeof(int)) + catch(bad_alloc e){ + ASSERT_TRUE(true); + } +} + +TYPED_TEST(TestAllocator11, allocate2) { + typedef typename TestFixture::pointer pointer; + my_allocator x; + try{ + pointer p = x.allocate(-100); + } + // we should get an exception when we try to allocate negative stuff + catch(bad_alloc e){ + ASSERT_TRUE(true); + } +} + +TYPED_TEST(TestAllocator11, allocate3) { + typedef typename TestFixture::pointer pointer; + my_allocator x; + try{ + pointer p = x.allocate(5); + x.construct(p, 2); + ASSERT_EQ(p[0], 2); + } + // we should never get an exception in this case + catch(bad_alloc e){ + ASSERT_FALSE(true); + } +} + +template +struct TestAllocator12 : testing::Test { + // -------- + // typedefs + // -------- + typedef typename A::pointer pointer;}; + +typedef testing::Types< + my_allocator> + our_types_2; + +TYPED_TEST_CASE(TestAllocator12, our_types_2); + +TYPED_TEST(TestAllocator12, deallocate1) { + typedef typename TestFixture::pointer pointer; + my_allocator x; + try{ + pointer p = x.allocate(5); + x.deallocate(p-10, 100); + } + // we should get an exception when we try to deallocate with a pointer that doesn't point to stuff we allocated + catch(invalid_argument e){ + ASSERT_TRUE(true); + } +} + +TYPED_TEST(TestAllocator12, deallocate2) { + typedef typename TestFixture::pointer pointer; + my_allocator x; + try{ + pointer p = x.allocate(5); + x.deallocate(0, 5); + } + // we should get an exception when we try to deallocate with a pointer that doesn't point to stuff we allocated + catch(invalid_argument e){ + ASSERT_TRUE(true); + } +} + +TYPED_TEST(TestAllocator12, deallocate3) { + typedef typename TestFixture::pointer pointer; + my_allocator x; + try{ + pointer p = x.allocate(1); + x.construct(p,10); + x.deallocate(p, 1); + ASSERT_EQ(p[0],10); + } + // we should never get an exception when we try to deallocate correctly + catch(invalid_argument e){ + ASSERT_TRUE(false); + } +} \ No newline at end of file diff --git a/amd3938-TestAllocator.out b/amd3938-TestAllocator.out new file mode 100644 index 0000000..0474a38 --- /dev/null +++ b/amd3938-TestAllocator.out @@ -0,0 +1,146 @@ +==3248== Memcheck, a memory error detector +==3248== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. +==3248== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info +==3248== Command: ./TestAllocator +==3248== +Running main() from gtest_main.cc +[==========] Running 26 tests from 15 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 (10 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 (5 ms) +[ RUN ] TestAllocator1/1.test_10 +[ OK ] TestAllocator1/1.test_10 (4 ms) +[----------] 2 tests from TestAllocator1/1 (9 ms total) + +[----------] 2 tests from TestAllocator1/2, where TypeParam = my_allocator +[ RUN ] TestAllocator1/2.test_1 +[ OK ] TestAllocator1/2.test_1 (8 ms) +[ RUN ] TestAllocator1/2.test_10 +[ OK ] TestAllocator1/2.test_10 (3 ms) +[----------] 2 tests from TestAllocator1/2 (11 ms total) + +[----------] 2 tests from TestAllocator1/3, where TypeParam = my_allocator +[ RUN ] TestAllocator1/3.test_1 +[ OK ] TestAllocator1/3.test_1 (8 ms) +[ RUN ] TestAllocator1/3.test_10 +[ OK ] TestAllocator1/3.test_10 (3 ms) +[----------] 2 tests from TestAllocator1/3 (11 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) + +[----------] 1 test from TestAllocator4 +[ RUN ] TestAllocator4.index +[ OK ] TestAllocator4.index (4 ms) +[----------] 1 test from TestAllocator4 (4 ms total) + +[----------] 1 test from TestAllocator5 +[ RUN ] TestAllocator5.index +[ OK ] TestAllocator5.index (36 ms) +[----------] 1 test from TestAllocator5 (36 ms total) + +[----------] 1 test from TestAllocator6 +[ RUN ] TestAllocator6.index +[ OK ] TestAllocator6.index (3 ms) +[----------] 1 test from TestAllocator6 (3 ms total) + +[----------] 1 test from TestAllocator7 +[ RUN ] TestAllocator7.valid +==3248== Conditional jump or move depends on uninitialised value(s) +==3248== at 0x40AA2B: my_allocator::valid() const (in /v/filer4b/v38q001/alyjan/Desktop/allocator-alyjan-chris/TestAllocator) +==3248== by 0x406644: TestAllocator7_valid_Test::TestBody() (in /v/filer4b/v38q001/alyjan/Desktop/allocator-alyjan-chris/TestAllocator) +==3248== by 0x43FE52: void testing::internal::HandleExceptionsInMethodIfSupported(testing::Test*, void (testing::Test::*)(), char const*) (in /v/filer4b/v38q001/alyjan/Desktop/allocator-alyjan-chris/TestAllocator) +==3248== by 0x43298C: testing::Test::Run() (in /v/filer4b/v38q001/alyjan/Desktop/allocator-alyjan-chris/TestAllocator) +==3248== by 0x432A23: testing::TestInfo::Run() (in /v/filer4b/v38q001/alyjan/Desktop/allocator-alyjan-chris/TestAllocator) +==3248== by 0x432B24: testing::TestCase::Run() (in /v/filer4b/v38q001/alyjan/Desktop/allocator-alyjan-chris/TestAllocator) +==3248== by 0x432D9C: testing::internal::UnitTestImpl::RunAllTests() (in /v/filer4b/v38q001/alyjan/Desktop/allocator-alyjan-chris/TestAllocator) +==3248== by 0x43307D: testing::UnitTest::Run() (in /v/filer4b/v38q001/alyjan/Desktop/allocator-alyjan-chris/TestAllocator) +==3248== by 0x40539F: main (in /v/filer4b/v38q001/alyjan/Desktop/allocator-alyjan-chris/TestAllocator) +==3248== +[ OK ] TestAllocator7.valid (2 ms) +[----------] 1 test from TestAllocator7 (2 ms total) + +[----------] 1 test from TestAllocator8 +[ RUN ] TestAllocator8.valid +==3248== Conditional jump or move depends on uninitialised value(s) +==3248== at 0x40AA2B: my_allocator::valid() const (in /v/filer4b/v38q001/alyjan/Desktop/allocator-alyjan-chris/TestAllocator) +==3248== by 0x406A2B: TestAllocator8_valid_Test::TestBody() (in /v/filer4b/v38q001/alyjan/Desktop/allocator-alyjan-chris/TestAllocator) +==3248== by 0x43FE52: void testing::internal::HandleExceptionsInMethodIfSupported(testing::Test*, void (testing::Test::*)(), char const*) (in /v/filer4b/v38q001/alyjan/Desktop/allocator-alyjan-chris/TestAllocator) +==3248== by 0x43298C: testing::Test::Run() (in /v/filer4b/v38q001/alyjan/Desktop/allocator-alyjan-chris/TestAllocator) +==3248== by 0x432A23: testing::TestInfo::Run() (in /v/filer4b/v38q001/alyjan/Desktop/allocator-alyjan-chris/TestAllocator) +==3248== by 0x432B24: testing::TestCase::Run() (in /v/filer4b/v38q001/alyjan/Desktop/allocator-alyjan-chris/TestAllocator) +==3248== by 0x432D9C: testing::internal::UnitTestImpl::RunAllTests() (in /v/filer4b/v38q001/alyjan/Desktop/allocator-alyjan-chris/TestAllocator) +==3248== by 0x43307D: testing::UnitTest::Run() (in /v/filer4b/v38q001/alyjan/Desktop/allocator-alyjan-chris/TestAllocator) +==3248== by 0x40539F: main (in /v/filer4b/v38q001/alyjan/Desktop/allocator-alyjan-chris/TestAllocator) +==3248== +[ OK ] TestAllocator8.valid (3 ms) +[----------] 1 test from TestAllocator8 (3 ms total) + +[----------] 1 test from TestAllocator9 +[ RUN ] TestAllocator9.valid +[ OK ] TestAllocator9.valid (2 ms) +[----------] 1 test from TestAllocator9 (2 ms total) + +[----------] 3 tests from TestAllocator11/0, where TypeParam = my_allocator +[ RUN ] TestAllocator11/0.allocate1 +[ OK ] TestAllocator11/0.allocate1 (4 ms) +[ RUN ] TestAllocator11/0.allocate2 +[ OK ] TestAllocator11/0.allocate2 (3 ms) +[ RUN ] TestAllocator11/0.allocate3 +[ OK ] TestAllocator11/0.allocate3 (2 ms) +[----------] 3 tests from TestAllocator11/0 (9 ms total) + +[----------] 3 tests from TestAllocator12/0, where TypeParam = my_allocator +[ RUN ] TestAllocator12/0.deallocate1 +[ OK ] TestAllocator12/0.deallocate1 (6 ms) +[ RUN ] TestAllocator12/0.deallocate2 +[ OK ] TestAllocator12/0.deallocate2 (3 ms) +[ RUN ] TestAllocator12/0.deallocate3 +[ OK ] TestAllocator12/0.deallocate3 (2 ms) +[----------] 3 tests from TestAllocator12/0 (11 ms total) + +[----------] Global test environment tear-down +[==========] 26 tests from 15 test cases ran. (166 ms total) +[ PASSED ] 26 tests. +==3248== +==3248== HEAP SUMMARY: +==3248== in use at exit: 0 bytes in 0 blocks +==3248== total heap usage: 1,153 allocs, 1,153 frees, 154,200 bytes allocated +==3248== +==3248== All heap blocks were freed -- no leaks are possible +==3248== +==3248== For counts of detected and suppressed errors, rerun with: -v +==3248== Use --track-origins=yes to see where uninitialised values come from +==3248== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0) +File 'TestAllocator.c++' +Lines executed:88.32% of 137 +Branches executed:62.54% of 694 +Taken at least once:33.00% of 694 +Calls executed:49.60% of 750 +Creating 'TestAllocator.c++.gcov'